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 1 by douglas, 2002-12-04T20:22:59-08:00 vs.
Revision 24 by douglas, 2002-12-22T00:58:21-08:00

# Line 50 | Line 50
50  
51   #include "HttpHandler.h"
52  
53 #ifdef _WIN32
54 #define PATH_SEPARATOR "\\"
55 #define ENV_SEPARATOR ';'
56 #else
57 #define PATH_SEPARATOR "/"
58 #define ENV_SEPARATOR ':'
59 #endif
60
53   HttpHandler::HttpHandler()
54   {
55 <        string arguments = "(Ljava/lang/String;ILjava/lang/String;Ljava/lang/Strin"
64 <                + string("g;Ljava/lang/String;)Ljava/lang/String;");
55 >        buffer = new char[BUFSIZ + 1];
56  
57 <        begin = 0;
58 <
59 <        setJarPath();
57 > #ifdef _WIN32
58 >        if (WSAStartup(MAKEWORD(2, 0), &data) != 0)
59 >        {
60 >                error(program + ": WSAStartup");
61 >                exit(1);
62 >        }
63 > #endif // _WIN32
64  
65 < //      options[0].optionString = jarPath;
66 < //      memset(&vm_args, 0, sizeof (vm_args));
72 < //      vm_args.version = JNI_VERSION_1_4;
73 < //      vm_args.nOptions = 1;
74 < //      vm_args.options = options;
75 <
76 < //      status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
77 <
78 < //      if (status != JNI_ERR)
79 < //      {
80 < //              cls = env->FindClass("HttpConnector");
81 <
82 < //              if (cls != 0)
83 < //              {
84 < //                      mid = env->GetStaticMethodID(cls, "connect", arguments.c_str());
85 < //              }
86 < //      }
65 >        length = 0;
66 >        chunked = false;
67   }
68  
69   HttpHandler::~HttpHandler()
70   {
71 < //      if (status != JNI_ERR)
92 < //      {
93 < //              jvm->DestroyJavaVM();
94 < //      }
71 >        delete [] buffer;
72  
73 < //      delete [] jarPath;
73 > #ifdef _WIN32
74 >        WSACleanup();
75 > #endif // _WIN32
76   }
77  
78 < bool HttpHandler::connect(URL& url)
78 > bool HttpHandler::handle(URL &url, bool head)
79   {
80          bool answer = false;
81  
82 < //      if (status != JNI_ERR)
83 < //      {
84 < //              if (cls != 0)
85 < //              {
86 < //                      if (mid != 0)
108 < //                      {
109 < //                              jstring addressJ = env->NewStringUTF(url.getAddress().c_str());
110 < //                              jint portJ = url.getPort();
111 < //                              jstring pathJ = env->NewStringUTF(url.getPath().c_str());
112 < //                              jstring programNameJ = env->NewStringUTF(programName.c_str());
113 < //                              jstring programVersionJ =
114 < //                                      env->NewStringUTF(programVersion.c_str());
115 <
116 < //                              jstring pageJ = (jstring)env->CallStaticObjectMethod(cls, mid,
117 < //                                      addressJ, portJ, pathJ, programNameJ, programVersionJ);
118 <
119 < //                              const char* pageC = env->GetStringUTFChars(pageJ, 0);
120 < //                              page = pageC;
121 < //                              env->ReleaseStringUTFChars(pageJ, pageC);
122 <
123 < //                              if (page != "unknown host\n" && page != "io exception\n" &&
124 < //                                      page != "bad headers\n") answer = true;
125 < //                      }
126 < //              }
127 < //      }
82 >        if ((http = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
83 >        {
84 >                error(program + ": Socket");
85 >                exit(1);
86 >        }
87  
88 <        return answer;
89 < }
88 >        sockaddr_in address;
89 >        hostent* host;
90  
91 < HttpHandler& HttpHandler::getline(string& line, char endline)
133 < {
134 <        int end = page.find(endline, begin);
135 <        int newline = page.find('\n', begin);
91 >        address.sin_family = AF_INET;
92  
93 <        if (newline < end || end == string::npos)
93 >        if ((host = gethostbyname(url.getAddress().c_str())) == NULL)
94          {
95 <                end = newline;
95 >                error(program + ": Host: " + url.getAddress(), true);
96 >                return answer;
97          }
98  
99 <        line = page.substr(begin, end - begin);
99 >        address.sin_addr = *((in_addr*)*host->h_addr_list);
100 >        address.sin_port = htons(url.getPort());
101  
102 <        if (end == string::npos)
102 >        if (connect(http, (sockaddr*)&address, sizeof(sockaddr_in)) ==
103 >                SOCKET_ERROR)
104          {
105 <                begin = end;
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 <                begin = end + 1;
115 >                putline("GET " + url.getPath() + " HTTP/1.1");
116          }
117  
118 <        return *this;
119 < }
118 >        putline("Accept: text/html; text/plain");
119 >        putline("User-Agent: " + agent(true) + ' ' + platform());
120  
121 < bool HttpHandler::good()
122 < {
123 <        bool answer = true;
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 > //      putline("Referer: " + ¿referer?);
136 >        putline("Connection: close");
137 >        putline();
138 >
139 >        code response;
140 >        string line;
141 >
142 >        do
143 >        {
144 >                line = getline();
145  
146 <        if (begin >= page.length())
146 >                if (line.find("HTTP/") != 0)
147 >                {
148 >                        return answer;
149 >                }
150 >
151 >                unsigned dot = line.find('.');
152 >                unsigned space = line.find(' ');
153 >
154 >                unsigned major = strtoul(line.substr(5, dot - 5).c_str(), 0, 10);
155 >                unsigned minor = strtoul(line.substr(dot + 1, space - dot - 1).c_str(),
156 >                        0, 10);
157 >
158 >                if (major > 1)
159 >                {
160 >                        cerr << program << ": Potentially Incompatible Server: HTTP/" <<
161 >                                major << "." << minor << "\n";
162 >
163 >                        return answer;
164 >                }
165 >
166 >                response = code(strtoul(line.substr(space + 1).c_str(), 0, 10));
167 >
168 >                if (response < ok) do line = getline(); while (line != "");
169 >        }
170 >        while (response < ok);
171 >
172 >        do
173          {
174 <                answer = false;
174 >                line = getline();
175 >
176 >                if (line != "")
177 >                {
178 >                        unsigned colon = line.find(':');
179 >
180 >                        string field = line.substr(0, colon);
181 >                        string value = line.substr(colon + 1);
182 >
183 >                        while (isspace(value[0])) value.erase(0, 1);
184 >
185 >                        if (field == "Content-Type")
186 >                        {
187 >                                type = value;
188 >                        }
189 >                        else if (field == "Content-Length")
190 >                        {
191 >                                length = strtoul(value.c_str(), 0, 10);
192 >                        }
193 >                        else if (field == "Location")
194 >                        {
195 >                                location = value;
196 >                        }
197 >                        else if (field == "Transfer-Encoding")
198 >                        {
199 >                                chunked = value == "chunked";
200 >                        }
201 >                }
202          }
203 <        else if (begin == string::npos)
203 >        while (line != "");
204 >
205 >        switch (response)
206          {
207 <                answer = false;
207 >        case ok:
208 >                if (debug) cerr << "response = " << response << "\n";
209 >                answer = true;
210 >                break;
211 >        case choices:
212 >        case moved:
213 >        case found:
214 >                if (debug) cerr << "response = " << response << "\n"
215 >                        << "location = " << location << "\n";
216 >                location = getLink(location, url);
217 >                break;
218 >        case notfound:
219 >        case internal:
220 >                if (debug) cerr << "response = " << response << "\n";
221 >                break;
222 >        default:
223 >                if (debug) cerr << "response = " << response << "\n";
224 >                if (response <= 299)
225 >                {
226 >                        answer = true;
227 >                }
228 >                else if (response <= 399)
229 >                {
230 >                        location = getLink(location, url);
231 >                }
232 >                break;
233          }
234  
235 +        if (!head && answer) populate();
236 +
237          return answer;
238   }
239  
240 + HttpHandler& HttpHandler::getline(string& line, char endline)
241 + {
242 +        unsigned end = page.find(endline);
243 +        unsigned newline = page.find('\n');
244 +
245 +        if (newline < end || end == string::npos)
246 +        {
247 +                end = newline;
248 +        }
249 +
250 +        line = page.substr(0, end);
251 +        page.erase(0, (end == string::npos ? end : end + 1));
252 +
253 +        return *this;
254 + }
255 +
256   void HttpHandler::clear()
257   {
258 <        begin = 0;
258 >        closesocket(http);
259 >
260 >        type = "";
261 >        length = 0;
262 >        location = "";
263          page = "";
264 +        chunked = false;
265   }
266  
267 < void HttpHandler::setJarPath()
267 > void HttpHandler::populate()
268   {
269 <        const string jarFile = "HttpConnector.jar";
181 <        string jarFilePath = jarFile;
182 <
183 <        ifstream fin(jarFilePath.c_str());
184 <        if (!fin.is_open())
269 >        if (!chunked)
270          {
271 <                unsigned end = program.rfind(PATH_SEPARATOR);
272 <                if (end != string::npos)
271 >                unsigned left = length;
272 >
273 >                while (left > 0)
274                  {
275 <                        string path = program.substr(0, end);
275 >                        memset(buffer, 0, BUFSIZ + 1);
276  
277 <                        jarFilePath = path + PATH_SEPARATOR + jarFile;
277 >                        unsigned bytes = left > BUFSIZ ? BUFSIZ : left;
278 >                        unsigned received;
279  
280 <                        fin.open(jarFilePath.c_str());
194 <                        if (!fin.is_open())
280 >                        if ((received = recv(http, buffer, bytes, 0)) == SOCKET_ERROR)
281                          {
282 <                                cerr << program << ": Could not find required file: "
197 <                                        << jarFile << "\n";
282 >                                error(program + ": Recv");
283                                  exit(1);
284                          }
285 <                        else
285 >                        else if (received != bytes)
286                          {
287 <                                fin.close();
287 >                                left -= received;
288 >                                page += buffer;
289 >
290 >                                memset(buffer, 0, BUFSIZ + 1);
291 >
292 >                                bytes -= received;
293 >                                if (recv(http, buffer, bytes, 0) == SOCKET_ERROR)
294 >                                {
295 >                                        error(program + ": Recv");
296 >                                        exit(1);
297 >                                }
298                          }
299 +
300 +                        page += buffer;
301 +                        left -= bytes;
302                  }
303 <                else
303 >        }
304 >        else
305 >        {
306 >                unsigned chunk;
307 >
308 >                do
309                  {
310 <                        string envPath = getenv("PATH");
310 >                        chunk = strtoul(getline().c_str(), 0, 16);
311  
312 <                        if (debug) cerr << "envPath = " << envPath << "\n";
312 >                        unsigned left = chunk;
313  
314 <                        unsigned begin = 0;
212 <                        do
314 >                        while (left > 0)
315                          {
316 <                                unsigned end = envPath.find(ENV_SEPARATOR, begin);
215 <                                string path = envPath.substr(begin, end);
316 >                                memset(buffer, 0, BUFSIZ + 1);
317  
318 <                                if (debug) cerr << "path = " << path << "\n";
318 >                                unsigned bytes = left > BUFSIZ ? BUFSIZ : left;
319 >                                unsigned received;
320  
321 <                                jarFilePath = path + PATH_SEPARATOR + jarFile;
322 <
323 <                                fin.open(jarFilePath.c_str());
324 <                                if (fin.is_open())
321 >                                if ((received = recv(http, buffer, bytes, 0)) == SOCKET_ERROR)
322 >                                {
323 >                                        error(program + ": Recv");
324 >                                        exit(1);
325 >                                }
326 >                                else if (received != bytes)
327                                  {
328 <                                        fin.close();
329 <                                        break;
328 >                                        left -= received;
329 >                                        page += buffer;
330 >
331 >                                        memset(buffer, 0, BUFSIZ + 1);
332 >
333 >                                        bytes -= received;
334 >                                        if (recv(http, buffer, bytes, 0) == SOCKET_ERROR)
335 >                                        {
336 >                                                error(program + ": Recv");
337 >                                                exit(1);
338 >                                        }
339                                  }
340  
341 <                                begin = end != string::npos ? end + 1 : string::npos;
341 >                                page += buffer;
342 >                                left -= bytes;
343                          }
230                        while (begin < envPath.length());
344  
345 <                        if (begin == string::npos)
346 <                        {
234 <                                cerr << program << ": Could not find required file: "
235 <                                        << jarFile << "\n";
236 <                                exit(1);
237 <                        }
345 >                        getline();
346 >                        length += chunk;
347                  }
348 +                while (chunk > 0);
349          }
350 <        else
350 >
351 >        for (unsigned index = 0; index < page.length(); index++)
352          {
353 <                fin.close();
353 >                if (page[index] == '\r' && (index + 1 < page.length()) ? page[index +
354 >                        1] == '\n' : false)
355 >                {
356 >                        page.erase(index, 1);
357 >                }
358 >                else if (page[index] == '\r')
359 >                {
360 >                        page[index] = '\n';
361 >                }
362          }
363 + }
364  
365 <        string jarPath = "-Djava.class.path=" + jarFilePath;
365 > void HttpHandler::putline(const string line)
366 > {
367 >        sprintf(buffer, "%s\r\n", line.c_str());
368 >        if (send(http, buffer, strlen(buffer), 0) == SOCKET_ERROR)
369 >        {
370 >                error(program + ": Send");
371 >                exit(1);
372 >        }
373 > }
374  
375 < //      this->jarPath = new char[jarPath.length()];
376 < //      strcpy(this->jarPath, jarPath.c_str());
375 > string HttpHandler::getline()
376 > {
377 >        string line;
378 >        char byte;
379 >
380 >        do
381 >        {
382 >                if (recv(http, &byte, 1, 0) == SOCKET_ERROR)
383 >                {
384 >                        error(program + ": Recv");
385 >                }
386 >
387 >                if (byte != '\r' && byte != '\n')
388 >                {
389 >                        line += byte;
390 >                }
391 >        }
392 >        while (byte != '\n');
393 >
394 >        return line;
395 > }
396 >
397 > void HttpHandler::error(const string& prefix, bool host)
398 > {
399 > #ifdef _WIN32
400 >        string error;
401 >
402 >        switch (WSAGetLastError())
403 >        {
404 >        case WSAEACCES:
405 >                error = "Permission denied.";
406 >                break;
407 >        case WSAEADDRINUSE:
408 >                error = "Address already in use.";
409 >                break;
410 >        case WSAEADDRNOTAVAIL:
411 >                error = "Cannot assign requested address.";
412 >                break;
413 >        case WSAEAFNOSUPPORT:
414 >                error = "Address family not supported by protocol family.";
415 >                break;
416 >        case WSAEALREADY:
417 >                error = "Operation already in progress.";
418 >                break;
419 >        case WSAECONNABORTED:
420 >                error = "Software caused connection abort.";
421 >                break;
422 >        case WSAECONNREFUSED:
423 >                error = "Connection refused.";
424 >                break;
425 >        case WSAECONNRESET:
426 >                error = "Connection reset by peer.";
427 >                break;
428 >        case WSAEDESTADDRREQ:
429 >                error = "Destination address required.";
430 >                break;
431 >        case WSAEFAULT:
432 >                error = "Bad address.";
433 >                break;
434 >        case WSAEHOSTDOWN:
435 >                error = "Host is down.";
436 >                break;
437 >        case WSAEHOSTUNREACH:
438 >                error = "No route to host.";
439 >                break;
440 >        case WSAEINPROGRESS:
441 >                error = "Operation now in progress.";
442 >                break;
443 >        case WSAEINTR:
444 >                error = "Interrupted function call.";
445 >                break;
446 >        case WSAEINVAL:
447 >                error = "Invalid argument.";
448 >                break;
449 >        case WSAEISCONN:
450 >                error = "Socket is already connected.";
451 >                break;
452 >        case WSAEMFILE:
453 >                error = "Too many open files.";
454 >                break;
455 >        case WSAEMSGSIZE:
456 >                error = "Message too long.";
457 >                break;
458 >        case WSAENETDOWN:
459 >                error = "Network is down.";
460 >                break;
461 >        case WSAENETRESET:
462 >                error = "Network dropped connection on reset.";
463 >                break;
464 >        case WSAENETUNREACH:
465 >                error = "Network is unreachable.";
466 >                break;
467 >        case WSAENOBUFS:
468 >                error = "No buffer space available.";
469 >                break;
470 >        case WSAENOPROTOOPT:
471 >                error = "Bad protocol option.";
472 >                break;
473 >        case WSAENOTCONN:
474 >                error = "Socket is not connected.";
475 >                break;
476 >        case WSAENOTSOCK:
477 >                error = "Socket operation on non-socket.";
478 >                break;
479 >        case WSAEOPNOTSUPP:
480 >                error = "Operation not supported.";
481 >                break;
482 >        case WSAEPFNOSUPPORT:
483 >                error = "Protocol family not supported.";
484 >                break;
485 >        case WSAEPROCLIM:
486 >                error = "Too many processes.";
487 >                break;
488 >        case WSAEPROTONOSUPPORT:
489 >                error = "Protocol not supported.";
490 >                break;
491 >        case WSAEPROTOTYPE:
492 >                error = "Protocol wrong type for socket.";
493 >                break;
494 >        case WSAESHUTDOWN:
495 >                error = "Cannot send after socket shutdown.";
496 >                break;
497 >        case WSAESOCKTNOSUPPORT:
498 >                error = "Socket type not supported.";
499 >                break;
500 >        case WSAETIMEDOUT:
501 >                error = "Connection timed out.";
502 >                break;
503 >        case WSATYPE_NOT_FOUND:
504 >                error = "Class type not found.";
505 >                break;
506 >        case WSAEWOULDBLOCK:
507 >                error = "Resource temporarily unavailable.";
508 >                break;
509 >        case WSAHOST_NOT_FOUND:
510 >                error = "Host not found.";
511 >                break;
512 >        case WSA_INVALID_HANDLE:
513 >                error = "Specified event object handle is invalid.";
514 >                break;
515 >        case WSA_INVALID_PARAMETER:
516 >                error = "One or more parameters are invalid.";
517 >                break;
518 > //      case WSAINVALIDPROCTABLE:
519 > //              error = "Invalid procedure table from service provider.";
520 > //              break;
521 > //      case WSAINVALIDPROVIDER:
522 > //              error = "Invalid service provider version number.";
523 > //              break;
524 >        case WSA_IO_INCOMPLETE:
525 >                error = "Overlapped I/O event object not in signaled state.";
526 >                break;
527 >        case WSA_IO_PENDING:
528 >                error = "Overlapped operations will complete later.";
529 >                break;
530 >        case WSA_NOT_ENOUGH_MEMORY:
531 >                error = "Insufficient memory available.";
532 >                break;
533 >        case WSANOTINITIALISED:
534 >                error = "Successful WSAStartup not yet performed.";
535 >                break;
536 >        case WSANO_DATA:
537 >                error = "Valid name, no data record of requested type.";
538 >                break;
539 >        case WSANO_RECOVERY:
540 >                error = "This is a non-recoverable error.";
541 >                break;
542 > //      case WSAPROVIDERFAILEDINIT:
543 > //              error = "Unable to initialize a service provider.";
544 > //              break;
545 >        case WSASYSCALLFAILURE:
546 >                error = "System call failure.";
547 >                break;
548 >        case WSASYSNOTREADY:
549 >                error = "Network subsystem is unavailable.";
550 >                break;
551 >        case WSATRY_AGAIN:
552 >                error = "Non-authoritative host not found.";
553 >                break;
554 >        case WSAVERNOTSUPPORTED:
555 >                error = "WINSOCK.DLL version out of range.";
556 >                break;
557 >        case WSAEDISCON:
558 >                error = "Graceful shutdown in progress.";
559 >                break;
560 >        case WSA_OPERATION_ABORTED:
561 >                error = "Overlapped operation aborted.";
562 >                break;
563 >        default:
564 >                error = "Unknown error.";
565 >                break;
566 >        }
567 >
568 >        cerr << prefix << ": " << error << "\n";
569 > #else
570 >        if (host)
571 >        {
572 >                herror(prefix.c_str());
573 >        }
574 >        else
575 >        {
576 >                perror(prefix.c_str());
577 >        }
578 > #endif // _WIN32
579   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines