00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "rcs_defs.hh"
00010
00011 static int inet_file_initialized = 0;
00012
00013 #if defined(WIN32) && !defined(gnuwin32)
00014 #include <windows.h>
00015 #include <winbase.h>
00016 #include <wininet.h>
00017
00018 #ifndef NO_STDIO
00019 #include <stdio.h>
00020 #endif
00021
00022 #include <string.h>
00023
00024
00025 #include "rcs_prnt.hh"
00026 #include "inetfile.hh"
00027
00028 #ifdef UNDER_CE
00029 #include "rcs_ce.h"
00030 #endif
00031
00032 HINTERNET hInternetSession = NULL;
00033
00034
00035 class INET_FILE
00036 {
00037 public:
00038 INET_FILE ();
00039 ~INET_FILE ();
00040 #ifndef NO_STDIO
00041 FILE *fp;
00042 #endif
00043 HINTERNET hURL;
00044 HANDLE hFile;
00045 BOOL url_read;
00046 DWORD bytes_read;
00047 int end_of_file;
00048 int last_buffer_read;
00049 int bytes_in_buffer;
00050 int bytes_left_in_buffer;
00051 char buffer[4096];
00052 char url[4096];
00053 char *ptr_in_buffer;
00054
00055 private:
00056 INET_FILE (INET_FILE & ifile)
00057 {
00058 };
00059 };
00060
00061 INET_FILE::~INET_FILE ()
00062 {
00063 #ifndef NO_STDIO
00064 if (NULL != fp)
00065 {
00066 fclose (fp);
00067 fp = NULL;
00068 }
00069 #endif
00070 }
00071
00072 INET_FILE::INET_FILE ()
00073 {
00074 #ifndef NO_STDIO
00075 fp = NULL;
00076 #endif
00077 hURL = NULL;
00078 hFile = INVALID_HANDLE_VALUE;
00079 bytes_in_buffer = 0;
00080 bytes_left_in_buffer = 0;
00081 end_of_file = 0;
00082 url_read = FALSE;
00083 last_buffer_read = 0;
00084 ptr_in_buffer = buffer;
00085 bytes_read = 0;
00086 memset (url, 0, 4096);
00087 memset (buffer, 0, 4096);
00088 }
00089
00090
00091
00092
00093 int RCS_EXPORT
00094 inet_file_init (const char *agent_name, char *agent_version, int debug)
00095 {
00096 if (hInternetSession == NULL)
00097 {
00098 if (NULL == agent_name)
00099 {
00100 agent_name = "InetFileAgent";
00101 }
00102
00103
00104 #if defined(UNICODE) && defined(UNDER_CE)
00105 wchar_t wagent_name[64];
00106 RCS_CE_ASCII_TO_UNICODE (wagent_name, agent_name, 64);
00107 hInternetSession = InternetOpen (wagent_name,
00108 INTERNET_OPEN_TYPE_PRECONFIG,
00109 NULL,
00110 NULL,
00111 INTERNET_FLAG_ASYNC);
00112 #else
00113 hInternetSession = InternetOpen (agent_name,
00114 0,
00115 NULL,
00116 NULL,
00117 0);
00118 #endif
00119 }
00120 if (hInternetSession == NULL)
00121 {
00122 rcs_print_error ("Can not open internet session. (Error =%d)\n",
00123 GetLastError ());
00124 return -1;
00125 }
00126 return 0;
00127 }
00128
00129 RCS_EXPORT INET_FILE *
00130 inet_file_open (const char *url, char *type)
00131 {
00132 HINTERNET hURL;
00133 INET_FILE *ifp;
00134 #ifndef NO_STDIO
00135 FILE *fp;
00136 #endif
00137
00138 if (url == NULL)
00139 {
00140 return NULL;
00141 }
00142
00143 if (strncmp (url, "http:", 5) && strncmp (url, "ftp:", 4)
00144 && strncmp (url, "https:", 6) && strncmp (url, "file:", 5))
00145 {
00146 #ifndef NO_STDIO
00147 fp = fopen (url, type);
00148 if (NULL != fp)
00149 {
00150 ifp = new INET_FILE;
00151 strncpy (ifp->url, url, 4096);
00152 ifp->fp = fp;
00153 return ifp;
00154 }
00155 return NULL;
00156 #else
00157 #if defined(UNICODE) && defined(UNDER_CE)
00158 wchar_t wurl[256];
00159 RCS_CE_ASCII_TO_UNICODE (wurl, url, 256);
00160 HANDLE hFile = CreateFile (wurl,
00161 GENERIC_READ,
00162 FILE_SHARE_READ,
00163 NULL,
00164 OPEN_EXISTING,
00165 FILE_ATTRIBUTE_NORMAL,
00166 NULL);
00167 #else
00168 HANDLE hFile = CreateFile (url,
00169 GENERIC_READ,
00170 FILE_SHARE_READ,
00171 NULL,
00172 OPEN_EXISTING,
00173 FILE_ATTRIBUTE_NORMAL,
00174 NULL);
00175 #endif
00176
00177 if (hFile != INVALID_HANDLE_VALUE)
00178 {
00179 ifp = new INET_FILE;
00180 strncpy (ifp->url, url, 4096);
00181 ifp->hFile = hFile;
00182 return ifp;
00183 }
00184
00185 return NULL;
00186 #endif
00187 }
00188
00189
00190 if (hInternetSession == NULL)
00191 {
00192 if (inet_file_init (NULL, NULL, 0) < 0)
00193 {
00194 return NULL;
00195 }
00196 }
00197 #if defined(UNICODE) && defined(UNDER_CE)
00198 wchar_t wurl[256];
00199 RCS_CE_ASCII_TO_UNICODE (wurl, url, 256);
00200 hURL = InternetOpenUrl (hInternetSession,
00201 wurl, NULL, 0, INTERNET_FLAG_RAW_DATA, 0);
00202 #else
00203 hURL = InternetOpenUrl (hInternetSession,
00204 url,
00205 NULL,
00206 0,
00207 INTERNET_FLAG_EXISTING_CONNECT |
00208 INTERNET_FLAG_RELOAD, 0);
00209 #endif
00210 if (hURL == NULL)
00211 {
00212 rcs_print_error ("Can not open URL %s. (Error =%d)\n",
00213 url, GetLastError ());
00214 return NULL;
00215 }
00216
00217 ifp = new INET_FILE;
00218 if (NULL != ifp)
00219 {
00220 strncpy (ifp->url, url, 4096);
00221 }
00222 return ifp;
00223 }
00224
00225
00226 RCS_EXPORT char *
00227 inet_file_gets (char *str, int maxlen, INET_FILE * inet_file)
00228 {
00229 int bytes_in_str = 0;
00230 char *ptr_in_str = str;
00231 int end_of_line = 0;
00232 if (NULL == inet_file || NULL == str)
00233 {
00234 return NULL;
00235 }
00236 #ifndef NO_STDIO
00237 if (NULL != inet_file->fp)
00238 {
00239 return (fgets (str, maxlen, inet_file->fp));
00240 }
00241 #endif
00242
00243
00244 memset (str, 0, maxlen);
00245 while (!end_of_line && bytes_in_str < maxlen - 1 && !inet_file->end_of_file)
00246 {
00247 if (inet_file->bytes_left_in_buffer > 0)
00248 {
00249 if (*inet_file->ptr_in_buffer == '\n')
00250 {
00251 *ptr_in_str = 0;
00252 end_of_line = 1;
00253 inet_file->ptr_in_buffer++;
00254 inet_file->bytes_left_in_buffer--;
00255 break;
00256 }
00257 else if (*inet_file->ptr_in_buffer == '\r')
00258 {
00259 inet_file->ptr_in_buffer++;
00260 inet_file->bytes_left_in_buffer--;
00261 }
00262 else if (*inet_file->ptr_in_buffer == 0)
00263 {
00264 inet_file->bytes_left_in_buffer = 0;
00265 inet_file->bytes_in_buffer = 0;
00266 inet_file->ptr_in_buffer = inet_file->buffer;
00267 }
00268 else
00269 {
00270 *ptr_in_str = *inet_file->ptr_in_buffer;
00271 ptr_in_str++;
00272 bytes_in_str++;
00273 inet_file->ptr_in_buffer++;
00274 inet_file->bytes_left_in_buffer--;
00275 }
00276 }
00277 else
00278 {
00279 if (inet_file->last_buffer_read)
00280 {
00281 inet_file->end_of_file = 1;
00282 *ptr_in_str = 0;
00283 inet_file->bytes_left_in_buffer = 0;
00284 inet_file->bytes_in_buffer = 0;
00285 inet_file->ptr_in_buffer = inet_file->buffer;
00286 }
00287 else
00288 {
00289 memset (inet_file->buffer, 0, 4096);
00290 if (NULL != inet_file->hURL)
00291 {
00292 inet_file->url_read = InternetReadFile (inet_file->hURL,
00293 inet_file->buffer,
00294 4095,
00295 &inet_file->
00296 bytes_read);
00297 if (!inet_file->url_read)
00298 {
00299 rcs_print_error
00300 ("Error from InternetReadFile of %s. (error = %d)\n",
00301 inet_file->url, GetLastError ());
00302 inet_file->end_of_file = 1;
00303 *ptr_in_str = 0;
00304 return NULL;
00305 }
00306 }
00307 else if (INVALID_HANDLE_VALUE != inet_file->hFile)
00308 {
00309 inet_file->url_read = ReadFile (inet_file->hFile,
00310 inet_file->buffer,
00311 4095,
00312 &inet_file->bytes_read,
00313 NULL);
00314 if (!inet_file->url_read)
00315 {
00316 rcs_print_error
00317 ("Error from ReadFile of %s. (error = %d)\n",
00318 inet_file->url, GetLastError ());
00319 inet_file->end_of_file = 1;
00320 *ptr_in_str = 0;
00321 return NULL;
00322 }
00323 }
00324 else
00325 {
00326 return NULL;
00327 }
00328 if (inet_file->bytes_read < 4095)
00329 {
00330 inet_file->last_buffer_read = 1;
00331 }
00332 inet_file->bytes_left_in_buffer = inet_file->bytes_read;
00333 inet_file->bytes_in_buffer = inet_file->bytes_read;
00334 inet_file->ptr_in_buffer = inet_file->buffer;
00335 }
00336 }
00337 }
00338 return str;
00339 }
00340
00341 RCS_EXPORT int
00342 inet_file_eof (INET_FILE * inet_file)
00343 {
00344 if (NULL == inet_file)
00345 {
00346 return 1;
00347 }
00348 #ifndef NO_STDIO
00349 if (NULL != inet_file->fp)
00350 {
00351 return (feof (inet_file->fp));
00352 }
00353 #endif
00354 return inet_file->end_of_file;
00355 }
00356
00357
00358 RCS_EXPORT int
00359 inet_file_close (INET_FILE * inet_file)
00360 {
00361 if (NULL != inet_file)
00362 {
00363 #ifndef NO_STDIO
00364 if (NULL != inet_file->fp)
00365 {
00366 fclose (inet_file->fp);
00367 inet_file->fp = NULL;
00368 }
00369 #endif
00370 if (NULL != inet_file->hURL)
00371 {
00372 InternetCloseHandle (inet_file->hURL);
00373 inet_file->hURL = NULL;
00374 }
00375 if (INVALID_HANDLE_VALUE != inet_file->hFile)
00376 {
00377 CloseHandle (inet_file->hFile);
00378 inet_file->hFile = INVALID_HANDLE_VALUE;
00379 }
00380 inet_file->end_of_file = 1;
00381 inet_file->ptr_in_buffer = inet_file->buffer;
00382 inet_file->bytes_left_in_buffer = 0;
00383 #if 0
00384
00385
00386 delete inet_file;
00387 #endif
00388 }
00389 return 0;
00390 }
00391
00392
00393
00394 RCS_EXPORT int
00395 inet_file_exit ()
00396 {
00397 if (NULL != hInternetSession)
00398 {
00399 InternetCloseHandle (hInternetSession);
00400 hInternetSession = NULL;
00401 }
00402 return 0;
00403 }
00404
00405
00406 RCS_EXPORT int
00407 inet_file_rewind (INET_FILE * ifp)
00408 {
00409 if (NULL == ifp)
00410 {
00411 return -1;
00412 }
00413 #ifndef NO_STDIO
00414 if (ifp->fp != NULL)
00415 {
00416 rewind (ifp->fp);
00417 return 0;
00418 }
00419 #endif
00420
00421 if (INVALID_HANDLE_VALUE != ifp->hFile)
00422 {
00423 CloseHandle (ifp->hFile);
00424 #if defined(UNICODE) && defined(UNDER_CE)
00425 wchar_t wurl[256];
00426 RCS_CE_ASCII_TO_UNICODE (wurl, ifp->url, 256);
00427 ifp->hFile = CreateFile (wurl, GENERIC_READ,
00428 FILE_SHARE_READ,
00429 NULL,
00430 OPEN_EXISTING,
00431 FILE_ATTRIBUTE_NORMAL,
00432 NULL);
00433 #else
00434 ifp->hFile = CreateFile (ifp->url, GENERIC_READ,
00435 FILE_SHARE_READ,
00436 NULL,
00437 OPEN_EXISTING,
00438 FILE_ATTRIBUTE_NORMAL,
00439 NULL);
00440 #endif
00441 ifp->bytes_in_buffer = 0;
00442 ifp->bytes_left_in_buffer = 0;
00443 ifp->end_of_file = 0;
00444 ifp->url_read = FALSE;
00445 ifp->last_buffer_read = 0;
00446 ifp->ptr_in_buffer = ifp->buffer;
00447 ifp->bytes_read = 0;
00448 memset (ifp->buffer, 0, 4096);
00449 return 0;
00450 }
00451 else if (NULL != ifp->hURL)
00452 {
00453 InternetCloseHandle (ifp->hURL);
00454 #if defined(UNICODE) && defined(UNDER_CE)
00455 wchar_t wurl[256];
00456 RCS_CE_ASCII_TO_UNICODE (wurl, ifp->url, 256);
00457 ifp->hURL = InternetOpenUrl (hInternetSession,
00458 wurl,
00459 NULL,
00460 0,
00461 INTERNET_FLAG_EXISTING_CONNECT |
00462 INTERNET_FLAG_RELOAD, 0);
00463 #else
00464 ifp->hURL = InternetOpenUrl (hInternetSession,
00465 ifp->url,
00466 NULL,
00467 0,
00468 INTERNET_FLAG_EXISTING_CONNECT |
00469 INTERNET_FLAG_RELOAD, 0);
00470 #endif
00471 if (ifp->hURL == NULL)
00472 {
00473 rcs_print_error ("Can not open URL %s. (Error =%d)\n",
00474 ifp->url, GetLastError ());
00475 return -1;
00476 }
00477 }
00478 ifp->bytes_in_buffer = 0;
00479 ifp->bytes_left_in_buffer = 0;
00480 ifp->end_of_file = 0;
00481 ifp->url_read = FALSE;
00482 ifp->last_buffer_read = 0;
00483 ifp->ptr_in_buffer = ifp->buffer;
00484 ifp->bytes_read = 0;
00485 memset (ifp->buffer, 0, 4096);
00486 return 0;
00487 }
00488
00489
00490 #else
00491
00492
00493
00494 #include "inetfile.hh"
00495
00496 #ifndef NO_STDIO
00497 #include <stdio.h>
00498 #endif
00499 #include <string.h>
00500 #include <stdlib.h>
00501
00502 #ifdef HAVE_LIBDL
00503 #ifndef DYNAMICALLY_LOAD_LIBWWW
00504 #define DYNAMICALLY_LOAD_LIBWWW
00505 #endif
00506 #endif
00507
00508 #ifndef DYNAMICALLY_LOAD_LIBWWW
00509 #include "WWWLib.h"
00510 #include "WWWHTTP.h"
00511 #include "WWWInit.h"
00512 #define DL(x) x
00513 #else
00514 extern "C"
00515 {
00516 #define CORE_TRACE (WWWTRACE & SHOW_CORE_TRACE)
00517 #define SHOW_CORE_TRACE 0x2000
00518 #define SHOW_STREAM_TRACE 0x40
00519 #define SHOW_PROTOCOL_TRACE 0x80
00520 #define PARSE_ALL 31
00521 #define BOOL char
00522 #define HT_FREE(pointer) {ptr_to_HTMemory_free((pointer));((pointer))=NULL;}
00523 #define WWW_SOURCE ptr_to_HTAtom_for("*
00524
00525
00526
00527
00528
00529
00530 #define WWWTRACE (*WWW_TraceFlag)
00531 #else
00532 static int WWW_TraceFlag = 0;
00533 #define WWWTRACE (WWW_TraceFlag)
00534 #endif
00535 #define YES (BOOL)1
00536
00537
00538 typedef struct _HTAtom HTAtom;
00539 struct _HTAtom
00540 {
00541 HTAtom *next;
00542 };
00543 typedef void *HTAnchor;
00544 typedef void *HTStream;
00545 typedef void *HTChunk;
00546 typedef void *HTFormat;
00547 #define DL(x) ptr_to_##x
00548
00549 static void (*ptr_to_HTMemory_free) (void *) = NULL;
00550 static HTRequest *(*ptr_to_HTRequest_new) (void) = NULL;
00551 static BOOL (*ptr_to_HTLibInit) (const char *AppName,
00552 const char *AppVersion) = NULL;
00553 static HTList *(*ptr_to_HTList_new) (void) = NULL;
00554 static void (*ptr_to_HTTransportInit) (void) = NULL;
00555 static void (*ptr_to_HTProtocolInit) (void) = NULL;
00556 static void (*ptr_to_HTNetInit) (void) = NULL;
00557 static void (*ptr_to_HTConverterInit) (HTList * conversions) = NULL;
00558 static void (*ptr_to_HTFormat_setConversion) (HTList * list) = NULL;
00559 static void (*ptr_to_HTEncoderInit) (HTList * encodings) = NULL;
00560 static void (*ptr_to_HTFormat_setTransferCoding) (HTList * encodings) =
00561 NULL;
00562 static void (*ptr_to_HTMIMEInit) (void) = NULL;
00563 static HTAtom *(*ptr_to_HTAtom_for) (const char *string) = NULL;
00564 static void (*ptr_to_HTRequest_setOutputStream) (HTRequest * request,
00565 HTStream * output);
00566 static void (*ptr_to_HTRequest_setPreemptive) (HTRequest * request,
00567 BOOL mode) = NULL;
00568 static char *(*ptr_to_HTGetCurrentDirectoryURL) (void) = NULL;
00569 static void (*ptr_to_HTRequest_setOutputFormat) (HTRequest * request,
00570 HTFormat format) = NULL;
00571 static char *(*ptr_to_HTParse) (const char *aName, const char *relatedName,
00572 int wanted) = NULL;
00573 static HTAnchor *(*ptr_to_HTAnchor_findAddress) (const char *address) =
00574 NULL;
00575 static HTChunk *(*ptr_to_HTLoadAnchorToChunk) (HTAnchor * anchor,
00576 HTRequest * request) = NULL;
00577 static char *(*ptr_to_HTChunk_toCString) (HTChunk * ch) = NULL;
00578 static void (*ptr_to_HTRequest_delete) (HTRequest * request) = NULL;
00579 static void (*ptr_to_HTFormat_deleteAll) (void) = NULL;
00580 static BOOL (*ptr_to_HTLibTerminate) (void) = NULL;
00581
00582 typedef void (*HTMemory_free_func_type) (void *);
00583 typedef HTRequest *(*HTRequest_new_func_type) (void);
00584 typedef BOOL (*HTLibInit_func_type) (const char *AppName,
00585 const char *AppVersion);
00586 typedef HTList *(*HTList_new_func_type) (void);
00587 typedef void (*HTTransportInit_func_type) (void);
00588 typedef void (*HTProtocolInit_func_type) (void);
00589 typedef void (*HTNetInit_func_type) (void);
00590 typedef void (*HTConverterInit_func_type) (HTList * conversions);
00591 typedef void (*HTFormat_setConversion_func_type) (HTList * list);
00592 typedef void (*HTEncoderInit_func_type) (HTList * encodings);
00593 typedef void (*HTFormat_setTransferCoding_func_type) (HTList * encodings);
00594 typedef void (*HTMIMEInit_func_type) (void);
00595 typedef HTAtom *(*HTAtom_for_func_type) (const char *string);
00596 typedef void (*HTRequest_setOutputStream_func_type) (HTRequest * request,
00597 HTStream * output);
00598 typedef void (*HTRequest_setPreemptive_func_type) (HTRequest * request,
00599 BOOL mode);
00600 typedef char *(*HTGetCurrentDirectoryURL_func_type) (void);
00601 typedef void (*HTRequest_setOutputFormat_func_type) (HTRequest * request,
00602 HTFormat format);
00603 typedef char *(*HTParse_func_type) (const char *aName,
00604 const char *relatedName, int wanted);
00605 typedef HTAnchor *(*HTAnchor_findAddress_func_type) (const char *address);
00606 typedef HTChunk *(*HTLoadAnchorToChunk_func_type) (HTAnchor * anchor,
00607 HTRequest * request);
00608 typedef char *(*HTChunk_toCString_func_type) (HTChunk * ch);
00609 typedef void (*HTRequest_delete_func_type) (HTRequest * request);
00610 typedef void (*HTFormat_deleteAll_func_type) (void);
00611 typedef BOOL (*HTLibTerminate_func_type) (void);
00612
00613 static int load_libwww_status = 0;
00614
00615 #include <dlfcn.h>
00616
00617 int load_libwww_functions (void *handle)
00618 {
00619 ptr_to_HTMemory_free =
00620 (HTMemory_free_func_type) dlsym (handle, "HTMemory_free");
00621 if (NULL == ptr_to_HTMemory_free)
00622 {
00623 return -1;
00624 }
00625 ptr_to_HTRequest_new =
00626 (HTRequest_new_func_type) dlsym (handle, "HTRequest_new");
00627 if (NULL == ptr_to_HTRequest_new)
00628 {
00629 return -1;
00630 }
00631 ptr_to_HTLibInit = (HTLibInit_func_type) dlsym (handle, "HTLibInit");
00632 if (NULL == ptr_to_HTLibInit)
00633 {
00634 return -1;
00635 }
00636 ptr_to_HTList_new = (HTList_new_func_type) dlsym (handle, "HTList_new");
00637 if (NULL == ptr_to_HTList_new)
00638 {
00639 return -1;
00640 }
00641
00642 ptr_to_HTTransportInit =
00643 (HTTransportInit_func_type) dlsym (handle, "HTTransportInit");
00644 if (NULL == ptr_to_HTTransportInit)
00645 {
00646 return -1;
00647 }
00648
00649 ptr_to_HTProtocolInit =
00650 (HTProtocolInit_func_type) dlsym (handle, "HTProtocolInit");
00651 if (NULL == ptr_to_HTProtocolInit)
00652 {
00653 return -1;
00654 }
00655
00656
00657 ptr_to_HTNetInit = (HTNetInit_func_type) dlsym (handle, "HTNetInit");
00658 if (NULL == ptr_to_HTNetInit)
00659 {
00660 return -1;
00661 }
00662
00663 ptr_to_HTConverterInit =
00664 (HTConverterInit_func_type) dlsym (handle, "HTConverterInit");
00665 if (NULL == ptr_to_HTConverterInit)
00666 {
00667 return -1;
00668 }
00669
00670
00671 ptr_to_HTFormat_setConversion =
00672 (HTFormat_setConversion_func_type) dlsym (handle,
00673 "HTFormat_setConversion");
00674 if (NULL == ptr_to_HTFormat_setConversion)
00675 {
00676 return -1;
00677 }
00678
00679
00680 ptr_to_HTEncoderInit =
00681 (HTEncoderInit_func_type) dlsym (handle, "HTEncoderInit");
00682 if (NULL == ptr_to_HTEncoderInit)
00683 {
00684 return -1;
00685 }
00686
00687 ptr_to_HTFormat_setTransferCoding =
00688 (HTFormat_setTransferCoding_func_type) dlsym (handle,
00689 "HTFormat_setTransferCoding");
00690 if (NULL == ptr_to_HTFormat_setTransferCoding)
00691 {
00692 return -1;
00693 }
00694
00695 ptr_to_HTMIMEInit = (HTMIMEInit_func_type) dlsym (handle, "HTMIMEInit");
00696 if (NULL == ptr_to_HTMIMEInit)
00697 {
00698 return -1;
00699 }
00700
00701 ptr_to_HTAtom_for = (HTAtom_for_func_type) dlsym (handle, "HTAtom_for");
00702 if (NULL == ptr_to_HTAtom_for)
00703 {
00704 return -1;
00705 }
00706
00707 ptr_to_HTRequest_setOutputStream =
00708 (HTRequest_setOutputStream_func_type) dlsym (handle,
00709 "HTRequest_setOutputStream");
00710 if (NULL == ptr_to_HTRequest_setOutputStream)
00711 {
00712 return -1;
00713 }
00714
00715 ptr_to_HTRequest_setPreemptive =
00716 (HTRequest_setPreemptive_func_type) dlsym (handle,
00717 "HTRequest_setPreemptive");
00718 if (NULL == ptr_to_HTRequest_setPreemptive)
00719 {
00720 return -1;
00721 }
00722
00723 ptr_to_HTGetCurrentDirectoryURL =
00724 (HTGetCurrentDirectoryURL_func_type) dlsym (handle,
00725 "HTGetCurrentDirectoryURL");
00726 if (NULL == ptr_to_HTGetCurrentDirectoryURL)
00727 {
00728 return -1;
00729 }
00730
00731 ptr_to_HTRequest_setOutputFormat =
00732 (HTRequest_setOutputFormat_func_type) dlsym (handle,
00733 "HTRequest_setOutputFormat");
00734 if (NULL == ptr_to_HTRequest_setOutputFormat)
00735 {
00736 return -1;
00737 }
00738
00739
00740 ptr_to_HTParse = (HTParse_func_type) dlsym (handle, "HTParse");
00741 if (NULL == ptr_to_HTParse)
00742 {
00743 return -1;
00744 }
00745
00746
00747 ptr_to_HTAnchor_findAddress =
00748 (HTAnchor_findAddress_func_type) dlsym (handle, "HTAnchor_findAddress");
00749 if (NULL == ptr_to_HTAnchor_findAddress)
00750 {
00751 return -1;
00752 }
00753
00754
00755 ptr_to_HTLoadAnchorToChunk =
00756 (HTLoadAnchorToChunk_func_type) dlsym (handle, "HTLoadAnchorToChunk");
00757 if (NULL == ptr_to_HTLoadAnchorToChunk)
00758 {
00759 return -1;
00760 }
00761
00762
00763 ptr_to_HTChunk_toCString =
00764 (HTChunk_toCString_func_type) dlsym (handle, "HTChunk_toCString");
00765 if (NULL == ptr_to_HTChunk_toCString)
00766 {
00767 return -1;
00768 }
00769
00770
00771 ptr_to_HTRequest_delete =
00772 (HTRequest_delete_func_type) dlsym (handle, "HTRequest_delete");
00773 if (NULL == ptr_to_HTRequest_delete)
00774 {
00775 return -1;
00776 }
00777
00778
00779 ptr_to_HTFormat_deleteAll =
00780 (HTFormat_deleteAll_func_type) dlsym (handle, "HTFormat_deleteAll");
00781 if (NULL == ptr_to_HTFormat_deleteAll)
00782 {
00783 return -1;
00784 }
00785
00786
00787 ptr_to_HTLibTerminate =
00788 (HTLibTerminate_func_type) dlsym (handle, "HTLibTerminate");
00789 if (NULL == ptr_to_HTLibTerminate)
00790 {
00791 return -1;
00792 }
00793 return 0;
00794 }
00795
00796 static void *libwww_handle = NULL;
00797 int show_load_libwww_errors = 1;
00798
00799 int load_libwww ()
00800 {
00801 char libwww_name[256];
00802 char *ldpath = getenv ("LD_LIBRARY_PATH");
00803 libwww_handle = dlopen (NULL, RTLD_LAZY | RTLD_GLOBAL);
00804 memset (libwww_name, 0, 256);
00805 if (NULL != libwww_handle)
00806 {
00807 if (load_libwww_functions (libwww_handle) == 0)
00808 {
00809 load_libwww_status = 1;
00810 return 0;
00811 }
00812 }
00813 else
00814 {
00815 fprintf (stderr, "dlopen failed. %s\n", dlerror ());
00816 }
00817 libwww_handle = dlopen ("libwww.so", RTLD_LAZY | RTLD_GLOBAL);
00818 if (NULL != libwww_handle)
00819 {
00820 if (load_libwww_functions (libwww_handle) == 0)
00821 {
00822 load_libwww_status = 1;
00823 return 0;
00824 }
00825 }
00826 else
00827 {
00828 fprintf (stderr, "dlopen(%s) failed. %s\n", libwww_name, dlerror ());
00829 }
00830
00831 if (NULL != ldpath)
00832 {
00833 char *dir = strtok (ldpath, ";:,");
00834 while (NULL != dir)
00835 {
00836 if (*dir == 0)
00837 {
00838 break;
00839 }
00840 strncpy (libwww_name, dir, 256);
00841 strncat (libwww_name, "/libwww.so", 256 - strlen (dir));
00842 libwww_handle = dlopen (libwww_name, RTLD_LAZY | RTLD_GLOBAL);
00843 if (NULL != libwww_handle)
00844 {
00845 if (load_libwww_functions (libwww_handle) == 0)
00846 {
00847 load_libwww_status = 1;
00848 return 0;
00849 }
00850 }
00851 else
00852 {
00853 fprintf (stderr, "dlopen(%s) failed. %s\n", libwww_name,
00854 dlerror ());
00855 }
00856 dir = strtok (NULL, ";:,");
00857 }
00858 }
00859
00860 strncpy (libwww_name, "/isd/proj/rcslib/plat/" PLATNAME "/lib", 256);
00861 strcat (libwww_name, "/libwww.so");
00862 libwww_handle = dlopen (libwww_name, RTLD_LAZY | RTLD_GLOBAL);
00863 if (NULL != libwww_handle)
00864 {
00865 if (load_libwww_functions (libwww_handle) == 0)
00866 {
00867 load_libwww_status = 1;
00868 return 0;
00869 }
00870 }
00871 else
00872 {
00873 fprintf (stderr, "dlopen(%s) failed. %s\n", libwww_name, dlerror ());
00874 }
00875 strncpy (libwww_name, "/usr/local/rcslib/plat/" PLATNAME "/lib", 256);
00876 strcat (libwww_name, "/libwww.so");
00877 libwww_handle = dlopen (libwww_name, RTLD_LAZY | RTLD_GLOBAL);
00878 if (NULL != libwww_handle)
00879 {
00880 if (load_libwww_functions (libwww_handle) == 0)
00881 {
00882 load_libwww_status = 1;
00883 return 0;
00884 }
00885 }
00886 else
00887 {
00888 fprintf (stderr, "dlopen(%s) failed. %s\n", libwww_name, dlerror ());
00889 }
00890 strncpy (libwww_name, "/usr/local/nist/rcslib/plat/" PLATNAME "/lib",
00891 256);
00892 strcat (libwww_name, "/libwww.so");
00893 libwww_handle = dlopen (libwww_name, RTLD_LAZY | RTLD_GLOBAL);
00894 if (NULL != libwww_handle)
00895 {
00896 if (load_libwww_functions (libwww_handle) == 0)
00897 {
00898 load_libwww_status = 1;
00899 return 0;
00900 }
00901 }
00902 else
00903 {
00904 fprintf (stderr, "dlopen(%s) failed. %s\n", libwww_name, dlerror ());
00905 }
00906 return (load_libwww_status = -1);
00907 }
00908
00909
00910 }
00911
00912
00913
00914 #endif
00915
00916
00917 static HTRequest *request = NULL;
00918 static HTList *converters = NULL;
00919 static HTList *encodings = NULL;
00920
00921 class INET_FILE
00922 {
00923 public:
00924 INET_FILE ();
00925 ~INET_FILE ();
00926 char *buffer;
00927 char *current_line;
00928 FILE *local_fp;
00929
00930 private:
00931 INET_FILE (INET_FILE & ifile); // Don't copy me.
00932 };
00933
00934
00935 INET_FILE::INET_FILE ()
00936 {
00937 buffer = NULL;
00938 current_line = NULL;
00939 local_fp = NULL;
00940 }
00941
00942 INET_FILE::~INET_FILE ()
00943 {
00944 if (NULL != local_fp)
00945 {
00946 fclose (local_fp);
00947 local_fp = NULL;
00948 }
00949 else if (NULL != buffer)
00950 {
00951 HT_FREE (buffer);
00952 buffer = NULL;
00953 }
00954 current_line = NULL;
00955 }
00956
00957
00958 RCS_EXPORT int
00959 inet_file_init (const char *agent_name, char *agent_version, int debug)
00960 {
00961 #ifdef DYNAMICALLY_LOAD_LIBWWW
00962 switch (load_libwww_status)
00963 {
00964 case 0:
00965 if (load_libwww () == -1)
00966 {
00967 fprintf (stderr, "Can't load www library.\n");
00968 return (-1);
00969 }
00970 break;
00971 case 1:
00972 break;
00973
00974 case -1:
00975 default:
00976 return (-1);
00977 }
00978 #endif
00979
00980 if (NULL == request)
00981 {
00982 request = DL (HTRequest_new ());
00983 }
00984 if (NULL == converters)
00985 {
00986 converters = DL (HTList_new ());
00987 }
00988 if (NULL == encodings)
00989 {
00990 encodings = DL (HTList_new ());
00991 }
00992
00993 if (NULL == agent_name)
00994 {
00995 agent_name = "InetFileAgent";
00996 }
00997 if (NULL == agent_version)
00998 {
00999 agent_version = "1.0";
01000 }
01001
01002 DL (HTLibInit (agent_name, agent_version));
01003
01004
01005 if (debug)
01006 {
01007 WWWTRACE = SHOW_CORE_TRACE + SHOW_STREAM_TRACE + SHOW_PROTOCOL_TRACE;
01008 }
01009
01010
01011 DL (HTTransportInit ());
01012
01013
01014 DL (HTProtocolInit ());
01015
01016
01017 DL (HTNetInit ());
01018
01019
01020 DL (HTConverterInit (converters));
01021 DL (HTFormat_setConversion (converters));
01022
01023
01024 DL (HTEncoderInit (encodings));
01025 DL (HTFormat_setTransferCoding (encodings));
01026
01027
01028 DL (HTMIMEInit ());
01029
01030
01031 DL (HTRequest_setOutputFormat (request, WWW_SOURCE));
01032 DL (HTRequest_setPreemptive (request, YES));
01033
01034 inet_file_initialized = 1;
01035
01036
01037 return 0;
01038 }
01039
01040
01041 RCS_EXPORT INET_FILE *
01042 inet_file_open (const char *url, char *type)
01043 {
01044 HTChunk *chunk = NULL;
01045 char *string = NULL;
01046 INET_FILE *inet_file = NULL;
01047
01048 if (strncmp (url, "http:", 5) && strncmp (url, "ftp:", 4)
01049 && strncmp (url, "https:", 6) && strncmp (url, "file:", 5))
01050 {
01051 inet_file = new INET_FILE ();
01052 inet_file->local_fp = fopen (url, type);
01053 if (NULL == inet_file->local_fp)
01054 {
01055 delete inet_file;
01056 return NULL;
01057 }
01058 return inet_file;
01059 }
01060
01061 if (!inet_file_initialized)
01062 {
01063 inet_file_init (NULL, NULL, 0);
01064 }
01065
01066 if (NULL == url || NULL == request || NULL == converters ||
01067 NULL == encodings)
01068 {
01069 if (NULL != inet_file)
01070 {
01071 delete inet_file;
01072 }
01073 return NULL;
01074 }
01075
01076 char *cwd = DL (HTGetCurrentDirectoryURL ());
01077 char *absolute_url = DL (HTParse (url, cwd, PARSE_ALL));
01078 if (NULL == absolute_url)
01079 {
01080 if (NULL != inet_file)
01081 {
01082 delete inet_file;
01083 }
01084 return NULL;
01085 }
01086 HTAnchor *anchor = DL (HTAnchor_findAddress (absolute_url));
01087 chunk = DL (HTLoadAnchorToChunk (anchor, request));
01088 HT_FREE (absolute_url);
01089 HT_FREE (cwd);
01090
01091
01092 if (chunk == NULL)
01093 {
01094 if (NULL != inet_file)
01095 {
01096 delete inet_file;
01097 }
01098 return NULL;
01099 }
01100 string = DL (HTChunk_toCString (chunk));
01101 if (string == NULL)
01102 {
01103 if (NULL != inet_file)
01104 {
01105 delete inet_file;
01106 }
01107 return NULL;
01108 }
01109 if (*string == 0)
01110 {
01111 if (NULL != inet_file)
01112 {
01113 delete inet_file;
01114 }
01115 return NULL;
01116 }
01117 inet_file = new INET_FILE;
01118 if (NULL == inet_file)
01119 {
01120 return NULL;
01121 }
01122 inet_file->buffer = string;
01123 inet_file->current_line = inet_file->buffer;
01124 inet_file->local_fp = NULL;
01125
01126 return inet_file;
01127 }
01128
01129
01130 RCS_EXPORT char *
01131 inet_file_gets (char *str, int maxlen, INET_FILE * inet_file)
01132 {
01133 unsigned long bytes_to_copy;
01134 char *next_line;
01135 if (NULL == str || NULL == inet_file)
01136 {
01137 return NULL;
01138 }
01139 if (NULL != inet_file->local_fp)
01140 {
01141 return fgets (str, maxlen, inet_file->local_fp);
01142 }
01143 bytes_to_copy = 0;
01144 next_line = strchr (inet_file->current_line, '\n');
01145 if (next_line != NULL)
01146 {
01147 bytes_to_copy =
01148 ((unsigned long) next_line) -
01149 ((unsigned long) inet_file->current_line);
01150 }
01151 else
01152 {
01153 bytes_to_copy = strlen (inet_file->current_line);
01154 }
01155 bytes_to_copy =
01156 ((int) bytes_to_copy) > ((int) maxlen - 1) ? maxlen - 1 : bytes_to_copy;
01157 strncpy (str, inet_file->current_line, bytes_to_copy);
01158 str[bytes_to_copy] = 0;
01159 if (next_line != NULL)
01160 {
01161 inet_file->current_line = next_line + 1;
01162 }
01163 else
01164 {
01165 inet_file->current_line = NULL;
01166 }
01167 return str;
01168 }
01169
01170 RCS_EXPORT int
01171 inet_file_close (INET_FILE * inet_file)
01172 {
01173 if (NULL != inet_file)
01174 {
01175 if (NULL != inet_file->local_fp)
01176 {
01177 fclose (inet_file->local_fp);
01178 inet_file->local_fp = NULL;
01179 }
01180 else if (NULL != inet_file->buffer)
01181 {
01182 HT_FREE (inet_file->buffer);
01183 inet_file->buffer = NULL;
01184 }
01185 delete inet_file;
01186 }
01187 return 0;
01188 }
01189
01190 RCS_EXPORT int
01191 inet_file_eof (INET_FILE * inet_file)
01192 {
01193 if (NULL == inet_file)
01194 {
01195 return 1;
01196 }
01197 if (NULL != inet_file->local_fp)
01198 {
01199 return feof (inet_file->local_fp);
01200 }
01201 if (NULL == inet_file->current_line || NULL == inet_file->buffer)
01202 {
01203 return 1;
01204 }
01205 return !(*inet_file->current_line);
01206 }
01207
01208
01209 RCS_EXPORT int
01210 inet_file_exit ()
01211 {
01212 #ifdef DYNAMICALLY_LOAD_LIBWWW
01213 if (load_libwww_status != 1)
01214 {
01215 return -1;
01216 }
01217 #endif
01218
01219
01220 DL (HTRequest_delete (request));
01221 DL (HTFormat_deleteAll ());
01222
01223
01224 DL (HTLibTerminate ());
01225
01226 request = NULL;
01227 converters = NULL;
01228 encodings = NULL;
01229 inet_file_initialized = 0;
01230
01231 #ifdef DYNAMICALLY_LOAD_LIBWWW
01232 if (NULL != libwww_handle)
01233 {
01234 dlclose (libwww_handle);
01235 libwww_handle = NULL;
01236 }
01237 #endif
01238
01239 return 0;
01240 }
01241
01242 RCS_EXPORT int
01243 inet_file_rewind (INET_FILE * ifp)
01244 {
01245 if (NULL != ifp->local_fp)
01246 {
01247 rewind ((FILE *) ifp->local_fp);
01248 return 0;
01249 }
01250 else
01251 {
01252 ifp->current_line = ifp->buffer;
01253 }
01254 return 0;
01255 }
01256
01257 #endif