-
Notifications
You must be signed in to change notification settings - Fork 0
/
nanohttp.c
1875 lines (1671 loc) · 46.3 KB
/
nanohttp.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
/*
* nanohttp.c: minimalist HTTP GET implementation to fetch external subsets.
* focuses on size, streamability, reentrancy and portability
*
* This is clearly not a general purpose HTTP implementation
* If you look for one, check:
* http://www.w3.org/Library/
*
* See Copyright for the status of this software.
*
*/
#define NEED_SOCKETS
#define IN_LIBXML
#include "libxml.h"
#ifdef LIBXML_HTTP_ENABLED
#include <string.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_RESOLV_H
#ifdef HAVE_ARPA_NAMESER_H
#include <arpa/nameser.h>
#endif
#include <resolv.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifndef HAVE_POLL_H
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#else
#include <poll.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#ifdef SUPPORT_IP6
#include <resolv.h>
#endif
#ifdef HAVE_ZLIB_H
#include <zlib.h>
#endif
#ifdef VMS
#include <stropts>
#define XML_SOCKLEN_T unsigned int
#define SOCKET int
#endif
#if defined(__MINGW32__) || defined(_WIN32_WCE)
#define _WINSOCKAPI_
#include <wsockcompat.h>
#include <winsock2.h>
#undef XML_SOCKLEN_T
#define XML_SOCKLEN_T unsigned int
#endif
#include <libxml/globals.h>
#include <libxml/xmlerror.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h> /* for xmlStr(n)casecmp() */
#include <libxml/nanohttp.h>
#include <libxml/globals.h>
#include <libxml/uri.h>
/**
* A couple portability macros
*/
#ifndef _WINSOCKAPI_
#if !defined(__BEOS__) || defined(__HAIKU__)
#define closesocket(s) close(s)
#endif
#define SOCKET int
#endif
#ifdef __BEOS__
#ifndef PF_INET
#define PF_INET AF_INET
#endif
#endif
#ifndef XML_SOCKLEN_T
#define XML_SOCKLEN_T unsigned int
#endif
#ifndef SOCKET
#define SOCKET int
#endif
#ifdef STANDALONE
#define DEBUG_HTTP
#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n)
#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b)
#endif
#define XML_NANO_HTTP_MAX_REDIR 10
#define XML_NANO_HTTP_CHUNK 4096
#define XML_NANO_HTTP_CLOSED 0
#define XML_NANO_HTTP_WRITE 1
#define XML_NANO_HTTP_READ 2
#define XML_NANO_HTTP_NONE 4
typedef struct xmlNanoHTTPCtxt {
char *protocol; /* the protocol name */
char *hostname; /* the host name */
int port; /* the port */
char *path; /* the path within the URL */
char *query; /* the query string */
SOCKET fd; /* the file descriptor for the socket */
int state; /* WRITE / READ / CLOSED */
char *out; /* buffer sent (zero terminated) */
char *outptr; /* index within the buffer sent */
char *in; /* the receiving buffer */
char *content; /* the start of the content */
char *inptr; /* the next byte to read from network */
char *inrptr; /* the next byte to give back to the client */
int inlen; /* len of the input buffer */
int last; /* return code for last operation */
int returnValue; /* the protocol return value */
int version; /* the protocol version */
int ContentLength; /* specified content length from HTTP header */
char *contentType; /* the MIME type for the input */
char *location; /* the new URL in case of redirect */
char *authHeader; /* contents of {WWW,Proxy}-Authenticate header */
char *encoding; /* encoding extracted from the contentType */
char *mimeType; /* Mime-Type extracted from the contentType */
#ifdef HAVE_ZLIB_H
z_stream *strm; /* Zlib stream object */
int usesGzip; /* "Content-Encoding: gzip" was detected */
#endif
} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr;
static int initialized = 0;
static char *proxy = NULL; /* the proxy name if any */
static int proxyPort; /* the proxy port if any */
static unsigned int timeout = 60;/* the select() timeout in seconds */
static int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len );
/**
* xmlHTTPErrMemory:
* @extra: extra informations
*
* Handle an out of memory condition
*/
static void
xmlHTTPErrMemory(const char *extra)
{
__xmlSimpleError(XML_FROM_HTTP, XML_ERR_NO_MEMORY, NULL, NULL, extra);
}
/**
* A portability function
*/
static int socket_errno(void) {
#ifdef _WINSOCKAPI_
return(WSAGetLastError());
#else
return(errno);
#endif
}
#ifdef SUPPORT_IP6
static
int have_ipv6(void) {
int s;
s = socket (AF_INET6, SOCK_STREAM, 0);
if (s != -1) {
close (s);
return (1);
}
return (0);
}
#endif
/**
* xmlNanoHTTPInit:
*
* Initialize the HTTP protocol layer.
* Currently it just checks for proxy informations
*/
void
xmlNanoHTTPInit(void) {
const char *env;
#ifdef _WINSOCKAPI_
WSADATA wsaData;
#endif
if (initialized)
return;
#ifdef _WINSOCKAPI_
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
return;
#endif
if (proxy == NULL) {
proxyPort = 80;
env = getenv("no_proxy");
if (env && ((env[0] == '*') && (env[1] == 0)))
goto done;
env = getenv("http_proxy");
if (env != NULL) {
xmlNanoHTTPScanProxy(env);
goto done;
}
env = getenv("HTTP_PROXY");
if (env != NULL) {
xmlNanoHTTPScanProxy(env);
goto done;
}
}
done:
initialized = 1;
}
/**
* xmlNanoHTTPCleanup:
*
* Cleanup the HTTP protocol layer.
*/
void
xmlNanoHTTPCleanup(void) {
if (proxy != NULL) {
xmlFree(proxy);
proxy = NULL;
}
#ifdef _WINSOCKAPI_
if (initialized)
WSACleanup();
#endif
initialized = 0;
return;
}
/**
* xmlNanoHTTPScanURL:
* @ctxt: an HTTP context
* @URL: The URL used to initialize the context
*
* (Re)Initialize an HTTP context by parsing the URL and finding
* the protocol host port and path it indicates.
*/
static void
xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ctxt, const char *URL) {
xmlURIPtr uri;
/*
* Clear any existing data from the context
*/
if (ctxt->protocol != NULL) {
xmlFree(ctxt->protocol);
ctxt->protocol = NULL;
}
if (ctxt->hostname != NULL) {
xmlFree(ctxt->hostname);
ctxt->hostname = NULL;
}
if (ctxt->path != NULL) {
xmlFree(ctxt->path);
ctxt->path = NULL;
}
if (ctxt->query != NULL) {
xmlFree(ctxt->query);
ctxt->query = NULL;
}
if (URL == NULL) return;
uri = xmlParseURIRaw(URL, 1);
if (uri == NULL)
return;
if ((uri->scheme == NULL) || (uri->server == NULL)) {
xmlFreeURI(uri);
return;
}
ctxt->protocol = xmlMemStrdup(uri->scheme);
ctxt->hostname = xmlMemStrdup(uri->server);
if (uri->path != NULL)
ctxt->path = xmlMemStrdup(uri->path);
else
ctxt->path = xmlMemStrdup("/");
if (uri->query != NULL)
ctxt->query = xmlMemStrdup(uri->query);
if (uri->port != 0)
ctxt->port = uri->port;
xmlFreeURI(uri);
}
/**
* xmlNanoHTTPScanProxy:
* @URL: The proxy URL used to initialize the proxy context
*
* (Re)Initialize the HTTP Proxy context by parsing the URL and finding
* the protocol host port it indicates.
* Should be like http://myproxy/ or http://myproxy:3128/
* A NULL URL cleans up proxy informations.
*/
void
xmlNanoHTTPScanProxy(const char *URL) {
xmlURIPtr uri;
if (proxy != NULL) {
xmlFree(proxy);
proxy = NULL;
}
proxyPort = 0;
#ifdef DEBUG_HTTP
if (URL == NULL)
xmlGenericError(xmlGenericErrorContext,
"Removing HTTP proxy info\n");
else
xmlGenericError(xmlGenericErrorContext,
"Using HTTP proxy %s\n", URL);
#endif
if (URL == NULL) return;
uri = xmlParseURIRaw(URL, 1);
if ((uri == NULL) || (uri->scheme == NULL) ||
(strcmp(uri->scheme, "http")) || (uri->server == NULL)) {
__xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Syntax Error\n");
if (uri != NULL)
xmlFreeURI(uri);
return;
}
proxy = xmlMemStrdup(uri->server);
if (uri->port != 0)
proxyPort = uri->port;
xmlFreeURI(uri);
}
/**
* xmlNanoHTTPNewCtxt:
* @URL: The URL used to initialize the context
*
* Allocate and initialize a new HTTP context.
*
* Returns an HTTP context or NULL in case of error.
*/
static xmlNanoHTTPCtxtPtr
xmlNanoHTTPNewCtxt(const char *URL) {
xmlNanoHTTPCtxtPtr ret;
ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt));
if (ret == NULL) {
xmlHTTPErrMemory("allocating context");
return(NULL);
}
memset(ret, 0, sizeof(xmlNanoHTTPCtxt));
ret->port = 80;
ret->returnValue = 0;
ret->fd = -1;
ret->ContentLength = -1;
xmlNanoHTTPScanURL(ret, URL);
return(ret);
}
/**
* xmlNanoHTTPFreeCtxt:
* @ctxt: an HTTP context
*
* Frees the context after closing the connection.
*/
static void
xmlNanoHTTPFreeCtxt(xmlNanoHTTPCtxtPtr ctxt) {
if (ctxt == NULL) return;
if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
if (ctxt->path != NULL) xmlFree(ctxt->path);
if (ctxt->query != NULL) xmlFree(ctxt->query);
if (ctxt->out != NULL) xmlFree(ctxt->out);
if (ctxt->in != NULL) xmlFree(ctxt->in);
if (ctxt->contentType != NULL) xmlFree(ctxt->contentType);
if (ctxt->encoding != NULL) xmlFree(ctxt->encoding);
if (ctxt->mimeType != NULL) xmlFree(ctxt->mimeType);
if (ctxt->location != NULL) xmlFree(ctxt->location);
if (ctxt->authHeader != NULL) xmlFree(ctxt->authHeader);
#ifdef HAVE_ZLIB_H
if (ctxt->strm != NULL) {
inflateEnd(ctxt->strm);
xmlFree(ctxt->strm);
}
#endif
ctxt->state = XML_NANO_HTTP_NONE;
if (ctxt->fd >= 0) closesocket(ctxt->fd);
ctxt->fd = -1;
xmlFree(ctxt);
}
/**
* xmlNanoHTTPSend:
* @ctxt: an HTTP context
*
* Send the input needed to initiate the processing on the server side
* Returns number of bytes sent or -1 on error.
*/
static int
xmlNanoHTTPSend(xmlNanoHTTPCtxtPtr ctxt, const char *xmt_ptr, int outlen)
{
int total_sent = 0;
#ifdef HAVE_POLL_H
struct pollfd p;
#else
struct timeval tv;
fd_set wfd;
#endif
if ((ctxt->state & XML_NANO_HTTP_WRITE) && (xmt_ptr != NULL)) {
while (total_sent < outlen) {
int nsent = send(ctxt->fd, xmt_ptr + total_sent,
outlen - total_sent, 0);
if (nsent > 0)
total_sent += nsent;
else if ((nsent == -1) &&
#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
(socket_errno() != EAGAIN) &&
#endif
(socket_errno() != EWOULDBLOCK)) {
__xmlIOErr(XML_FROM_HTTP, 0, "send failed\n");
if (total_sent == 0)
total_sent = -1;
break;
} else {
/*
* No data sent
* Since non-blocking sockets are used, wait for
* socket to be writable or default timeout prior
* to retrying.
*/
#ifndef HAVE_POLL_H
#ifndef _WINSOCKAPI_
if (ctxt->fd > FD_SETSIZE)
return -1;
#endif
tv.tv_sec = timeout;
tv.tv_usec = 0;
FD_ZERO(&wfd);
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4018)
#endif
FD_SET(ctxt->fd, &wfd);
#ifdef _MSC_VER
#pragma warning(pop)
#endif
(void) select(ctxt->fd + 1, NULL, &wfd, NULL, &tv);
#else
p.fd = ctxt->fd;
p.events = POLLOUT;
(void) poll(&p, 1, timeout * 1000);
#endif /* !HAVE_POLL_H */
}
}
}
return total_sent;
}
/**
* xmlNanoHTTPRecv:
* @ctxt: an HTTP context
*
* Read information coming from the HTTP connection.
* This is a blocking call (but it blocks in select(), not read()).
*
* Returns the number of byte read or -1 in case of error.
*/
static int
xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt)
{
#ifdef HAVE_POLL_H
struct pollfd p;
#else
fd_set rfd;
struct timeval tv;
#endif
while (ctxt->state & XML_NANO_HTTP_READ) {
if (ctxt->in == NULL) {
ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
if (ctxt->in == NULL) {
xmlHTTPErrMemory("allocating input");
ctxt->last = -1;
return (-1);
}
ctxt->inlen = 65000;
ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
}
if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
int delta = ctxt->inrptr - ctxt->in;
int len = ctxt->inptr - ctxt->inrptr;
memmove(ctxt->in, ctxt->inrptr, len);
ctxt->inrptr -= delta;
ctxt->content -= delta;
ctxt->inptr -= delta;
}
if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
int d_inptr = ctxt->inptr - ctxt->in;
int d_content = ctxt->content - ctxt->in;
int d_inrptr = ctxt->inrptr - ctxt->in;
char *tmp_ptr = ctxt->in;
ctxt->inlen *= 2;
ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
if (ctxt->in == NULL) {
xmlHTTPErrMemory("allocating input buffer");
xmlFree(tmp_ptr);
ctxt->last = -1;
return (-1);
}
ctxt->inptr = ctxt->in + d_inptr;
ctxt->content = ctxt->in + d_content;
ctxt->inrptr = ctxt->in + d_inrptr;
}
ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
if (ctxt->last > 0) {
ctxt->inptr += ctxt->last;
return (ctxt->last);
}
if (ctxt->last == 0) {
return (0);
}
if (ctxt->last == -1) {
switch (socket_errno()) {
case EINPROGRESS:
case EWOULDBLOCK:
#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
case EAGAIN:
#endif
break;
case ECONNRESET:
case ESHUTDOWN:
return (0);
default:
__xmlIOErr(XML_FROM_HTTP, 0, "recv failed\n");
return (-1);
}
}
#ifdef HAVE_POLL_H
p.fd = ctxt->fd;
p.events = POLLIN;
if ((poll(&p, 1, timeout * 1000) < 1)
#if defined(EINTR)
&& (errno != EINTR)
#endif
)
return (0);
#else /* !HAVE_POLL_H */
#ifndef _WINSOCKAPI_
if (ctxt->fd > FD_SETSIZE)
return 0;
#endif
tv.tv_sec = timeout;
tv.tv_usec = 0;
FD_ZERO(&rfd);
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4018)
#endif
FD_SET(ctxt->fd, &rfd);
#ifdef _MSC_VER
#pragma warning(pop)
#endif
if ((select(ctxt->fd + 1, &rfd, NULL, NULL, &tv) < 1)
#if defined(EINTR)
&& (errno != EINTR)
#endif
)
return (0);
#endif /* !HAVE_POLL_H */
}
return (0);
}
/**
* xmlNanoHTTPReadLine:
* @ctxt: an HTTP context
*
* Read one line in the HTTP server output, usually for extracting
* the HTTP protocol informations from the answer header.
*
* Returns a newly allocated string with a copy of the line, or NULL
* which indicate the end of the input.
*/
static char *
xmlNanoHTTPReadLine(xmlNanoHTTPCtxtPtr ctxt) {
char buf[4096];
char *bp = buf;
int rc;
while (bp - buf < 4095) {
if (ctxt->inrptr == ctxt->inptr) {
if ( (rc = xmlNanoHTTPRecv(ctxt)) == 0) {
if (bp == buf)
return(NULL);
else
*bp = 0;
return(xmlMemStrdup(buf));
}
else if ( rc == -1 ) {
return ( NULL );
}
}
*bp = *ctxt->inrptr++;
if (*bp == '\n') {
*bp = 0;
return(xmlMemStrdup(buf));
}
if (*bp != '\r')
bp++;
}
buf[4095] = 0;
return(xmlMemStrdup(buf));
}
/**
* xmlNanoHTTPScanAnswer:
* @ctxt: an HTTP context
* @line: an HTTP header line
*
* Try to extract useful informations from the server answer.
* We currently parse and process:
* - The HTTP revision/ return code
* - The Content-Type, Mime-Type and charset used
* - The Location for redirect processing.
*
* Returns -1 in case of failure, the file descriptor number otherwise
*/
static void
xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) {
const char *cur = line;
if (line == NULL) return;
if (!strncmp(line, "HTTP/", 5)) {
int version = 0;
int ret = 0;
cur += 5;
while ((*cur >= '0') && (*cur <= '9')) {
version *= 10;
version += *cur - '0';
cur++;
}
if (*cur == '.') {
cur++;
if ((*cur >= '0') && (*cur <= '9')) {
version *= 10;
version += *cur - '0';
cur++;
}
while ((*cur >= '0') && (*cur <= '9'))
cur++;
} else
version *= 10;
if ((*cur != ' ') && (*cur != '\t')) return;
while ((*cur == ' ') || (*cur == '\t')) cur++;
if ((*cur < '0') || (*cur > '9')) return;
while ((*cur >= '0') && (*cur <= '9')) {
ret *= 10;
ret += *cur - '0';
cur++;
}
if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return;
ctxt->returnValue = ret;
ctxt->version = version;
} else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) {
const xmlChar *charset, *last, *mime;
cur += 13;
while ((*cur == ' ') || (*cur == '\t')) cur++;
if (ctxt->contentType != NULL)
xmlFree(ctxt->contentType);
ctxt->contentType = xmlMemStrdup(cur);
mime = (const xmlChar *) cur;
last = mime;
while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
(*last != ';') && (*last != ','))
last++;
if (ctxt->mimeType != NULL)
xmlFree(ctxt->mimeType);
ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
if (charset != NULL) {
charset += 8;
last = charset;
while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
(*last != ';') && (*last != ','))
last++;
if (ctxt->encoding != NULL)
xmlFree(ctxt->encoding);
ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
}
} else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) {
const xmlChar *charset, *last, *mime;
cur += 12;
if (ctxt->contentType != NULL) return;
while ((*cur == ' ') || (*cur == '\t')) cur++;
ctxt->contentType = xmlMemStrdup(cur);
mime = (const xmlChar *) cur;
last = mime;
while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
(*last != ';') && (*last != ','))
last++;
if (ctxt->mimeType != NULL)
xmlFree(ctxt->mimeType);
ctxt->mimeType = (char *) xmlStrndup(mime, last - mime);
charset = xmlStrstr(BAD_CAST ctxt->contentType, BAD_CAST "charset=");
if (charset != NULL) {
charset += 8;
last = charset;
while ((*last != 0) && (*last != ' ') && (*last != '\t') &&
(*last != ';') && (*last != ','))
last++;
if (ctxt->encoding != NULL)
xmlFree(ctxt->encoding);
ctxt->encoding = (char *) xmlStrndup(charset, last - charset);
}
} else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) {
cur += 9;
while ((*cur == ' ') || (*cur == '\t')) cur++;
if (ctxt->location != NULL)
xmlFree(ctxt->location);
if (*cur == '/') {
xmlChar *tmp_http = xmlStrdup(BAD_CAST "http://");
xmlChar *tmp_loc =
xmlStrcat(tmp_http, (const xmlChar *) ctxt->hostname);
ctxt->location =
(char *) xmlStrcat (tmp_loc, (const xmlChar *) cur);
} else {
ctxt->location = xmlMemStrdup(cur);
}
} else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"WWW-Authenticate:", 17)) {
cur += 17;
while ((*cur == ' ') || (*cur == '\t')) cur++;
if (ctxt->authHeader != NULL)
xmlFree(ctxt->authHeader);
ctxt->authHeader = xmlMemStrdup(cur);
} else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Proxy-Authenticate:", 19)) {
cur += 19;
while ((*cur == ' ') || (*cur == '\t')) cur++;
if (ctxt->authHeader != NULL)
xmlFree(ctxt->authHeader);
ctxt->authHeader = xmlMemStrdup(cur);
#ifdef HAVE_ZLIB_H
} else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Encoding:", 17) ) {
cur += 17;
while ((*cur == ' ') || (*cur == '\t')) cur++;
if ( !xmlStrncasecmp( BAD_CAST cur, BAD_CAST"gzip", 4) ) {
ctxt->usesGzip = 1;
ctxt->strm = xmlMalloc(sizeof(z_stream));
if (ctxt->strm != NULL) {
ctxt->strm->zalloc = Z_NULL;
ctxt->strm->zfree = Z_NULL;
ctxt->strm->opaque = Z_NULL;
ctxt->strm->avail_in = 0;
ctxt->strm->next_in = Z_NULL;
inflateInit2( ctxt->strm, 31 );
}
}
#endif
} else if ( !xmlStrncasecmp( BAD_CAST line, BAD_CAST"Content-Length:", 15) ) {
cur += 15;
ctxt->ContentLength = strtol( cur, NULL, 10 );
}
}
/**
* xmlNanoHTTPConnectAttempt:
* @addr: a socket address structure
*
* Attempt a connection to the given IP:port endpoint. It forces
* non-blocking semantic on the socket, and allow 60 seconds for
* the host to answer.
*
* Returns -1 in case of failure, the file descriptor number otherwise
*/
static int
xmlNanoHTTPConnectAttempt(struct sockaddr *addr)
{
#ifndef HAVE_POLL_H
fd_set wfd;
#ifdef _WINSOCKAPI_
fd_set xfd;
#endif
struct timeval tv;
#else /* !HAVE_POLL_H */
struct pollfd p;
#endif /* !HAVE_POLL_H */
int status;
int addrlen;
SOCKET s;
#ifdef SUPPORT_IP6
if (addr->sa_family == AF_INET6) {
s = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
addrlen = sizeof(struct sockaddr_in6);
} else
#endif
{
s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
addrlen = sizeof(struct sockaddr_in);
}
if (s == -1) {
#ifdef DEBUG_HTTP
perror("socket");
#endif
__xmlIOErr(XML_FROM_HTTP, 0, "socket failed\n");
return (-1);
}
#ifdef _WINSOCKAPI_
{
u_long one = 1;
status = ioctlsocket(s, FIONBIO, &one) == SOCKET_ERROR ? -1 : 0;
}
#else /* _WINSOCKAPI_ */
#if defined(VMS)
{
int enable = 1;
status = ioctl(s, FIONBIO, &enable);
}
#else /* VMS */
#if defined(__BEOS__) && !defined(__HAIKU__)
{
bool noblock = true;
status =
setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &noblock,
sizeof(noblock));
}
#else /* __BEOS__ */
if ((status = fcntl(s, F_GETFL, 0)) != -1) {
#ifdef O_NONBLOCK
status |= O_NONBLOCK;
#else /* O_NONBLOCK */
#ifdef F_NDELAY
status |= F_NDELAY;
#endif /* F_NDELAY */
#endif /* !O_NONBLOCK */
status = fcntl(s, F_SETFL, status);
}
if (status < 0) {
#ifdef DEBUG_HTTP
perror("nonblocking");
#endif
__xmlIOErr(XML_FROM_HTTP, 0, "error setting non-blocking IO\n");
closesocket(s);
return (-1);
}
#endif /* !__BEOS__ */
#endif /* !VMS */
#endif /* !_WINSOCKAPI_ */
if (connect(s, addr, addrlen) == -1) {
switch (socket_errno()) {
case EINPROGRESS:
case EWOULDBLOCK:
break;
default:
__xmlIOErr(XML_FROM_HTTP, 0,
"error connecting to HTTP server");
closesocket(s);
return (-1);
}
}
#ifndef HAVE_POLL_H
tv.tv_sec = timeout;
tv.tv_usec = 0;
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4018)
#endif
#ifndef _WINSOCKAPI_
if (s > FD_SETSIZE)
return -1;
#endif
FD_ZERO(&wfd);
FD_SET(s, &wfd);
#ifdef _WINSOCKAPI_
FD_ZERO(&xfd);
FD_SET(s, &xfd);
switch (select(s + 1, NULL, &wfd, &xfd, &tv))
#else
switch (select(s + 1, NULL, &wfd, NULL, &tv))
#endif
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#else /* !HAVE_POLL_H */
p.fd = s;
p.events = POLLOUT;
switch (poll(&p, 1, timeout * 1000))
#endif /* !HAVE_POLL_H */
{
case 0:
/* Time out */
__xmlIOErr(XML_FROM_HTTP, 0, "Connect attempt timed out");
closesocket(s);
return (-1);
case -1:
/* Ermm.. ?? */
__xmlIOErr(XML_FROM_HTTP, 0, "Connect failed");
closesocket(s);
return (-1);
}
#ifndef HAVE_POLL_H
if (FD_ISSET(s, &wfd)
#ifdef _WINSOCKAPI_
|| FD_ISSET(s, &xfd)
#endif
)
#else /* !HAVE_POLL_H */
if (p.revents == POLLOUT)
#endif /* !HAVE_POLL_H */
{
XML_SOCKLEN_T len;
len = sizeof(status);
#ifdef SO_ERROR
if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char *) &status, &len) <
0) {
/* Solaris error code */
__xmlIOErr(XML_FROM_HTTP, 0, "getsockopt failed\n");