forked from whiteblock/hobbits
-
Notifications
You must be signed in to change notification settings - Fork 7
/
request.h
65 lines (50 loc) · 1.64 KB
/
request.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
#ifndef HOBBIT_REQUEST_H
#define HOBBIT_REQUEST_H
#include <string>
#include <vector>
#include <stdexcept>
#include <algorithm>
#include "util.h"
namespace hobbit{
struct ewp_request {
std::string proto;
std::string version;
std::string command;
std::string header;
std::string body;
ewp_request(const std::string& in)
{
this->parse(in);
}
void parse(const std::string& in)
{
size_t index = in.find('\n');
const auto request_line = in.substr(0,index);
auto request = explode(request_line,' ');
if(request.size() < 5){
throw std::invalid_argument("Not enough parameters");
//Not enough parameters
}
this->proto = request[0];
this->version = request[1];
this->command = request[2];
if(index != std::string::npos){
const auto request_body = in.substr(index+1);
this->header = request_body.substr(0,std::stoi(request[3]));
this->body = request_body.substr(std::stoi(request[3]),std::stoi(request[4]));
}
}
std::string marshal() const noexcept
{
std::string out = this->proto;
out += " " + this->version;
out += " " + this->command;
out += " " + std::to_string(this->header.size());
out += " " + std::to_string(this->body.size());
out += "\n";
out += this->header + this->body;
return out;
}
};
}
#endif