forked from bakagirl/Arachne-WWW-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTP.C
1577 lines (1377 loc) · 36.8 KB
/
HTTP.C
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
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ========================================================================
// Arachne WWW browser HTTP functions
// (c)1997-2000 Michael Polak, Arachne Labs
// ========================================================================
#ifdef POSIX
#ifdef LINUX
#include <errno.h>
#elif defined (CLEMENTINE)
#include <clementine/errno.h>
extern int posixErrNo;
#define errno posixErrNo
#endif
#endif
#include "arachne.h"
#include "internet.h"
#define HTTP_QUICK_CONNECT 6 //first quick connect attempt (seconds)
#define HTTP_ASLEEP 60 //timeout for "empty documents" (esp. via proxy)
//...for images, timeouts are divided by 2
struct Http_parameters http_parameters;
#ifndef LINUX
void find_keepalive_socket(char *hostname)
{
if(GLOBAL.backgroundimages==BACKGROUND_EMPTY)
{
if(!strcmpi(hostname,sock_keepalive[1-socknum]) && !closing[1-socknum])
{
socknum=1-socknum;
socket=sock[socknum];
status=0;
}
}
}
#endif
char exestr[40]="\0";
void makeexestr(char *exestr);
int authenticated_http(struct Url *url,struct HTTPrecord *cache)
{
longword host=0;
//!!JdS 2004/2/15 {
// char str[IE_MAXLEN+2];
char str[MAXARGBUF+2], cookiestr[MAXARGBUF+2]; // The '+2' is for "\r\n"
//!!JdS 2004/2/15 }
int count=0;
char *ptr;
char *querystring=NULL,*cachecontrol;
char header_done=0;
int postindex=0;
char contentlength[80]="";
char authorization[128]="";
char acceptcharset[80]="";
// char cookiestr[2*IE_MAXLEN]=""; [JdS 2004/2/15]
char *nocache="Cache-Control: no-cache\r\nPragma: no-cache\r\n";
char *httpcommand="GET";
int line;
int ql=0;
char *uri,pocitac[STRINGSIZE]; //uri = uniform resource identifier
char referstr[URLSIZE+16]="";
char portstr[10]="";
int port,i;
char ftp=0,alive=0;
int delay=HTTP_QUICK_CONNECT, attempt=1;
#ifdef POSIX
struct sockaddr_in sin;
fd_set rfds, efds;
struct timeval tv;
#endif
char willkeepalive=0;
char *keepalive="\0";
//!!glennmcc: Dec 21, 2007 -- at Ray's suggestion, also write outgoing traffic
char outgoing[1024]="\0";
//!!glennmcc: end
if(!tcpip && !httpstub)return 0;
#ifdef MSDOS
if(tcpip)
free_socket();
#endif
/*create string with executable description - DOS, Linux, etc. Created only once*/
if(!*exestr)
makeexestr(exestr);
if(http_parameters.referer)
sprintf(referstr,"Referer: %s\r\n",Referer);
if(!GLOBAL.isimage)
{
ptr=configvariable(&ARACHNEcfg,"AcceptCharset",NULL);
if(ptr)
{
sprintf(str,"Accept-Charset: %s\r\n",ptr);
makestr(acceptcharset,str,79);
}
}
//normal URL:
strcpy(pocitac,url->host);
port=url->port;
uri=url->file;
while(!strncmp(uri,"/..",3)) // do not descend beyond root directory !
uri+=3;
//use proxy server ?
ftp=(toupper(url->protocol[0])=='F');
if(ftp || http_parameters.useproxy)
{
char *no4all=NULL;
if(ftp)
ptr=NULL;
else
{
ptr=configvariable(&ARACHNEcfg,"NoProxy",NULL);
no4all=configvariable(&ARACHNEcfg,"NoProxy4all",NULL);
}
if( (!ptr || !strstr(strlwr(ptr),strlwr(pocitac)) ) &&
(!no4all || !strstr(strlwr(pocitac), strlwr(no4all))) )
{
if(ftp)
ptr=configvariable(&ARACHNEcfg,"FTPproxy",NULL);
else
ptr=configvariable(&ARACHNEcfg,"HTTPproxy",NULL);
if(ptr)
{
makestr(pocitac,ptr,79);
ptr=strrchr(pocitac,':');
if(ptr)
{
*ptr++='\0';
port=atoi(ptr);
}
else
port=80;
uri=cache->URL;
}
}
}//end if proxy
//this will appear in "Host:" http header field..
if(url->port!=80)
sprintf(portstr,":%d",url->port);
#ifndef LINUX
find_keepalive_socket(pocitac);
#endif
if(tcpip && !httpstub && strcmpi(sock_keepalive[socknum],pocitac))
{
GlobalLogoStyle=0; //SDL set resolve animation
/*
#ifdef POSIX
{ //blocking version - not necessary, if non-blocking reslove_fn() implemented in asockets.h works
struct hostent *phe; // host information entry
if((phe = gethostbyname(pocitac)) == NULL)
{
if((host = inet_addr(pocitac)) < 0)
host=0;
}
else
host = *((longword *) phe->h_addr_list[0]);
} //end temporary DNS code
#else
*/
host=resolve_fn( pocitac, (sockfunct_t) TcpIdleFunc ); //SDL
//#endif
if(!host)
{
DNSerr(pocitac);
return 0;
}
}
i=0;
cookiestr[0]='\0'; //!!JdS 2004/2/15
strlwr(url->host);
/**** Start of superseded code [JdS 2004/3/2] ****
while(i<cookies.lines)
{
ptr=ie_getline(&cookies,i);
if(ptr)
{
strcpy(str,ptr);
decompose_inetstr(str);
if(getarg("domain",&ptr) && strstr(url->host,ptr)) // getarg() was getvar() [JdS 2004/1/30]
{
if(getarg("path",&ptr) && strstr(url->file,ptr)) // getarg() was getvar() [JdS 2004/1/30]
{
//!!JdS 2004/2/15 {
// if(strlen(cookiestr)+strlen(str)+10<2*IE_MAXLEN)
if(strlen(cookiestr)+strlen(str)+10<MAXARGBUF)
//!!JdS 2004/2/15 }
{
if(cookiestr[0])
strcat(cookiestr,"; ");
else
strcat(cookiestr,"Cookie: ");
strcat(cookiestr,str);
}
}
}
}
i++;
}//loop
**** End of superseded code [JdS 2004/3/2] ****/
/**** Start of newer cookie code [JdS 2004/3/2] ****/
while (i+CookieCrumbs<=cookies.lines)
{
//Piece together a cookie from crumbs in the 'cookies' jar
ie_getcookie(str,&cookies,i);
//If a cookie was found, check if it matches our domain and path
if (str[0])
{
decompose_inetstr(str);
if (getarg("domain",&ptr) && strstr(url->host,ptr))
{
if (getarg("path",&ptr) && strstr(url->file,ptr))
{
if (strlen(cookiestr)+strlen(str)+10<MAXARGBUF)
{
if (cookiestr[0])
strcat(cookiestr,"; ");
else
strcat(cookiestr,"Cookie: ");
strcat(cookiestr,str);
}
}
}
}
i += CookieCrumbs;
}//while
/**** End of newer cookie code [JdS 2004/3/2] ****/
if(cookiestr[0])
{
strcat(cookiestr,"\r\n");
// puts(cookiestr);
}
out:
if(url->user[0] || AUTHENTICATION->flag>=AUTH_OK)
{
str[0]='\0';
if(AUTHENTICATION->flag>=AUTH_OK)
sprintf(authorization,"%s:%s",AUTHENTICATION->user,AUTHENTICATION->password);
else
{
sprintf(authorization,"%s:%s",url->user,url->password);
strcpy(AUTHENTICATION->user,url->user);
strcpy(AUTHENTICATION->password,url->password);
}
base64code((unsigned char *)authorization,str);
sprintf(authorization,"Authorization: Basic %s\r\n",str);
}
//this is experimental proxy authorization code !!!
if(AUTHENTICATION->proxy)
{
char tmp[128];
sprintf(str,"%s:%s",
configvariable(&ARACHNEcfg,"ProxyUsername",NULL),
configvariable(&ARACHNEcfg,"ProxyPassword",NULL));
base64code((unsigned char *)str,tmp);
sprintf(str,"Proxy-authorization: Basic %s\r\n",tmp);
strcat(authorization,str);
}
//end experiment
// bad idea ?
// if(sock_keepalive[socknum][0] && !tcp_tick(socket)) //connection was lost ?
// sock_keepalive[socknum][0]='\0';
if(tcpip && !httpstub && strcmpi(sock_keepalive[socknum],pocitac))
{
retry:
GlobalLogoStyle=2; //SDL set connect animation
#ifdef POSIX
sin.sin_addr.s_addr = host;
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
bzero(&(sin.sin_zero), 8); /* zero the rest of the struct */
sprintf(str,msg_con,pocitac,port);
outs(str);
/* create socket */
socknum=socket(PF_INET, SOCK_STREAM, 0);
if(socknum < 0)
{
sprintf(str,msg_errcon,pocitac);
outs(str);
return 0;
}
/* make socket non-blocking */
fcntl (socknum, F_SETFL, O_NONBLOCK);
/* connect to server */
while(connect(socknum, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
if(TcpIdleFunc())
return 0;
}
/*
//old style, synchrounous (blocking) connect. Not good for Arachne.
if(connect(socknum, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
sprintf(str,msg_errcon,pocitac);
outs(str);
return 0;
}
*/
#else
status=tcp_open(socket, locport(), host, port, NULL );
if (status!=1)
{
sprintf(str,msg_errcon,pocitac);
outs(str);
return 0;
}
//!!glennmcc: Sep 27, 2008 -- increase D/L speed on cable & DSL
//many thanks to 'mik' for pointing me in the right direction. :)
if(status==1)
{
#ifdef DEBUG
char sp[80];
sprintf(sp,"Available stack = %u bytes",_SP);
outs(sp);
Piip(); Piip();
#endif
if(_SP>(1024*SETBUFSIZE))
{
char setbuf[1024*SETBUFSIZE];
sock_setbuf(socket, (unsigned char *)setbuf, 1024*SETBUFSIZE);
user_interface.multitasking=MULTI_SAFE;
}
}
//!!glennmcc: end
sprintf(str,msg_con,pocitac,port);
outs(str);
if (_ip_delay0(socket, delay, (sockfunct_t) TcpIdleFunc, &status )) //SDL
{
if(attempt==3)
goto sock_err;
else
{
if(attempt==2)
{
delay=sock_delay;
if(GLOBAL.isimage)
delay/=2;
}
attempt++;
sock_abort(socket);
goto retry;
}
}//wait for connection
#endif
}//end if not TCP/IP open (or connection is alive)
else
alive=1;
#ifndef LINUX
//initialize keepalive mechanism:
if(http_parameters.keepalive)
makestr(sock_keepalive[socknum],pocitac,STRINGSIZE);
sock_datalen[socknum]=0;
#endif
//SDL set data animation
GlobalLogoStyle=1;
//echo cookie string
if(cookiestr[0])
outs(cookiestr);
else if(authorization[0])
outs(authorization);
else if(alive)
{
sprintf(str,MSG_ALIVE,pocitac,uri);
outs(str);
//printf("[%s]",str);
}
else
{
sprintf(str,MSG_REQ,pocitac,uri);
outs(str);
}
//odesilam formular metodou POST ?
//(metodu GET jsem uz zmaknul jinde...)
// tr.: am I sending form by method POST ?
// (method GET I have already done elsewhere)
if(GLOBAL.postdata==2) //method==POST
{
querystring=ie_getswap(GLOBAL.postdataptr);
if(!querystring)
MALLOCERR();
ql=strlen(querystring);
httpcommand="POST";
//the problem with CGI forms was "x-www-form-urlencoded"....
sprintf(contentlength,"Content-type: application/x-www-form-urlencoded\r\nContent-length: %d\r\n",ql);
}
if(GLOBAL.reload || GLOBAL.postdata)
cachecontrol=nocache;
else
cachecontrol="\0";
{
//!!glennmcc: Sep 10, 2008 -- configurable useragent string
char useragent[120];
ptr=configvariable(&ARACHNEcfg,"UserAgent",NULL);
if(ptr) sprintf(useragent,"%s",ptr);
if(!ptr || strlen(useragent)<10)
sprintf(useragent,"xChaos_Arachne (DOS /5.%s%s)",VER,beta);
//!!glennmcc: end
//!!glennmcc: Aug 20, 2005
//don't include 'exestr' nor video settings in User-agent
/*
char colordepth[10],*c="HiColor";
#ifdef HICOLOR
if (xg_256!=MM_Hic)
{
#endif
sprintf(colordepth,"%dc",x_getmaxcol()+1);
c=colordepth;
#ifdef HICOLOR
}
#endif
*/
//!!glennmcc: end
if(http_parameters.keepalive)
keepalive="Connection: Keep-Alive\n";
//!!glennmcc: Jan 18, 2011 -- fix intermittent posting problems
if(querystring) sleep(1);//Piip();
//!!glennmcc: end
//!!glennmcc: Aug 20, 2005
//don't include 'exestr' nor video settings in User-agent
//User-agent: xChaos_Arachne/4.%s%s (%s; %dx%d,%s; www.arachne.cz)\r\n\
//removed as indicated by '^'________^^^^^^^^^^^^^^___
//also changed to 'version 5 type'
//also changed to my own web site address
sprintf(p->buf,"\
%s %s HTTP/1.0\r\n\
User-agent: %s\r\n\
Accept: */*\r\n\
Host: %s%s\r\n\
%s%s%s%s%s%s%s\r\n",
httpcommand,uri,
//!!glennmcc: Sep 10, 2008 -- configurable useragent string
//VER,beta,//moved above for configurable useragent string
// replaced with 'useragent'
useragent,
// httpcommand,uri,VER,beta,exestr,x_maxx()+1,x_maxy()+1,c,
//removed as indicated by '^'_^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^___
//!!glennmcc: end
url->host,portstr,
keepalive,
cachecontrol,
contentlength,
cookiestr,
authorization,
referstr,
acceptcharset);
}
//!!glennmcc: Dec 21, 2007 -- at Ray's suggestion, also write outgoing traffic
if(!outgoing[0])
sprintf(outgoing,"<b>Sent to Server:</b>\r\n\r\n\
%s<hr><b>Received from Server:</b>\r\n\r\n",p->buf);
//!!glennmcc: end
if(tcpip && !httpstub) //if TCP/IP is enabled
{
#ifdef POSIX
if(sock_puts(socknum, p->buf)<0) //send HTTP reques....
{
outs(MSG_CLOSED);
return 0;
}
#else
sock_puts(socket, (unsigned char *)p->buf); //send HTTP reques....
#endif
if(querystring) //if query string has to be posted
{
outs(MSG_POST);
#ifdef POSIX
if(sock_puts(socknum, querystring)<0) //send HTTP reques....
{
outs(MSG_CLOSED);
return 0;
}
#else
//!!glennmcc Jul 16, 2005 -- fix intermitant posting problem
// by sending 16 byte chunks instead of 512 bytes
while(postindex+16<ql)
// while(postindex+512<ql)
{
/*this is needed only for WATTCP*/
while(sock_tbleft(socket)<16) //SDL
// while(sock_tbleft(socket)<512) //SDL
{
sock_tick(socket,&status);
xChLogoTICK(1); // animation of logo
if(GUITICK())
goto post_aborted;
}
querystring=ie_getswap(GLOBAL.postdataptr);
if(!querystring)
MALLOCERR();
sock_tick(socket, &status ); //I shift TCP/IP
sock_fastwrite(socket, (unsigned char *)&querystring[postindex] ,16);
// sock_fastwrite(socket, (unsigned char *)&querystring[postindex] ,512);
postindex+=16;
// postindex+=512;
//!!glennmcc: end
}//loop
if(postindex<ql)
{
while(sock_tbleft(socket)<strlen(&querystring[postindex])) //SDL
{
sock_tick(socket,&status);
xChLogoTICK(1); // animation of logo
if(GUITICK())
goto post_aborted;
}
querystring=ie_getswap(GLOBAL.postdataptr);
if(!querystring)
MALLOCERR();
sock_tick(socket, &status ); //I shift TCP/IP
sock_fastwrite(socket, (unsigned char *)&querystring[postindex] ,strlen(&querystring[postindex]));
}
sock_tick(socket, &status ); //I shift TCP/IP
sock_puts(socket, (unsigned char *)"\r\n");
sprintf(str,MSG_SENT,ql);
outs(str);
post_aborted:
if(GLOBAL.gotolocation || GLOBAL.abort)
goto abort;
#endif
}//endif posting querystring...
}//endif tcpip
//!!glennmcc: begin Oct 17, 2004 -- re-enable HTTPSTUB
//(this entire section had been commented-out)
// /*
#ifndef LINUX
else //httpstub = non TCP/IP stuf ========================================
{
int f,l;
char reqname[80];
struct ffblk ff;
//sprintf(timstr,"%ld",time(NULL));
outs("Generating http/stub request...");
strcpy(reqname,cache->locname);
ptr=strrchr(reqname,'.');
if(ptr)
strcpy(ptr,".REQ");
f=a_fast_open(reqname,O_BINARY|O_WRONLY|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE);
if (f>=0)
{
write(f,p->buf ,strlen(p->buf));
//!!glennmcc Oct 17, 2004 -- commented-out to prevent compiler errors
/*
if(*poststring)
{
querystring=ie_getswap(GLOBAL.postdataptr);
if(!querystring)
MALLOCERR();
write(f, querystring,strlen(querystring));
}
*/
close(f);
}
outs("Waiting for http/stub answer...");
ptr=strstr(reqname,".REQ");
if(ptr)
strcpy(ptr,".OK");
do
{
l=0;
while(l++<500)
{
xChLogoTICK(1); // animation of logo
GUITICK();
}
if(GLOBAL.abort)
return 0;
}
while(findfirst(reqname,&ff,0));
f=a_fast_open(reqname,O_RDONLY|O_TEXT,0);
if(f)
{
p->httplen=a_read(f,p->buf,BUF-1);
//!!glennmcc Oct 17, 2004 -- commented-out to prevent compiler errors
// p->buf[httplen]='\0';
close(f);
}
ptr=strstr(reqname,".OK");
if(ptr)
strcpy(ptr,".TMP");
strcpy(cache->locname,reqname);
goto analyse;
} // ====================================================================
#endif
// */
//!!glennmcc: end Oct 17, 2004 -- re-enable HTTPSTUB
//let's initialize this session.
p->httplen=0;
cache->size=0l;
cache->knowsize=0;
cache->dynamic=1;
if(GLOBAL.isimage)
strcpy(cache->mime,"image/gif");
else
strcpy(cache->mime,"text/html");
p->buf[0]='\0';
// READ HEADER:
{
#ifndef POSIX
int iddle=0;
#endif
long timer=time(NULL),asleep;
do
{
#ifdef POSIX
xChLogoTICK(10); // animation of logo
#else
xChLogoTICK(1); // animation of logo
#endif
asleep=time(NULL)-timer;
if(GLOBAL.isimage)
asleep*=2;
if(asleep>HTTP_ASLEEP)
goto abort;
if(GUITICK())
{
if(GLOBAL.gotolocation || GLOBAL.abort)
goto abort;
}
#ifdef GGI
IfRequested_ggiFlush();
#endif
#ifdef POSIX
tv.tv_sec = 0;
tv.tv_usec = 500;
FD_ZERO (&rfds);
FD_ZERO (&efds);
FD_SET (socknum, &rfds);
FD_SET (socknum, &efds);
select (socknum+1, &rfds, NULL, &efds, &tv);
if (FD_ISSET (socknum, &efds) && errno!=EINTR)
{
outs(MSG_CLOSED);
return 0;
}
count=read(socknum, &(p->buf[p->httplen]),BUF-p->httplen);
if(count<0)
{
if (errno != EAGAIN) {
outs(MSG_CLOSED);
return 0;
}
else count = 0;
}
p->httplen+=count;
p->buf[p->httplen]='\0';
if(strstr(p->buf,"\r\n\r\n") || strstr(p->buf,"\r\r") || strstr(p->buf,"\n\n") || p->httplen>=p->buf)
header_done=1;
#else
if (sock_dataready(socket ))
{
if(p->httplen+256<BUF)
{
count=sock_fastread(socket, (unsigned char *)&(p->buf[p->httplen]), 256);
p->httplen+=count;
p->buf[p->httplen]='\0';
if(strstr(p->buf,"\r\n\r\n") || strstr(p->buf,"\r\r") || strstr(p->buf,"\n\n"))
header_done=1;
}
else
{
count=sock_fastread(socket, (unsigned char *)str, 256);
str[count]='\0';
if(strstr(str,"\r\n\r\n") || strstr(str,"\r\r") || strstr(str,"\n\n"))
header_done=1;
}
sprintf(str,MSG_READ,count);
outs(str);
}//endif
else
iddle++;
if(iddle>1000 && !tcp_tick(socket))
{
sockmsg(1,socknum);
header_done=1;
}
#endif
}
while(!header_done);
}
#ifndef POSIX
goto analyse;
sock_err:
sockmsg(status,socknum);
if(p->httplen==0)
return 0;
analyse:
#endif
if(strncmp(p->buf,"HTTP",4) && p->buf[0] && p->httplen)
{
count=0;
goto write2cache;
}
count=0;
line=0;
while(count<p->httplen)
{
if(p->buf[count]=='\n')
{
//!!JdS 2004/3/7 {
// makestr(str,&(p->buf[line]),IE_MAXLEN);
makestr(str,&(p->buf[line]),MAXARGBUF);
//!!JdS 2004/3/7 }
ptr=strchr(str,'\r');
if(ptr)*ptr='\0';
ptr=strchr(str,'\n');
if(ptr)*ptr='\0';
// -------------------------------- empty line -> end of HTTP header
if(!str[0] && !GLOBAL.redirection && AUTHENTICATION->flag!=AUTH_REQUIRED)
goto write2cache;
//!!glennmcc: Mar 29, 2010 -- compensate for some servers not having a space
//between the ':' and the spec in the HTTP header
//such-as 'content-type:text/plain' instead of the correct
//format 'content-type: text/plain'
// ptr=strstr(str,": ");//original line
ptr=strstr(str,":");
//changed that line and changed several ocurrences below
//of ptr[2] to ptr[1+space]
if(ptr)
{
int space=0;
if(ptr[1]==' ') space=1;
*ptr='\0';
// ----------------------------------------------- Content-type:
if(!strcmpi(str,"Content-type"))
{
makestr(cache->mime,&ptr[1+space],STRINGSIZE-1); /* including charset= */
strlwr(cache->mime);
#ifdef EXP
{
char ext[5], *mimestr="\0", mime=0;
get_extension(cache->URL,ext);
if(
(
strstr(cache->mime,"text/plain") ||
strstr(cache->mime,"application/octet-stream")
) &&
(
!strcmpi(ext,"avi") ||
!strcmpi(ext,"f4v") ||
!strcmpi(ext,"flv") ||
!strcmpi(ext,"mp4") ||
!strcmpi(ext,"mpeg") ||
!strcmpi(ext,"mpg") ||
!strcmpi(ext,"mov") ||
!strcmpi(ext,"ogg") ||
!strcmpi(ext,"oggv") ||
!strcmpi(ext,"webm") ||
!strcmpi(ext,"wmv")
)
)
{
Piip();
strcpy(mimestr,"video/");
mimestr[79]='\0';
strncat(mimestr,ext,70);
makestr(cache->mime,mimestr,STRINGSIZE-1);
mime=1;
}
if(!mime)
makestr(cache->mime,&ptr[1+space],STRINGSIZE-1); /* including charset= */
strlwr(cache->mime);
}
#endif
}
// ----------------------------------------------- Content-length:
else if(!strcmpi(str,"Content-length"))
{
cache->size=atol(&ptr[1+space]);
cache->knowsize=1;
//!!glennmcc: July 08, 2006 -- only do a BGDL if it was
// specifcally requested via CTRL+Enter/CTRL+LeftClick
// or via Enter/LeftClick when size is not known
if(GLOBAL.backgr==2) GLOBAL.backgr=0;
//!!glennmcc: end
}
// ----------------------------------------------- Last-modified:
else if(!strcmpi(str,"Last-modified"))
{
cache->dynamic=0;
}
// ----------------------------------------------- Connection:
else if((!strcmpi(str,"Connection") || !strcmpi(str,"Proxy-Connection"))
&& !strncmpi(&ptr[1+space],"Keep-Alive",10))
{
willkeepalive=1;
}
// ----------------------------------------------- Set-Cookie:
//!!Ray: Dec 18, 2007 -- some servers say simply 'cookie' instead
else if((!strcmpi(str,"Set-Cookie") || !strcmpi(str,"Cookie"))
&& http_parameters.acceptcookies)
// else if(!strcmpi(str,"Set-Cookie") && http_parameters.acceptcookies)
//!!Ray: end
{
//!!JdS 2004/2/15 {
// char *pom1=NULL,*pom2=NULL,*p,*newcookie=NULL;
char *p;
//!!JdS 2004/2/15 }
char domain[80],path[80];
outs(&ptr[2]);
//!!JdS 2004/2/15 {
// // Allow for independent control of pom* & newcookie size [JdS 2004/1/17]
// #define POMSIZE IE_MAXLEN
// #define COOKIESIZE IE_MAXLEN
// pom1=farmalloc(POMSIZE); // Was (IE_MAXLEN) [JdS 2004/1/17]
// pom2=farmalloc(POMSIZE); // Was (IE_MAXLEN) [JdS 2004/1/17]
// newcookie=farmalloc(COOKIESIZE); // Was (IE_MAXLEN) [JdS 2004/1/17]
// if(!pom1 || !pom2 || !newcookie)
// memerr();
//
// makestr(pom1,&ptr[2],POMSIZE-1); // Was (,,IE_MAXLEN-1) [JdS 2004/1/17]
// strcpy(newcookie,pom1); //its safe to call strcpy
// decompose_inetstr(pom1);
makestr(cookiestr,&ptr[2],MAXARGBUF-1);
decompose_inetstr(&ptr[2]);
//!!JdS 2004/2/15 }
if(!getarg("path",&p)) // getarg() was getvar() [JdS 2004/1/30]
{
//p=url->file;
//!!JdS 2004/2/15 {
// joinstr(newcookie,COOKIESIZE,"; path=/"); // Was strcat() [JdS 2004/1/17]
joinstr(cookiestr,MAXARGBUF,"; path=/");
//!!JdS 2004/2/15 }
}
makestr(path,p,79);
if(!getarg("domain",&p)) // getarg() was getvar() [JdS 2004/1/30]
{
//!!JdS 2004/2/15 {
// joinstr(newcookie,COOKIESIZE,"; domain="); // Was strcat() [JdS 2004/1/17]
joinstr(cookiestr,MAXARGBUF,"; domain=");
//!!JdS 2004/2/15 }
if(GLOBAL.redirection)
{
struct Url newurl;
AnalyseURL(GLOBAL.location,&newurl,GLOBAL_LOCATION_AS_BASEURL);
makestr(domain,newurl.host,79);
p=domain;
}
else
p=url->host;
//!!JdS 2004/2/15 {
// joinstr(newcookie,COOKIESIZE,p); // Was strcat() [JdS 2004/1/17]
joinstr(cookiestr,MAXARGBUF,p);
//!!JdS 2004/2/15 }
}
if(p!=domain)
makestr(domain,p,79);
/**** Start of superseded code [JdS 2004/3/3] ****
cookies.y=0;
while(cookies.y<cookies.lines)
{
//!!JdS 2004/2/15 {
// strcpy(pom2,ie_getline(&cookies,cookies.y));
// decompose_inetstr(pom2);
strcpy(str,ie_getline(&cookies,cookies.y));
decompose_inetstr(str);
//!!JdS 2004/2/15 }
getarg("domain",&p); // getarg() was getvar() [JdS 2004/1/30]
if(strstr(domain,p) && getarg("path",&p)) // getarg() was getvar() [JdS 2004/1/30]
if(!strcmp(path,p))
{
//!!JdS 2004/2/15 {
// p=strchr(pom2,'=');
// if(p && !strncmp(newcookie,pom2,(int)(p-pom2)))
p=strchr(str,'=');
if(p && !strncmp(cookiestr,str,(int)(p-str)))
//!!JdS 2004/2/15 }
{
//replace old cookie with new cookie:
ie_delline(&cookies,cookies.y);
//!!JdS 2004/2/15 {
// ie_insline(&cookies,cookies.y,newcookie);
ie_insline(&cookies,cookies.y,cookiestr);
//!!JdS 2004/2/15 }
goto cont;
}
}
cookies.y++;
} //while
if(cookies.lines==cookies.maxlines)
ie_delline(&cookies,0);
//!!JdS 2004/2/15 {
// ie_insline(&cookies,cookies.lines,newcookie);
ie_insline(&cookies,cookies.lines,cookiestr);
//!!JdS 2004/2/15 }
**** End of superseded code [JdS 2004/3/3] ****/