Changes in protocols/yahoo/yahoo_httplib.c [9034ba0:fcc2da9]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/yahoo/yahoo_httplib.c
r9034ba0 rfcc2da9 30 30 # define strrchr rindex 31 31 # endif 32 char *strchr (), *strrchr();32 char *strchr (), *strrchr (); 33 33 # if !HAVE_MEMCPY 34 34 # define memcpy(d, s, n) bcopy ((s), (d), (n)) … … 36 36 # endif 37 37 #endif 38 38 39 39 40 #include <errno.h> … … 62 63 extern enum yahoo_log_level log_level; 63 64 64 int yahoo_tcp_readline(char *ptr, int maxlen, void *fd)65 int yahoo_tcp_readline(char *ptr, int maxlen, int fd) 65 66 { 66 67 int n, rc; … … 70 71 71 72 do { 72 rc = YAHOO_CALLBACK(ext_yahoo_read)(fd, &c, 1);73 } while (rc == -1 && (errno == EINTR || errno == EAGAIN));/* this is bad - it should be done asynchronously */73 rc = read(fd, &c, 1); 74 } while(rc == -1 && (errno == EINTR || errno == EAGAIN)); /* this is bad - it should be done asynchronously */ 74 75 75 76 if (rc == 1) { 76 if (c == '\r')/* get rid of \r */77 if(c == '\r') /* get rid of \r */ 77 78 continue; 78 79 *ptr = c; … … 82 83 } else if (rc == 0) { 83 84 if (n == 1) 84 return (0); /* EOF, no data */85 return (0); /* EOF, no data */ 85 86 else 86 break; /* EOF, w/ data */87 break; /* EOF, w/ data */ 87 88 } else { 88 89 return -1; … … 95 96 96 97 static int url_to_host_port_path(const char *url, 97 char *host, int *port, char *path, int *ssl)98 { 99 char *urlcopy =NULL;100 char *slash =NULL;101 char *colon =NULL;102 98 char *host, int *port, char *path) 99 { 100 char *urlcopy=NULL; 101 char *slash=NULL; 102 char *colon=NULL; 103 103 104 /* 104 105 * http://hostname … … 110 111 * http://hostname:port/path 111 112 * http://hostname:port/path:foo 112 * and https:// variants of the above113 113 */ 114 114 115 if (strstr(url, "http://") == url) { 116 urlcopy = strdup(url + 7); 117 } else if (strstr(url, "https://") == url) { 118 urlcopy = strdup(url + 8); 119 *ssl = 1; 115 if(strstr(url, "http://") == url) { 116 urlcopy = strdup(url+7); 120 117 } else { 121 118 WARNING(("Weird url - unknown protocol: %s", url)); … … 126 123 colon = strchr(urlcopy, ':'); 127 124 128 if (!colon || (slash && slash < colon)) { 129 if (*ssl) 130 *port = 443; 131 else 132 *port = 80; 125 if(!colon || (slash && slash < colon)) { 126 *port = 80; 133 127 } else { 134 128 *colon = 0; 135 *port = atoi(colon +1);136 } 137 138 if 129 *port = atoi(colon+1); 130 } 131 132 if(!slash) { 139 133 strcpy(path, "/"); 140 134 } else { … … 144 138 145 139 strcpy(host, urlcopy); 146 140 147 141 FREE(urlcopy); 148 142 … … 152 146 static int isurlchar(unsigned char c) 153 147 { 154 return (isalnum(c) );148 return (isalnum(c) || '-' == c || '_' == c); 155 149 } 156 150 157 151 char *yahoo_urlencode(const char *instr) 158 152 { 159 int ipos = 0, bpos =0;153 int ipos=0, bpos=0; 160 154 char *str = NULL; 161 155 int len = strlen(instr); 162 156 163 if (!(str = y_new(char, 3 *len + 1)))164 165 166 while 167 while 157 if(!(str = y_new(char, 3*len + 1) )) 158 return ""; 159 160 while(instr[ipos]) { 161 while(isurlchar(instr[ipos])) 168 162 str[bpos++] = instr[ipos++]; 169 if 163 if(!instr[ipos]) 170 164 break; 171 172 snprintf(&str[bpos], 4, "%%% 02x", instr[ipos] & 0xff);173 bpos +=3;165 166 snprintf(&str[bpos], 4, "%%%.2x", instr[ipos]); 167 bpos+=3; 174 168 ipos++; 175 169 } 176 str[bpos] ='\0';170 str[bpos]='\0'; 177 171 178 172 /* free extra alloc'ed mem. */ 179 173 len = strlen(str); 180 str = y_renew(char, str, len +1);174 str = y_renew(char, str, len+1); 181 175 182 176 return (str); … … 185 179 char *yahoo_urldecode(const char *instr) 186 180 { 187 int ipos = 0, bpos =0;181 int ipos=0, bpos=0; 188 182 char *str = NULL; 189 char entity[3] = { 0, 0, 0};183 char entity[3]={0,0,0}; 190 184 unsigned dec; 191 185 int len = strlen(instr); 192 186 193 if (!(str = y_new(char, len + 1)))194 195 196 while 197 while (instr[ipos] && instr[ipos] !='%')198 if (instr[ipos] =='+') {199 str[bpos++] =' ';187 if(!(str = y_new(char, len+1) )) 188 return ""; 189 190 while(instr[ipos]) { 191 while(instr[ipos] && instr[ipos]!='%') 192 if(instr[ipos]=='+') { 193 str[bpos++]=' '; 200 194 ipos++; 201 195 } else 202 196 str[bpos++] = instr[ipos++]; 203 if 197 if(!instr[ipos]) 204 198 break; 205 206 if (instr[ipos + 1] && instr[ipos +2]) {199 200 if(instr[ipos+1] && instr[ipos+2]) { 207 201 ipos++; 208 entity[0] =instr[ipos++];209 entity[1] =instr[ipos++];202 entity[0]=instr[ipos++]; 203 entity[1]=instr[ipos++]; 210 204 sscanf(entity, "%2x", &dec); 211 205 str[bpos++] = (char)dec; … … 214 208 } 215 209 } 216 str[bpos] ='\0';210 str[bpos]='\0'; 217 211 218 212 /* free extra alloc'ed mem. */ 219 213 len = strlen(str); 220 str = y_renew(char, str, len +1);214 str = y_renew(char, str, len+1); 221 215 222 216 return (str); … … 225 219 char *yahoo_xmldecode(const char *instr) 226 220 { 227 int ipos = 0, bpos = 0, epos =0;221 int ipos=0, bpos=0, epos=0; 228 222 char *str = NULL; 229 char entity[4] = { 0, 0, 0, 0};230 char *entitymap[5][2] ={231 {"amp;", "&"},223 char entity[4]={0,0,0,0}; 224 char *entitymap[5][2]={ 225 {"amp;", "&"}, 232 226 {"quot;", "\""}, 233 {"lt;", "<"},234 {"gt;", "<"},227 {"lt;", "<"}, 228 {"gt;", "<"}, 235 229 {"nbsp;", " "} 236 230 }; … … 238 232 int len = strlen(instr); 239 233 240 if (!(str = y_new(char, len + 1)))241 242 243 while 244 while (instr[ipos] && instr[ipos] !='&')245 if (instr[ipos] =='+') {246 str[bpos++] =' ';234 if(!(str = y_new(char, len+1) )) 235 return ""; 236 237 while(instr[ipos]) { 238 while(instr[ipos] && instr[ipos]!='&') 239 if(instr[ipos]=='+') { 240 str[bpos++]=' '; 247 241 ipos++; 248 242 } else 249 243 str[bpos++] = instr[ipos++]; 250 if (!instr[ipos] || !instr[ipos +1])244 if(!instr[ipos] || !instr[ipos+1]) 251 245 break; 252 246 ipos++; 253 247 254 if 248 if(instr[ipos] == '#') { 255 249 ipos++; 256 epos =0;257 while 258 entity[epos++] =instr[ipos++];250 epos=0; 251 while(instr[ipos] != ';') 252 entity[epos++]=instr[ipos++]; 259 253 sscanf(entity, "%u", &dec); 260 254 str[bpos++] = (char)dec; … … 262 256 } else { 263 257 int i; 264 for (i = 0; i < 5; i++)265 if (!strncmp(instr + ipos, entitymap[i][0],266 267 str[bpos++] = entitymap[i][1][0];258 for (i=0; i<5; i++) 259 if(!strncmp(instr+ipos, entitymap[i][0], 260 strlen(entitymap[i][0]))) { 261 str[bpos++] = entitymap[i][1][0]; 268 262 ipos += strlen(entitymap[i][0]); 269 263 break; … … 271 265 } 272 266 } 273 str[bpos] ='\0';267 str[bpos]='\0'; 274 268 275 269 /* free extra alloc'ed mem. */ 276 270 len = strlen(str); 277 str = y_renew(char, str, len +1);271 str = y_renew(char, str, len+1); 278 272 279 273 return (str); 280 274 } 281 275 282 typedef void (*http_connected) (int id, void *fd, int error);276 typedef void (*http_connected)(int id, int fd, int error); 283 277 284 278 struct callback_data { … … 289 283 }; 290 284 291 static void connect_complete( void *fd, int error, void *data)285 static void connect_complete(int fd, int error, void *data) 292 286 { 293 287 struct callback_data *ccd = data; 294 if (error == 0) 295 YAHOO_CALLBACK(ext_yahoo_write) (fd, ccd->request, 296 strlen(ccd->request)); 297 free(ccd->request); 288 if(error == 0 && fd > 0) 289 write(fd, ccd->request, strlen(ccd->request)); 290 FREE(ccd->request); 298 291 ccd->callback(ccd->id, fd, error, ccd->user_data); 299 292 FREE(ccd); 300 293 } 301 294 302 static void yahoo_send_http_request(int id, char *host, int port, char *request, 303 yahoo_get_fd_callback callback, void *data, int use_ssl)304 { 305 struct callback_data *ccd =y_new0(struct callback_data, 1);295 static void yahoo_send_http_request(int id, char *host, int port, char *request, 296 yahoo_get_fd_callback callback, void *data) 297 { 298 struct callback_data *ccd=y_new0(struct callback_data, 1); 306 299 ccd->callback = callback; 307 300 ccd->id = id; 308 301 ccd->request = strdup(request); 309 302 ccd->user_data = data; 310 311 YAHOO_CALLBACK(ext_yahoo_connect_async) (id, host, port, 312 connect_complete, ccd, use_ssl); 313 } 314 315 void yahoo_http_post(int id, const char *url, const char *cookies, 316 long content_length, yahoo_get_fd_callback callback, void *data) 303 304 YAHOO_CALLBACK(ext_yahoo_connect_async)(id, host, port, connect_complete, ccd); 305 } 306 307 void yahoo_http_post(int id, const char *url, const char *cookies, long content_length, 308 yahoo_get_fd_callback callback, void *data) 317 309 { 318 310 char host[255]; … … 320 312 char path[255]; 321 313 char buff[1024]; 322 int ssl = 0; 323 324 if (!url_to_host_port_path(url, host, &port, path, &ssl)) 314 315 if(!url_to_host_port_path(url, host, &port, path)) 325 316 return; 326 317 327 /* thanks to kopete dumpcap */ 328 snprintf(buff, sizeof(buff), 329 "POST %s HTTP/1.1\r\n" 330 "Cookie: %s\r\n" 331 "User-Agent: Mozilla/5.0\r\n" 332 "Host: %s\r\n" 333 "Content-Length: %ld\r\n" 334 "Cache-Control: no-cache\r\n" 335 "\r\n", path, cookies, host, content_length); 336 337 yahoo_send_http_request(id, host, port, buff, callback, data, ssl); 338 } 339 340 void yahoo_http_get(int id, const char *url, const char *cookies, int http11, 341 int keepalive, yahoo_get_fd_callback callback, void *data) 318 snprintf(buff, sizeof(buff), 319 "POST %s HTTP/1.0\r\n" 320 "Content-length: %ld\r\n" 321 "User-Agent: Mozilla/4.5 [en] (" PACKAGE "/" VERSION ")\r\n" 322 "Host: %s:%d\r\n" 323 "Cookie: %s\r\n" 324 "\r\n", 325 path, content_length, 326 host, port, 327 cookies); 328 329 yahoo_send_http_request(id, host, port, buff, callback, data); 330 } 331 332 void yahoo_http_get(int id, const char *url, const char *cookies, 333 yahoo_get_fd_callback callback, void *data) 342 334 { 343 335 char host[255]; 344 336 int port = 80; 345 337 char path[255]; 346 char buff[2048]; 347 char cookiebuff[1024]; 348 int ssl = 0; 349 350 if (!url_to_host_port_path(url, host, &port, path, &ssl)) 338 char buff[1024]; 339 340 if(!url_to_host_port_path(url, host, &port, path)) 351 341 return; 352 342 353 /* Allow cases when we don't need to send a cookie */354 if (cookies)355 snprintf(cookiebuff, sizeof(cookiebuff), "Cookie: %s\r\n",356 cookies);357 else358 cookiebuff[0] = '\0';359 360 snprintf(buff, sizeof(buff), 361 "GET %s HTTP/1.%s\r\n"362 "%sHost: %s\r\n" 363 "User-Agent: Mozilla/4.5 [en] (" PACKAGE "/" VERSION ")\r\n" 364 "Accept: */*\r\n" 365 "%s" "\r\n", path, http11?"1":"0", cookiebuff, host,366 keepalive? "Connection: Keep-Alive\r\n":"Connection: close\r\n");367 368 yahoo_send_http_request(id, host, port, buff, callback, data, ssl); 369 } 370 371 void yahoo_http_head(int id, const char *url, const char *cookies, int len, 372 char *payload, yahoo_get_fd_callback callback, void *data)373 { 374 char host[255];375 int port = 80;376 char path[255]; 377 char buff[2048];378 char cookiebuff[1024]; 379 i nt ssl = 0;380 381 if (!url_to_host_port_path(url, host, &port, path, &ssl))343 snprintf(buff, sizeof(buff), 344 "GET %s HTTP/1.0\r\n" 345 "Host: %s:%d\r\n" 346 "User-Agent: Mozilla/4.5 [en] (" PACKAGE "/" VERSION ")\r\n" 347 "Cookie: %s\r\n" 348 "\r\n", 349 path, host, port, cookies); 350 351 yahoo_send_http_request(id, host, port, buff, callback, data); 352 } 353 354 struct url_data { 355 yahoo_get_url_handle_callback callback; 356 void *user_data; 357 }; 358 359 static void yahoo_got_url_fd(int id, int fd, int error, void *data) 360 { 361 char *tmp=NULL; 362 char buff[1024]; 363 unsigned long filesize=0; 364 char *filename=NULL; 365 int n; 366 367 struct url_data *ud = data; 368 369 if(error || fd < 0) { 370 ud->callback(id, fd, error, filename, filesize, ud->user_data); 371 FREE(ud); 382 372 return; 383 384 /* Allow cases when we don't need to send a cookie */ 385 if (cookies) 386 snprintf(cookiebuff, sizeof(cookiebuff), "Cookie: %s\r\n", 387 cookies); 388 else 389 cookiebuff[0] = '\0'; 390 391 snprintf(buff, sizeof(buff), 392 "HEAD %s HTTP/1.0\r\n" 393 "Accept: */*\r\n" 394 "Host: %s:%d\r\n" 395 "User-Agent: Mozilla/4.5 [en] (" PACKAGE "/" VERSION ")\r\n" 396 "%s" 397 "Content-Length: %d\r\n" 398 "Cache-Control: no-cache\r\n" 399 "\r\n%s", path, host, port, cookiebuff, len, 400 payload?payload:""); 401 402 yahoo_send_http_request(id, host, port, buff, callback, data, ssl); 403 } 404 373 } 374 375 while((n=yahoo_tcp_readline(buff, sizeof(buff), fd)) > 0) { 376 LOG(("Read:%s:\n", buff)); 377 if(!strcmp(buff, "")) 378 break; 379 380 if( !strncasecmp(buff, "Content-length:", 381 strlen("Content-length:")) ) { 382 tmp = strrchr(buff, ' '); 383 if(tmp) 384 filesize = atol(tmp); 385 } 386 387 if( !strncasecmp(buff, "Content-disposition:", 388 strlen("Content-disposition:")) ) { 389 tmp = strstr(buff, "name="); 390 if(tmp) { 391 tmp+=strlen("name="); 392 if(tmp[0] == '"') { 393 char *tmp2; 394 tmp++; 395 tmp2 = strchr(tmp, '"'); 396 if(tmp2) 397 *tmp2 = '\0'; 398 } else { 399 char *tmp2; 400 tmp2 = strchr(tmp, ';'); 401 if(!tmp2) 402 tmp2 = strchr(tmp, '\r'); 403 if(!tmp2) 404 tmp2 = strchr(tmp, '\n'); 405 if(tmp2) 406 *tmp2 = '\0'; 407 } 408 409 filename = strdup(tmp); 410 } 411 } 412 } 413 414 LOG(("n == %d\n", n)); 415 LOG(("Calling callback, filename:%s, size: %ld\n", filename, filesize)); 416 ud->callback(id, fd, error, filename, filesize, ud->user_data); 417 FREE(ud); 418 FREE(filename); 419 } 420 421 void yahoo_get_url_fd(int id, const char *url, const struct yahoo_data *yd, 422 yahoo_get_url_handle_callback callback, void *data) 423 { 424 char buff[1024]; 425 struct url_data *ud = y_new0(struct url_data, 1); 426 snprintf(buff, sizeof(buff), "Y=%s; T=%s", yd->cookie_y, yd->cookie_t); 427 ud->callback = callback; 428 ud->user_data = data; 429 yahoo_http_get(id, url, buff, yahoo_got_url_fd, ud); 430 } 431
Note: See TracChangeset
for help on using the changeset viewer.