00001
00002
00003
00004
00005
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
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
00090 for(j=0;i<len;i++,j++)
00091 {
00092 if(url[i] == '/')
00093 break;
00094
00095 *(server+j) = url[i];
00096 }
00097
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
00121
00122 #ifdef WIN_NT
00123 int rc;
00124
00125 WSADATA wsadata;
00126
00127 rc = WSAStartup(2, &wsadata);
00128 *source = (char*)malloc(MAXSTRLEN);
00129 if(rc) {
00130 sprintf( *source, "WSAStartup FAILED: err=%d\n", (int) GetLastError());
00131 return FALSE;
00132 }
00133 #endif
00134
00135 if(*source == NULL)
00136 {
00137 *source = (char*)malloc(MAXSTRLEN);
00138 }
00139 len = strlen( server);
00140
00141 for( i = 0; i < len && server[i] != ':'; i++);
00142
00143 if (server[i] == ':')
00144 {
00145 server[i] = 0;
00146 port = atoi(server + i + 1);
00147 }
00148
00149
00150 if ( (hp = gethostbyname(server)) == NULL)
00151 {
00152 sprintf(*source, "hostname error for %s", server);
00153 return FALSE;
00154 }
00155
00156 pptr = (struct in_addr **) hp->h_addr_list;
00157
00158
00159 for ( ; *pptr != NULL; pptr++) {
00160
00161 sockfd = socket(AF_INET, SOCK_STREAM, 0);
00162
00163
00164 memset(&servaddr, 0, sizeof(servaddr));
00165 servaddr.sin_family = AF_INET;
00166 servaddr.sin_port = htons((unsigned short)port);
00167 memcpy(&servaddr.sin_addr, *pptr, sizeof(struct in_addr));
00168 if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) == 0)
00169 break;
00170 #ifdef WIN_NT
00171 closesocket(sockfd);
00172 #else
00173 close(sockfd);
00174 #endif
00175 return FALSE;
00176 }
00177 if (*pptr == NULL)
00178 {
00179 return FALSE;
00180 }
00181
00182 len = 0;
00183
00184
00185
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 while( (len != 0) && ( len != -1)){
00196 *source = (char*) realloc( *source, ((i+1) * MAXSTRLEN) + 1);
00197 tempstr = (*source) + maxlen;
00198 len = recv( sockfd, tempstr , MAXSTRLEN, 0);
00199
00200 if( (len != 0) && (len != -1) ){
00201 maxlen+=len;
00202 (*source)[maxlen]='\0';
00203 }
00204 i++;
00205 }
00206
00207
00208
00209 if( strstr( *source, "Error 400")!= NULL){
00210 strcpy( *source, "400 Bad Request");
00211 return FALSE;
00212 }
00213
00214 if( strstr( *source, "Error 401")!= NULL){
00215 strcpy( *source, "401 Unauthorized");
00216 return FALSE;
00217 }
00218
00219 if( strstr( *source, "Error 402")!= NULL){
00220 strcpy( *source, "402 Payment Required");
00221 return FALSE;
00222 }
00223
00224 if( strstr( *source, "Error 403")!= NULL){
00225 strcpy( *source, "403 Forbidden");
00226 return FALSE;
00227 }
00228
00229 if( strstr( *source, "Error 404")!= NULL){
00230
00231 strcpy( *source, "Error 404 File not found");
00232 return FALSE;
00233 }
00234
00235 if( strstr( *source, "Error 405")!= NULL){
00236 strcpy( *source, "405 Method Not Allowed");
00237 return FALSE;
00238 }
00239
00240 if( strstr( *source, "Error 406")!= NULL){
00241 strcpy( *source, "406 Not Acceptable");
00242 return FALSE;
00243 }
00244
00245 if( strstr( *source, "Error 407")!= NULL){
00246 strcpy( *source, "407 Proxy Authentication Required");
00247 return FALSE;
00248 }
00249
00250
00251 if( strstr( *source, "Error 408")!= NULL){
00252 strcpy( *source, "408 Request Timeout");
00253 return FALSE;
00254 }
00255
00256
00257 if( strstr( *source, "Error 409")!= NULL){
00258 strcpy( *source, "409 Conflict");
00259 return FALSE;
00260 }
00261
00262 if( strstr( *source, "Error 410")!= NULL){
00263 strcpy( *source, "410 Gone");
00264 return FALSE;
00265 }
00266
00267 if( strstr( *source, "Error 411")!= NULL){
00268 strcpy( *source, "411 Length Required"); ;
00269 return FALSE;
00270 }
00271 if( strstr( *source, "Error 412")!= NULL){
00272 strcpy( *source, "412 Precondition Failed");
00273 return FALSE;
00274 }
00275
00276 if( strstr( *source, "Error 413")!= NULL){
00277 strcpy( *source, "413 Request Entity Too Large");
00278 return FALSE;
00279 }
00280
00281 if( strstr( *source, "Error 414")!= NULL){
00282 strcpy( *source, "414 Request-URI Too Longe") ;
00283 return FALSE;
00284 }
00285
00286 if( strstr( *source, "Error 415")!= NULL){
00287 strcpy( *source, "415 Unsupported Media Type");
00288 return FALSE;
00289 }
00290
00291 if( strstr( *source, "Error 416")!= NULL){
00292 strcpy( *source, "416 Requested Range Not Satisfiable");
00293 return FALSE;
00294 }
00295
00296 if( strstr( *source, "Error 417")!= NULL){
00297 strcpy( *source, "417 Expectation Failed");
00298 return FALSE;
00299 }
00300
00301 if( strstr( *source, "Error 500")!= NULL){
00302 strcpy( *source, "500 Internal Server Error");
00303 return FALSE;
00304 }
00305
00306 if( strstr( *source, "Error 501")!= NULL){
00307 strcpy( *source, "501 Not Implemented");
00308 return FALSE;
00309 }
00310 if( strstr( *source, "Error 502")!= NULL){
00311 strcpy( *source, "502 Bad Gateway");
00312 return FALSE;
00313 }
00314
00315 if( strstr( *source, "Error 503")!= NULL){
00316 strcpy( *source, "503 Service Unavailable") ;
00317 return FALSE;
00318 }
00319
00320 if( strstr( *source, "Error 504")!= NULL){
00321 strcpy( *source, "504 Gateway Timeout");
00322 return FALSE;
00323 }
00324
00325 if( strstr( *source, "Error 505")!= NULL){
00326 strcpy( *source, "505 HTTP Version Not Supported");
00327 return FALSE;
00328 }
00329
00330 #ifdef WIN_NT
00331 closesocket(sockfd);
00332 #else
00333 close(sockfd);
00334 #endif
00335
00336 return TRUE;
00337 }