-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest.cc
134 lines (116 loc) · 3.03 KB
/
request.cc
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "request.hpp"
#include <boost/tokenizer.hpp>
#include <sstream>
#include <iostream>
namespace http{
namespace server{
std::unique_ptr<Request> Request::Parse(const std::string& raw_request){
std::unique_ptr<Request> req(new Request);
req->raw_request_ = raw_request;
std::istringstream iss(raw_request);
std::string request_line;
std::getline(iss, request_line);
if (request_line[request_line.size()-1] == '\r')
{
request_line.pop_back();
}
size_t method_end = request_line.find(" ");
req->method_ = request_line.substr(0, method_end);
size_t uri_end = request_line.find(" ", method_end+1);
req->uri_ = request_line.substr(method_end+1, uri_end-method_end-1);
req->version_ = request_line.substr(uri_end+1);
std::string cur_header;
size_t separator;
std::string header_name;
std::string header_value;
while(std::getline(iss, cur_header))
{
if (cur_header[cur_header.size()-1] == '\r') // get rid of extra carriage returns
{
cur_header.pop_back();
}
if (cur_header == "") // break once we reach the empty line separating the headers and the body
{
break;
}
separator = cur_header.find(": ");
header_name = cur_header.substr(0, separator);
header_value = cur_header.substr(separator+2);
req->headers_.push_back(std::make_pair(header_name, header_value));
}
std::string body;
std::getline(iss, body);
req->body_ = body;
return req;
}
std::string Request::raw_request() const{
return raw_request_;
}
std::string Request::method() const{
return method_;
}
std::string Request::uri() const{
return uri_;
}
std::string Request::version() const{
return version_;
}
using Headers = std::vector<std::pair<std::string, std::string>>;
Headers Request::headers() const {
return headers_;
}
std::string Request::body() const {
return body_;
}
void Request::set_raw_request(std::string raw_request)
{
raw_request_ = raw_request;
}
void Request::set_method(std::string method)
{
method_ = method;
}
void Request::set_uri(std::string uri)
{
uri_= uri;
}
void Request::set_version(std::string version)
{
version_ = version;
}
void Request::set_body(std::string body)
{
body_ = body;
}
void Request::set_headers(Headers headers)
{
headers_ = headers;
}
std::string Request::get_header(const std::string header_name)
{
for (const auto& header : headers_)
{
if (header.first == header_name)
{
return header.second;
}
}
return "";
}
bool Request::accept_gzip() const
{
for (const auto& header : headers_)
{
if (header.first == "Accept-Encoding")
{
if (header.second.find("gzip") != std::string::npos)
{
return true;
}
return false;
}
}
return false;
}
}
}