-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.cpp
426 lines (393 loc) · 10.5 KB
/
http.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// copyright Patrick Horgan
// source is open, feel free to use it as you wish with no restrictions
// except that this copyright notice must be preserved intact
#include "http.h"
#include <cstring>
#include <iostream>
#include <sstream>
#include <vector>
#include <fstream>
/*
All this stuff is syntax for HTTP
reserved = gen-delims / sub-delims
gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
/ "*" / "+" / "," / ";" / "="
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "-"
ALPHA = *( %41-5A / %61-7A )
DIGIT = *( %30-39 )
pct-encoded = "%" HEXDIG HEXDIG
HEXDIG = "A" / "B" / "C" / "D" / "E" / "F"
/ "a" / "b" / "c" / "d" / "e" / "f" /
; %41-46 / %61-66
URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
hier-part = "//" authority path-abempty
/ path-absolute
/ path-rootless
/ path-empty
path = path-abempty ; begins with "/" or is empty
/ path-absolute ; begins with "/" but not "//"
/ path-noscheme ; begins with a non-colon segment
/ path-rootless ; begins with a segment
/ path-empty ; zero characters
path-abempty = *( "/" segment )
path-absolute = "/" [ segment-nz *( "/" segment ) ]
path-noscheme = segment-nz-nc *( "/" segment )
path-rootless = segment-nz *( "/" segment )
path-empty = 0<pchar>
segment = *pchar
segment-nz = 1*pchar
segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
; non-zero-length segment without any colon ":"
pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
query = *( pchar / "/" / "?" )
fragment = *( pchar / "/" / "?" )
authority = [ userinfo "@" ] host [ ":" port ]
userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
host = IP-literal / IPv4address / reg-name
IP-literal = "[" ( IPv6address / IPvFuture ) "]"
IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
IPv6address = 6( h16 ":" ) ls32
/ "::" 5( h16 ":" ) ls32
/ [ h16 ] "::" 4( h16 ":" ) ls32
/ [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
/ [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
/ [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
/ [ *4( h16 ":" ) h16 ] "::" ls32
/ [ *5( h16 ":" ) h16 ] "::" h16
/ [ *6( h16 ":" ) h16 ] "::"
ls32 = ( h16 ":" h16 ) / IPv4address
; least-significant 32 bits of address
h16 = 1*4HEXDIG
; 16 bits of address represented in hexadecimal
IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
dec-octet = DIGIT ; 0-9
/ %x31-39 DIGIT ; 10-99
/ "1" 2DIGIT ; 100-199
/ "2" %x30-34 DIGIT ; 200-249
/ "25" %x30-35 ; 250-255
reg-name = *( unreserved / pct-encoded / sub-delims )
port = *DIGIT
*/
/*
std::stringstream ss;
char timebuffer[30];
struct tm thetm;
time_t thetime_t=time(NULL);
gmtime_r(&thetime_t,&thetm);
strftime(timebuffer,30,"%a, %d %b %Y %T GMT",&thetm);
*/
std::string
file2string(std::string filename)
{
std::ifstream file(filename.c_str());
if(!file.is_open()){
// If they passed a bad file name, or one we have no read access to,
// we pass back an empty string.
return "";
}
// find out how much data there is
file.seekg(0,std::ios::end);
std::streampos length = file.tellg();
file.seekg(0,std::ios::beg);
// Get a string that size and fill it
std::string s(length,0);
file.read(&s[0],length);
file.close();
return s;
}
fileblob::~fileblob()
{
if(blob){
delete [] blob;
blob=0;
}
}
std::string
fileblob::curdir()
{
int lastslash=path.rfind('/');
return std::string(path.substr(0,lastslash));
}
std::string
fileblob::ext()
{
int thedot=path.rfind('.');
return std::string(path.substr(thedot+1));
}
fileblob::fileblob(std::string filename):stringvalid(false)
{
std::ifstream file(filename.c_str());
if(!file.is_open()){
// If they passed a bad file name, or one we have no read access to,
// we pass back an empty string.
blob_size=0;
blob=0;
return;
}
path=filename;
// find out how much data there is
file.seekg(0,std::ios::end);
std::streampos fsize=0, length = file.tellg()-fsize;
file.seekg(0,std::ios::beg);
blob_size=length;
try{
blob=new uint8_t[blob_size];
}catch(...){
blob=0;
blob_size=0;
return;
}
try{
file.read(reinterpret_cast<char *>(blob),length);
file.close();
}catch(...){
blob=0;
blob_size=0;
}
}
// copy constructor for authority
authority::authority(const authority&a):
userinfo(a.userinfo),host(a.host),port(a.port){}
// usual use is to get a string and parse it
authority::authority(std::string s)
{
size_t p,n;
if((p=s.find('@'))!=std::string::npos){
userinfo=std::string(s,0,p);
p+=1;
}else{
p=0;
}
if((n=s.find(':',p))!=std::string::npos){
port=std::string(s,n+1);
host=std::string(s,p,n-p);
}else{
host=std::string(s,p);
}
}
// assignment operator
authority&
authority::operator=(const authority& a)
{
userinfo=a.userinfo;
host=a.host;
port=a.port;
return *this;
}
// simplified version gets a host and port explicitly
authority::authority(std::string h,std::string p):host(h),port(p){}
// explicit user host and port
authority::authority(std::string u,std::string h,std::string p):
userinfo(u),host(h),port(p){}
// Convert the authority back to uri text
std::string
authority::to_string() const
{
std::string retval;
if(userinfo!=""){
retval+=userinfo+'@';
}
retval+=host;
if(port!=""){
retval+=":"+port;
}
return retval;
}
// main constructor for http_request_line
http_request_line::http_request_line(const char *inrequest,
std::map<std::string,std::string>& headers)
{
// we expect the line to have any trailing \r or \n removed
const char *ctr=inrequest,*savectr;
int util;
valid=false;
while(*ctr!=' '&&*ctr) ctr++;
if(*ctr=='\0'){
// no request method or anything else
return;
}
method=std::string(inrequest,ctr);
if(!isvalidmethod()){
return;
}
while(*ctr==' '&&*ctr) ctr++;
if(*ctr=='\0'){
// no uri
return;
}
// pointing at beginning of uri
savectr=ctr;
while(*ctr!=' '&&*ctr) ctr++;
if(*ctr=='\0'){
// nothing past the request so it looks like a 0.9 request
major_release=0;
minor_release=9;
// here's the problem
theuri=uri(savectr,headers["Host"]);
valid=true;
return;
}
theuri=uri(std::string(savectr,ctr),headers["Host"]);
// either pointing at HTTP/1.x or space
while(*ctr==' '&&*ctr) ctr++;
if(*ctr=='\0'){
// once again it was a 0.9 but with trailing space
major_release=0;
minor_release=9;
valid=true;
return;
}
// now we should be pointing at HTTP/1.x
savectr=ctr;
if(strncmp("HTTP/",ctr,5)!=0){
return;
}
ctr+=5;
// now pointing at the major part of the release
if(*ctr>='0' && *ctr<='9'){
util=0;
while(*ctr>='0' && *ctr<='9'){
util=10*util+*ctr-'0';
ctr++;
}
}else{
// no major version so return invalid
return;
}
if(*ctr!='.'){
return;
}
major_release=util;
ctr++;
if(*ctr>='0' && *ctr<='9'){
util=0;
while(*ctr>='0' && *ctr<='9'){
util=10*util+*ctr-'0';
ctr++;
}
}else{
// no minor version so return invalid
return;
}
minor_release=util;
valid=true;
return;
}
// returns false if request isn't valid
inline bool
http_request_line::isvalidmethod()
{
return method=="GET" || method=="OPTIONS" || method=="HEAD"
|| method=="POST" || method=="PUT" || method=="DELETE"
|| method=="TRACE" || method=="CONNECT";
}
// getter routines
std::string http_request_line::get_minor_release()
{
std::stringstream ss;
ss << minor_release;
return ss.str();
}
std::string http_request_line::get_major_release()
{
std::stringstream ss;
ss << major_release;
return ss.str();
}
// turn a http_request_line object back into a http request
std::string
http_request_line::to_string() const
{
std::stringstream ssmaj;
std::stringstream ssmin;
ssmaj << major_release;
ssmin << minor_release;
return std::string(method+" "+theuri.to_string()+" HTTP/"+ssmaj.str()+"."+ssmin.str());
}
// turn a uri object into a uri string
std::string
uri::to_string() const
{
//URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
std::string rets="";
if(scheme!=""){
rets+=scheme+"://";
}
if(auth.is_valid()){
rets+=auth.to_string();
}
rets+=path;
if(query!=""){
rets+="?"+query;
}
if(fragment!=""){
rets+="#"+fragment;
}
return rets;
}
// if the uri had a file and the file had an extension, this will return it,
// otherwise an empty string
std::string uri::get_ext()
{
std::string ext="";
size_t idx;
if((idx=path.rfind("."))!=std::string::npos){
ext=path.substr(idx+1);
std::transform(ext.begin(),ext.end(),ext.begin(),::tolower);
}
return ext;
}
// assignment operator for uris
uri& uri::operator=(const uri& u)
{
scheme=u.scheme;
path=u.path;
auth=u.auth;
query=u.query;
fragment=u.fragment;
valid=u.valid;
return *this;
}
// main constructor for uri, expects the uri from the request and the host
// from the headers
uri::uri(std::string r,std::string h)
{
// URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
// http://dobby.com/fooburger/doohicky.html?a=sturgess#wacky
// schm auth path query fragment
size_t pos,pos2,begin=0;
valid=false;
scheme="http";
//std::cerr << "uri(" << r << ',' << h << ")\n";
auth=authority(h);
if((pos=r.find('?',begin))!=std::string::npos){
if((pos2=r.find('#',begin))!=std::string::npos){
path=r.substr(begin,pos);
query=r.substr(pos+1,pos2);
fragment=r.substr(pos2+1);
}else{
path=r.substr(begin,pos);
query=r.substr(pos+1);
}
}else{
path=r.substr(begin);
}
}
// Will make sure the scheme matches the syntax of scheme
bool
uri::isvalidscheme()
{
// scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
std::string::iterator i=scheme.begin();
if(!isalpha(*i++)){
return false;
}
for(;i!=scheme.end();i++){
if(!(isalpha(*i)||isdigit(*i)||*i=='+'||*i=='-'||*i=='.')){
return false;
}
}
return true;
}