29 #ifndef HTTP_CONTENT_H
30 #define HTTP_CONTENT_H
33 #include <unordered_map>
174 return HTTPMessage::HttpStatus::Continue;
176 return HTTPMessage::HttpStatus::SwitchingProtocol;
178 return HTTPMessage::HttpStatus::Ok;
180 return HTTPMessage::HttpStatus::Created;
182 return HTTPMessage::HttpStatus::Accepted;
184 return HTTPMessage::HttpStatus::NonAuthoritativeInformation;
186 return HTTPMessage::HttpStatus::NoContent;
188 return HTTPMessage::HttpStatus::ResetContent;
190 return HTTPMessage::HttpStatus::PartialContent;
192 return HTTPMessage::HttpStatus::MultipleChoice;
194 return HTTPMessage::HttpStatus::MovedPermanently;
196 return HTTPMessage::HttpStatus::Found;
198 return HTTPMessage::HttpStatus::SeeOther;
200 return HTTPMessage::HttpStatus::NotModified;
203 return HTTPMessage::HttpStatus::TemporaryRedirect;
205 return HTTPMessage::HttpStatus::PermanentRedirect;
207 return HTTPMessage::HttpStatus::BadRequest;
209 return HTTPMessage::HttpStatus::Unauthorized;
212 return HTTPMessage::HttpStatus::PaymentRequired;
214 return HTTPMessage::HttpStatus::Forbidden;
216 return HTTPMessage::HttpStatus::NotFound;
218 return HTTPMessage::HttpStatus::MethodNotAllowed;
220 return HTTPMessage::HttpStatus::NotAcceptable;
222 return HTTPMessage::HttpStatus::ProxyAuthenticationRequired;
224 return HTTPMessage::HttpStatus::RequestTimeout;
226 return HTTPMessage::HttpStatus::Conflict;
228 return HTTPMessage::HttpStatus::Gone;
230 return HTTPMessage::HttpStatus::LengthRequired;
232 return HTTPMessage::HttpStatus::PreconditionFailed;
234 return HTTPMessage::HttpStatus::PayloadTooLarge;
236 return HTTPMessage::HttpStatus::UriTooLong;
238 return HTTPMessage::HttpStatus::UnsupportedMediaType;
240 return HTTPMessage::HttpStatus::RangeNotSatisfiable;
242 return HTTPMessage::HttpStatus::ExpectationFailed;
245 return HTTPMessage::HttpStatus::ImaTeapot;
247 return HTTPMessage::HttpStatus::MisdirectedRequest;
249 return HTTPMessage::HttpStatus::TooEarly;
251 return HTTPMessage::HttpStatus::UpgradeRequired;
253 return HTTPMessage::HttpStatus::PreconditionRequired;
255 return HTTPMessage::HttpStatus::TooManyRequests;
257 return HTTPMessage::HttpStatus::RequestHeaderFieldsTooLarge;
259 return HTTPMessage::HttpStatus::UnavailableForLegalReasons;
261 return HTTPMessage::HttpStatus::InternalServerError;
263 return HTTPMessage::HttpStatus::NotImplemented;
265 return HTTPMessage::HttpStatus::BadGateway;
267 return HTTPMessage::HttpStatus::ServiceUnavailable;
269 return HTTPMessage::HttpStatus::GatewayTimeout;
271 return HTTPMessage::HttpStatus::HttpVersionNotSupported;
273 return HTTPMessage::HttpStatus::VariantAlsoNegotiates;
275 return HTTPMessage::HttpStatus::InsufficientStorage;
277 return HTTPMessage::HttpStatus::NotExtended;
279 return HTTPMessage::HttpStatus::NetworkAuthenticationRequired;
281 return HTTPMessage::HttpStatus::InternalServerError;
298 return "Switching Protocol";
306 return "Non-Authoritative Information";
310 return "Reset Content";
312 return "Partial Content";
314 return "Multiple Choice";
316 return "Moved Permanently";
322 return "Not Modified";
325 return "Temporary Redirect";
327 return "Permanent Redirect";
329 return "Bad Request";
331 return "Unauthorized";
334 return "Payment Required";
340 return "Method Not Allowed";
342 return "Not Acceptable";
344 return "Proxy Authentication Required";
346 return "Request Timeout";
352 return "Length Required";
354 return "Precondition Failed";
356 return "Payload Too Large";
358 return "URI Too Long";
360 return "Unsupported Media Type";
362 return "Requested Range Not Satisfiable";
364 return "Expectation Failed";
367 return "I'm a teapot";
369 return "Misdirected Request";
373 return "Upgrade Required";
375 return "Precondition Required";
377 return "Too Many Requests";
379 return "Request Header Fields Too Large";
381 return "Unavailable for Legal Reasons";
383 return "Internal Server Error";
385 return "Not Implemented";
387 return "Bad Gateway";
389 return "Service Unavailable";
391 return "Gateway Timeout";
393 return "HTTP Version Not Supported";
395 return "Variant Also Negotiates";
397 return "Insufficient Storage";
399 return "Not Extended";
401 return "Network Authentication Required";
414 if(name ==
"Content-Length") {
416 }
else if(name ==
"Request URI"){
418 }
else if(name ==
"Host"){
440 m_headers.insert(headers.begin(), headers.end());
449 inline std::string
GetHeader(
const std::string& name)
const
471 else if (
m ==
"HEAD")
473 else if (
m ==
"PATCH")
475 else if (
m ==
"POST")
529 const auto schemeEndPosition = url.find(
"://");
530 if (schemeEndPosition != std::string::npos)
532 m_scheme = url.substr(0, schemeEndPosition);
533 m_path = url.substr(schemeEndPosition + 3);
541 const auto fragmentPosition =
m_path.find(
'#');
544 if (fragmentPosition != std::string::npos)
545 m_path.resize(fragmentPosition);
547 const auto pathPosition =
m_path.find(
'/');
549 if (pathPosition == std::string::npos)
560 const auto portPosition =
m_domain.find(
':');
562 if (portPosition != std::string::npos)
635 std::stringstream output;
638 if (
m_method == HTTPMessage::HttpMethod::NONE)
659 output << header.first <<
": " << header.second <<
CarriageReturn;
703 m_body = std::vector<uint8_t>(body);
739 const std::string& url,
740 const std::string& method
751 const std::string& url,
752 const std::string& method,
753 const std::string& body)
759 std::vector<uint8_t>(body.begin(), body.end())
764 const std::string& url,
765 const std::string& method,
766 const std::vector<uint8_t>& body
772 SetHeader(
"Accept-Language",
"en-us,en;q=0.5");
773 SetHeader(
"Accept-Encoding",
"gzip,deflate");
774 SetHeader(
"Accept-Charset",
"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
777 SetHeader(
"Cookie",
"PHPSESSID=r2t5uvjq435r4q7ib3vtdjq120");
796 const std::string& body
800 std::vector<uint8_t>(body.begin(), body.end())
806 const std::vector<uint8_t>& body
811 SetHeader(
"Server",
"QKDNetSim KMS/3.36");
812 SetHeader(
"Date",
"Sat, 28 Aug 2022 04:36:25 GMT");
816 SetHeader(
"Vary",
"Accept-Encoding, Cookie");
817 SetHeader(
"Cache-Control",
"max-age=3600, public");
824 const std::string& body,
825 const std::unordered_map<std::string, std::string>& headers
830 std::vector<uint8_t>(body.begin(), body.end())
836 const std::vector<uint8_t>& body,
837 const std::unordered_map<std::string, std::string>& headers
954 std::string cleanText = buffer;
956 Parse(httpMessage, std::vector<uint8_t>(cleanText.begin(), cleanText.end()));
977 std::string temp =
"";
980 bool skipNext =
false;
983 std::string headerKey =
"";
986 bool hasMessageBody =
false;
989 size_t bodyStartIndex = 0;
991 for (
size_t index = 0; index < buffer.size(); index++)
993 uint8_t character = buffer[index];
1008 hasMessageBody =
true;
1010 bodyStartIndex = index;
1020 if (character ==
' ')
1052 if (character ==
' ')
1063 else if (character ==
'\r')
1082 int code = std::stoi(temp);
1094 else if (character ==
'\r')
1128 httpMessage->
SetHeader(headerKey, temp);
1158 if (hasMessageBody){
1161 buffer.begin() + bodyStartIndex,
1185 return !(c>=0 && c <128);
1190 std::remove_if(str.begin(),str.end(),
invalidChar), str.end()
The basic class to represent both HTTP requests and responses.
void SetVersion(const std::string &version)
Set the version of this HTTP message to the string specified.
std::string GetHeader(const std::string &name) const
Get the string value of a single header from the message.
std::string m_path
The path for the resource specified in the message.
uint16_t GetStatusCode() const
Get the status code for this message.
void CreateRequest(const std::string &url, const std::string &method, const std::string &body)
void CreateResponse(const HttpStatus status, const std::string &body)
std::string GetVersion() const
Get the current HTTP version for this message.
std::string GetRequestUri() const
std::string ToString()
Takes the headers added to the message along with the body and outputs it to a std::string for use in...
void SetMethod(const std::string &m)
Set the associated message method for this message.
@ PATCH
Http Method Patch.
@ DELETE
Http Method Delete.
std::string GetMethodToString() const
std::vector< uint8_t > & GetMessageBody()
Get the body vector for this message.
std::string m_version
The version used for this HTTP message as a string.
void CreateResponse(const HttpStatus status, const std::string &body, const std::unordered_map< std::string, std::string > &headers)
std::string GetUri() const
Grab the uri.
void CreateRequest(const std::string &url, const std::string &method)
@ NetworkAuthenticationRequired
@ UnavailableForLegalReasons
@ NonAuthoritativeInformation
@ RequestHeaderFieldsTooLarge
@ ProxyAuthenticationRequired
@ HttpVersionNotSupported
std::string StatusTextFromStatusCode(const uint16_t statusCode)
To be returned with a status code in a response is a status text describing the status code by text r...
void CreateRequest(const std::string &url, const std::string &method, const std::vector< uint8_t > &body)
std::string GetMessageBodyString()
std::unordered_map< std::string, std::string > m_headers
An unordered_map of headers using a std::string for both the key and the value.
std::string m_request_uri
void SetStatusMessage(const std::string &message)
Set the status message of this HTTP message.
uint16_t m_statusCode
A status code for this message.
std::string m_statusMessage
A status message to be associated with the status code for this message.
size_t GetContentLength()
Return the size of the binary body vector.
std::string GetPath() const
Grab the current associated path of this message.
void CreateResponse(const HttpStatus status, const std::vector< uint8_t > &body)
void SetFragmented(bool value)
void CreateResponse(const HttpStatus status)
void SetPath(const std::string &url)
Set the path of this message, which will be used if it is a request.
void SetMessageBody(const std::string &body)
Set the body of this message to a string value.
void SetStatusCode(uint16_t code)
Set the status code of this HTTP message.
size_t HeaderCount()
Return the amount of headers in the message.
HTTPMessage::HttpStatus GetStatus()
To be returned with a status code in a response is a status text describing the status code by text r...
bool m_fragmented
Whether the payload size is less then content-length.
void SetMessageBody(const std::vector< uint8_t > &body)
Set the body of this message to an unsigned 8-bit binary value.
HTTPMessage & SetHeader(const std::string &name, const std::string &value)
Set a header in the map to the value provided.
HTTPMessage & SetHeaders(const std::unordered_map< std::string, std::string > &headers)
Set a number of headers based on a generic map of keys and values.
HTTPMessage::HttpMethod GetMethod() const
uint32_t GetHeadersSize()
std::string GetStatusMessage() const
Get the current status message for this message.
HTTPMessage::HttpMethod m_method
The HTTP method for this message.
void CreateResponse(const HttpStatus status, const std::vector< uint8_t > &body, const std::unordered_map< std::string, std::string > &headers)
std::vector< uint8_t > m_body
A vector of unsigned 8-bit integers used to store message bodies.
A basic class to parse a HTTP message, both request and response.
void Parse(HTTPMessage *httpMessage, const std::string &buffer)
Parse a std::string to a HTTP message.
static bool invalidChar(char c)
void Parse(HTTPMessage *httpMessage, const std::vector< uint8_t > &buffer)
Parse a binary vector to an HTTP message.
void stripUnicode(std::string &str)
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Every class exported by the ns3 library is enclosed in the ns3 namespace.
const std::string HTTPVersion1
The string for HTTP version 1.0.
MessageParserState
An enum of states that the HTTPMessageParser can be in.
const std::string CarriageReturn
HTTP defines that any lines must be seperated with a carriage return.
const std::string HTTPVersion11
The string for HTTP version 1.1.