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 13 by douglas, 2002-12-06T19:56:56-08:00 vs.
Revision 179 by douglas, 2003-07-05T19:47:23-07:00

# Line 1 | Line 1
1   /* ============================================================================
2   * Douglas Thrift's Search Engine License
3   *
4 < * Copyright (C) 2002, Douglas Thrift. All Rights Reserved.
4 > * Copyright (C) 2002-2003, 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   *
# Line 50 | Line 50
50  
51   #include "HttpHandler.h"
52  
53 + // Lovely C Sockets!
54 + #ifndef _WIN32
55 + // BSD Sockets
56 + #include <unistd.h>
57 + #include <sys/types.h>
58 + #include <sys/socket.h>
59 + #include <netinet/in.h>
60 + #include <netdb.h>
61 +
62 + #define INVALID_SOCKET -1
63 + #define SOCKET_ERROR -1
64 +
65 + inline int closesocket(SOCKET s) { return close(s); }
66 + #endif
67 +
68   HttpHandler::HttpHandler()
69   {
70 +        buffer = new char[BUFSIZ + 1];
71 +
72   #ifdef _WIN32
73 <        WSADATA data;
57 <        if (WSAStartup(MAKEWORD(2, 0) != 0, &data))
73 >        if (WSAStartup(MAKEWORD(2, 0), &data) != 0)
74          {
75 <                error(program + ": WSAStartup()");
75 >                error(program + ": WSAStartup");
76                  exit(1);
77          }
78   #endif // _WIN32
79  
80 <        begin = 0;
80 >        length = 0;
81 >        chunked = false;
82   }
83  
84   HttpHandler::~HttpHandler()
85   {
86 +        delete [] buffer;
87 +
88   #ifdef _WIN32
89          WSACleanup();
90   #endif // _WIN32
91   }
92  
93 < bool HttpHandler::connect(URL &url, bool head)
93 > bool HttpHandler::handle(URL &url, const string referer, bool head)
94   {
95          bool answer = false;
96  
97 < //      if (status != JNI_ERR)
98 < //      {
99 < //              if (cls != 0)
100 < //              {
101 < //                      if (mid != 0)
102 < //                      {
103 < //                              jstring addressJ = env->NewStringUTF(url.getAddress().c_str());
104 < //                              jint portJ = url.getPort();
105 < //                              jstring pathJ = env->NewStringUTF(url.getPath().c_str());
106 < //                              jstring programNameJ = env->NewStringUTF(programName.c_str());
107 < //                              jstring programVersionJ =
108 < //                                      env->NewStringUTF(programVersion.c_str());
109 <
110 < //                              jstring pageJ = (jstring)env->CallStaticObjectMethod(cls, mid,
111 < //                                      addressJ, portJ, pathJ, programNameJ, programVersionJ);
112 <
113 < //                              const char* pageC = env->GetStringUTFChars(pageJ, 0);
114 < //                              page = pageC;
115 < //                              env->ReleaseStringUTFChars(pageJ, pageC);
116 <
117 < //                              if (page != "unknown host\n" && page != "io exception\n" &&
118 < //                                      page != "bad headers\n") answer = true;
119 < //                      }
120 < //              }
121 < //      }
97 >        if ((http = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
98 >        {
99 >                error(program + ": Socket");
100 >                exit(1);
101 >        }
102 >
103 >        sockaddr_in address;
104 >        hostent* host;
105 >
106 >        address.sin_family = AF_INET;
107 >
108 >        if ((host = gethostbyname(url.getAddress().c_str())) == NULL)
109 >        {
110 >                error(program + ": Host: " + url.getAddress(), true);
111 >                return answer;
112 >        }
113 >
114 >        address.sin_addr = *((in_addr*)*host->h_addr_list);
115 >        address.sin_port = htons(url.getPort());
116 >
117 >        if (connect(http, (sockaddr*)&address, sizeof(sockaddr_in)) ==
118 >                SOCKET_ERROR)
119 >        {
120 >                error(program + ": Connect");
121 >                return answer;
122 >        }
123 >
124 >        if (head)
125 >        {
126 >                putline("HEAD " + url.getPath() + " HTTP/1.1");
127 >        }
128 >        else
129 >        {
130 >                putline("GET " + url.getPath() + " HTTP/1.1");
131 >        }
132 >
133 >        putline("Accept: text/html; text/plain");
134 >        putline("User-Agent: " + agent(true) + ' ' + platform());
135 >
136 >        if (url.getPort() == 80)
137 >        {
138 >                putline("Host: " + url.getAddress());
139 >        }
140 >        else
141 >        {
142 >                char* port = new char[1024];
143 >                sprintf(port, "%u", url.getPort());
144 >
145 >                putline("Host: " + url.getAddress() + ':' + port);
146 >
147 >                delete [] port;
148 >        }
149 >
150 >        if (referer != "")
151 >        {
152 >                putline("Referer: " + referer);
153 >        }
154 >
155 >        putline("Connection: close");
156 >        putline();
157 >
158 >        code response;
159 >        string line;
160 >
161 >        do
162 >        {
163 >                line = getline();
164 >
165 >                if (line.find("HTTP/") != 0)
166 >                {
167 >                        return answer;
168 >                }
169 >
170 >                unsigned dot = line.find('.');
171 >                unsigned space = line.find(' ');
172 >
173 >                unsigned major = strtoul(line.substr(5, dot - 5).c_str(), 0, 10);
174 >                unsigned minor = strtoul(line.substr(dot + 1, space - dot - 1).c_str(),
175 >                        0, 10);
176 >
177 >                if (major > 1)
178 >                {
179 >                        cerr << program << ": Potentially Incompatible Server: HTTP/" <<
180 >                                major << "." << minor << "\n";
181 >
182 >                        return answer;
183 >                }
184 >
185 >                response = code(strtoul(line.substr(space + 1).c_str(), 0, 10));
186 >
187 >                if (response < ok) do line = getline(); while (line != "");
188 >        }
189 >        while (response < ok);
190 >
191 >        do
192 >        {
193 >                line = getline();
194 >
195 >                if (line != "")
196 >                {
197 >                        unsigned colon = line.find(':');
198 >
199 >                        string field = line.substr(0, colon);
200 >                        string value = line.substr(colon + 1);
201 >
202 >                        while (isspace(value[0])) value.erase(0, 1);
203 >
204 >                        if (field == "Content-Type")
205 >                        {
206 >                                type = value;
207 >                        }
208 >                        else if (field == "Content-Length")
209 >                        {
210 >                                length = strtoul(value.c_str(), 0, 10);
211 >                        }
212 >                        else if (field == "Location")
213 >                        {
214 >                                location = value;
215 >                        }
216 >                        else if (field == "Transfer-Encoding")
217 >                        {
218 >                                chunked = value == "chunked";
219 >                        }
220 >                }
221 >        }
222 >        while (line != "");
223 >
224 >        switch (response)
225 >        {
226 >        case ok:
227 >                if (debug) cerr << "response = " << response << "\n";
228 >                answer = true;
229 >                break;
230 >        case choices:
231 >        case moved:
232 >        case found:
233 >                if (debug) cerr << "response = " << response << "\n"
234 >                        << "location = " << location << "\n";
235 >                location = getLink(location, url);
236 >                break;
237 >        case notfound:
238 >        case internal:
239 >                if (debug) cerr << "response = " << response << "\n";
240 >                break;
241 >        default:
242 >                if (debug) cerr << "response = " << response << "\n";
243 >                if (response <= 299)
244 >                {
245 >                        answer = true;
246 >                }
247 >                else if (response <= 399)
248 >                {
249 >                        location = getLink(location, url);
250 >                }
251 >                break;
252 >        }
253 >
254 >        if (!head && answer) populate();
255  
256          return answer;
257   }
258  
259   HttpHandler& HttpHandler::getline(string& line, char endline)
260   {
261 <        int end = page.find(endline, begin);
262 <        int newline = page.find('\n', begin);
261 >        unsigned end = page.find(endline);
262 >        unsigned newline = page.find('\n');
263  
264          if (newline < end || end == string::npos)
265          {
266                  end = newline;
267          }
268  
269 <        line = page.substr(begin, end - begin);
269 >        line = page.substr(0, end);
270 >        page.erase(0, (end == string::npos ? end : end + 1));
271 >
272 >        return *this;
273 > }
274  
275 <        if (end == string::npos)
275 > void HttpHandler::clear()
276 > {
277 >        closesocket(http);
278 >
279 >        type = "";
280 >        length = 0;
281 >        location = "";
282 >        page = "";
283 >        chunked = false;
284 > }
285 >
286 > void HttpHandler::populate()
287 > {
288 >        if (!chunked)
289          {
290 <                begin = end;
290 >                unsigned left = length;
291 >
292 >                while (left > 0)
293 >                {
294 >                        memset(buffer, 0, BUFSIZ + 1);
295 >
296 >                        unsigned bytes = left > BUFSIZ ? BUFSIZ : left;
297 >                        unsigned received;
298 >
299 >                        while (true)
300 >                        {
301 >                                if ((received = recv(http, buffer, bytes, 0)) == SOCKET_ERROR)
302 >                                {
303 >                                        error(program + ": Recv");
304 >                                        exit(1);
305 >                                }
306 >                                else if (received != bytes)
307 >                                {
308 >                                        left -= received;
309 >                                        page += buffer;
310 >
311 >                                        memset(buffer, 0, BUFSIZ + 1);
312 >
313 >                                        bytes -= received;
314 >                                }
315 >                                else
316 >                                {
317 >                                        break;
318 >                                }
319 >                        }
320 >
321 >                        page += buffer;
322 >                        left -= bytes;
323 >                }
324          }
325          else
326          {
327 <                begin = end + 1;
327 >                unsigned chunk;
328 >
329 >                do
330 >                {
331 >                        chunk = strtoul(getline().c_str(), 0, 16);
332 >
333 >                        unsigned left = chunk;
334 >
335 >                        while (left > 0)
336 >                        {
337 >                                memset(buffer, 0, BUFSIZ + 1);
338 >
339 >                                unsigned bytes = left > BUFSIZ ? BUFSIZ : left;
340 >                                unsigned received;
341 >
342 >                                while (true)
343 >                                {
344 >                                        if ((received = recv(http, buffer, bytes, 0)) ==
345 >                                                SOCKET_ERROR)
346 >                                        {
347 >                                                error(program + ": Recv");
348 >                                                exit(1);
349 >                                        }
350 >                                        else if (received != bytes)
351 >                                        {
352 >                                                left -= received;
353 >                                                page += buffer;
354 >
355 >                                                memset(buffer, 0, BUFSIZ + 1);
356 >
357 >                                                bytes -= received;
358 >                                        }
359 >                                        else
360 >                                        {
361 >                                                break;
362 >                                        }
363 >                                }
364 >
365 >                                page += buffer;
366 >                                left -= bytes;
367 >                        }
368 >
369 >                        getline();
370 >                        length += chunk;
371 >                }
372 >                while (chunk > 0);
373          }
374  
375 <        return *this;
375 >        for (unsigned index = 0; index < page.length(); index++)
376 >        {
377 >                if (page[index] == '\r' && (index + 1 < page.length()) ? page[index +
378 >                        1] == '\n' : false)
379 >                {
380 >                        page.erase(index, 1);
381 >                }
382 >                else if (page[index] == '\r')
383 >                {
384 >                        page[index] = '\n';
385 >                }
386 >        }
387   }
388  
389 < bool HttpHandler::good()
389 > void HttpHandler::putline(const string line)
390   {
391 <        bool answer = true;
392 <
135 <        if (begin >= page.length())
391 >        sprintf(buffer, "%s\r\n", line.c_str());
392 >        if (send(http, buffer, strlen(buffer), 0) == SOCKET_ERROR)
393          {
394 <                answer = false;
395 <        }
139 <        else if (begin == string::npos)
140 <        {
141 <                answer = false;
394 >                error(program + ": Send");
395 >                exit(1);
396          }
143
144        return answer;
397   }
398  
399 < void HttpHandler::clear()
399 > string HttpHandler::getline()
400   {
401 <        begin = 0;
402 <        page = "";
401 >        string line;
402 >        char byte;
403 >
404 >        do
405 >        {
406 >                if (recv(http, &byte, 1, 0) == SOCKET_ERROR)
407 >                {
408 >                        error(program + ": Recv");
409 >                }
410 >
411 >                if (byte != '\r' && byte != '\n')
412 >                {
413 >                        line += byte;
414 >                }
415 >        }
416 >        while (byte != '\n');
417 >
418 >        return line;
419   }
420  
421 < void HttpHandler::error(const string prefix, bool host)
421 > void HttpHandler::error(const string& prefix, bool host)
422   {
423   #ifdef _WIN32
424          string error;
# Line 325 | Line 593 | void HttpHandler::error(const string pre
593   #else
594          if (host)
595          {
596 <                herror(prefix.c_str());
596 >                string error;
597 >
598 >                switch (h_errno)
599 >                {
600 >                case HOST_NOT_FOUND:
601 >                        error = "Unknown host";
602 >                        break;
603 >                case TRY_AGAIN:
604 >                        error = "Host name lookup failure";
605 >                        break;
606 >                case NO_RECOVERY:
607 >                        error = "Unknown server error";
608 >                        break;
609 >                case NO_DATA:
610 >                        error = "No address associated with name";
611 >                        break;
612 >                default:
613 >                        error = "Unknown error";
614 >                        break;
615 >                }
616 >
617 >                cerr << prefix << ": " << error << "\n";
618          }
619          else
620          {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines