ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/Search/HttpHandler.cpp
Revision: 145
Committed: 2003-05-31T14:31:50-07:00 (22 years ago) by douglas
File size: 12645 byte(s)
Log Message:
Found and fixed bug in HttpHandler::populate().

File Contents

# User Rev Content
1 douglas 1 /* ============================================================================
2     * Douglas Thrift's Search Engine License
3     *
4 douglas 28 * Copyright (C) 2002-2003, Douglas Thrift. All Rights Reserved.
5 douglas 1 * Redistribution and use in source and binary forms, with or without
6     * modification, are permitted provided that the following conditions are met:
7     *
8     * 1. Redistributions of source code must retain the above copyright notice,
9     * this list of conditions and the following disclaimer.
10     *
11     * 2. Redistributions in binary form must reproduce the above copyright notice,
12     * this list of conditions and the following disclaimer in the documentation
13     * and/or other materials provided with the distribution.
14     *
15     * 3. The end-user documentation included with the redistribution, if any, must
16     * include the following acknowledgment:
17     *
18     * "This product includes software developed by Douglas Thrift
19     * (http://computers.douglasthrift.net/searchengine/)."
20     *
21     * Alternately, this acknowledgment may appear in the software itself, if
22     * and wherever such third-party acknowledgments normally appear.
23     *
24     * 4. The names "Douglas Thrift" and "Douglas Thrift's Search Engine" must not
25     * be used to endorse or promote products derived from this software without
26     * specific prior written permission. For written permission, please visit
27     * http://www.douglasthrift.net/contact.cgi for contact information.
28     *
29     * 5. Products derived from this software may not be called "Douglas Thrift's
30     * Search Engine", nor may "Douglas Thrift's Search Engine" appear in their
31     * name, without prior written permission.
32     *
33     * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
34     * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35     * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
36     * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
37     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
39     * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40     * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41     * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43     * ============================================================================
44     */
45     // Douglas Thrift's Search Engine HTTP Handler
46     //
47     // Douglas Thrift
48     //
49     // HttpHandler.cpp
50    
51     #include "HttpHandler.h"
52    
53     HttpHandler::HttpHandler()
54     {
55 douglas 14 buffer = new char[BUFSIZ + 1];
56    
57 douglas 13 #ifdef _WIN32
58 douglas 14 if (WSAStartup(MAKEWORD(2, 0), &data) != 0)
59 douglas 13 {
60 douglas 17 error(program + ": WSAStartup");
61 douglas 13 exit(1);
62     }
63     #endif // _WIN32
64 douglas 1
65 douglas 18 length = 0;
66     chunked = false;
67 douglas 1 }
68    
69     HttpHandler::~HttpHandler()
70     {
71 douglas 14 delete [] buffer;
72    
73 douglas 13 #ifdef _WIN32
74     WSACleanup();
75     #endif // _WIN32
76 douglas 1 }
77    
78 douglas 25 bool HttpHandler::handle(URL &url, const string referer, bool head)
79 douglas 1 {
80     bool answer = false;
81    
82 douglas 14 if ((http = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
83     {
84     error(program + ": Socket");
85     exit(1);
86     }
87 douglas 1
88 douglas 14 sockaddr_in address;
89     hostent* host;
90 douglas 1
91 douglas 14 address.sin_family = AF_INET;
92 douglas 1
93 douglas 14 if ((host = gethostbyname(url.getAddress().c_str())) == NULL)
94     {
95     error(program + ": Host: " + url.getAddress(), true);
96     return answer;
97     }
98 douglas 1
99 douglas 14 address.sin_addr = *((in_addr*)*host->h_addr_list);
100     address.sin_port = htons(url.getPort());
101    
102     if (connect(http, (sockaddr*)&address, sizeof(sockaddr_in)) ==
103     SOCKET_ERROR)
104     {
105     error(program + ": Connect");
106     return answer;
107     }
108    
109     if (head)
110     {
111     putline("HEAD " + url.getPath() + " HTTP/1.1");
112     }
113     else
114     {
115     putline("GET " + url.getPath() + " HTTP/1.1");
116     }
117    
118     putline("Accept: text/html; text/plain");
119     putline("User-Agent: " + agent(true) + ' ' + platform());
120    
121     if (url.getPort() == 80)
122     {
123     putline("Host: " + url.getAddress());
124     }
125     else
126     {
127     char* port = new char[1024];
128     sprintf(port, "%u", url.getPort());
129    
130     putline("Host: " + url.getAddress() + ':' + port);
131    
132     delete [] port;
133     }
134    
135 douglas 25 if (referer != "")
136     {
137     putline("Referer: " + referer);
138     }
139    
140 douglas 18 putline("Connection: close");
141 douglas 14 putline();
142    
143 douglas 18 code response;
144     string line;
145 douglas 17
146 douglas 18 do
147 douglas 17 {
148 douglas 18 line = getline();
149 douglas 17
150 douglas 18 if (line.find("HTTP/") != 0)
151     {
152     return answer;
153     }
154 douglas 17
155 douglas 18 unsigned dot = line.find('.');
156     unsigned space = line.find(' ');
157 douglas 17
158 douglas 19 unsigned major = strtoul(line.substr(5, dot - 5).c_str(), 0, 10);
159     unsigned minor = strtoul(line.substr(dot + 1, space - dot - 1).c_str(),
160     0, 10);
161 douglas 17
162 douglas 24 if (major > 1)
163 douglas 18 {
164 douglas 19 cerr << program << ": Potentially Incompatible Server: HTTP/" <<
165     major << "." << minor << "\n";
166 douglas 18
167     return answer;
168     }
169    
170 douglas 19 response = code(strtoul(line.substr(space + 1).c_str(), 0, 10));
171 douglas 18
172     if (response < ok) do line = getline(); while (line != "");
173 douglas 17 }
174 douglas 18 while (response < ok);
175 douglas 17
176     do
177     {
178     line = getline();
179 douglas 18
180     if (line != "")
181     {
182     unsigned colon = line.find(':');
183    
184     string field = line.substr(0, colon);
185     string value = line.substr(colon + 1);
186    
187     while (isspace(value[0])) value.erase(0, 1);
188    
189 douglas 19 if (field == "Content-Type")
190     {
191     type = value;
192     }
193     else if (field == "Content-Length")
194     {
195     length = strtoul(value.c_str(), 0, 10);
196     }
197     else if (field == "Location")
198     {
199     location = value;
200     }
201     else if (field == "Transfer-Encoding")
202     {
203     chunked = value == "chunked";
204     }
205 douglas 18 }
206 douglas 17 }
207     while (line != "");
208    
209     switch (response)
210     {
211     case ok:
212 douglas 18 if (debug) cerr << "response = " << response << "\n";
213 douglas 17 answer = true;
214     break;
215 douglas 18 case choices:
216 douglas 17 case moved:
217     case found:
218 douglas 18 if (debug) cerr << "response = " << response << "\n"
219     << "location = " << location << "\n";
220     location = getLink(location, url);
221 douglas 17 break;
222     case notfound:
223     case internal:
224 douglas 18 if (debug) cerr << "response = " << response << "\n";
225 douglas 17 break;
226     default:
227 douglas 18 if (debug) cerr << "response = " << response << "\n";
228     if (response <= 299)
229     {
230     answer = true;
231     }
232     else if (response <= 399)
233     {
234     location = getLink(location, url);
235     }
236 douglas 17 break;
237     }
238    
239 douglas 19 if (!head && answer) populate();
240    
241 douglas 1 return answer;
242     }
243    
244     HttpHandler& HttpHandler::getline(string& line, char endline)
245     {
246 douglas 21 unsigned end = page.find(endline);
247     unsigned newline = page.find('\n');
248 douglas 1
249     if (newline < end || end == string::npos)
250     {
251     end = newline;
252     }
253    
254 douglas 20 line = page.substr(0, end);
255     page.erase(0, (end == string::npos ? end : end + 1));
256 douglas 1
257     return *this;
258     }
259    
260     void HttpHandler::clear()
261     {
262 douglas 18 closesocket(http);
263    
264 douglas 17 type = "";
265     length = 0;
266     location = "";
267 douglas 1 page = "";
268 douglas 18 chunked = false;
269 douglas 1 }
270    
271 douglas 19 void HttpHandler::populate()
272     {
273     if (!chunked)
274     {
275     unsigned left = length;
276    
277     while (left > 0)
278     {
279     memset(buffer, 0, BUFSIZ + 1);
280    
281     unsigned bytes = left > BUFSIZ ? BUFSIZ : left;
282 douglas 24 unsigned received;
283 douglas 19
284 douglas 145 while (true)
285 douglas 19 {
286 douglas 145 if ((received = recv(http, buffer, bytes, 0)) == SOCKET_ERROR)
287 douglas 24 {
288     error(program + ": Recv");
289     exit(1);
290     }
291 douglas 145 else if (received != bytes)
292     {
293     left -= received;
294     page += buffer;
295    
296     memset(buffer, 0, BUFSIZ + 1);
297    
298     bytes -= received;
299     }
300     else
301     {
302     break;
303     }
304 douglas 24 }
305    
306 douglas 19 page += buffer;
307     left -= bytes;
308     }
309     }
310     else
311     {
312 douglas 20 unsigned chunk;
313    
314     do
315     {
316     chunk = strtoul(getline().c_str(), 0, 16);
317    
318     unsigned left = chunk;
319    
320     while (left > 0)
321     {
322     memset(buffer, 0, BUFSIZ + 1);
323    
324     unsigned bytes = left > BUFSIZ ? BUFSIZ : left;
325 douglas 24 unsigned received;
326 douglas 20
327 douglas 145 while (true)
328 douglas 20 {
329 douglas 145 if ((received = recv(http, buffer, bytes, 0)) ==
330     SOCKET_ERROR)
331 douglas 24 {
332     error(program + ": Recv");
333     exit(1);
334     }
335 douglas 145 else if (received != bytes)
336     {
337     left -= received;
338     page += buffer;
339    
340     memset(buffer, 0, BUFSIZ + 1);
341    
342     bytes -= received;
343     }
344     else
345     {
346     break;
347     }
348 douglas 24 }
349    
350 douglas 20 page += buffer;
351     left -= bytes;
352     }
353    
354     getline();
355     length += chunk;
356     }
357     while (chunk > 0);
358 douglas 19 }
359    
360 douglas 20 for (unsigned index = 0; index < page.length(); index++)
361     {
362     if (page[index] == '\r' && (index + 1 < page.length()) ? page[index +
363     1] == '\n' : false)
364     {
365     page.erase(index, 1);
366     }
367     else if (page[index] == '\r')
368     {
369     page[index] = '\n';
370     }
371     }
372 douglas 19 }
373    
374 douglas 14 void HttpHandler::putline(const string line)
375     {
376     sprintf(buffer, "%s\r\n", line.c_str());
377     if (send(http, buffer, strlen(buffer), 0) == SOCKET_ERROR)
378     {
379     error(program + ": Send");
380     exit(1);
381     }
382     }
383    
384 douglas 17 string HttpHandler::getline()
385     {
386     string line;
387     char byte;
388    
389     do
390     {
391     if (recv(http, &byte, 1, 0) == SOCKET_ERROR)
392     {
393     error(program + ": Recv");
394     }
395    
396     if (byte != '\r' && byte != '\n')
397     {
398     line += byte;
399     }
400     }
401     while (byte != '\n');
402    
403     return line;
404     }
405    
406 douglas 18 void HttpHandler::error(const string& prefix, bool host)
407 douglas 1 {
408 douglas 13 #ifdef _WIN32
409     string error;
410 douglas 1
411 douglas 13 switch (WSAGetLastError())
412 douglas 1 {
413 douglas 13 case WSAEACCES:
414     error = "Permission denied.";
415     break;
416     case WSAEADDRINUSE:
417     error = "Address already in use.";
418     break;
419     case WSAEADDRNOTAVAIL:
420     error = "Cannot assign requested address.";
421     break;
422     case WSAEAFNOSUPPORT:
423     error = "Address family not supported by protocol family.";
424     break;
425     case WSAEALREADY:
426     error = "Operation already in progress.";
427     break;
428     case WSAECONNABORTED:
429     error = "Software caused connection abort.";
430     break;
431     case WSAECONNREFUSED:
432     error = "Connection refused.";
433     break;
434     case WSAECONNRESET:
435     error = "Connection reset by peer.";
436     break;
437     case WSAEDESTADDRREQ:
438     error = "Destination address required.";
439     break;
440     case WSAEFAULT:
441     error = "Bad address.";
442     break;
443     case WSAEHOSTDOWN:
444     error = "Host is down.";
445     break;
446     case WSAEHOSTUNREACH:
447     error = "No route to host.";
448     break;
449     case WSAEINPROGRESS:
450     error = "Operation now in progress.";
451     break;
452     case WSAEINTR:
453     error = "Interrupted function call.";
454     break;
455     case WSAEINVAL:
456     error = "Invalid argument.";
457     break;
458     case WSAEISCONN:
459     error = "Socket is already connected.";
460     break;
461     case WSAEMFILE:
462     error = "Too many open files.";
463     break;
464     case WSAEMSGSIZE:
465     error = "Message too long.";
466     break;
467     case WSAENETDOWN:
468     error = "Network is down.";
469     break;
470     case WSAENETRESET:
471     error = "Network dropped connection on reset.";
472     break;
473     case WSAENETUNREACH:
474     error = "Network is unreachable.";
475     break;
476     case WSAENOBUFS:
477     error = "No buffer space available.";
478     break;
479     case WSAENOPROTOOPT:
480     error = "Bad protocol option.";
481     break;
482     case WSAENOTCONN:
483     error = "Socket is not connected.";
484     break;
485     case WSAENOTSOCK:
486     error = "Socket operation on non-socket.";
487     break;
488     case WSAEOPNOTSUPP:
489     error = "Operation not supported.";
490     break;
491     case WSAEPFNOSUPPORT:
492     error = "Protocol family not supported.";
493     break;
494     case WSAEPROCLIM:
495     error = "Too many processes.";
496     break;
497     case WSAEPROTONOSUPPORT:
498     error = "Protocol not supported.";
499     break;
500     case WSAEPROTOTYPE:
501     error = "Protocol wrong type for socket.";
502     break;
503     case WSAESHUTDOWN:
504     error = "Cannot send after socket shutdown.";
505     break;
506     case WSAESOCKTNOSUPPORT:
507     error = "Socket type not supported.";
508     break;
509     case WSAETIMEDOUT:
510     error = "Connection timed out.";
511     break;
512     case WSATYPE_NOT_FOUND:
513     error = "Class type not found.";
514     break;
515     case WSAEWOULDBLOCK:
516     error = "Resource temporarily unavailable.";
517     break;
518     case WSAHOST_NOT_FOUND:
519     error = "Host not found.";
520     break;
521     case WSA_INVALID_HANDLE:
522     error = "Specified event object handle is invalid.";
523     break;
524     case WSA_INVALID_PARAMETER:
525     error = "One or more parameters are invalid.";
526     break;
527     // case WSAINVALIDPROCTABLE:
528     // error = "Invalid procedure table from service provider.";
529     // break;
530     // case WSAINVALIDPROVIDER:
531     // error = "Invalid service provider version number.";
532     // break;
533     case WSA_IO_INCOMPLETE:
534     error = "Overlapped I/O event object not in signaled state.";
535     break;
536     case WSA_IO_PENDING:
537     error = "Overlapped operations will complete later.";
538     break;
539     case WSA_NOT_ENOUGH_MEMORY:
540     error = "Insufficient memory available.";
541     break;
542     case WSANOTINITIALISED:
543     error = "Successful WSAStartup not yet performed.";
544     break;
545     case WSANO_DATA:
546     error = "Valid name, no data record of requested type.";
547     break;
548     case WSANO_RECOVERY:
549     error = "This is a non-recoverable error.";
550     break;
551     // case WSAPROVIDERFAILEDINIT:
552     // error = "Unable to initialize a service provider.";
553     // break;
554     case WSASYSCALLFAILURE:
555     error = "System call failure.";
556     break;
557     case WSASYSNOTREADY:
558     error = "Network subsystem is unavailable.";
559     break;
560     case WSATRY_AGAIN:
561     error = "Non-authoritative host not found.";
562     break;
563     case WSAVERNOTSUPPORTED:
564     error = "WINSOCK.DLL version out of range.";
565     break;
566     case WSAEDISCON:
567     error = "Graceful shutdown in progress.";
568     break;
569     case WSA_OPERATION_ABORTED:
570     error = "Overlapped operation aborted.";
571     break;
572     default:
573     error = "Unknown error.";
574     break;
575     }
576 douglas 1
577 douglas 13 cerr << prefix << ": " << error << "\n";
578     #else
579     if (host)
580     {
581     herror(prefix.c_str());
582 douglas 1 }
583     else
584     {
585 douglas 13 perror(prefix.c_str());
586 douglas 1 }
587 douglas 13 #endif // _WIN32
588     }