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 349 by Douglas Thrift, 2004-05-26T17:44:17-07:00 vs.
Revision 350 by Douglas Thrift, 2004-05-27T20:29:11-07:00

# Line 62 | Line 62
62   inline int closesocket(SOCKET s) { return close(s); }
63   #endif
64  
65 < HttpHandler::HttpHandler()
65 > #ifndef _OpenSSL_
66 > HttpHandler::HttpHandler() : binary(false), length(0), chunked(false)
67 > #else
68 > HttpHandler::HttpHandler() : binary(false), length(0), chunked(false),
69 >        tls(false)
70 > #endif
71   {
67        buffer = new char[BUFSIZ + 1];
68
72   #ifdef _WIN32
73          if (WSAStartup(MAKEWORD(2, 0), &data) != 0)
74          {
# Line 73 | Line 76 | HttpHandler::HttpHandler()
76                  exit(1);
77          }
78   #endif // _WIN32
76
77        binary = false;
78        length = 0;
79        chunked = false;
80 #ifdef _OpenSSL_
81        tls = false;
82 #endif
79   }
80  
81   HttpHandler::~HttpHandler()
82   {
87        delete [] buffer;
88
83   #ifdef _WIN32
84          WSACleanup();
85   #endif // _WIN32
# Line 93 | Line 87 | HttpHandler::~HttpHandler()
87  
88   bool HttpHandler::handle(URL &url, const string referer, bool head)
89   {
90 <        bool answer = false;
90 >        bool answer(false);
91  
92          if ((http = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
93          {
# Line 109 | Line 103 | bool HttpHandler::handle(URL &url, const
103          if ((host = gethostbyname(url.getAddress().c_str())) == NULL)
104          {
105                  error(program + ": Host: " + url.getAddress(), true);
106 +
107                  return answer;
108          }
109  
# Line 119 | Line 114 | bool HttpHandler::handle(URL &url, const
114                  SOCKET_ERROR)
115          {
116                  error(program + ": Connect");
117 +
118                  return answer;
119          }
120  
# Line 178 | Line 174 | bool HttpHandler::handle(URL &url, const
174          {
175                  line = getline();
176  
177 <                if (line.find("HTTP/") != 0)
182 <                {
183 <                        return answer;
184 <                }
185 <
186 <                unsigned dot = line.find('.');
187 <                unsigned space = line.find(' ');
188 <
189 <                unsigned major;
190 <                unsigned minor;
177 >                if (line.find("HTTP/") != 0) return answer;
178  
179 +                unsigned dot(line.find('.')), space(line.find(' ')), major, minor;
180                  istringstream number(line.substr(5, dot - 5) + " " + line.substr(dot
181                          + 1, space - dot - 1));
182  
# Line 205 | Line 193 | bool HttpHandler::handle(URL &url, const
193  
194                  number.clear();
195                  number.str(line.substr(space + 1, 3));
196 +
197                  number >> response;
198  
199                  if (response < ok) do line = getline(); while (line != "");
# Line 217 | Line 206 | bool HttpHandler::handle(URL &url, const
206  
207                  if (line != "")
208                  {
209 <                        unsigned colon = line.find(':');
210 <
222 <                        string field = line.substr(0, colon);
223 <                        string value = line.substr(colon + 1);
209 >                        unsigned colon(line.find(':'));
210 >                        string field(line.substr(0, colon)), value(line.substr(colon + 1));
211  
212                          while (isspace(value[0])) value.erase(0, 1);
213  
# Line 250 | Line 237 | bool HttpHandler::handle(URL &url, const
237          {
238          case ok:
239                  if (debug) cerr << "response = " << response << "\n";
240 +
241                  answer = true;
242                  break;
243          case choices:
# Line 257 | Line 245 | bool HttpHandler::handle(URL &url, const
245          case found:
246                  if (debug) cerr << "response = " << response << "\n"
247                          << "location = " << location << "\n";
248 +
249                  location = getLink(location, url);
250                  break;
251          case notfound:
# Line 265 | Line 254 | bool HttpHandler::handle(URL &url, const
254                  break;
255          default:
256                  if (debug) cerr << "response = " << response << "\n";
257 <                if (response <= 299)
258 <                {
270 <                        answer = true;
271 <                }
272 <                else if (response <= 399)
257 >
258 >                if (response <= 299) answer = true; else if (response <= 399)
259                  {
260                          location = getLink(location, url);
261                  }
# Line 297 | Line 283 | void HttpHandler::clear()
283          type = "";
284          length = 0;
285          location = "";
286 +
287          page.clear();
288          page.str("");
289 +
290          chunked = false;
291   #ifdef _OpenSSL_
292          tls = false;
# Line 309 | Line 297 | void HttpHandler::populate()
297   {
298          if (!chunked)
299          {
300 <                unsigned left = length;
300 >                unsigned left(length);
301  
302                  while (left > 0)
303                  {
304                          memset(buffer, 0, BUFSIZ + 1);
305  
306 <                        unsigned bytes = left > BUFSIZ ? BUFSIZ : left;
306 >                        unsigned bytes(left > BUFSIZ ? BUFSIZ : left);
307                          long received;
308  
309                          while (true)
# Line 337 | Line 325 | void HttpHandler::populate()
325                                  else if (received != bytes)
326                                  {
327                                          left -= received;
328 +
329                                          page << buffer;
330  
331                                          memset(buffer, 0, BUFSIZ + 1);
332  
333                                          bytes -= received;
334                                  }
335 <                                else
347 <                                {
348 <                                        break;
349 <                                }
335 >                                else break;
336                          }
337  
338                          page << buffer;
339 +
340                          left -= bytes;
341                  }
342          }
# Line 362 | Line 349 | void HttpHandler::populate()
349                          istringstream number(getline());
350  
351                          number.setf(ios_base::hex, ios_base::basefield);
352 +
353                          number >> chunk;
354  
355 <                        unsigned left = chunk;
355 >                        unsigned left(chunk);
356  
357                          while (left > 0)
358                          {
359                                  memset(buffer, 0, BUFSIZ + 1);
360  
361 <                                unsigned bytes = left > BUFSIZ ? BUFSIZ : left;
361 >                                unsigned bytes(left > BUFSIZ ? BUFSIZ : left);
362                                  long received;
363  
364                                  while (true)
# Line 388 | Line 376 | void HttpHandler::populate()
376                                          {
377                                                  !tls ? error(program + ": Recv") : error(program +
378                                                          ": SSL Read", int(received));
379 +
380                                                  exit(1);
381                                          }
382   #endif
# Line 400 | Line 389 | void HttpHandler::populate()
389  
390                                                  bytes -= received;
391                                          }
392 <                                        else
404 <                                        {
405 <                                                break;
406 <                                        }
392 >                                        else break;
393                                  }
394  
395                                  page << buffer;
396 +
397                                  left -= bytes;
398                          }
399  
400                          getline();
401 +
402                          length += chunk;
403                  }
404                  while (chunk > 0);
# Line 418 | Line 406 | void HttpHandler::populate()
406  
407          if (!binary)
408          {
409 <                string page = this->page.str();
409 >                string page(this->page.str());
410  
411 <                for (unsigned index = 0; index < page.length(); index++)
411 >                for (unsigned index(0); index < page.length(); index++)
412                  {
413 <                        if (page[index] == '\r' && (index + 1 < page.length()) ? page[index +
414 <                                1] == '\n' : false)
413 >                        if (page[index] == '\r' && (index + 1 < page.length()) ?
414 >                                page[index + 1] == '\n' : false)
415                          {
416                                  page.erase(index, 1);
417                          }
# Line 812 | Line 800 | bool HttpHandler::starttls()
800          {
801                  cerr << program << ": SSL CTX New: "
802                          << ERR_reason_error_string(ERR_get_error()) << "\n";
803 +
804                  return false;
805          }
806  
# Line 821 | Line 810 | bool HttpHandler::starttls()
810          {
811                  cerr << program << ": SSL Set FD: "
812                          << ERR_reason_error_string(ERR_get_error()) << "\n";
813 +
814                  return false;
815          }
816  
# Line 829 | Line 819 | bool HttpHandler::starttls()
819          if ((number = SSL_connect(ssl)) <= 0)
820          {
821                  error(program + ": SSL Connect", number);
822 +
823                  return false;
824          }
825  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines