-
Notifications
You must be signed in to change notification settings - Fork 7
/
requestparser.h
88 lines (70 loc) · 2.08 KB
/
requestparser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef REQUESTPARSER_H
#define REQUESTPARSER_H
/**
* @file requestparser.h
* @brief File contains definition of RequestParser
*/
#include <string>
#include <map>
/**
* @brief Extracts data from HTTP headers, given the data received from client socket
* @todo Implement extraction of request body (e.g. in POST requests)
*/
class RequestParser
{
bool half_end_of_line;
bool end_of_line;
bool first_line;
bool beginning;
std::string method;
std::string path;
std::string proto_ver;
std::string tmp_header_name;
std::string tmp_header_value;
char previous_char;
std::map<std::string, std::string> headers;
bool headers_available;
public:
/**
* Initialize a new parser instance.
*/
RequestParser();
/**
* Feeds the parser with next portion of data from client.
* Should be called in loop, after each chunk of data is received.
*
* @param buf Pointer to the buffer in which data from client socket is stored
* @param size Size of the chunk of data
*/
void processChunk(const char *buf, size_t size);
/**
* Check if the end of headers was reached by the parser.
*
* @return Information if all data was already extracted from headers and can be safely accessed
*/
bool allHeadersAvailable();
/**
* Get extracted headers.
*
* @return Headers in form of std::map (name -> value)
*/
std::map<std::string, std::string> getHeaders();
/**
* @return String representing the HTTP method used in request
*/
std::string getMethod();
/**
* @return String representing the requested path
*/
std::string getPath();
/**
* @return String representing the protocol (and version) used to perform the request
*/
std::string getProtocol();
/**
* Prepare the object to handle another request (clear all extracted data).
*/
void reset();
virtual ~RequestParser() = default;
};
#endif // REQUESTPARSER_H