diff --git a/software/inc/HttpRequest.hpp b/software/inc/HttpRequest.hpp new file mode 100644 index 0000000..cfa784c --- /dev/null +++ b/software/inc/HttpRequest.hpp @@ -0,0 +1,47 @@ +/** + * Let There Be Light project + * + * @file HttpRequest.hpp + */ + +/* Includes -------------------------------------------- */ +#include + +/* Defines --------------------------------------------- */ + +/* Type definitions ------------------------------------ */ + +/* Variable declarations ------------------------------- */ + +/* Functions ------------------------------------------- */ + +/* Class definition ------------------------------------ */ +class HttpRequest { + public: + /* Constructors */ + HttpRequest(); + + /* Destructors */ + virtual ~HttpRequest(); + + /* Request parser */ + int parseRequest(const std::string &pRequest); + + /* Getters */ + std::string request(void) const; + std::string method(void) const; + std::string URL(void) const; + std::string shortURL(void) const; + std::string query(void) const; + std::string httpVersion(void) const; + + protected: + std::string mRequestStr; + + std::string mMethod; + std::string mURL; + std::string mShortURL; + std::string mQuery; + std::string mHttpVersion; + private: +}; \ No newline at end of file diff --git a/software/src/HttpRequest.cpp b/software/src/HttpRequest.cpp new file mode 100644 index 0000000..624514b --- /dev/null +++ b/software/src/HttpRequest.cpp @@ -0,0 +1,125 @@ +/** + * Let There Be Light project + * + * @file HttpRequest.cpp + */ + +/* Includes -------------------------------------------- */ +#include "HttpRequest.hpp" + +#include +#include +#include +#include + +/* Defines --------------------------------------------- */ + +/* Type definitions ------------------------------------ */ + +/* Variable declarations ------------------------------- */ + +/* Functions ------------------------------------------- */ +static void split(const std::string &pStr, const char pDelim, std::vector &pWords) { + std::stringstream lStringStream(pStr); + std::string lWord; + + while (getline(lStringStream, lWord, pDelim)) { + pWords.push_back(lWord); + } +} + +static std::vector split(const std::string &pStr, char pDelim) { + std::vector lWords; + + split(pStr, pDelim, lWords); + + return lWords; +} + +/* Class implementation -------------------------------- */ +HttpRequest::HttpRequest() { + /* Empty */ +} + +HttpRequest::~HttpRequest() { + /* Empty */ +} + +int HttpRequest::parseRequest(const std::string &pRequestStr) { + std::vector lLines; + + if(pRequestStr.empty()) { + std::cerr << "[ERROR] Request is empty, nothing to parse !" << std::endl; + return -1; + } + + /* Set full request in class */ + mRequestStr = pRequestStr; + + std::istringstream lStrStream(pRequestStr); + std::string lLine; + + while (std::getline(lStrStream, lLine)) { + if(lLine.empty()) { + break; + } + + //std::cout << "[DEBUG] line n°" << lLines.size() + 1U << " = " << lLine << std::endl; + + lLines.push_back(lLine); + } + + /* Get Method, URL & HTTP version */ + { + std::vector lWords = split(lLines[0], ' '); + + mMethod = lWords[0U]; + mURL = lWords[1U]; + mHttpVersion = lWords[2U]; + + if (mURL.find('?') != std::string::npos) { + mShortURL = mURL.substr(0U, mURL.find('?')); + mQuery = mURL.substr(mURL.find('?') + 1U); + } else { + mShortURL = mURL; + } + + /* Print result for debugging purposes */ + /* + std::cout << "[DEBUG] Line 1 : " << std::endl + << "Method : " << mMethod << std::endl + << "URL : " << mURL << std::endl + << "Short URL : " << mShortURL << std::endl + << "Query : " << mQuery << std::endl + << "HTTP Version : " << mHttpVersion << std::endl + << std::endl; + */ + } + + return 0; +} + +/* Getters */ +std::string HttpRequest::request(void) const { + return mRequestStr; +} + +std::string HttpRequest::method(void) const { + return mMethod; +} + +std::string HttpRequest::URL(void) const { + return mURL; +} + +std::string HttpRequest::shortURL(void) const { + return mShortURL; +} + +std::string HttpRequest::query(void) const { + return mQuery; +} + +std::string HttpRequest::httpVersion(void) const { + return mHttpVersion; +}