62 |
|
} |
63 |
|
#endif // _WIN32 |
64 |
|
|
65 |
< |
begin = 0; |
65 |
> |
length = 0; |
66 |
> |
chunked = false; |
67 |
|
} |
68 |
|
|
69 |
|
HttpHandler::~HttpHandler() |
132 |
|
delete [] port; |
133 |
|
} |
134 |
|
|
135 |
+ |
// putline("Referer: " + ?referer?); |
136 |
+ |
putline("Connection: close"); |
137 |
|
putline(); |
138 |
|
|
139 |
< |
string line = getline(); |
139 |
> |
code response; |
140 |
> |
string line; |
141 |
|
|
142 |
< |
if (line.find("HTTP/") != 0) |
142 |
> |
do |
143 |
|
{ |
144 |
< |
return answer; |
141 |
< |
} |
144 |
> |
line = getline(); |
145 |
|
|
146 |
< |
unsigned dot = line.find('.'); |
147 |
< |
unsigned space = line.find(' '); |
146 |
> |
if (line.find("HTTP/") != 0) |
147 |
> |
{ |
148 |
> |
return answer; |
149 |
> |
} |
150 |
|
|
151 |
< |
unsigned major = strtoul(line.substr(5, dot - 5).c_str(), 0, 0); |
152 |
< |
unsigned minor = strtoul(line.substr(dot + 1, space - dot - 1).c_str(), 0, |
148 |
< |
0); |
151 |
> |
unsigned dot = line.find('.'); |
152 |
> |
unsigned space = line.find(' '); |
153 |
|
|
154 |
< |
if (major > 1 || minor < 1) |
155 |
< |
{ |
156 |
< |
cerr << program << ": Potentially Incompatible Server: HTTP/" << major |
153 |
< |
<< "." << minor << "\n"; |
154 |
> |
unsigned major = strtoul(line.substr(5, dot - 5).c_str(), 0, 10); |
155 |
> |
unsigned minor = strtoul(line.substr(dot + 1, space - dot - 1).c_str(), |
156 |
> |
0, 10); |
157 |
|
|
158 |
< |
return answer; |
159 |
< |
} |
158 |
> |
if (major > 1 || minor < 1) |
159 |
> |
{ |
160 |
> |
cerr << program << ": Potentially Incompatible Server: HTTP/" << |
161 |
> |
major << "." << minor << "\n"; |
162 |
|
|
163 |
< |
code response = code(strtoul(line.substr(space + 1).c_str(), 0, 0)); |
163 |
> |
return answer; |
164 |
> |
} |
165 |
> |
|
166 |
> |
response = code(strtoul(line.substr(space + 1).c_str(), 0, 10)); |
167 |
> |
|
168 |
> |
if (response < ok) do line = getline(); while (line != ""); |
169 |
> |
} |
170 |
> |
while (response < ok); |
171 |
|
|
172 |
|
do |
173 |
|
{ |
174 |
|
line = getline(); |
175 |
+ |
|
176 |
+ |
if (line != "") |
177 |
+ |
{ |
178 |
+ |
unsigned colon = line.find(':'); |
179 |
+ |
|
180 |
+ |
string field = line.substr(0, colon); |
181 |
+ |
string value = line.substr(colon + 1); |
182 |
+ |
|
183 |
+ |
while (isspace(value[0])) value.erase(0, 1); |
184 |
+ |
|
185 |
+ |
if (field == "Content-Type") |
186 |
+ |
{ |
187 |
+ |
type = value; |
188 |
+ |
} |
189 |
+ |
else if (field == "Content-Length") |
190 |
+ |
{ |
191 |
+ |
length = strtoul(value.c_str(), 0, 10); |
192 |
+ |
} |
193 |
+ |
else if (field == "Location") |
194 |
+ |
{ |
195 |
+ |
location = value; |
196 |
+ |
} |
197 |
+ |
else if (field == "Transfer-Encoding") |
198 |
+ |
{ |
199 |
+ |
chunked = value == "chunked"; |
200 |
+ |
} |
201 |
+ |
} |
202 |
|
} |
203 |
|
while (line != ""); |
204 |
|
|
205 |
|
switch (response) |
206 |
|
{ |
207 |
|
case ok: |
208 |
+ |
if (debug) cerr << "response = " << response << "\n"; |
209 |
|
answer = true; |
210 |
|
break; |
211 |
< |
case choices: |
172 |
< |
break; |
211 |
> |
case choices: |
212 |
|
case moved: |
174 |
– |
break; |
213 |
|
case found: |
214 |
+ |
if (debug) cerr << "response = " << response << "\n" |
215 |
+ |
<< "location = " << location << "\n"; |
216 |
+ |
location = getLink(location, url); |
217 |
|
break; |
218 |
|
case notfound: |
178 |
– |
break; |
219 |
|
case internal: |
220 |
+ |
if (debug) cerr << "response = " << response << "\n"; |
221 |
|
break; |
222 |
|
default: |
223 |
+ |
if (debug) cerr << "response = " << response << "\n"; |
224 |
+ |
if (response <= 299) |
225 |
+ |
{ |
226 |
+ |
answer = true; |
227 |
+ |
} |
228 |
+ |
else if (response <= 399) |
229 |
+ |
{ |
230 |
+ |
location = getLink(location, url); |
231 |
+ |
} |
232 |
|
break; |
233 |
|
} |
234 |
|
|
235 |
+ |
if (!head && answer) populate(); |
236 |
+ |
|
237 |
|
return answer; |
238 |
|
} |
239 |
|
|
240 |
|
HttpHandler& HttpHandler::getline(string& line, char endline) |
241 |
|
{ |
242 |
< |
int end = page.find(endline, begin); |
243 |
< |
int newline = page.find('\n', begin); |
242 |
> |
unsigned end = page.find(endline); |
243 |
> |
unsigned newline = page.find('\n'); |
244 |
|
|
245 |
|
if (newline < end || end == string::npos) |
246 |
|
{ |
247 |
|
end = newline; |
248 |
|
} |
249 |
|
|
250 |
< |
line = page.substr(begin, end - begin); |
251 |
< |
|
200 |
< |
if (end == string::npos) |
201 |
< |
{ |
202 |
< |
begin = end; |
203 |
< |
} |
204 |
< |
else |
205 |
< |
{ |
206 |
< |
begin = end + 1; |
207 |
< |
} |
250 |
> |
line = page.substr(0, end); |
251 |
> |
page.erase(0, (end == string::npos ? end : end + 1)); |
252 |
|
|
253 |
|
return *this; |
254 |
|
} |
255 |
|
|
256 |
< |
bool HttpHandler::good() |
256 |
> |
void HttpHandler::clear() |
257 |
|
{ |
258 |
< |
bool answer = true; |
258 |
> |
closesocket(http); |
259 |
|
|
260 |
< |
if (begin >= page.length()) |
260 |
> |
type = ""; |
261 |
> |
length = 0; |
262 |
> |
location = ""; |
263 |
> |
page = ""; |
264 |
> |
chunked = false; |
265 |
> |
} |
266 |
> |
|
267 |
> |
void HttpHandler::populate() |
268 |
> |
{ |
269 |
> |
if (!chunked) |
270 |
|
{ |
271 |
< |
answer = false; |
271 |
> |
unsigned left = length; |
272 |
> |
|
273 |
> |
while (left > 0) |
274 |
> |
{ |
275 |
> |
memset(buffer, 0, BUFSIZ + 1); |
276 |
> |
|
277 |
> |
unsigned bytes = left > BUFSIZ ? BUFSIZ : left; |
278 |
> |
|
279 |
> |
if (recv(http, buffer, bytes, 0) == SOCKET_ERROR) |
280 |
> |
{ |
281 |
> |
error(program + ": Recv"); |
282 |
> |
exit(1); |
283 |
> |
} |
284 |
> |
|
285 |
> |
page += buffer; |
286 |
> |
left -= bytes; |
287 |
> |
} |
288 |
|
} |
289 |
< |
else if (begin == string::npos) |
289 |
> |
else |
290 |
|
{ |
291 |
< |
answer = false; |
223 |
< |
} |
291 |
> |
unsigned chunk; |
292 |
|
|
293 |
< |
return answer; |
294 |
< |
} |
293 |
> |
do |
294 |
> |
{ |
295 |
> |
chunk = strtoul(getline().c_str(), 0, 16); |
296 |
|
|
297 |
< |
void HttpHandler::clear() |
298 |
< |
{ |
299 |
< |
type = ""; |
300 |
< |
length = 0; |
301 |
< |
location = ""; |
302 |
< |
begin = 0; |
303 |
< |
page = ""; |
297 |
> |
unsigned left = chunk; |
298 |
> |
|
299 |
> |
while (left > 0) |
300 |
> |
{ |
301 |
> |
memset(buffer, 0, BUFSIZ + 1); |
302 |
> |
|
303 |
> |
unsigned bytes = left > BUFSIZ ? BUFSIZ : left; |
304 |
> |
|
305 |
> |
if (recv(http, buffer, bytes, 0) == SOCKET_ERROR) |
306 |
> |
{ |
307 |
> |
error(program + ": Recv"); |
308 |
> |
exit(1); |
309 |
> |
} |
310 |
> |
|
311 |
> |
page += buffer; |
312 |
> |
left -= bytes; |
313 |
> |
} |
314 |
> |
|
315 |
> |
getline(); |
316 |
> |
length += chunk; |
317 |
> |
} |
318 |
> |
while (chunk > 0); |
319 |
> |
} |
320 |
> |
|
321 |
> |
for (unsigned index = 0; index < page.length(); index++) |
322 |
> |
{ |
323 |
> |
if (page[index] == '\r' && (index + 1 < page.length()) ? page[index + |
324 |
> |
1] == '\n' : false) |
325 |
> |
{ |
326 |
> |
page.erase(index, 1); |
327 |
> |
} |
328 |
> |
else if (page[index] == '\r') |
329 |
> |
{ |
330 |
> |
page[index] = '\n'; |
331 |
> |
} |
332 |
> |
} |
333 |
|
} |
334 |
|
|
335 |
|
void HttpHandler::putline(const string line) |
364 |
|
return line; |
365 |
|
} |
366 |
|
|
367 |
< |
void HttpHandler::error(const string prefix, bool host) |
367 |
> |
void HttpHandler::error(const string& prefix, bool host) |
368 |
|
{ |
369 |
|
#ifdef _WIN32 |
370 |
|
string error; |