46 |
|
// |
47 |
|
// Douglas Thrift |
48 |
|
// |
49 |
< |
// $Id: HttpHandler.cpp,v 1.19 2003/07/15 08:01:00 douglas Exp $ |
49 |
> |
// $Id: HttpHandler.cpp,v 1.20 2003/07/18 07:17:10 douglas Exp $ |
50 |
|
|
51 |
|
#include "HttpHandler.h" |
52 |
|
|
77 |
|
} |
78 |
|
#endif // _WIN32 |
79 |
|
|
80 |
+ |
binary = false; |
81 |
|
length = 0; |
82 |
|
chunked = false; |
83 |
|
#ifdef _OpenSSL_ |
125 |
|
return answer; |
126 |
|
} |
127 |
|
|
128 |
+ |
#ifdef _OpenSSL_ |
129 |
+ |
if (url.getTls()) |
130 |
+ |
{ |
131 |
+ |
tls = true; |
132 |
+ |
|
133 |
+ |
if (!starttls()) return answer; |
134 |
+ |
} |
135 |
+ |
#endif |
136 |
+ |
|
137 |
|
if (head) |
138 |
|
{ |
139 |
|
putline("HEAD " + url.getPath() + " HTTP/1.1"); |
146 |
|
putline("Accept: text/html; text/plain"); |
147 |
|
#ifndef _OpenSSL_ |
148 |
|
putline("User-Agent: " + agent(true) + ' ' + platform()); |
149 |
+ |
|
150 |
+ |
if (url.getPort() == 80) |
151 |
|
#else |
152 |
|
putline("User-Agent: " + agent(true) + ' ' + platform() + ' ' |
153 |
|
+ openssl(true)); |
142 |
– |
#endif |
154 |
|
|
155 |
< |
if (url.getPort() == 80) |
155 |
> |
if (url.getPort() == 80 && tls || url.getPort() == 443 && tls) |
156 |
> |
#endif |
157 |
|
{ |
158 |
|
putline("Host: " + url.getAddress()); |
159 |
|
} |
160 |
|
else |
161 |
|
{ |
162 |
< |
char* port = new char[1024]; |
151 |
< |
sprintf(port, "%u", url.getPort()); |
162 |
> |
ostringstream port; |
163 |
|
|
164 |
< |
putline("Host: " + url.getAddress() + ':' + port); |
164 |
> |
port << url.getPort(); |
165 |
|
|
166 |
< |
delete [] port; |
166 |
> |
putline("Host: " + url.getAddress() + ':' + port.str()); |
167 |
|
} |
168 |
|
|
169 |
|
if (referer != "") |
275 |
|
return answer; |
276 |
|
} |
277 |
|
|
278 |
< |
HttpHandler& HttpHandler::getline(string& line, char endline) |
278 |
> |
void HttpHandler::clear() |
279 |
|
{ |
280 |
< |
unsigned end = page.find(endline); |
270 |
< |
unsigned newline = page.find('\n'); |
271 |
< |
|
272 |
< |
if (newline < end || end == string::npos) |
280 |
> |
if (tls) |
281 |
|
{ |
282 |
< |
end = newline; |
282 |
> |
SSL_shutdown(ssl); |
283 |
> |
SSL_free(ssl); |
284 |
> |
SSL_CTX_free(ctx); |
285 |
|
} |
286 |
|
|
277 |
– |
line = page.substr(0, end); |
278 |
– |
page.erase(0, (end == string::npos ? end : end + 1)); |
279 |
– |
|
280 |
– |
return *this; |
281 |
– |
} |
282 |
– |
|
283 |
– |
void HttpHandler::clear() |
284 |
– |
{ |
287 |
|
closesocket(http); |
288 |
|
|
289 |
|
type = ""; |
290 |
|
length = 0; |
291 |
|
location = ""; |
292 |
< |
page = ""; |
292 |
> |
page.clear(); |
293 |
> |
page.str(""); |
294 |
|
chunked = false; |
295 |
|
#ifdef _OpenSSL_ |
296 |
|
tls = false; |
308 |
|
memset(buffer, 0, BUFSIZ + 1); |
309 |
|
|
310 |
|
unsigned bytes = left > BUFSIZ ? BUFSIZ : left; |
311 |
< |
unsigned received; |
311 |
> |
long received; |
312 |
|
|
313 |
|
while (true) |
314 |
|
{ |
315 |
+ |
#ifndef _OpenSSL_ |
316 |
|
if ((received = recv(http, buffer, bytes, 0)) == SOCKET_ERROR) |
317 |
|
{ |
318 |
|
error(program + ": Recv"); |
319 |
|
exit(1); |
320 |
|
} |
321 |
+ |
#else |
322 |
+ |
if ((received = !tls ? recv(http, buffer, bytes, 0) : |
323 |
+ |
SSL_read(ssl, buffer, bytes)) <= 0) |
324 |
+ |
{ |
325 |
+ |
!tls ? error(program + ": Recv") : error(program + |
326 |
+ |
": SSL Read", received); |
327 |
+ |
} |
328 |
+ |
#endif |
329 |
|
else if (received != bytes) |
330 |
|
{ |
331 |
|
left -= received; |
332 |
< |
page += buffer; |
332 |
> |
page << buffer; |
333 |
|
|
334 |
|
memset(buffer, 0, BUFSIZ + 1); |
335 |
|
|
341 |
|
} |
342 |
|
} |
343 |
|
|
344 |
< |
page += buffer; |
344 |
> |
page << buffer; |
345 |
|
left -= bytes; |
346 |
|
} |
347 |
|
} |
360 |
|
memset(buffer, 0, BUFSIZ + 1); |
361 |
|
|
362 |
|
unsigned bytes = left > BUFSIZ ? BUFSIZ : left; |
363 |
< |
unsigned received; |
363 |
> |
long received; |
364 |
|
|
365 |
|
while (true) |
366 |
|
{ |
367 |
+ |
#ifndef _OpenSSL_ |
368 |
|
if ((received = recv(http, buffer, bytes, 0)) == |
369 |
|
SOCKET_ERROR) |
370 |
|
{ |
371 |
|
error(program + ": Recv"); |
372 |
|
exit(1); |
373 |
|
} |
374 |
+ |
#else |
375 |
+ |
if ((received = !tls ? recv(http, buffer, bytes, 0) : |
376 |
+ |
SSL_read(ssl, buffer, bytes)) <= 0) |
377 |
+ |
{ |
378 |
+ |
!tls ? error(program + ": Recv") : error(program + |
379 |
+ |
": SSL Read", received); |
380 |
+ |
exit(1); |
381 |
+ |
} |
382 |
+ |
#endif |
383 |
|
else if (received != bytes) |
384 |
|
{ |
385 |
|
left -= received; |
386 |
< |
page += buffer; |
386 |
> |
page << buffer; |
387 |
|
|
388 |
|
memset(buffer, 0, BUFSIZ + 1); |
389 |
|
|
395 |
|
} |
396 |
|
} |
397 |
|
|
398 |
< |
page += buffer; |
398 |
> |
page << buffer; |
399 |
|
left -= bytes; |
400 |
|
} |
401 |
|
|
405 |
|
while (chunk > 0); |
406 |
|
} |
407 |
|
|
408 |
< |
for (unsigned index = 0; index < page.length(); index++) |
408 |
> |
if (!binary) |
409 |
|
{ |
410 |
< |
if (page[index] == '\r' && (index + 1 < page.length()) ? page[index + |
411 |
< |
1] == '\n' : false) |
412 |
< |
{ |
391 |
< |
page.erase(index, 1); |
392 |
< |
} |
393 |
< |
else if (page[index] == '\r') |
410 |
> |
string page = this->page.str(); |
411 |
> |
|
412 |
> |
for (unsigned index = 0; index < page.length(); index++) |
413 |
|
{ |
414 |
< |
page[index] = '\n'; |
414 |
> |
if (page[index] == '\r' && (index + 1 < page.length()) ? page[index + |
415 |
> |
1] == '\n' : false) |
416 |
> |
{ |
417 |
> |
page.erase(index, 1); |
418 |
> |
} |
419 |
> |
else if (page[index] == '\r') |
420 |
> |
{ |
421 |
> |
page[index] = '\n'; |
422 |
> |
} |
423 |
|
} |
424 |
+ |
|
425 |
+ |
this->page.str(page); |
426 |
|
} |
427 |
|
} |
428 |
|
|
429 |
|
void HttpHandler::putline(const string line) |
430 |
|
{ |
431 |
|
sprintf(buffer, "%s\r\n", line.c_str()); |
432 |
+ |
|
433 |
+ |
#ifndef _OpenSSL_ |
434 |
|
if (send(http, buffer, strlen(buffer), 0) == SOCKET_ERROR) |
435 |
|
{ |
436 |
|
error(program + ": Send"); |
437 |
|
exit(1); |
438 |
|
} |
439 |
+ |
#else |
440 |
+ |
if (!tls) |
441 |
+ |
{ |
442 |
+ |
if (send(http, buffer, strlen(buffer), 0) == SOCKET_ERROR) |
443 |
+ |
{ |
444 |
+ |
error(program + ": Send"); |
445 |
+ |
exit(1); |
446 |
+ |
} |
447 |
+ |
} |
448 |
+ |
else |
449 |
+ |
{ |
450 |
+ |
int number; |
451 |
+ |
|
452 |
+ |
if ((number = SSL_write(ssl, buffer, strlen(buffer))) <= 0) |
453 |
+ |
{ |
454 |
+ |
error(program + ": SSL Write", number); |
455 |
+ |
exit(1); |
456 |
+ |
} |
457 |
+ |
} |
458 |
+ |
#endif |
459 |
|
} |
460 |
|
|
461 |
|
string HttpHandler::getline() |
465 |
|
|
466 |
|
do |
467 |
|
{ |
468 |
+ |
#ifndef _OpenSSL_ |
469 |
|
if (recv(http, &byte, 1, 0) == SOCKET_ERROR) |
470 |
|
{ |
471 |
|
error(program + ": Recv"); |
472 |
|
} |
473 |
+ |
#else |
474 |
+ |
if (!tls) |
475 |
+ |
{ |
476 |
+ |
if (recv(http, &byte, 1, 0) == SOCKET_ERROR) |
477 |
+ |
{ |
478 |
+ |
error(program + ": Recv"); |
479 |
+ |
} |
480 |
+ |
} |
481 |
+ |
else |
482 |
+ |
{ |
483 |
+ |
int number; |
484 |
+ |
|
485 |
+ |
if ((number = SSL_read(ssl, &byte, 1)) <= 0) |
486 |
+ |
{ |
487 |
+ |
error(program + ": SSL Read", number); |
488 |
+ |
} |
489 |
+ |
} |
490 |
+ |
#endif |
491 |
|
|
492 |
|
if (byte != '\r' && byte != '\n') |
493 |
|
{ |
703 |
|
} |
704 |
|
#endif // _WIN32 |
705 |
|
} |
706 |
+ |
|
707 |
+ |
#ifdef _OpenSSL_ |
708 |
+ |
void HttpHandler::error(const string& prefix, int number) |
709 |
+ |
{ |
710 |
+ |
string error; |
711 |
+ |
|
712 |
+ |
switch (SSL_get_error(ssl, number)) |
713 |
+ |
{ |
714 |
+ |
case SSL_ERROR_NONE: |
715 |
+ |
error = "The TLS/SSL I/O operation completed"; |
716 |
+ |
break; |
717 |
+ |
case SSL_ERROR_ZERO_RETURN: |
718 |
+ |
error = "The TLS/SSL connection has been closed"; |
719 |
+ |
break; |
720 |
+ |
case SSL_ERROR_WANT_READ: |
721 |
+ |
case SSL_ERROR_WANT_WRITE: |
722 |
+ |
case SSL_ERROR_WANT_CONNECT: |
723 |
+ |
// case SSL_ERROR_WANT_ACCEPT: |
724 |
+ |
case SSL_ERROR_WANT_X509_LOOKUP: |
725 |
+ |
error = "The operation did not complete"; |
726 |
+ |
break; |
727 |
+ |
case SSL_ERROR_SYSCALL: |
728 |
+ |
if (int err = ERR_get_error() != 0) |
729 |
+ |
{ |
730 |
+ |
error = ERR_reason_error_string(err); |
731 |
+ |
} |
732 |
+ |
else |
733 |
+ |
{ |
734 |
+ |
switch (number) |
735 |
+ |
{ |
736 |
+ |
case 0: |
737 |
+ |
error = "An EOF was observed that violates the protocol"; |
738 |
+ |
break; |
739 |
+ |
case -1: |
740 |
+ |
this->error(prefix); |
741 |
+ |
return; |
742 |
+ |
default: |
743 |
+ |
error = "Unknown error"; |
744 |
+ |
break; |
745 |
+ |
} |
746 |
+ |
} |
747 |
+ |
break; |
748 |
+ |
case SSL_ERROR_SSL: |
749 |
+ |
error = ERR_reason_error_string(ERR_get_error()); |
750 |
+ |
break; |
751 |
+ |
default: |
752 |
+ |
error = "Unknown error"; |
753 |
+ |
break; |
754 |
+ |
} |
755 |
+ |
|
756 |
+ |
cerr << prefix << ": " << error << "\n"; |
757 |
+ |
} |
758 |
+ |
|
759 |
+ |
bool HttpHandler::starttls() |
760 |
+ |
{ |
761 |
+ |
SSL_load_error_strings(); |
762 |
+ |
SSL_library_init(); |
763 |
+ |
|
764 |
+ |
// |
765 |
+ |
|
766 |
+ |
return true; |
767 |
+ |
} |
768 |
+ |
#endif |