fetch_file.c

00001 /*****************************************************************************
00002  *                        fetch_file.c
00003  * This file contains functions which are used to download remote files. If 
00004  * the input is a url, then the functions defined below are used to parse the
00005  * url and download the remote file.
00006  *
00007  ****************************************************************************/
00008 
00009 #include "xsb_config.h"
00010 #include "socketcall.h"
00011 #include <stdio.h>
00012 #include <errno.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 
00016 
00017 #define SA      struct sockaddr
00018 
00019 #define MAXSTRLEN 256
00020 
00021 int parse_url( const char * url, char * server, char *fname);
00022 int get_file_www(char *server, char * fname, char **buf);
00023 
00024 
00031 int parse_url( const char * url, char * server, char *fname)
00032 {
00033   int i,j;
00034   int len = strlen(url);
00035   char temp[MAXSTRLEN];
00036   
00037   int flag = 0, flag_file = 0;
00038   
00039   for(i = 0; i<MAXSTRLEN; i++)
00040     {
00041       *(server+i) = 0;
00042       *(fname+i) = 0;
00043       *(temp+i) = 0;
00044     }
00045   
00046   
00047   for( i=0;i<len;i++)
00048     {
00049       *(temp+i)=url[i];
00050       
00051       if( url[i] == ':')
00052         {
00053           
00054           flag = 1;
00055           i++;
00056           /*If the protocol is not file:// or http:// its an error*/
00057           if(strcmp( temp , "http:") && strcmp( temp, "file:"))
00058             return FALSE;
00059           
00060           if(!strcmp( temp, "http:")){
00061             flag_file = 1;
00062           }
00063           else if(!strcmp( temp, "file:")){
00064             flag_file = 2;
00065           }
00066           
00067           if( url[i] == '/' && url[i+1] == '/')
00068             {
00069               *(temp+i) = url[i];
00070               i = i+ 1;         
00071               *(temp+i) = url[i];
00072               i = i+1;
00073               break;
00074             }
00075           else
00076             return FALSE;
00077         }
00078     }
00079   
00080   if(!flag)
00081     return FALSE;
00082   
00083   if( flag_file == 2){
00084     strcpy( server, "file");
00085     strcpy( fname, url+i);
00086     return TRUE;
00087   }
00088   
00089   /*Extract the server*/
00090   for(j=0;i<len;i++,j++)
00091     {
00092       if(url[i] == '/')
00093         break;
00094       
00095       *(server+j) = url[i];
00096     }
00097   /*Extract the filename*/
00098   for(j=0;i<len;i++,j++)
00099     {
00100       *(fname+j) = url[i];
00101         }
00102   return TRUE;
00103 }
00104 
00111 int get_file_www(char *server, char *fname, char **source)
00112 {
00113         int sockfd;
00114         struct sockaddr_in      servaddr;
00115         struct in_addr          **pptr;
00116         struct hostent          *hp;
00117         int port = 80, len, i = 0, maxlen = 0;
00118         char *tempstr = NULL;
00119         
00120         /*Special socket handling for windows*/
00121 #ifdef WIN_NT
00122         int rc;
00123         WSADATA wsadata;
00124         
00125         rc = WSAStartup(2, &wsadata);
00126         *source = (char*)malloc(MAXSTRLEN);
00127         if(rc) {
00128           sprintf( *source, "WSAStartup FAILED: err=%d\n", (int) GetLastError());
00129           return FALSE;
00130         }
00131 #endif
00132         
00133         if(*source == NULL)
00134         {
00135                 *source = (char*)malloc(MAXSTRLEN);
00136         }
00137 
00138         len = strlen( server);
00139         
00140         for( i = 0; i < len && server[i] != ':'; i++);
00141         
00142         if (server[i] == ':')
00143           {
00144             server[i] = 0;
00145                 port = atoi(server + i + 1);
00146           }
00147 
00148         /*Resolve the hostname*/
00149         if ( (hp = gethostbyname(server)) == NULL)
00150           {
00151             sprintf(*source, "hostname error for %s", server);
00152             return FALSE;
00153           }
00154         
00155         pptr = (struct in_addr **) hp->h_addr_list;
00156         
00157         /* Open the socket connection */        
00158         for ( ; *pptr != NULL; pptr++) {
00159           
00160           sockfd = socket(AF_INET, SOCK_STREAM, 0);
00161           
00162           
00163           memset(&servaddr, 0, sizeof(servaddr));
00164           servaddr.sin_family = AF_INET;
00165           servaddr.sin_port = htons((unsigned short)port); 
00166           memcpy(&servaddr.sin_addr, *pptr, sizeof(struct in_addr));
00167           if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) == 0)
00168             break;              
00169 #ifdef WIN_NT
00170           closesocket(sockfd);
00171 #else
00172           close(sockfd);
00173 #endif
00174           return FALSE;
00175         }
00176         if (*pptr == NULL)
00177           {
00178             return FALSE;
00179           }
00180         
00181         len = 0;
00182         
00183 
00184         /*Issue the http get filename command*/
00185         //      printf("Server [%s] Fname [%s]\n", server, fname);      
00186         
00187         sprintf( *source, "GET %s\n\n", fname);
00188         len = strlen( *source);
00189         (*source)[ len] = '\0';
00190         send( sockfd, *source, strlen(*source), 0);
00191                     
00192         i=0;
00193         maxlen = 0;
00194 
00195         /* Download the file */
00196         while( (len != 0)  && ( len != -1)){
00197           *source = (char*) realloc( *source, ((i+1) * MAXSTRLEN) + 1);
00198            tempstr = (*source) + maxlen;
00199           len = recv( sockfd, tempstr , MAXSTRLEN, 0);
00200           
00201           if( (len != 0) && (len != -1) ){
00202             maxlen+=len;
00203             (*source)[maxlen]='\0';
00204           }
00205           i++;
00206         }
00207         
00208         //        printf("Downloaded [%s]\n", *source);
00209         /*Handle http errors*/
00210 
00211         if( (strstr( *source, "Error 400")!= NULL)
00212             || (strstr( *source, "400 Bad Request")!=NULL)){
00213           strcpy( *source, "400 Bad Request");
00214           return FALSE;
00215         }
00216         
00217         if( (strstr( *source, "Error 401")!= NULL)
00218             || (strstr( *source, "401 Unauthorized")!=NULL)){
00219           strcpy( *source, "401 Unauthorized");
00220           return FALSE;
00221         }
00222         
00223         if( (strstr( *source, "Error 402")!= NULL)
00224         || (strstr( *source, "402 Payment Required")!=NULL)){
00225           strcpy( *source, "402 Payment Required");
00226           return FALSE;
00227         }
00228         
00229         if( (strstr( *source, "Error 403")!= NULL)
00230             || strstr( *source, "403 Forbidden")!=NULL){
00231           strcpy( *source, "403 Forbidden");
00232           return FALSE;
00233         }
00234         
00235         if( (strstr( *source, "Error 404")!= NULL) 
00236             || (strstr( *source, "404 Not Found")!=NULL)){
00237             strcpy( *source, "Error 404 File not found");
00238             return FALSE;
00239           }
00240         
00241         if( (strstr( *source, "Error 405")!= NULL)
00242             || (strstr( *source, "405 Method Not Allowed")!=NULL)){
00243           strcpy( *source, "405 Method Not Allowed");
00244           return FALSE;
00245         }
00246         
00247         if( (strstr( *source, "Error 406")!= NULL)
00248             || (strstr( *source, "406 Not Acceptable")!=NULL)){
00249           strcpy( *source, "406 Not Acceptable");
00250           return FALSE;
00251         }
00252 
00253         if( (strstr( *source, "Error 407")!= NULL)
00254             || (strstr( *source, "407  Proxy Authentication Required")!=NULL)  ){
00255           strcpy( *source, "407 Proxy Authentication Required");
00256           return FALSE;
00257         }
00258         
00259         
00260         if( (strstr( *source, "Error 408")!= NULL)
00261         || (strstr( *source, "408 Request Timeout")!=NULL)){
00262           strcpy( *source, "408 Request Timeout");
00263           return FALSE;
00264         }
00265 
00266         if( (strstr( *source, "Error 409")!= NULL)
00267         || (strstr( *source, "409 Conflict")!=NULL)){
00268           strcpy( *source, "409 Conflict");
00269           return FALSE;
00270         }
00271 
00272 
00273         if( (strstr( *source, "Error 410")!= NULL)
00274          || (strstr( *source, "410 Gone")!=NULL)){
00275           strcpy( *source, "410 Gone");
00276           return FALSE;
00277         }
00278 
00279         if( (strstr( *source, "Error 411")!= NULL)
00280           || (strstr( *source, "411 Length Required")!=NULL)){
00281           strcpy( *source, "411 Length Required"); ;
00282           return FALSE;
00283         }
00284         if( (strstr( *source, "Error 412")!= NULL)
00285         || (strstr( *source, "412 Precondition failed")!=NULL)){
00286           strcpy( *source, "412 Precondition Failed");
00287           return FALSE;
00288         }
00289 
00290         if( (strstr( *source, "Error 413")!= NULL)
00291         || (strstr( *source, "413 Request Entity Too Large")!=NULL)){
00292           strcpy( *source, "413 Request Entity Too Large");
00293           return FALSE;
00294         }
00295 
00296         if( (strstr( *source, "Error 414")!= NULL)
00297         || (strstr( *source, "414 Request-URI Too Long")!=NULL)){
00298           strcpy( *source, "414 Request-URI Too Long") ;
00299           return FALSE;
00300         }
00301 
00302         if( (strstr( *source, "Error 415")!= NULL)
00303         || (strstr( *source, "Error 415")!=NULL)){
00304           strcpy( *source, "415 Unsupported Media Type");
00305           return FALSE;
00306         }
00307         
00308         if( (strstr( *source, "Error 416")!= NULL)
00309         || (strstr( *source, "416 Requested Range Not Satisfiable")!=NULL)){
00310           strcpy( *source, "416 Requested Range Not Satisfiable");
00311           return FALSE;
00312         }
00313 
00314         if( (strstr( *source, "Error 417")!= NULL)
00315         || (strstr( *source, "417 Expectation Failed")!=NULL)){
00316           strcpy( *source,  "417 Expectation Failed");
00317           return FALSE;
00318         }
00319 
00320         if( (strstr( *source, "Error 500")!= NULL)
00321         || (strstr( *source, "500 Internal Server Error")!=NULL)){
00322           strcpy( *source, "500 Internal Server Error");
00323           return FALSE;
00324         }
00325          
00326         if( (strstr( *source, "Error 501")!= NULL)
00327         || (strstr( *source, "501 Not Implemented")!=NULL)){
00328           strcpy( *source, "501 Not Implemented");
00329           return FALSE;
00330         }
00331         if( (strstr( *source, "Error 502")!= NULL)
00332         || (strstr( *source, "502 Bad Gateway")!=NULL)){
00333           strcpy( *source, "502 Bad Gateway");
00334           return FALSE;
00335         }
00336 
00337         if( (strstr( *source, "Error 503")!= NULL)
00338         || (strstr( *source, "503 Service Unavailable")!=NULL)){
00339           strstr( *source, "503 Service Unavailable") ;
00340           return FALSE;
00341         }
00342 
00343         if( (strstr( *source, "Error 504")!= NULL)
00344             || (strstr( *source, "504 Gateway Timeout")!=NULL))
00345           {
00346           strcpy( *source, "504 Gateway Timeout");
00347           return FALSE;
00348         }
00349 
00350         if( (strstr( *source, "Error 505")!= NULL)
00351         || (strstr( *source, "505 HTTP Version Not Supported")!=NULL)){
00352           strcpy( *source, "505 HTTP Version Not Supported");
00353           return FALSE;
00354         }
00355 
00356 #ifdef WIN_NT
00357         closesocket(sockfd);
00358 #else
00359         close(sockfd);
00360 #endif
00361 
00362         return TRUE;
00363 }

Generated on Wed Jul 26 13:30:45 2006 for XSB by  doxygen 1.4.5