-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#14] Imported the HttpRequest class from webexample
Signed-off-by: Clovis Durand <[email protected]>
- Loading branch information
Showing
2 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Let There Be Light project | ||
* | ||
* @file HttpRequest.hpp | ||
*/ | ||
|
||
/* Includes -------------------------------------------- */ | ||
#include <string> | ||
|
||
/* 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: | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/** | ||
* Let There Be Light project | ||
* | ||
* @file HttpRequest.cpp | ||
*/ | ||
|
||
/* Includes -------------------------------------------- */ | ||
#include "HttpRequest.hpp" | ||
|
||
#include <string> | ||
#include <vector> | ||
#include <iostream> | ||
#include <sstream> | ||
|
||
/* Defines --------------------------------------------- */ | ||
|
||
/* Type definitions ------------------------------------ */ | ||
|
||
/* Variable declarations ------------------------------- */ | ||
|
||
/* Functions ------------------------------------------- */ | ||
static void split(const std::string &pStr, const char pDelim, std::vector<std::string> &pWords) { | ||
std::stringstream lStringStream(pStr); | ||
std::string lWord; | ||
|
||
while (getline(lStringStream, lWord, pDelim)) { | ||
pWords.push_back(lWord); | ||
} | ||
} | ||
|
||
static std::vector<std::string> split(const std::string &pStr, char pDelim) { | ||
std::vector<std::string> lWords; | ||
|
||
split(pStr, pDelim, lWords); | ||
|
||
return lWords; | ||
} | ||
|
||
/* Class implementation -------------------------------- */ | ||
HttpRequest::HttpRequest() { | ||
/* Empty */ | ||
} | ||
|
||
HttpRequest::~HttpRequest() { | ||
/* Empty */ | ||
} | ||
|
||
int HttpRequest::parseRequest(const std::string &pRequestStr) { | ||
std::vector<std::string> lLines; | ||
|
||
if(pRequestStr.empty()) { | ||
std::cerr << "[ERROR] <parseRequest> 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] <HttpRequest::parseRequest> line n°" << lLines.size() + 1U << " = " << lLine << std::endl; | ||
|
||
lLines.push_back(lLine); | ||
} | ||
|
||
/* Get Method, URL & HTTP version */ | ||
{ | ||
std::vector<std::string> 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] <HttpRequest::parseRequest> 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; | ||
} |