Skip to content

Commit

Permalink
[#14] Imported the HttpRequest class from webexample
Browse files Browse the repository at this point in the history
Signed-off-by: Clovis Durand <[email protected]>
  • Loading branch information
Clovel committed Dec 12, 2019
1 parent 34e1d2a commit 47ba705
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
47 changes: 47 additions & 0 deletions software/inc/HttpRequest.hpp
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:
};
125 changes: 125 additions & 0 deletions software/src/HttpRequest.cpp
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;
}

0 comments on commit 47ba705

Please sign in to comment.