-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.cpp
54 lines (47 loc) · 1.46 KB
/
request.cpp
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
#include "request.hpp"
#define HTTP_MSIZE 8192
using namespace std;
void Request::parse(const string &msg){
std::istringstream strm(msg);
std::string firstline;
getline(strm, firstline);
std::string method_;
std::istringstream l0(firstline);
getline(l0, method_, ' ');
getline(l0, ressource, ' ');
getline(l0, protocol, ' ');
protocol.pop_back(); // getting rid of the \r
if (method_ == "GET") method = GET;
if (method_ == "POST") method = POST;
std::string line;
std::string::size_type index;
while(getline(strm, line) && line != "\r"){
index = line.find(':', 0);
if (index != std::string::npos){
header.insert(std::make_pair(
boost::algorithm::trim_copy(line.substr(0, index)),
boost::algorithm::trim_copy(line.substr(index +1))
));
}
}
}
Request::Request(const string &msg, Socket * sock){
parse(msg);
std::lock_guard<std::mutex> l{sock->mtx};
if (method == POST){
try{
body.assign(sock->read_body(4));
//body.assign(sock->read_body(std::stoi(header["Content-Length"])));
}
catch(std::invalid_argument& e){}
}
if (header["Connection"] == "keep-alive" || protocol != "HTTP/1.0"){
sock->keepalive = true;
}
}
Request::Request(const Request& other){
method = other.method;
header = other.header;
body = other.body;
ressource = other.ressource;
}