ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/Search/HttpHandler.cpp
(Generate patch)

Comparing trunk/Search/HttpHandler.cpp (file contents):
Revision 201 by douglas, 2003-07-15T01:01:00-07:00 vs.
Revision 214 by douglas, 2003-07-19T22:47:26-07:00

# Line 46 | Line 46
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.25 2003/07/20 05:47:26 douglas Exp $
50  
51   #include "HttpHandler.h"
52  
# Line 59 | Line 59
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  
# Line 77 | Line 74 | HttpHandler::HttpHandler()
74          }
75   #endif // _WIN32
76  
77 +        binary = false;
78          length = 0;
79          chunked = false;
80   #ifdef _OpenSSL_
# Line 124 | Line 122 | bool HttpHandler::handle(URL &url, const
122                  return answer;
123          }
124  
125 + #ifdef _OpenSSL_
126 +        if (url.getTls())
127 +        {
128 +                tls = true;
129 +
130 +                if (!starttls()) return answer;
131 +        }
132 + #endif
133 +
134          if (head)
135          {
136                  putline("HEAD " + url.getPath() + " HTTP/1.1");
# Line 136 | Line 143 | bool HttpHandler::handle(URL &url, const
143          putline("Accept: text/html; text/plain");
144   #ifndef _OpenSSL_
145          putline("User-Agent: " + agent(true) + ' ' + platform());
146 +
147 +        if (url.getPort() == 80)
148   #else
149          putline("User-Agent: " + agent(true) + ' ' + platform() + ' '
150                  + openssl(true));
142 #endif
151  
152 <        if (url.getPort() == 80)
152 >        if (url.getPort() == 80 && tls || url.getPort() == 443 && tls)
153 > #endif
154          {
155                  putline("Host: " + url.getAddress());
156          }
157          else
158          {
159 <                char* port = new char[1024];
151 <                sprintf(port, "%u", url.getPort());
159 >                ostringstream port;
160  
161 <                putline("Host: " + url.getAddress() + ':' + port);
161 >                port << url.getPort();
162  
163 <                delete [] port;
163 >                putline("Host: " + url.getAddress() + ':' + port.str());
164          }
165  
166          if (referer != "")
# Line 178 | Line 186 | bool HttpHandler::handle(URL &url, const
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                  {
# Line 190 | Line 203 | bool HttpHandler::handle(URL &url, const
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          }
# Line 215 | Line 230 | bool HttpHandler::handle(URL &url, const
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                          {
# Line 264 | Line 281 | bool HttpHandler::handle(URL &url, const
281          return answer;
282   }
283  
284 < HttpHandler& HttpHandler::getline(string& line, char endline)
284 > void HttpHandler::clear()
285   {
286 <        unsigned end = page.find(endline);
270 <        unsigned newline = page.find('\n');
271 <
272 <        if (newline < end || end == string::npos)
286 >        if (tls)
287          {
288 <                end = newline;
288 >                SSL_shutdown(ssl);
289 >                SSL_free(ssl);
290 >                SSL_CTX_free(ctx);
291          }
292  
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 {
293          closesocket(http);
294  
295          type = "";
296          length = 0;
297          location = "";
298 <        page = "";
298 >        page.clear();
299 >        page.str("");
300          chunked = false;
301   #ifdef _OpenSSL_
302          tls = false;
# Line 305 | Line 314 | void HttpHandler::populate()
314                          memset(buffer, 0, BUFSIZ + 1);
315  
316                          unsigned bytes = left > BUFSIZ ? BUFSIZ : left;
317 <                        unsigned received;
317 >                        long received;
318  
319                          while (true)
320                          {
321 + #ifndef _OpenSSL_
322                                  if ((received = recv(http, buffer, bytes, 0)) == SOCKET_ERROR)
323                                  {
324                                          error(program + ": Recv");
325                                          exit(1);
326                                  }
327 + #else
328 +                                if ((received = !tls ? recv(http, buffer, bytes, 0) :
329 +                                        SSL_read(ssl, buffer, bytes)) <= 0)
330 +                                {
331 +                                        !tls ? error(program + ": Recv") : error(program +
332 +                                                ": SSL Read", int(received));
333 +                                }
334 + #endif
335                                  else if (received != bytes)
336                                  {
337                                          left -= received;
338 <                                        page += buffer;
338 >                                        page << buffer;
339  
340                                          memset(buffer, 0, BUFSIZ + 1);
341  
# Line 329 | Line 347 | void HttpHandler::populate()
347                                  }
348                          }
349  
350 <                        page += buffer;
350 >                        page << buffer;
351                          left -= bytes;
352                  }
353          }
# Line 339 | Line 357 | void HttpHandler::populate()
357  
358                  do
359                  {
360 <                        chunk = strtoul(getline().c_str(), 0, 16);
360 >                        istringstream number(getline());
361 >
362 >                        number.setf(ios_base::hex, ios_base::basefield);
363 >                        number >> chunk;
364  
365                          unsigned left = chunk;
366  
# Line 348 | Line 369 | void HttpHandler::populate()
369                                  memset(buffer, 0, BUFSIZ + 1);
370  
371                                  unsigned bytes = left > BUFSIZ ? BUFSIZ : left;
372 <                                unsigned received;
372 >                                long received;
373  
374                                  while (true)
375                                  {
376 + #ifndef _OpenSSL_
377                                          if ((received = recv(http, buffer, bytes, 0)) ==
378                                                  SOCKET_ERROR)
379                                          {
380                                                  error(program + ": Recv");
381                                                  exit(1);
382                                          }
383 + #else
384 +                                        if ((received = !tls ? recv(http, buffer, bytes, 0) :
385 +                                                SSL_read(ssl, buffer, bytes)) <= 0)
386 +                                        {
387 +                                                !tls ? error(program + ": Recv") : error(program +
388 +                                                        ": SSL Read", int(received));
389 +                                                exit(1);
390 +                                        }
391 + #endif
392                                          else if (received != bytes)
393                                          {
394                                                  left -= received;
395 <                                                page += buffer;
395 >                                                page << buffer;
396  
397                                                  memset(buffer, 0, BUFSIZ + 1);
398  
# Line 373 | Line 404 | void HttpHandler::populate()
404                                          }
405                                  }
406  
407 <                                page += buffer;
407 >                                page << buffer;
408                                  left -= bytes;
409                          }
410  
# Line 383 | Line 414 | void HttpHandler::populate()
414                  while (chunk > 0);
415          }
416  
417 <        for (unsigned index = 0; index < page.length(); index++)
417 >        if (!binary)
418          {
419 <                if (page[index] == '\r' && (index + 1 < page.length()) ? page[index +
420 <                        1] == '\n' : false)
421 <                {
391 <                        page.erase(index, 1);
392 <                }
393 <                else if (page[index] == '\r')
419 >                string page = this->page.str();
420 >
421 >                for (unsigned index = 0; index < page.length(); index++)
422                  {
423 <                        page[index] = '\n';
423 >                        if (page[index] == '\r' && (index + 1 < page.length()) ? page[index +
424 >                                1] == '\n' : false)
425 >                        {
426 >                                page.erase(index, 1);
427 >                        }
428 >                        else if (page[index] == '\r')
429 >                        {
430 >                                page[index] = '\n';
431 >                        }
432                  }
433 +
434 +                this->page.str(page);
435          }
436   }
437  
438   void HttpHandler::putline(const string line)
439   {
440          sprintf(buffer, "%s\r\n", line.c_str());
441 +
442 + #ifndef _OpenSSL_
443          if (send(http, buffer, strlen(buffer), 0) == SOCKET_ERROR)
444          {
445                  error(program + ": Send");
446                  exit(1);
447          }
448 + #else
449 +        if (!tls)
450 +        {
451 +                if (send(http, buffer, strlen(buffer), 0) == SOCKET_ERROR)
452 +                {
453 +                        error(program + ": Send");
454 +                        exit(1);
455 +                }
456 +        }
457 +        else
458 +        {
459 +                int number;
460 +
461 +                if ((number = SSL_write(ssl, buffer, strlen(buffer))) <= 0)
462 +                {
463 +                        error(program + ": SSL Write", number);
464 +                        exit(1);
465 +                }
466 +        }
467 + #endif
468   }
469  
470   string HttpHandler::getline()
# Line 414 | Line 474 | string HttpHandler::getline()
474  
475          do
476          {
477 + #ifndef _OpenSSL_
478                  if (recv(http, &byte, 1, 0) == SOCKET_ERROR)
479                  {
480                          error(program + ": Recv");
481                  }
482 + #else
483 +                if (!tls)
484 +                {
485 +                        if (recv(http, &byte, 1, 0) == SOCKET_ERROR)
486 +                        {
487 +                                error(program + ": Recv");
488 +                        }
489 +                }
490 +                else
491 +                {
492 +                        int number;
493 +
494 +                        if ((number = SSL_read(ssl, &byte, 1)) <= 0)
495 +                        {
496 +                                error(program + ": SSL Read", number);
497 +                        }
498 +                }
499 + #endif
500  
501                  if (byte != '\r' && byte != '\n')
502                  {
# Line 633 | Line 712 | void HttpHandler::error(const string& pr
712          }
713   #endif // _WIN32
714   }
715 +
716 + #ifdef _OpenSSL_
717 + void HttpHandler::error(const string& prefix, int number)
718 + {
719 +        string error;
720 +
721 +        switch (SSL_get_error(ssl, number))
722 +        {
723 +        case SSL_ERROR_NONE:
724 +                error = "The TLS/SSL I/O operation completed";
725 +                break;
726 +        case SSL_ERROR_ZERO_RETURN:
727 +                error = "The TLS/SSL connection has been closed";
728 +                break;
729 +        case SSL_ERROR_WANT_READ:
730 +        case SSL_ERROR_WANT_WRITE:
731 +        case SSL_ERROR_WANT_CONNECT:
732 + //      case SSL_ERROR_WANT_ACCEPT:
733 +        case SSL_ERROR_WANT_X509_LOOKUP:
734 +                error = "The operation did not complete";
735 +                break;
736 +        case SSL_ERROR_SYSCALL:
737 +                if (int err = ERR_get_error() != 0)
738 +                {
739 +                        error = ERR_reason_error_string(err);
740 +                }
741 +                else
742 +                {
743 +                        switch (number)
744 +                        {
745 +                        case 0:
746 +                                error = "An EOF was observed that violates the protocol";
747 +                                break;
748 +                        case -1:
749 +                                this->error(prefix);
750 +                                return;
751 +                        default:
752 +                                error = "Unknown error";
753 +                                break;
754 +                        }
755 +                }
756 +                break;
757 +        case SSL_ERROR_SSL:
758 +                error = ERR_reason_error_string(ERR_get_error());
759 +                break;
760 +        default:
761 +                error = "Unknown error";
762 +                break;
763 +        }
764 +
765 +        cerr << prefix << ": " << error << "\n";
766 + }
767 +
768 + bool HttpHandler::starttls()
769 + {
770 +        SSL_load_error_strings();
771 +        SSL_library_init();
772 +
773 + #ifndef _urandomdev_
774 +        int pid = getpid();
775 +        int now = time(NULL);
776 +
777 +        unsigned seed = now > pid ? now - pid : pid - now;
778 +
779 +        char* junk = new char[seed % 30 + 2];
780 +        junk[0] = pid;
781 +        junk[seed % 30 + 1] = now;
782 +
783 +        srand(seed);
784 +
785 +        for (int 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 (int index = 1; index < seed % 30 + 2; index++)
795 +                {
796 +                        cerr << "   [" << index << "] = " << int(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 + }
837 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines