32#include <unordered_map>
297 return "Switching Protocol";
305 return "Non-Authoritative Information";
309 return "Reset Content";
311 return "Partial Content";
313 return "Multiple Choice";
315 return "Moved Permanently";
321 return "Not Modified";
324 return "Temporary Redirect";
326 return "Permanent Redirect";
328 return "Bad Request";
330 return "Unauthorized";
333 return "Payment Required";
339 return "Method Not Allowed";
341 return "Not Acceptable";
343 return "Proxy Authentication Required";
345 return "Request Timeout";
351 return "Length Required";
353 return "Precondition Failed";
355 return "Payload Too Large";
357 return "URI Too Long";
359 return "Unsupported Media Type";
361 return "Requested Range Not Satisfiable";
363 return "Expectation Failed";
366 return "I'm a teapot";
368 return "Misdirected Request";
372 return "Upgrade Required";
374 return "Precondition Required";
376 return "Too Many Requests";
378 return "Request Header Fields Too Large";
380 return "Unavailable for Legal Reasons";
382 return "Internal Server Error";
384 return "Not Implemented";
386 return "Bad Gateway";
388 return "Service Unavailable";
390 return "Gateway Timeout";
392 return "HTTP Version Not Supported";
394 return "Variant Also Negotiates";
396 return "Insufficient Storage";
398 return "Not Extended";
400 return "Network Authentication Required";
413 if(name ==
"Content-Length") {
415 }
else if(name ==
"Request URI"){
417 }
else if(name ==
"Host"){
439 m_headers.insert(headers.begin(), headers.end());
448 inline std::string
GetHeader(
const std::string& name)
const
470 else if (m ==
"HEAD")
472 else if (m ==
"PATCH")
474 else if (m ==
"POST")
736 const std::string&
url,
737 const std::string& method
748 const std::string&
url,
749 const std::string& method,
750 const std::string&
body)
756 std::vector<uint8_t>(
body.begin(),
body.end())
761 const std::string&
url,
762 const std::string& method,
763 const std::vector<uint8_t>&
body
769 SetHeader(
"Accept-Language",
"en-us,en;q=0.5");
770 SetHeader(
"Accept-Encoding",
"gzip,deflate");
771 SetHeader(
"Accept-Charset",
"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
774 SetHeader(
"Cookie",
"PHPSESSID=r2t5uvjq435r4q7ib3vtdjq120");
792 const std::string&
body
796 std::vector<uint8_t>(
body.begin(),
body.end())
802 const std::vector<uint8_t>&
body
807 SetHeader(
"Server",
"QKDNetSim KMS/3.36");
808 SetHeader(
"Date",
"Sat, 28 Aug 2022 04:36:25 GMT");
812 SetHeader(
"Vary",
"Accept-Encoding, Cookie");
813 SetHeader(
"Cache-Control",
"max-age=3600, public");
819 const std::string&
body,
820 const std::unordered_map<std::string, std::string>& headers
825 std::vector<uint8_t>(
body.begin(),
body.end())
831 const std::vector<uint8_t>&
body,
832 const std::unordered_map<std::string, std::string>& headers
972 std::string
temp =
"";
986 for (
size_t index = 0; index < buffer.size(); index++)
1191 while (std::getline(
ss, line)) {
1192 if (line.find(
"Content-Length:") != std::string::npos) {
1214 return !(
c>=0 &&
c <128);
1219 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::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()
HTTPMessage & SetHeader(const std::string &name, const std::string &value)
Set a header in the map to the value provided.
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
std::vector< uint8_t > & GetMessageBody()
Get the body vector for this message.
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.
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)
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.
void CreateResponse(const HttpStatus status)
bool IsFragmented() const
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.
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::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.
size_t GetContentLength() const
Return the size of the binary body vector.
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.
HTTPMessage::HttpStatus GetStatus() const
To be returned with a status code in a response is a status text describing the status code by text r...
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.
bool TryExtractHttpMessage(const std::string &bufferStr, std::string &outMessage, size_t &messageSize)
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)
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
const std::string HTTPVersion1
The string for HTTP version 1.0.
static unsigned int value(char c)
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.