-
Notifications
You must be signed in to change notification settings - Fork 7
/
parser.cpp
201 lines (178 loc) · 4.63 KB
/
parser.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "parser.h"
std::vector<std::string> explode( std::string delim, std::string source );
bool parse_http( std::string pdata, http_request *req )
{
// printf("--------\nparse_http call with packet:\n%s\n-----------\n", pdata.c_str() );
std::string method, path, host, full_url, data;
int http=1;
int posf, pos, tam;
/*
// Find headers part ( before '\r\n' ):
if( pos = pdata.find("\r\n\r\n",0) == std::string::npos )
return false; // packet has no http header part
*/
//data = pdata.substr(0, pos+1);
data = pdata;
// std::vector<std::string> strings = explode( "\r\n", pdata );
std::vector<std::string> strings = explode( "\r\n", pdata );
if( strings.size() < 2 ) {
// printf("\nstrings.size()\n");
return false; // too low strings
}
// First string must me GET/POST/HEAD
std::string line, tmp;
line = strings.front();
posf = line.find(" ", 0); // first value before whitespace
tmp = line.substr(0, posf);
// printf("\n\nTRYING GET/POST/HEAD\n");
if( tmp == "GET" || tmp == "POST" || tmp == "HEAD" ) {
// valid request beginning
method = tmp;
// find path
pos = line.find(" ", posf+1);
// printf("\n\nTRYING first match\n");
if( pos == std::string::npos ) {
// printf("\npos == std::string::npos\n");
return false;
}
tmp = line.substr( posf+1, pos-(posf+1) );
// printf("\n\nTRYING 2ND MATCH\n");
if( tmp == "" ) {
// printf("\ntmp==''\n");
return false;
}
path = tmp;
// http ver.:
posf = line.find("\r", pos);
// printf("\n\nTRYING http ver\n");
if( pos == std::string::npos ) {
// printf("\npos == std::string::npos (2)\n");
return false;
}
tmp = line.substr(pos+1, posf);
// printf("\n\ntrying http/1.1\n");
if( tmp == "HTTP/1.1" )
http = 1;
else if ( tmp == "HTTP/1.0" )
http = 0;
else {
// printf("\nhttp1.0/0.0 failed\n");
return false;
}
} else {
// printf("\n NOT get/post/head\n");
return false;
}
// printf("%s","Got method and http version.\n");
// loop trough all other lines
// for( std::string& str : strings ) {
for( std::vector<std::string>::iterator it = strings.begin(); it != strings.end(); ++it ) {
std::string str = *it;
if( str.length() < 4 )
continue;
// printf("STRING: '%s'\n", str.c_str() );
posf = str.find(" ", 0);
if( posf != std::string::npos ) {
// string with spaces
tmp = str.substr(0, posf);
if( make_lowercase( tmp ) == "host:" )
{
tmp = str.substr( posf+1 );
// any spaces more?
if( tmp.find(" ", 0) != std::string::npos ) {
return false;
}
host = tmp;
// printf("host found: '%s'\n", host.c_str());
break;
}
}
}
// if http/1.1, path must begin with /
if( http == 1 ) {
if( path.substr(0,1) != "/" ) {
// printf("\nvrong http 1.1 path\n");
return false;
}
full_url = host + path;
}
if( http == 0 ) {
// address begins with slash?
if( path.substr(0,1) == "/" )
{
if( host != "" )
full_url = host+path;
} else {
// path contains full url
if( path.substr(0,4) == "http" ) {
tmp = path.substr(0, 7); // cut http://
full_url = tmp;
// get domain info
if( pos = tmp.find("/") != std::string::npos ) {
host = path.substr(0, pos); // eg. http://www.ru/
} else {
host = path; // eg. http://www.ru
}
}
}
}
if( host == "" || path == "" || method == "" ) {
// printf("\nno host/path/method\nhost: '%s' path: '%s' method: '%s'\n", host.c_str(), path.c_str(), method.c_str() );
return false;
}
else
{
req->host = host;
req->full_url = full_url;
req->method = method;
return true;
}
}
std::istream &getln( std::istream & in, std::string & out )
{
char c;
while( in.get(c).good()) {
if( c == '\n' ) {
c = in.peek();
if( in.good() ) {
if( c == '\r' ) {
in.ignore();
}
}
break;
}
out.append(1, c);
}
return in;
}
std::string spaces( std::string src ) {
// src.erase( std::unique(src.begin(), src.end(),[](char a, char b){ return a == ' ' && b == ' '; } ), src.end() );
for( int j=0; j<src.length(); j++ ) {
if( src[j] == ' ' && src[j+1] == ' ' )
src.erase(j,1);
}
return src;
}
std::vector<std::string> explode( std::string delim, std::string source )
{
std::vector<std::string> arr;
size_t pos = 0;
std::string token;
int i=0;
while((pos = source.find(delim)) != std::string::npos) {
token = source.substr( 0, pos );
source.erase(0, pos + delim.length() );
token = spaces( token );
arr.push_back(token);
if( i > 7 )
break; // take only first 4 strings
i++;
}
return arr;
}
std::string make_lowercase( const std::string& in )
{
std::string out;
std::transform( in.begin(), in.end(), std::back_inserter( out ), ::tolower );
return out;
}