-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.cpp
More file actions
executable file
·144 lines (117 loc) · 4.51 KB
/
parse.cpp
File metadata and controls
executable file
·144 lines (117 loc) · 4.51 KB
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
135
136
137
138
139
140
141
142
143
144
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jjosephi <jjosephi@student.42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/05 21:59:47 by jjosephi #+# #+# */
/* Updated: 2020/06/15 00:42:00 by jjosephi ### ########.fr */
/* */
/* ************************************************************************** */
#include "server.hpp"
#include <cstdio>
#include <stdlib.h>
#include <map>
const char* ws = " \t\n\r\f\v";
// trim from end of string (right)
inline std::string& rtrim(std::string& s, const char* t = ws)
{
s.erase(s.find_last_not_of(t) + 1);
return s;
}
// trim from beginning of string (left)
inline std::string& ltrim(std::string& s, const char* t = ws)
{
s.erase(0, s.find_first_not_of(t));
return s;
}
// trim from both ends of string (right then left)
inline std::string& trim(std::string& s, const char* t = ws)
{
return ltrim(rtrim(s, t), t);
}
std::map<std::string, std::string> parse_request_line(const char *s, int *error){
std::map<std::string, std::string> request;
const char *head = s;
const char *tail = s;
size_t len = strlen(s);
const char *msg_end = s + len;
//RFC 7230 3.1.1
if (len > 8000)
*error = 400;
// Find request type
while (tail != msg_end && *tail != ' ') ++tail;
request["Type"] = std::string(head, tail);
//RFC 7230 3.1.1
if (tail - head > 8)
*error = 501;
// Find path
while (tail != msg_end && *tail == ' ') ++tail;
head = tail;
while (tail != msg_end && *tail != ' ') ++tail;
request["Path"] = std::string(head, tail);
//RFC 7230 3.1.1
if (tail - head > 2000)
*error = 414;
// Find HTTP version
while (tail != msg_end && *tail == ' ') ++tail;
head = tail;
while (tail != msg_end && *tail != '\r') ++tail;
request["Version"] = std::string(head, tail);
return (request);
}
std::map<std::string, std::string> parse_headers(const char *s, int *error){
std::map<std::string, std::string> headers;
std::string header;
std::string::size_type index;
std::istringstream resp(s);
while (std::getline(resp, header) && header != "\r") {
index = header.find(':', 0);
if(index != std::string::npos) {
std::string key = header.substr(0, index);
std::string value = header.substr(index + 1);
// RFC 7230, 3.2.4
if (key.find(' ') != std::string::npos)
*error = 400;
headers.insert(std::make_pair(
trim(key, ws),
trim(value, ws)
));
}
}
//RFC 7230 5.4
if (headers.find("Host") == headers.end() )
*error = 400;
headers["request"] = std::string(s);
return (headers);
}
//TODO: implement check for obs fold (RFC 7230, 3.2.4)
//https://stackoverflow.com/questions/31237198/is-it-possible-to-include-multiple-crlfs-in-a-http-header-field/31324422
// int main(int argc, char* argv[]) {
// //Random request message here
// const char *s =
// "GET / HTTP/1.1\r\n"
// "Host: index/8080"
// "Connection: keep-alive\r\n"
// "Upgrade-Insecure-Requests: 1\r\n"
// "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
// "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0.3 Safari/604.5.6\r\n"
// "Accept-Language: en-us\r\n"
// "DNT: 1\r\n"
// "Accept-Encoding: gzip, deflate\r\n"
// "\r\n"
// "Usually GET requests don\'t have a body\r\n"
// "But I don\'t care in this case :)";
// //TODO: Parse body of the request
// std::map<std::string, std::string> request = parse_request_line(s);
// std::map<std::string, std::string> headers = parse_headers(s);
// //Printing here, causes warnings(C++11). Just for testing purposes.
// for(auto& kv: request) {
// std::cout << "KEY: `" << kv.first << "`, VALUE: `" << kv.second << '`' << std::endl;
// }
// for(auto& kv: headers) {
// std::cout << "KEY: `" << kv.first << "`, VALUE: `" << kv.second << '`' << std::endl;
// }
// return(0);
// }