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
|
/***************************************
$Header: /home/amb/wwwoffle/RCS/parse.c 2.31 1998/08/22 10:53:30 amb Exp $
WWWOFFLE - World Wide Web Offline Explorer - Version 2.3.
Functions to parse the HTTP requests.
******************/ /******************
Written by Andrew M. Bishop
This file Copyright 1996,97,98 Andrew M. Bishop
It may be distributed under the GNU Public License, version 2, or
any higher version. See section COPYING of the GNU Public license
for conditions under which this file may be redistributed.
***************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "wwwoffle.h"
#include "misc.h"
#include "proto.h"
#include "errors.h"
#include "config.h"
/*+ Headers from a request that can be re-used in automatically generated requests. +*/
static char *reusable_headers[]={"User-Agent",
"Accept"};
/*+ The headers from the request that are re-usable. +*/
static char *reusable_header=NULL;
/*++++++++++++++++++++++++++++++++++++++
Parse the request to the server.
char *ParseRequest Returns the URL or NULL if it failed.
int fd The file descriptor to read the request from.
char **request_head Return the header of the request.
char **request_body Return the body of the request.
++++++++++++++++++++++++++++++++++++++*/
char *ParseRequest(int fd,char **request_head,char **request_body)
{
char *url=NULL,*line=NULL;
int i,length=-1;
*request_head=NULL;
*request_body=NULL;
while((line=read_line_or_timeout(fd,line)))
{
if(!strncasecmp("Content-Length:",line,15))
length=atoi(&line[15]);
/* Find re-usable headers (for recursive requests) */
for(i=0;i<sizeof(reusable_headers)/sizeof(char*);i++)
if(!strncmp(line,reusable_headers[i],strlen(reusable_headers[i])))
{
if(reusable_header)
{
reusable_header=(char*)realloc((void*)reusable_header,strlen(reusable_header)+strlen(line)+1);
strcat(reusable_header,line);
}
else
{
reusable_header=(char*)malloc(strlen(line)+1);
strcpy(reusable_header,line);
}
}
/* Check for firewall operation. */
if(!strncasecmp("Host:",line,5) && url && *url=='/')
{
char *p,*host=(char*)malloc(strlen(line));
p=line+6;
while(*p==' ' || *p=='\t')
p++;
strcpy(host,p);
p=host+strlen(host)-1;
while(*p=='\r' || *p=='\n' || *p==' ' || *p=='\t')
*p--=0;
p=url;
url=(char*)malloc(strlen(url)+strlen(host)+8);
strcpy(url,"http://");
strcat(url,host);
strcat(url,p);
free(p);
free(host);
}
/* Check for passwords */
if(!strncasecmp("Authorization:",line,14))
{
char *p=line+strlen(line)-1,*copy,*userpass;
int l;
while(*p=='\n' || *p=='\r' || *p==' ')
*p--=0;
p=line+15;
while(*p==' ') p++;
while(*p!=' ') p++;
while(*p==' ') p++;
userpass=Base64Decode(p,&l);
p=url;
if(*url=='/')
{
char *localhost=GetLocalHost(1);
copy=(char*)malloc(strlen(url)+strlen(userpass)+strlen(localhost)+10);
strcpy(copy,"http://");
strcat(copy,userpass);
strcat(copy,"@");
strcat(copy,localhost);
}
else
{
copy=(char*)malloc(strlen(url)+strlen(userpass)+2);
while(*p!=':') p++; p++;
while(*p=='/') p++;
strncpy(copy,url,p-url);
copy[p-url]=0;
strcat(copy,userpass);
strcat(copy,"@");
}
strcat(copy,p);
free(url);
url=copy;
continue;
}
/* Create the request body */
if(!*request_head) /* first line */
{
*request_head=(char*)malloc(strlen(line)+1);
strcpy(*request_head,line);
url=(char*)malloc(strlen(line));
if(sscanf(line,"%*s %s",url)!=1)
{free(url);return(NULL);}
}
else
{
*request_head=(char*)realloc((void*)*request_head,strlen(line)+strlen(*request_head)+1);
strcat(*request_head,line);
}
if(*line=='\r' || *line=='\n')
break;
}
if(line)
free(line);
if(!*request_head)
return(NULL);
if(!strncasecmp("POST",*request_head,4))
{
if(length==-1)
{free(url);return(NULL);}
*request_body=(char*)malloc(length+1);
if(length)
{
int m,l=length;
do
{
m=read_data_or_timeout(fd,&(*request_body)[length-l],l);
}
while(m>0 && (l-=m));
if(l)
{free(url);return(NULL);}
(*request_body)[length]=0;
empty_buffer(fd);
}
else
*(*request_body)=0;
url=(char*)realloc((void*)url,strlen(url)+32);
if(strchr(url,'?'))
{
char *from=url+strlen(url),*to=from+1;
while(*from!='?')
*to--=*from--;
*to='!';
}
else
strcat(url,"?");
sprintf(url+strlen(url),"!POST:%s",MakeHash(*request_body));
}
return(url);
}
/*++++++++++++++++++++++++++++++++++++++
Modify the request to ask for changes since the spooled file.
int RequestChanges Returns 1 if the file needs changes made, 0 if not, or -1 in case of an error.
int fd The file descriptor of the spooled file.
char **request_head The head of the HTTP request to modify.
++++++++++++++++++++++++++++++++++++++*/
int RequestChanges(int fd,char **request_head)
{
struct stat buf;
char *reply;
int status=0;
reply=read_line(fd,NULL);
if(reply)
{
sscanf(reply,"%*s %d",&status);
free(reply);
}
if(status==0)
{
PrintMessage(Debug,"Empty or no status");
return(-1);
}
else if(status>=200 && status<400 && !fstat(fd,&buf))
{
if((time(NULL)-buf.st_mtime)<RequestChanged)
{
PrintMessage(Debug,"Too new");
return(0);
}
else
{
char *if_mod=(char*)malloc(64);
char *copy=(char*)malloc(strlen(*request_head)+64);
char *eol=strchr(*request_head,'\n');
sprintf(if_mod,"If-Modified-Since: %s",RFC822Date(buf.st_mtime,1));
*eol=0; eol++;
strcpy(copy,*request_head);
strcat(copy,"\n");
strcat(copy,if_mod);
strcat(copy,"\r\n");
strcat(copy,eol);
free(*request_head);
free(if_mod);
*request_head=copy;
}
}
return(1);
}
/*++++++++++++++++++++++++++++++++++++++
Return the location that the URL has been moved to.
char *MovedLocation Returns the new URL.
URL *Url The original URL.
char *reply_head The head of the original HTTP reply.
++++++++++++++++++++++++++++++++++++++*/
char *MovedLocation(URL *Url,char *reply_head)
{
char *location,*eol,oldeol;
char *new;
location=GetHTTPHeader(reply_head,"Location:");
if(!location)
return(NULL);
location+=10;
eol=strchr(location,'\n');
if(eol[-1]=='\r')
eol--;
oldeol=*eol;
*eol=0;
new=LinkURL(Url,location);
if(new==location)
{
new=(char*)malloc(strlen(location)+1);
strcpy(new,location);
}
*eol=oldeol;
return(new);
}
/*++++++++++++++++++++++++++++++++++++++
Create a new request for a page.
char *RequestURL Ask for a page.
URL *Url The URL to get.
char *referer The Refering URL or NULL if none.
++++++++++++++++++++++++++++++++++++++*/
char *RequestURL(URL *Url,char *referer)
{
char *new,*authheader=NULL;
if(Url->user)
{
char *userpass=(char*)malloc(strlen(Url->user)+(Url->pass?strlen(Url->pass):0)+2);
authheader=(char*)malloc(strlen(Url->user)+(Url->pass?strlen(Url->pass):0)+32);
strcpy(userpass,Url->user);
strcat(userpass,":");
if(Url->pass)
strcat(userpass,Url->pass);
sprintf(authheader,"Authorization: Basic %s\r\n",Base64Encode(userpass,strlen(userpass)));
free(userpass);
}
new=(char*)malloc(strlen(Url->name)+16+
(referer?strlen(referer)+16:0)+
(authheader?strlen(authheader):0)+
(reusable_header?strlen(reusable_header):0));
sprintf(new,"GET %s HTTP/1.0\r\n",Url->name);
if(referer)
sprintf(&new[strlen(new)],"Referer: %s\r\n",referer);
if(authheader)
strcat(new,authheader);
if(reusable_header)
strcat(new,reusable_header);
strcat(new,"\r\n");
return(new);
}
/*++++++++++++++++++++++++++++++++++++++
Modify the request taking into account censoring of header and modified URL.
char *ModifyRequest Return the new request.
URL *Url The actual URL.
char *request_head The original head of the HTTP request possibly with a different URL.
++++++++++++++++++++++++++++++++++++++*/
char *ModifyRequest(URL *Url,char *request_head)
{
char *new;
char *hostheader=(char*)malloc(strlen(Url->host)+16),*closeheader,*authheader="";
char *bol,*to,http[16];
/* Make up the new headers. */
sprintf(hostheader,"Host: %s\r\n",Url->host);
closeheader="Connection: close\r\n";
if(Url->user)
{
char *userpass=(char*)malloc(strlen(Url->user)+(Url->pass?strlen(Url->pass):0)+2);
authheader=(char*)malloc(strlen(Url->user)+(Url->pass?strlen(Url->pass):0)+32);
strcpy(userpass,Url->user);
strcat(userpass,":");
if(Url->pass)
strcat(userpass,Url->pass);
sprintf(authheader,"Authorization: Basic %s\r\n",Base64Encode(userpass,strlen(userpass)));
free(userpass);
}
new=(char*)malloc(2*strlen(request_head)+strlen(closeheader)+strlen(hostheader)+strlen(authheader)+strlen(Url->name));
/* Parse the old header and create a new one. */
sscanf(request_head,"%s %*s %s",new,http);
strcat(new," ");
strcat(new,Url->name);
/* Remove the false arguments from POSTed URLs. */
if(!strncasecmp(new,"POST",4))
{
char *pling=strstr(new,"?!");
char *pling2=strchr(++pling+1,'!');
if(pling2)
for(;pling<pling2;pling++)
*pling=*(pling+1);
*(pling-1)=0;
}
strcat(new," ");
strcat(new,http);
strcat(new,"\r\n");
strcat(new,hostheader);
/* Check for HTTP 1.1 and add a Connection header */
if(!strncmp(http,"HTTP/1.1",8))
strcat(new,closeheader);
/* Add an authentication header. */
if(*authheader)
{
strcat(new,authheader);
free(authheader);
}
/* Censor the header */
to=new+strlen(new);
bol=strchr(request_head,'\n')+1;
while(*bol)
{
char *censor;
if(!strncasecmp("Host:",bol,5) ||
!strncasecmp("Connection:",bol,11) ||
!strncasecmp("Proxy-Connection:",bol,17) ||
!strncasecmp("Pragma: wwwoffle",bol,16))
bol=strchr(bol,'\n');
else
{
char *eol=strchr(bol,'\n');
if(*(eol-1)=='\r')
*(eol-1)=0;
else
*eol=0;
if(!strncasecmp("Referer:",bol,8))
{
char *pling=strstr(bol,"?!");
if(pling)
{
char *pling2=strchr(++pling+1,'!');
if(pling2)
for(;pling<pling2;pling++)
*pling=*(pling+1);
*(pling-1)=0;
}
while(*bol)
*to++=*bol++;
*to++='\r';
*to++='\n';
}
else if((censor=CensoredHeader(bol)))
{
char *bolc=censor;
while(*bolc)
*to++=*bolc++;
*to++='\r';
*to++='\n';
if(censor!=bol)
{
PrintMessage(Debug,"Censor replaced '%s' by '%s'",bol,censor);
free(censor);
}
}
else
PrintMessage(Debug,"Censored '%s'",bol);
bol=eol;
}
bol++;
}
*to=0;
/* tidy up and exit. */
free(hostheader);
free(request_head);
return(new);
}
/*++++++++++++++++++++++++++++++++++++++
Change the request to one that contains an authorisation string if required.
char *MakeRequestAuthorised Returns a new request with the authorisation if required or else the old request.
char *proxy The name of the proxy.
char *request_head The old HTTP request head.
++++++++++++++++++++++++++++++++++++++*/
char *MakeRequestAuthorised(char *proxy,char *request_head)
{
char *new=request_head;
char *userpass=WhatProxyAuth(proxy);
if(userpass)
{
char *userpassencoded=Base64Encode(userpass,strlen(userpass));
char *auth=(char*)malloc(strlen(userpassencoded)+32);
char *bol,*eol;
sprintf(auth,"Proxy-Authorization: Basic %s\r\n",userpassencoded);
new=(char*)malloc(strlen(request_head)+strlen(auth)+1);
if((bol=GetHTTPHeader(request_head,"Proxy-Authorization:")))
eol=strchr(++bol,'\n')+1;
else
bol=eol=strchr(request_head,'\n')+1;
strncpy(new,request_head,bol-request_head);
new[bol-request_head]=0;
strcat(new,auth);
strcat(new,eol);
free(auth);
}
return(new);
}
/*++++++++++++++++++++++++++++++++++++++
Change the request from one to a proxy to a normal one.
char *MakeRequestNonProxy Return a new request that is suitable for a non-proxy server.
char *request_head The buffer containing the head of the HTTP request.
++++++++++++++++++++++++++++++++++++++*/
char *MakeRequestNonProxy(char *request_head)
{
char *new=(char*)malloc(strlen(request_head)),*r=request_head,*n=new;
/* The URL is already in canonical form because of the ModifyRequest() function. */
while(*r!=' ') /* 'GET ' */
*n++=*r++;
*n++=*r++;
while(*r!=':') /* 'http://' */
r++;
r+=3;
while(*r!='/') /* 'www.host.domain/' */
r++;
strcpy(n,r);
return(new);
}
/*++++++++++++++++++++++++++++++++++++++
Parse the reply from the server.
int ParseReply Return the numeric status of the reply.
URL *Url The URL that we are reading.
char *reply_head Return the head of the HTTP reply.
++++++++++++++++++++++++++++++++++++++*/
int ParseReply(URL *Url,char **reply_head)
{
char *line=NULL;
int status=0;
*reply_head=NULL;
while((line=(Url->Protocol->readhead)(line)))
{
if(!*reply_head)
{
*reply_head=(char*)malloc(strlen(line)+1);
strcpy(*reply_head,line);
}
else
{
*reply_head=(char*)realloc((void*)*reply_head,strlen(line)+strlen(*reply_head)+1);
strcat(*reply_head,line);
}
if(*line=='\r' || *line=='\n')
break;
}
if(!line)
return(0);
if(sscanf(*reply_head,"%*s %d",&status)!=1)
status=0;
return(status);
}
/*++++++++++++++++++++++++++++++++++++++
Find the status of a spooled page.
int SpooledPageStatus Returns the status number.
URL *Url The URL to check.
++++++++++++++++++++++++++++++++++++++*/
int SpooledPageStatus(URL *Url)
{
int spool=OpenWebpageSpoolFile(1,Url);
int status=0;
if(spool!=-1)
{
char *reply;
init_buffer(spool);
reply=read_line(spool,NULL);
if(reply)
{
sscanf(reply,"%*s %d",&status);
free(reply);
}
close(spool);
}
return(status);
}
/*++++++++++++++++++++++++++++++++++++++
Search through a HTTP header for a specified line.
char *GetHTTPHeader Returns the header line or NULL if none.
char *head The header to search through.
char* line The line to look for.
++++++++++++++++++++++++++++++++++++++*/
char *GetHTTPHeader(char *head,char* line)
{
char *bol=head;
int n=strlen(line);
while(bol && *bol)
if(!strncasecmp(bol,line,n))
break;
else
bol=strchr(bol,'\n')+1;
if(bol && !*bol)
bol=NULL;
return(bol);
}
|