1 |
|
/* ============================================================================ |
2 |
|
* Douglas Thrift's Search Engine License |
3 |
|
* |
4 |
< |
* Copyright (C) 2002-2003, Douglas Thrift. All Rights Reserved. |
4 |
> |
* Copyright (C) 2002-2004, Douglas Thrift. All Rights Reserved. |
5 |
|
* Redistribution and use in source and binary forms, with or without |
6 |
|
* modification, are permitted provided that the following conditions are met: |
7 |
|
* |
46 |
|
// |
47 |
|
// Douglas Thrift |
48 |
|
// |
49 |
< |
// $Id: HttpHandler.cpp,v 1.20 2003/07/18 07:17:10 douglas Exp $ |
49 |
> |
// $Id$ |
50 |
|
|
51 |
< |
#include "HttpHandler.h" |
51 |
> |
#include "HttpHandler.hpp" |
52 |
|
|
53 |
|
// Lovely C Sockets! |
54 |
|
#ifndef _WIN32 |
59 |
|
#include <netinet/in.h> |
60 |
|
#include <netdb.h> |
61 |
|
|
62 |
– |
#define INVALID_SOCKET -1 |
63 |
– |
#define SOCKET_ERROR -1 |
64 |
– |
|
62 |
|
inline int closesocket(SOCKET s) { return close(s); } |
63 |
|
#endif |
64 |
|
|
186 |
|
unsigned dot = line.find('.'); |
187 |
|
unsigned space = line.find(' '); |
188 |
|
|
189 |
< |
unsigned major = strtoul(line.substr(5, dot - 5).c_str(), 0, 10); |
190 |
< |
unsigned minor = strtoul(line.substr(dot + 1, space - dot - 1).c_str(), |
191 |
< |
0, 10); |
189 |
> |
unsigned major; |
190 |
> |
unsigned minor; |
191 |
> |
|
192 |
> |
istringstream number(line.substr(5, dot - 5) + " " + line.substr(dot |
193 |
> |
+ 1, space - dot - 1)); |
194 |
> |
|
195 |
> |
number >> major; |
196 |
> |
number >> minor; |
197 |
|
|
198 |
|
if (major > 1) |
199 |
|
{ |
203 |
|
return answer; |
204 |
|
} |
205 |
|
|
206 |
< |
response = code(strtoul(line.substr(space + 1).c_str(), 0, 10)); |
206 |
> |
number.clear(); |
207 |
> |
number.str(line.substr(space + 1, 3)); |
208 |
> |
number >> response; |
209 |
|
|
210 |
|
if (response < ok) do line = getline(); while (line != ""); |
211 |
|
} |
230 |
|
} |
231 |
|
else if (field == "Content-Length") |
232 |
|
{ |
233 |
< |
length = strtoul(value.c_str(), 0, 10); |
233 |
> |
istringstream number(value); |
234 |
> |
|
235 |
> |
number >> length; |
236 |
|
} |
237 |
|
else if (field == "Location") |
238 |
|
{ |
283 |
|
|
284 |
|
void HttpHandler::clear() |
285 |
|
{ |
286 |
+ |
#ifdef _OpenSSL_ |
287 |
|
if (tls) |
288 |
|
{ |
289 |
|
SSL_shutdown(ssl); |
290 |
|
SSL_free(ssl); |
291 |
|
SSL_CTX_free(ctx); |
292 |
|
} |
293 |
+ |
#endif |
294 |
|
|
295 |
|
closesocket(http); |
296 |
|
|
331 |
|
SSL_read(ssl, buffer, bytes)) <= 0) |
332 |
|
{ |
333 |
|
!tls ? error(program + ": Recv") : error(program + |
334 |
< |
": SSL Read", received); |
334 |
> |
": SSL Read", int(received)); |
335 |
|
} |
336 |
|
#endif |
337 |
|
else if (received != bytes) |
359 |
|
|
360 |
|
do |
361 |
|
{ |
362 |
< |
chunk = strtoul(getline().c_str(), 0, 16); |
362 |
> |
istringstream number(getline()); |
363 |
> |
|
364 |
> |
number.setf(ios_base::hex, ios_base::basefield); |
365 |
> |
number >> chunk; |
366 |
|
|
367 |
|
unsigned left = chunk; |
368 |
|
|
387 |
|
SSL_read(ssl, buffer, bytes)) <= 0) |
388 |
|
{ |
389 |
|
!tls ? error(program + ": Recv") : error(program + |
390 |
< |
": SSL Read", received); |
390 |
> |
": SSL Read", int(received)); |
391 |
|
exit(1); |
392 |
|
} |
393 |
|
#endif |
772 |
|
SSL_load_error_strings(); |
773 |
|
SSL_library_init(); |
774 |
|
|
775 |
< |
// |
775 |
> |
#ifndef _urandomdev_ |
776 |
> |
int pid(getpid()), now(time(NULL)); |
777 |
> |
unsigned seed(now > pid ? now - pid : pid - now); |
778 |
> |
char* junk = new char[seed % 30 + 2]; |
779 |
> |
|
780 |
> |
junk[0] = pid; |
781 |
> |
junk[seed % 30 + 1] = now; |
782 |
> |
|
783 |
> |
srand(seed); |
784 |
> |
|
785 |
> |
for (size_t index = 1; index < seed % 30 + 1; index++) |
786 |
> |
{ |
787 |
> |
junk[index] = rand(); |
788 |
> |
} |
789 |
> |
|
790 |
> |
if (debug) |
791 |
> |
{ |
792 |
> |
cerr << "junk = {\n"; |
793 |
> |
|
794 |
> |
for (size_t index = 1; index < seed % 30 + 2; index++) |
795 |
> |
{ |
796 |
> |
cerr << " [" << index << "] = " << unsigned(junk[index]) << "\n"; |
797 |
> |
} |
798 |
> |
|
799 |
> |
cerr << "}\n"; |
800 |
> |
} |
801 |
> |
|
802 |
> |
RAND_seed(junk, seed % 30 + 2); |
803 |
> |
|
804 |
> |
delete junk; |
805 |
> |
#else |
806 |
> |
if (debug) cerr << "junk = /dev/urandom\n"; |
807 |
> |
#endif |
808 |
> |
|
809 |
> |
ctx = SSL_CTX_new(TLSv1_client_method()); |
810 |
> |
|
811 |
> |
if (ctx == NULL) |
812 |
> |
{ |
813 |
> |
cerr << program << ": SSL CTX New: " |
814 |
> |
<< ERR_reason_error_string(ERR_get_error()) << "\n"; |
815 |
> |
return false; |
816 |
> |
} |
817 |
> |
|
818 |
> |
ssl = SSL_new(ctx); |
819 |
> |
|
820 |
> |
if (SSL_set_fd(ssl, http) == 0) |
821 |
> |
{ |
822 |
> |
cerr << program << ": SSL Set FD: " |
823 |
> |
<< ERR_reason_error_string(ERR_get_error()) << "\n"; |
824 |
> |
return false; |
825 |
> |
} |
826 |
> |
|
827 |
> |
int number; |
828 |
> |
|
829 |
> |
if ((number = SSL_connect(ssl)) <= 0) |
830 |
> |
{ |
831 |
> |
error(program + ": SSL Connect", number); |
832 |
> |
return false; |
833 |
> |
} |
834 |
|
|
835 |
|
return true; |
836 |
|
} |