Re-wrote HTML request/response parsing

It's now much more robust, supports requests received over multiple recvs. Supports POST variables. Supports 'content-length' header.
This commit is contained in:
Fred Nicolson
2017-04-10 17:24:57 +01:00
parent eaefd93096
commit 6b1ef27764
10 changed files with 336 additions and 172 deletions
+37 -10
View File
@@ -79,6 +79,7 @@ namespace fr
};
Http();
virtual ~Http() = default;
/*!
* Parse a raw request or response from a string
@@ -111,14 +112,6 @@ namespace fr
*/
void set_type(RequestType type);
/*!
* Access a header
*
* @param key The name of the header data to access/create
* @return The header data.
*/
std::string &operator[](const std::string &key);
/*!
* Sets the request body
*
@@ -153,6 +146,18 @@ namespace fr
*/
std::string &post(const std::string &key);
/*!
* Returns a reference to a header.
* Can be used to either set/get the value.
* If the key does not exist, then it will be
* created and an empty value will be returned.
*
* @param key The name of the header
* @return A reference to the header
*/
std::string &header(const std::string &key);
/*!
* Checks to see if a given GET variable exists
*
@@ -169,6 +174,15 @@ namespace fr
*/
bool post_exists(const std::string &key) const;
/*!
* Checks to see if a given header exists.
* Note: 'key' should be lower case.
*
* @param key The name of the header
* @return True if it does. False otherwise.
*/
bool header_exists(const std::string &key) const;
/*!
* Returns the requested URI
*
@@ -248,9 +262,22 @@ namespace fr
return (int)strtol(&hex[0], 0, 16);
}
/*!
* Converts a parameter list to a vector pair.
* i.e: ?bob=10&fish=hey
* to: <bob, 10>, <fish, hey>
*
* @param str The string to parse
* @return The vector containing the results pairs
*/
std::vector<std::pair<std::string, std::string>> parse_argument_list(const std::string &str);
void parse_header_line(const std::string &str);
//Other request info
std::unordered_map<std::string, std::string> headers;
std::unordered_map<std::string, std::string> get_variables;
std::unordered_map<std::string, std::string> header_data;
std::unordered_map<std::string, std::string> post_data;
std::unordered_map<std::string, std::string> get_data;
std::string body;
RequestType request_type;
std::string uri;
+35 -1
View File
@@ -16,8 +16,9 @@ namespace fr
{
public:
//Constructors
HttpRequest() = default;
HttpRequest();
HttpRequest(HttpRequest &&other) = default;
virtual ~HttpRequest() = default;
/*!
* Parse a HTTP response.
@@ -33,6 +34,39 @@ namespace fr
* @return The constructed HTTP request.
*/
std::string construct(const std::string &host) const override;
private:
/*!
* Parses the request header.
*
* @param header_end_pos The position in 'body' of the end of the header
*/
void parse_header(ssize_t header_end_pos);
/*!
* Parses the POST data from the body
*/
void parse_post_body();
/*!
* Parses the header type (GET/POST) from the given string.
*
* @param str The first header line
*/
void parse_header_type(const std::string &str);
/*!
* Parses the header URI
*
* @param str The first header line
*/
void parse_header_uri(const std::string &str);
//State
bool header_ended;
ssize_t last_parsed_character;
size_t content_length;
};
}
+13
View File
@@ -18,6 +18,7 @@ namespace fr
//Constructors
HttpResponse() = default;
HttpResponse(HttpResponse &&other) = default;
virtual ~HttpResponse() = default;
/*!
* Parse a HTTP response.
@@ -33,6 +34,18 @@ namespace fr
* @return The constructed HTTP response.
*/
std::string construct(const std::string &host) const override;
private:
/*!
* Parses the request header.
*
* @param header_end_pos The position in 'body' of the end of the header
*/
void parse_header(ssize_t header_end_pos);
//State
bool header_ended;
size_t content_length;
};
}
+3
View File
@@ -6,6 +6,7 @@
#define FRNETLIB_HTTPSOCKET_H
#include "Http.h"
#include "Socket.h"
namespace fr
{
@@ -27,6 +28,8 @@ namespace fr
}
virtual ~HttpSocket() = default;
Socket::Status receive(Http &request)
{
size_t received = 0;
+2
View File
@@ -25,6 +25,8 @@ namespace fr
}
virtual ~Packet() = default;
//Nasty constructor to allow things like Packet{1, 2, 3, "bob"}.
template <typename T, typename ...Args>
Packet(T const &part, Args &&...args)
+2
View File
@@ -12,6 +12,8 @@ namespace fr
class Packetable
{
public:
virtual ~Packetable() = default;
/*!
* Called to pack class data into the 'destination'
* packet.
+1 -1
View File
@@ -24,7 +24,7 @@ namespace fr
public:
SSLSocket(std::shared_ptr<SSLContext> ssl_context) noexcept;
~SSLSocket() noexcept;
virtual ~SSLSocket() noexcept;
SSLSocket(SSLSocket &&) noexcept = default;