1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
|
/* Snownews - A lightweight console RSS newsreader
* $Id: netio.c 1173 2006-10-14 21:41:42Z kiza $
*
* Copyright 2003-2004 Oliver Feiler <kiza@kcore.de>
* http://kiza.kcore.de/software/snownews/
*
* netio.c
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/* OS X needs this, otherwise socklen_t is not defined. */
#ifdef __APPLE__
# define _BSD_SOCKLEN_T_
#endif
/* BeOS does not define socklen_t. Using uint as suggested by port creator. */
#ifdef __BEOS__
# define socklen_t unsigned int
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <assert.h>
#include <ncurses.h>
#include "config.h"
#include "main.h"
#include "interface.h"
#include "conversions.h"
#include "ui-support.h"
#include "net-support.h"
#include "io-internal.h"
#include "zlib_interface.h"
static int const MAX_HTTP_REDIRECTS = 10; /* Maximum number of redirects we will follow. */
static int const NET_TIMEOUT = 20; /* Global network timeout in sec */
static int const NET_READ = 1;
static int const NET_WRITE = 2;
extern char *proxyname; /* Hostname of proxyserver. */
extern unsigned short proxyport; /* Port on proxyserver to use. */
extern char *useragent;
/* Waits NET_TIMEOUT seconds for the socket to return data.
*
* Returns
*
* 0 Socket is ready
* -1 Error occurred (netio_error is set)
*/
int NetPoll (struct feed * cur_ptr, int * my_socket, int rw) {
fd_set rfdsr;
fd_set rfdsw;
struct timeval tv;
int retval; /* FD_ISSET + assert == Heisenbug? */
/* Set global network timeout */
tv.tv_sec = NET_TIMEOUT;
tv.tv_usec = 0;
FD_ZERO(&rfdsr);
FD_ZERO(&rfdsw);
if (rw == NET_READ) {
FD_SET(*my_socket, &rfdsr);
if (select (*my_socket+1, &rfdsr, NULL, NULL, &tv) == 0) {
/* Timed out */
cur_ptr->netio_error = NET_ERR_TIMEOUT;
return -1;
}
retval = FD_ISSET (*my_socket, &rfdsr);
assert (retval);
if (!retval) {
/* Wtf? */
cur_ptr->netio_error = NET_ERR_UNKNOWN;
return -1;
}
} else if (rw == NET_WRITE) {
FD_SET(*my_socket, &rfdsw);
if (select (*my_socket+1, NULL, &rfdsw, NULL, &tv) == 0) {
/* Timed out */
cur_ptr->netio_error = NET_ERR_TIMEOUT;
return -1;
}
retval = FD_ISSET (*my_socket, &rfdsw);
assert (retval);
if (!retval) {
/* Wtf? */
cur_ptr->netio_error = NET_ERR_UNKNOWN;
return -1;
}
} else {
cur_ptr->netio_error = NET_ERR_UNKNOWN;
return -1;
}
return 0;
}
/* Connect network sockets.
*
* Returns
*
* 0 Connected
* -1 Error occured (netio_error is set)
*/
int NetConnect (int * my_socket, char * host, struct feed * cur_ptr, int httpproto, int suppressoutput) {
char tmp[512];
struct sockaddr_in address;
struct hostent *remotehost;
socklen_t len;
char *realhost;
unsigned short port;
realhost = strdup(host);
if (sscanf (host, "%[^:]:%hd", realhost, &port) != 2) {
port = 80;
}
if (!suppressoutput) {
if (cur_ptr->title == NULL)
snprintf (tmp, sizeof(tmp), _("Downloading \"%s\""), cur_ptr->feedurl);
else
snprintf (tmp, sizeof(tmp), _("Downloading \"%s\""), cur_ptr->title);
UIStatus (tmp, 0, 0);
}
/* Create a inet stream TCP socket. */
*my_socket = socket (AF_INET, SOCK_STREAM, 0);
if (*my_socket == -1) {
cur_ptr->netio_error = NET_ERR_SOCK_ERR;
return -1;
}
/* If proxyport is 0 we didn't execute the if http_proxy statement in main
so there is no proxy. On any other value of proxyport do proxyrequests instead. */
if (proxyport == 0) {
/* Lookup remote IP. */
remotehost = gethostbyname (realhost);
if (remotehost == NULL) {
close (*my_socket);
free (realhost);
cur_ptr->netio_error = NET_ERR_HOST_NOT_FOUND;
return -1;
}
/* Set the remote address. */
address.sin_family = AF_INET;
address.sin_port = htons(port);
memcpy (&address.sin_addr.s_addr, remotehost->h_addr_list[0], remotehost->h_length);
/* Connect socket. */
cur_ptr->connectresult = connect (*my_socket, (struct sockaddr *) &address, sizeof(address));
/* Check if we're already connected.
BSDs will return 0 on connect even in nonblock if connect was fast enough. */
if (cur_ptr->connectresult != 0) {
/* If errno is not EINPROGRESS, the connect went wrong. */
if (errno != EINPROGRESS) {
close (*my_socket);
free (realhost);
cur_ptr->netio_error = NET_ERR_CONN_REFUSED;
return -1;
}
if ((NetPoll (cur_ptr, my_socket, NET_WRITE)) == -1) {
close (*my_socket);
free (realhost);
return -1;
}
/* We get errno of connect back via getsockopt SO_ERROR (into connectresult). */
len = sizeof(cur_ptr->connectresult);
getsockopt(*my_socket, SOL_SOCKET, SO_ERROR, &cur_ptr->connectresult, &len);
if (cur_ptr->connectresult != 0) {
close (*my_socket);
free (realhost);
cur_ptr->netio_error = NET_ERR_CONN_FAILED; /* ->strerror(cur_ptr->connectresult) */
return -1;
}
}
} else {
/* Lookup proxyserver IP. */
remotehost = gethostbyname (proxyname);
if (remotehost == NULL) {
close (*my_socket);
free (realhost);
cur_ptr->netio_error = NET_ERR_HOST_NOT_FOUND;
return -1;
}
/* Set the remote address. */
address.sin_family = AF_INET;
address.sin_port = htons(proxyport);
memcpy (&address.sin_addr.s_addr, remotehost->h_addr_list[0], remotehost->h_length);
/* Connect socket. */
cur_ptr->connectresult = connect (*my_socket, (struct sockaddr *) &address, sizeof(address));
/* Check if we're already connected.
BSDs will return 0 on connect even in nonblock if connect was fast enough. */
if (cur_ptr->connectresult != 0) {
if (errno != EINPROGRESS) {
close (*my_socket);
free (realhost);
cur_ptr->netio_error = NET_ERR_CONN_REFUSED;
return -1;
}
if ((NetPoll (cur_ptr, my_socket, NET_WRITE)) == -1) {
close (*my_socket);
free (realhost);
return -1;
}
len = sizeof(cur_ptr->connectresult);
getsockopt(*my_socket, SOL_SOCKET, SO_ERROR, &cur_ptr->connectresult, &len);
if (cur_ptr->connectresult != 0) {
close (*my_socket);
free (realhost);
cur_ptr->netio_error = NET_ERR_CONN_FAILED; /* ->strerror(cur_ptr->connectresult) */
return -1;
}
}
}
free (realhost);
return 0;
}
/*
* Main network function.
* (Now with a useful function description *g*)
*
* This function returns the HTTP request's body (deflating gzip encoded data
* if needed).
* Updates passed feed struct with values gathered from webserver.
* Handles all redirection and HTTP status decoding.
* Returns NULL pointer if no data was received and sets netio_error.
*/
char * NetIO (int * my_socket, char * host, char * url, struct feed * cur_ptr, char * authdata, int httpproto, int suppressoutput) {
char netbuf[4096]; /* Network read buffer. */
char *body; /* XML body. */
unsigned int length;
FILE *stream; /* Stream socket. */
#ifdef USE_UNSUPPORTED_AND_BROKEN_CODE
int chunked = 0; /* Content-Encoding: chunked received? */
#endif
int redirectcount; /* Number of HTTP redirects followed. */
char httpstatus[4]; /* HTTP status sent by server. */
char tmp[256];
char servreply[128]; /* First line of server reply */
char *tmpstatus;
char *savestart; /* Save start position of pointers. */
char *tmphost; /* Pointers needed to strsep operation. */
char *newhost; /* New hostname if we need to redirect. */
char *newurl; /* New document name ". */
char *newlocation;
char *tmpstring; /* Temp pointers. */
char *freeme, *freeme2;
char *redirecttarget;
int retval;
int handled;
int tmphttpstatus;
int inflate = 0; /* Whether feed data needs decompressed with zlib. */
int len;
char * inflatedbody;
int quirksmode = 0; /* IIS operation mode. */
int authfailed = 0; /* Avoid repeating failed auth requests endlessly. */
if (!suppressoutput) {
if (cur_ptr->title == NULL)
snprintf (tmp, sizeof(tmp), _("Downloading \"http://%s%s\""), host, url);
else
snprintf (tmp, sizeof(tmp), _("Downloading \"%s\""), cur_ptr->title);
UIStatus (tmp, 0, 0);
}
redirectcount = 0;
/* Goto label to redirect reconnect. */
tryagain:
/* Reconstruct digest authinfo for every request so we don't reuse
the same nonce value for more than one request.
This happens one superflous time on 303 redirects. */
if ((cur_ptr->authinfo != NULL) && (cur_ptr->servauth != NULL)) {
if (strstr (cur_ptr->authinfo, " Digest ") != NULL) {
NetSupportAuth(cur_ptr, authdata, url, cur_ptr->servauth);
}
}
/* Open socket. */
stream = fdopen (*my_socket, "r+");
if (stream == NULL) {
/* This is a serious non-continueable OS error as it will probably not
go away if we retry.
BeOS will stupidly return SUCCESS here making this code silently fail on BeOS. */
cur_ptr->netio_error = NET_ERR_SOCK_ERR;
return NULL;
}
/* Again is proxyport == 0, non proxy mode, otherwise make proxy requests. */
if (proxyport == 0) {
/* Request URL from HTTP server. */
if (cur_ptr->lastmodified != NULL) {
fprintf(stream,
"GET %s HTTP/1.0\r\nAccept-Encoding: gzip\r\nAccept: application/rdf+xml,application/rss+xml,application/xml,text/xml;q=0.9,*/*;q=0.1\r\nUser-Agent: %s\r\nConnection: close\r\nHost: %s\r\nIf-Modified-Since: %s\r\n%s%s\r\n",
url,
useragent,
host,
cur_ptr->lastmodified,
(cur_ptr->authinfo ? cur_ptr->authinfo : ""),
(cur_ptr->cookies ? cur_ptr->cookies : ""));
} else {
fprintf(stream,
"GET %s HTTP/1.0\r\nAccept-Encoding: gzip\r\nAccept: application/rdf+xml,application/rss+xml,application/xml,text/xml;q=0.9,*/*;q=0.1\r\nUser-Agent: %s\r\nConnection: close\r\nHost: %s\r\n%s%s\r\n",
url,
useragent,
host,
(cur_ptr->authinfo ? cur_ptr->authinfo : ""),
(cur_ptr->cookies ? cur_ptr->cookies : ""));
}
fflush(stream); /* We love Solaris, don't we? */
} else {
/* Request URL from HTTP server. */
if (cur_ptr->lastmodified != NULL) {
fprintf(stream,
"GET http://%s%s HTTP/1.0\r\nAccept-Encoding: gzip\r\nAccept: application/rdf+xml,application/rss+xml,application/xml,text/xml;q=0.9,*/*;q=0.1\r\nUser-Agent: %s\r\nConnection: close\r\nHost: %s\r\nIf-Modified-Since: %s\r\n%s%s\r\n",
host,
url,
useragent,
host,
cur_ptr->lastmodified,
(cur_ptr->authinfo ? cur_ptr->authinfo : ""),
(cur_ptr->cookies ? cur_ptr->cookies : ""));
} else {
fprintf(stream,
"GET http://%s%s HTTP/1.0\r\nAccept-Encoding: gzip\r\nAccept: application/rdf+xml,application/rss+xml,application/xml,text/xml;q=0.9,*/*;q=0.1\r\nUser-Agent: %s\r\nConnection: close\r\nHost: %s\r\n%s%s\r\n",
host,
url,
useragent,
host,
(cur_ptr->authinfo ? cur_ptr->authinfo : ""),
(cur_ptr->cookies ? cur_ptr->cookies : ""));
}
fflush(stream); /* We love Solaris, don't we? */
}
if ((NetPoll (cur_ptr, my_socket, NET_READ)) == -1) {
fclose (stream);
return NULL;
}
if ((fgets (servreply, sizeof(servreply), stream)) == NULL) {
fclose (stream);
return NULL;
}
if (checkValidHTTPHeader((unsigned char *)servreply, sizeof(servreply)) != 0) {
cur_ptr->netio_error = NET_ERR_HTTP_PROTO_ERR;
fclose (stream);
return NULL;
}
tmpstatus = strdup(servreply);
savestart = tmpstatus;
memset (httpstatus, 0, 4); /* Nullify string so valgrind shuts up. */
/* Set pointer to char after first space.
HTTP/1.0 200 OK
^
Copy three bytes into httpstatus. */
strsep (&tmpstatus, " ");
if (tmpstatus == NULL) {
cur_ptr->netio_error = NET_ERR_HTTP_PROTO_ERR;
fclose (stream);
free (savestart); /* Probably more leaks when doing auth and abort here. */
return NULL;
}
strncpy (httpstatus, tmpstatus, 3);
free (savestart);
cur_ptr->lasthttpstatus = atoi (httpstatus);
/* If the redirectloop was run newhost and newurl were allocated.
We need to free them here. */
if ((redirectcount > 0) && (authdata == NULL)) {
free (host);
free (url);
}
tmphttpstatus = cur_ptr->lasthttpstatus;
handled = 1;
/* Check HTTP server response and handle redirects. */
do {
switch (tmphttpstatus) {
case 200: /* OK */
/* Received good status from server, clear problem field. */
cur_ptr->netio_error = NET_ERR_OK;
cur_ptr->problem = 0;
/* Avoid looping on 20x status codes. */
handled = 1;
break;
case 300: /* Multiple choice and everything 300 not handled is fatal. */
cur_ptr->netio_error = NET_ERR_HTTP_NON_200;
fclose (stream);
return NULL;
case 301:
/* Permanent redirect. Change feed->feedurl to new location.
Done some way down when we have extracted the new url. */
case 302: /* Found */
case 303: /* See Other */
case 307: /* Temp redirect. This is HTTP/1.1 */
redirectcount++;
/* Give up if we reach MAX_HTTP_REDIRECTS to avoid loops. */
if (redirectcount > MAX_HTTP_REDIRECTS) {
cur_ptr->netio_error = NET_ERR_REDIRECT_COUNT_ERR;
fclose (stream);
return NULL;
}
while (!feof(stream)) {
if ((fgets (netbuf, sizeof(netbuf), stream)) == NULL) {
/* Something bad happened. Server sent stupid stuff. */
cur_ptr->netio_error = NET_ERR_HTTP_PROTO_ERR;
fclose (stream);
return NULL;
}
if (checkValidHTTPHeader((unsigned char *)netbuf, sizeof(netbuf)) != 0) {
cur_ptr->netio_error = NET_ERR_HTTP_PROTO_ERR;
fclose (stream);
return NULL;
}
/* Split netbuf into hostname and trailing url.
Place hostname in *newhost and tail into *newurl.
Close old connection and reconnect to server.
Do not touch any of the following code! :P */
if (strncasecmp (netbuf, "Location", 8) == 0) {
redirecttarget = strdup (netbuf);
freeme = redirecttarget;
/* Remove trailing \r\n from line. */
redirecttarget[strlen(redirecttarget)-2] = 0;
/* In theory pointer should now be after the space char
after the word "Location:" */
strsep (&redirecttarget, " ");
if (redirecttarget == NULL) {
cur_ptr->problem = 1;
cur_ptr->netio_error = NET_ERR_REDIRECT_ERR;
free (freeme);
fclose (stream);
return NULL;
}
/* Location must start with "http", otherwise switch on quirksmode. */
if (strncmp(redirecttarget, "http", 4) != 0)
quirksmode = 1;
/* If the Location header is invalid we need to construct
a correct one here before proceeding with the program.
This makes headers like
"Location: fuck-the-protocol.rdf" work.
In violalation of RFC1945, RFC2616. */
if (quirksmode) {
len = 7 + strlen(host) + strlen(redirecttarget) + 3;
newlocation = malloc(len);
memset (newlocation, 0, len);
strcat (newlocation, "http://");
strcat (newlocation, host);
if (redirecttarget[0] != '/')
strcat (newlocation, "/");
strcat (newlocation, redirecttarget);
} else
newlocation = strdup (redirecttarget);
/* This also frees redirecttarget. */
free (freeme);
/* Change cur_ptr->feedurl on 301. */
if (cur_ptr->lasthttpstatus == 301) {
/* Check for valid redirection URL */
if (checkValidHTTPURL((unsigned char *)newlocation) != 0) {
cur_ptr->problem = 1;
cur_ptr->netio_error = NET_ERR_REDIRECT_ERR;
fclose (stream);
return NULL;
}
if (!suppressoutput) {
UIStatus (_("URL points to permanent redirect, updating with new location..."), 1, 0);
printlog (cur_ptr, _("URL points to permanent redirect, updating with new location..."));
}
free (cur_ptr->feedurl);
if (authdata == NULL)
cur_ptr->feedurl = strdup (newlocation);
else {
/* Include authdata in newly constructed URL. */
len = strlen(authdata) + strlen(newlocation) + 2;
cur_ptr->feedurl = malloc (len);
newurl = strdup(newlocation);
freeme2 = newurl;
strsep (&newurl, "/");
strsep (&newurl, "/");
snprintf (cur_ptr->feedurl, len, "http://%s@%s", authdata, newurl);
free (freeme2);
}
}
freeme = newlocation;
strsep (&newlocation, "/");
strsep (&newlocation, "/");
tmphost = newlocation;
/* The following line \0-terminates tmphost in overwriting the first
/ after the hostname. */
strsep (&newlocation, "/");
/* newlocation must now be the absolute path on newhost.
If not we've been redirected to somewhere totally stupid
(oh yeah, no offsite linking, go to our fucking front page).
Say goodbye to the webserver in this case. In fact, we don't
even say goodbye, but just drop the connection. */
if (newlocation == NULL) {
cur_ptr->netio_error = NET_ERR_REDIRECT_ERR;
fclose (stream);
return NULL;
}
newhost = strdup (tmphost);
newlocation--;
newlocation[0] = '/';
newurl = strdup (newlocation);
free (freeme);
/* Close connection. */
fclose (stream);
/* Reconnect to server. */
if ((NetConnect (my_socket, newhost, cur_ptr, httpproto, suppressoutput)) != 0) {
return NULL;
}
host = newhost;
url = newurl;
goto tryagain;
}
}
break;
case 304:
/* Not modified received. We can close stream and return from here.
Not very friendly though. :) */
fclose (stream);
/* Received good status from server, clear problem field. */
cur_ptr->netio_error = NET_ERR_OK;
cur_ptr->problem = 0;
/* This should be freed everywhere where we return
and current feed uses auth. */
if ((redirectcount > 0) && (authdata != NULL)) {
free (host);
free (url);
}
return NULL;
case 401:
/* Authorization.
Parse rest of header and rerequest URL from server using auth mechanism
requested in WWW-Authenticate header field. (Basic or Digest) */
break;
case 404:
cur_ptr->netio_error = NET_ERR_HTTP_404;
fclose (stream);
return NULL;
case 410: /* The feed is gone. Politely remind the user to unsubscribe. */
cur_ptr->netio_error = NET_ERR_HTTP_410;
fclose (stream);
return NULL;
case 400:
cur_ptr->netio_error = NET_ERR_HTTP_NON_200;
fclose (stream);
return NULL;
default:
/* unknown error codes have to be treated like the base class */
if (handled) {
/* first pass, modify error code to base class */
handled = 0;
tmphttpstatus -= tmphttpstatus % 100;
} else {
/* second pass, give up on unknown error base class */
cur_ptr->netio_error = NET_ERR_HTTP_NON_200;
printlog (cur_ptr, servreply);
fclose (stream);
return NULL;
}
}
} while(!handled);
/* Read rest of HTTP header and parse what we need. */
while (!feof(stream)) {
if ((NetPoll (cur_ptr, my_socket, NET_READ)) == -1) {
fclose (stream);
return NULL;
}
if ((fgets (netbuf, sizeof(netbuf), stream)) == NULL)
break;
if (checkValidHTTPHeader((unsigned char *)netbuf, sizeof(netbuf)) != 0) {
cur_ptr->netio_error = NET_ERR_HTTP_PROTO_ERR;
fclose (stream);
return NULL;
}
if (strncasecmp (netbuf, "Transfer-Encoding", 17) == 0) {
/* Chunked transfer encoding. HTTP/1.1 extension.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1 */
/* This is not supported, because the contributed dechunk function
does not work with binary data and fails valgrind tests.
Disabled as of 1.5.7. */
#ifdef USE_UNSUPPORTED_AND_BROKEN_CODE
#warning ===The function decodedechunked() is not safe for binary data. Since you specifically requested it to be compiled in you probably know better what you are doing than me. Do not report bugs for this code.===
if (strstr (netbuf, "chunked") != NULL)
chunked = 1;
#else
cur_ptr->netio_error = NET_ERR_CHUNKED;
cur_ptr->problem = 1;
fclose (stream);
return NULL;
#endif
}
/* Get last modified date. This is only relevant on HTTP 200. */
if ((strncasecmp (netbuf, "Last-Modified", 13) == 0) &&
(cur_ptr->lasthttpstatus == 200)) {
tmpstring = strdup(netbuf);
freeme = tmpstring;
strsep (&tmpstring, " ");
if (tmpstring == NULL)
free (freeme);
else {
free(cur_ptr->lastmodified);
cur_ptr->lastmodified = strdup(tmpstring);
if (cur_ptr->lastmodified[strlen(cur_ptr->lastmodified)-1] == '\n')
cur_ptr->lastmodified[strlen(cur_ptr->lastmodified)-1] = '\0';
if (cur_ptr->lastmodified[strlen(cur_ptr->lastmodified)-1] == '\r')
cur_ptr->lastmodified[strlen(cur_ptr->lastmodified)-1] = '\0';
free(freeme);
}
}
if (strncasecmp (netbuf, "Content-Encoding", 16) == 0) {
if (strstr (netbuf, "gzip") != NULL)
inflate = 1;
}
if (strncasecmp (netbuf, "Content-Type", 12) == 0) {
tmpstring = strdup(netbuf);
freeme = tmpstring;
strsep(&tmpstring, " ");
if (tmpstring == NULL)
free (freeme);
else {
freeme2 = NULL;
freeme2 = strstr(tmpstring, ";");
if (freeme2 != NULL)
freeme2[0] = '\0';
free(cur_ptr->content_type);
cur_ptr->content_type = strdup(tmpstring);
if (cur_ptr->content_type[strlen(cur_ptr->content_type)-1] == '\n')
cur_ptr->content_type[strlen(cur_ptr->content_type)-1] = '\0';
if (cur_ptr->content_type[strlen(cur_ptr->content_type)-1] == '\r')
cur_ptr->content_type[strlen(cur_ptr->content_type)-1] = '\0';
free(freeme);
}
}
/* HTTP authentication
*
* RFC 2617 */
if ((strncasecmp (netbuf, "WWW-Authenticate", 16) == 0) &&
(cur_ptr->lasthttpstatus == 401)) {
if (authfailed) {
/* Don't repeat authrequest if it already failed before! */
cur_ptr->netio_error = NET_ERR_AUTH_FAILED;
fclose (stream);
return NULL;
}
/* Remove trailing \r\n from line. */
if (netbuf[strlen(netbuf)-1] == '\n')
netbuf[strlen(netbuf)-1] = '\0';
if (netbuf[strlen(netbuf)-1] == '\r')
netbuf[strlen(netbuf)-1] = '\0';
authfailed++;
/* Make a copy of the WWW-Authenticate header. We use it to
reconstruct a new auth reply on every loop. */
free (cur_ptr->servauth);
cur_ptr->servauth = strdup (netbuf);
/* Load authinfo into cur_ptr->authinfo. */
retval = NetSupportAuth(cur_ptr, authdata, url, netbuf);
switch (retval) {
case 1:
cur_ptr->netio_error = NET_ERR_AUTH_NO_AUTHINFO;
fclose (stream);
return NULL;
break;
case 2:
cur_ptr->netio_error = NET_ERR_AUTH_GEN_AUTH_ERR;
fclose (stream);
return NULL;
break;
case -1:
cur_ptr->netio_error = NET_ERR_AUTH_UNSUPPORTED;
fclose (stream);
return NULL;
break;
default:
break;
}
/* Close current connection and reconnect to server. */
fclose (stream);
if ((NetConnect (my_socket, host, cur_ptr, httpproto, suppressoutput)) != 0) {
return NULL;
}
/* Now that we have an authinfo, repeat the current request. */
goto tryagain;
}
/* This seems to be optional and probably not worth the effort since we
don't issue a lot of consecutive requests. */
/*if ((strncasecmp (netbuf, "Authentication-Info", 19) == 0) ||
(cur_ptr->lasthttpstatus == 200)) {
}*/
/* HTTP RFC 2616, Section 19.3 Tolerant Applications.
Accept CRLF and LF line ends in the header field. */
if ((strcmp(netbuf, "\r\n") == 0) || (strcmp(netbuf, "\n") == 0))
break;
}
/* If the redirectloop was run newhost and newurl were allocated.
We need to free them here.
But _after_ the authentication code since it needs these values! */
if ((redirectcount > 0) && (authdata != NULL)) {
free (host);
free (url);
}
/**********************
* End of HTTP header *
**********************/
/* Init pointer so strncat works.
Workaround class hack. */
body = malloc(1);
body[0] = '\0';
length = 0;
/* Read stream until EOF and return it to parent. */
while (!feof(stream)) {
if ((NetPoll (cur_ptr, my_socket, NET_READ)) == -1) {
fclose (stream);
return NULL;
}
/* Since we handle binary data if we read compressed input we
need to use fread instead of fgets after reading the header. */
retval = fread (netbuf, 1, sizeof(netbuf), stream);
if (retval == 0)
break;
body = realloc (body, length+retval);
memcpy (body+length, netbuf, retval);
length += retval;
if (retval != 4096)
break;
}
body = realloc(body, length+1);
body[length] = '\0';
cur_ptr->content_length = length;
/* Close connection. */
fclose (stream);
#ifdef USE_UNSUPPORTED_AND_BROKEN_CODE
if (chunked) {
if (decodechunked(body, &length) == NULL) {
free (body);
cur_ptr->netio_error = NET_ERR_HTTP_PROTO_ERR;
return NULL;
}
}
#endif
/* If inflate==1 we need to decompress the content.. */
if (inflate == 1) {
/* gzipinflate */
if (jg_gzip_uncompress (body, length, (void **)&inflatedbody, &cur_ptr->content_length) != 0) {
free (body);
cur_ptr->netio_error = NET_ERR_GZIP_ERR;
return NULL;
}
/* Copy uncompressed data back to body. */
free (body);
body = inflatedbody;
}
return body;
}
/* Returns allocated string with body of webserver reply.
Various status info put into struct feed * cur_ptr.
Set suppressoutput=1 to disable ncurses calls. */
char * DownloadFeed (char * url, struct feed * cur_ptr, int suppressoutput) {
int my_socket = 0;
int url_fixup = 0;
char *host; /* Needs to freed. */
char *tmphost;
char *freeme;
char *returndata;
char *authdata = NULL;
char *tmpstr;
int httpproto = 0; /* 0: http; 1: https */
if (checkValidHTTPURL((unsigned char *)url) != 0) {
cur_ptr->problem = 1;
cur_ptr->netio_error = NET_ERR_HTTP_PROTO_ERR;
return NULL;
}
/* strstr will match _any_ substring. Not good, use strncasecmp with length 5! */
if (strncasecmp (url, "https", 5) == 0)
httpproto = 1;
else
httpproto = 0;
strsep (&url, "/");
strsep (&url, "/");
tmphost = url;
strsep (&url, "/");
if (url == NULL) {
/* Assume "/" is input is exhausted. */
url = strdup("/");
url_fixup = 1;
}
/* If tmphost contains an '@', extract username and pwd. */
if (strchr (tmphost, '@') != NULL) {
tmpstr = tmphost;
strsep (&tmphost, "@");
authdata = strdup (tmpstr);
}
host = strdup (tmphost);
/* netio() might change pointer of host to something else if redirect
loop is executed. Make a copy so we can correctly free everything. */
freeme = host;
/* Only run if url was != NULL above. */
if (!url_fixup) {
url--;
url[0] = '/';
if (url[strlen(url)-1] == '\n') {
url[strlen(url)-1] = '\0';
}
}
if ((NetConnect (&my_socket, host, cur_ptr, httpproto, suppressoutput)) != 0) {
free (freeme);
free (authdata);
if (url_fixup)
free(url);
cur_ptr->problem = 1;
return NULL;
}
returndata = NetIO (&my_socket, host, url, cur_ptr, authdata, httpproto, suppressoutput);
if ((returndata == NULL) && (cur_ptr->netio_error != NET_ERR_OK)) {
cur_ptr->problem = 1;
}
/* url will be freed in the calling function. */
free (freeme); /* This is *host. */
free (authdata);
if (url_fixup)
free(url);
return returndata;
}
|