-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.cc
219 lines (183 loc) · 4.14 KB
/
http.cc
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
/* http.cpp -- parser for http returned form data
Copyright (C) 1997-2013, Brian Bray
*/
#include <map>
#include <sstream>
#define NOTRACE
#include "bw/trace.h"
#include "bw/http.h"
#include "bw/exception.h"
#include "bw/bwassert.h"
#include "bw/string.h"
#include <cstdlib>
#include <cstring>
#include <cctype>
extern char **environ;
#define INBUFSIZE 4000
namespace bw {
using std::map;
using std::istream;
// internal routines
bool allspace(const char* psz)
{
while(isspace(*psz))
++psz;
return *psz=='\0';
}
bool startstr(const char* buf, const char* prefix)
{
return strncmp(buf,prefix,strlen(prefix))==0;
}
String extractkey(const char* buf, const String key)
{
String res = buf;
int loc = res.indexOf(key+"=");
if (loc<0)
return "";
res = res.substring(loc+key.length()+1);
if (res[0]=='"') {
res = res.substring(1);
loc = res.indexOf('"');
if (loc>=0)
res = res.substring(0,loc);
}
trace << "Extracting " << key << " from " << buf << " :" << res << std::endl;
return res;
}
map< String, String > getAnyData()
{
char* psz = getenv("REQUEST_METHOD");
String method;
if (psz)
method = psz;
if (method=="GET")
return getGetData();
else if (method=="POST") {
psz = getenv("CONTENT_TYPE");
String datatype;
if (psz)
datatype = getenv("CONTENT_TYPE");
if (datatype.startsWith("multipart/form-data;"))
return getMultiData();
else
return getPostData();
}
return map< String, String>();
}
map< String, String > getPostData()
{
return parseHTTPData( std::cin );
}
map< String, String > getGetData()
{
char *psz = getenv("QUERY_STRING");
if (psz) {
std::istringstream is(psz);
return parseHTTPData(is);
}
map< String, String > nullmap;
return nullmap;
}
map< String, String > getMultiData()
{
using std::cin;
using std::endl;
String divide;
char inbuf[INBUFSIZE];
String keyword;
String filename;
map< String, String > res;
char *contenttype = getenv("CONTENT_TYPE");
if (contenttype) {
trace << "Content type is \"" << contenttype << "\"." << endl;
String str = contenttype;
if (startstr(contenttype,"multipart/form-data;"))
divide = extractkey(str,"boundary");
}
if (divide=="") {
throw BFormatException("Incorrect data format");
}
divide = "--" + divide;
*inbuf = '\0';
while( cin && !startstr(inbuf,divide) )
cin.getline( inbuf, INBUFSIZE );
while (cin) {
bool inheader=true;
keyword = "";
filename = "";
while( cin && inheader) {
cin.getline( inbuf, INBUFSIZE );
if (allspace(inbuf)) {
inheader = false;
} else if (startstr(inbuf,"Content-Disposition:")) {
keyword = extractkey(inbuf,"name");
filename = extractkey(inbuf,"filename");
}
// Other header lines ignored
}
if (cin && keyword=="")
throw BFormatException("Multipart data format error");
if (filename!="")
res[keyword+"_name"] = filename;
// Data starts with next input line
while( cin && !inheader) {
cin.getline( inbuf, INBUFSIZE );
if (startstr( inbuf, divide ))
inheader = true;
else {
int lastloc = strlen(inbuf)-1;
if (inbuf[lastloc]=='\r')
inbuf[lastloc] = '\0';
res[keyword] = res[keyword] + inbuf + "\n";
}
}
if (keyword!="")
trace << "Read " << keyword << "=" << res[keyword] << endl;
}
trace << "Exiting multiread" << endl;
return res;
}
String getValue(istream& is , char term)
{
const String digits("0123456789ABCDEF");
char ic;
int nc1, nc2;
String ret;
while (is.get(ic)) {
if (ic==term)
break;
if (ic=='%') {
if (!is.get(ic))
break;
nc1 = digits.indexOf(toupper(ic));
if (!is.get(ic))
break;
nc2 = digits.indexOf(toupper(ic));
if (nc1==-1 || nc2==-1)
throw BFormatException("Invalid hex digit");
ic = (char)( (nc1<<4) | nc2 );
} else if (ic=='+') {
ic = ' ';
}
if (ic=='\0')
throw BFormatException("Null character in input");
ret.append(ic);
}
return ret;
}
map< String, String > parseHTTPData(istream& is)
{
map< String, String > varmap;
String key;
String value;
while (is) {
key = getValue( is, '=' );
value = getValue( is, '&' );
if (key!="")
varmap[key] = value;
}
if (!is.eof())
throw BFormatException("Unexpected I/O error");
return varmap;
}
} // namespace bw