-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement HTTPRequest and HTTPResponse (#30)
- Loading branch information
1 parent
20af9d8
commit b92379d
Showing
10 changed files
with
337 additions
and
243 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
This file was deleted.
Oops, something went wrong.
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,62 @@ | ||
#pragma once | ||
|
||
#include <sstream> | ||
#include <string> | ||
#include <unordered_map> | ||
|
||
// Reference https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages#http_requests | ||
|
||
namespace nadjieb { | ||
namespace net { | ||
class HTTPRequest { | ||
public: | ||
HTTPRequest(const std::string& message) { parse(message); } | ||
|
||
void parse(const std::string& message) { | ||
std::istringstream iss(message); | ||
|
||
std::getline(iss, method_, ' '); | ||
std::getline(iss, target_, ' '); | ||
std::getline(iss, version_, '\r'); | ||
|
||
std::string line; | ||
std::getline(iss, line); | ||
|
||
while (true) { | ||
std::getline(iss, line); | ||
if (line == "\r") { | ||
break; | ||
} | ||
|
||
std::string key; | ||
std::string value; | ||
std::istringstream iss_header(line); | ||
std::getline(iss_header, key, ':'); | ||
std::getline(iss_header, value, ' '); | ||
std::getline(iss_header, value, '\r'); | ||
|
||
headers_[key] = value; | ||
} | ||
|
||
body_ = iss.str().substr(iss.tellg()); | ||
} | ||
|
||
const std::string& getMethod() const { return method_; } | ||
|
||
const std::string& getTarget() const { return target_; } | ||
|
||
const std::string& getVersion() const { return version_; } | ||
|
||
const std::string& getValue(const std::string& key) { return headers_[key]; } | ||
|
||
const std::string& getBody() const { return body_; } | ||
|
||
private: | ||
std::string method_; | ||
std::string target_; | ||
std::string version_; | ||
std::unordered_map<std::string, std::string> headers_; | ||
std::string body_; | ||
}; | ||
} // namespace net | ||
} // namespace nadjieb |
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,42 @@ | ||
#pragma once | ||
|
||
#include <sstream> | ||
#include <string> | ||
#include <unordered_map> | ||
|
||
// Reference https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages#http_responses | ||
|
||
namespace nadjieb { | ||
namespace net { | ||
class HTTPResponse { | ||
public: | ||
std::string serialize() { | ||
const std::string delimiter = "\r\n"; | ||
std::stringstream stream; | ||
|
||
stream << version_ << ' ' << status_code_ << ' ' << status_text_ << delimiter; | ||
|
||
for (const auto& header : headers_) { | ||
stream << header.first << ": " << header.second << delimiter; | ||
} | ||
|
||
stream << delimiter << body_; | ||
|
||
return stream.str(); | ||
} | ||
|
||
void setVersion(const std::string& version) { version_ = version; } | ||
void setStatusCode(const int& status_code) { status_code_ = status_code; } | ||
void setStatusText(const std::string& status_text) { status_text_ = status_text; } | ||
void setValue(const std::string& key, const std::string& value) { headers_[key] = value; } | ||
void setBody(const std::string& body) { body_ = body; } | ||
|
||
private: | ||
std::string version_; | ||
int status_code_; | ||
std::string status_text_; | ||
std::unordered_map<std::string, std::string> headers_; | ||
std::string body_; | ||
}; | ||
} // namespace net | ||
} // namespace nadjieb |
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
Oops, something went wrong.