This repository has been archived by the owner on Dec 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
http.c
886 lines (771 loc) · 18.2 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
/* $Id$ */
/*
* Copyright (c) 2016 Kristaps Dzonsons <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <arpa/inet.h>
#if TLS_API < 20160801
# include <sys/stat.h>
# include <sys/mman.h>
#endif
#include <assert.h>
#include <ctype.h>
#include <err.h>
#if TLS_API < 20160801
# include <fcntl.h>
#endif
#include <limits.h>
#include <netdb.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <tls.h>
#include <unistd.h>
#include "http.h"
#include "extern.h"
#ifndef DEFAULT_CA_FILE
# define DEFAULT_CA_FILE "/etc/ssl/cert.pem"
#endif
/*
* A buffer for transferring HTTP/S data.
*/
struct httpxfer {
char *hbuf; /* header transfer buffer */
size_t hbufsz; /* header buffer size */
int headok; /* header has been parsed */
char *bbuf; /* body transfer buffer */
size_t bbufsz; /* body buffer size */
int bodyok; /* body has been parsed */
char *headbuf; /* lookaside buffer for headers */
struct httphead *head; /* parsed headers */
size_t headsz; /* number of headers */
};
/*
* An HTTP/S connection object.
*/
struct http {
int fd; /* connected socket */
short port; /* port number */
struct source src; /* endpoint (raw) host */
char *path; /* path to request */
char *host; /* name of endpoint host */
struct tls *ctx; /* if TLS */
writefp writer; /* write function */
readfp reader; /* read function */
};
struct httpcfg {
struct tls_config *tlscfg;
};
static ssize_t
dosysread(char *buf, size_t sz, const struct http *http)
{
ssize_t rc;
rc = read(http->fd, buf, sz);
if (rc < 0)
warn("%s: read", http->src.ip);
return (rc);
}
static ssize_t
dosyswrite(const void *buf, size_t sz, const struct http *http)
{
ssize_t rc;
rc = write(http->fd, buf, sz);
if (rc < 0)
warn("%s: write", http->src.ip);
return(rc);
}
static ssize_t
dotlsread(char *buf, size_t sz, const struct http *http)
{
ssize_t rc;
do {
rc = tls_read(http->ctx, buf, sz);
} while (TLS_WANT_POLLIN == rc || TLS_WANT_POLLOUT == rc);
if (rc < 0)
warnx("%s: tls_read: %s",
http->src.ip,
tls_error(http->ctx));
return (rc);
}
static ssize_t
dotlswrite(const void *buf, size_t sz, const struct http *http)
{
ssize_t rc;
do {
rc = tls_write(http->ctx, buf, sz);
} while (TLS_WANT_POLLIN == rc || TLS_WANT_POLLOUT == rc);
if (rc < 0)
warnx("%s: tls_write: %s",
http->src.ip,
tls_error(http->ctx));
return (rc);
}
/*
* Free the resources of an http_init() object.
*/
void
http_uninit(struct httpcfg *p)
{
if (NULL == p)
return;
if (NULL != p->tlscfg)
tls_config_free(p->tlscfg);
free(p);
}
/*
* Work around the "lazy-loading" problem in earlier versions of libtls.
* In these systems, using tls_config_set_ca_file() would only cause the
* file location to be copied.
* It would then be used after the pledge and/or chroot.
* We work around this by loading the file directly.
*/
#if TLS_API < 20160801
static int
http_config_set_ca_file(struct httpcfg *p)
{
int fd, rc = 0;
struct stat st;
void *buf;
if (-1 == (fd = open(DEFAULT_CA_FILE, O_RDONLY, 0))) {
warn("%s", DEFAULT_CA_FILE);
return(0);
} else if (-1 == fstat(fd, &st)) {
warn("%s", DEFAULT_CA_FILE);
close(fd);
return(0);
}
buf = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (MAP_FAILED == buf) {
warn("%s", DEFAULT_CA_FILE);
close(fd);
return(0);
}
if (-1 == tls_config_set_ca_mem(p->tlscfg, buf, st.st_size))
warn("%s: tls_config_set_ca_file", DEFAULT_CA_FILE);
else
rc = 1;
munmap(buf, st.st_size);
close(fd);
return(rc);
}
#endif
/*
* This function allocates a configuration shared among multiple
* connections.
* It will generally be called once, then used in a series of
* connections.
* Returns the configuration object or NULL on errors.
* A returned object must be freed with http_deinit().
*/
struct httpcfg *
http_init(void)
{
struct httpcfg *p;
/* Can be called more than once. */
if (-1 == tls_init()) {
warn("tls_init");
return (NULL);
}
if (NULL == (p = malloc(sizeof(struct httpcfg)))) {
warn("calloc");
return (NULL);
} else if (NULL == (p->tlscfg = tls_config_new())) {
warn("tls_config_new");
goto err;
}
tls_config_set_protocols(p->tlscfg, TLS_PROTOCOLS_ALL);
#if TLS_API < 20160801
if ( ! http_config_set_ca_file(p))
goto err;
#else
if (-1 == tls_config_set_ca_file(p->tlscfg, DEFAULT_CA_FILE)) {
warn("tls_config_set_ca_file: %s",
tls_config_error(p->tlscfg));
goto err;
}
#endif
if (-1 == tls_config_set_ciphers(p->tlscfg, "compat")) {
#if TLS_API < 20160801
warn("tls_config_set_ciphers");
#else
warn("tls_config_set_ciphers: %s",
tls_config_error(p->tlscfg));
#endif
goto err;
}
return (p);
err:
http_uninit(p);
return (NULL);
}
static ssize_t
http_read(char *buf, size_t sz, const struct http *http)
{
ssize_t ssz, xfer;
xfer = 0;
do {
if ((ssz = http->reader(buf, sz, http)) < 0)
return (-1);
if (0 == ssz)
break;
xfer += ssz;
sz -= ssz;
buf += ssz;
} while (ssz > 0 && sz > 0);
return (xfer);
}
static int
http_write(const char *buf, size_t sz, const struct http *http)
{
ssize_t ssz, xfer;
xfer = sz;
while (sz > 0) {
if ((ssz = http->writer(buf, sz, http)) < 0)
return (-1);
sz -= ssz;
buf += (size_t)ssz;
}
return (xfer);
}
void
http_disconnect(struct http *http)
{
int rc;
if (NULL != http->ctx) {
do {
rc = tls_close(http->ctx);
} while (TLS_WANT_POLLIN == rc || TLS_WANT_POLLOUT == rc);
if (rc < 0)
warnx("%s: tls_close: %s",
http->src.ip, tls_error(http->ctx));
if (-1 == close(http->fd))
warn("%s: close", http->src.ip);
tls_free(http->ctx);
} else if (-1 != http->fd) {
/* Non-TLS connection. */
if (-1 == close(http->fd))
warn("%s: close", http->src.ip);
}
http->fd = -1;
http->ctx = NULL;
}
void
http_free(struct http *http)
{
if (NULL == http)
return;
http_disconnect(http);
free(http->host);
free(http->path);
free(http->src.ip);
free(http);
}
struct http *
http_alloc(struct httpcfg *cfg,
const struct source *addrs, size_t addrsz,
const char *host, short port, const char *path)
{
struct sockaddr_storage ss;
int family, fd, c;
socklen_t len;
size_t cur, i = 0;
struct http *http;
/* Do this while we still have addresses to connect. */
again:
if (i == addrsz)
return (NULL);
cur = i++;
/* Convert to PF_INET or PF_INET6 address from string. */
memset(&ss, 0, sizeof(struct sockaddr_storage));
if (4 == addrs[cur].family) {
family = PF_INET;
((struct sockaddr_in *)&ss)->sin_family = AF_INET;
((struct sockaddr_in *)&ss)->sin_port = htons(port);
c = inet_pton(AF_INET, addrs[cur].ip,
&((struct sockaddr_in *)&ss)->sin_addr);
len = sizeof(struct sockaddr_in);
} else if (6 == addrs[cur].family) {
family = PF_INET6;
((struct sockaddr_in6 *)&ss)->sin6_family = AF_INET6;
((struct sockaddr_in6 *)&ss)->sin6_port = htons(port);
c = inet_pton(AF_INET6, addrs[cur].ip,
&((struct sockaddr_in6 *)&ss)->sin6_addr);
len = sizeof(struct sockaddr_in6);
} else {
warnx("%s: unknown family", addrs[cur].ip);
goto again;
}
if (c < 0) {
warn("%s: inet_ntop", addrs[cur].ip);
goto again;
} else if (0 == c) {
warnx("%s: inet_ntop", addrs[cur].ip);
goto again;
}
/* Create socket and connect. */
fd = socket(family, SOCK_STREAM, 0);
if (-1 == fd) {
warn("%s: socket", addrs[cur].ip);
goto again;
} else if (-1 == connect(fd, (struct sockaddr *)&ss, len)) {
warn("%s: connect", addrs[cur].ip);
close(fd);
goto again;
}
/* Allocate the communicator. */
http = calloc(1, sizeof(struct http));
if (NULL == http) {
warn("calloc");
close(fd);
return (NULL);
}
http->fd = fd;
http->port = port;
http->src.family = addrs[cur].family;
http->src.ip = strdup(addrs[cur].ip);
http->host = strdup(host);
http->path = strdup(path);
if (NULL == http->src.ip ||
NULL == http->host ||
NULL == http->path) {
warn("strdup");
goto err;
}
/* If necessary, do our TLS setup. */
if (443 != port) {
http->writer = dosyswrite;
http->reader = dosysread;
return (http);
}
http->writer = dotlswrite;
http->reader = dotlsread;
if (NULL == (http->ctx = tls_client())) {
warn("tls_client");
goto err;
} else if (-1 == tls_configure(http->ctx, cfg->tlscfg)) {
warnx("%s: tls_configure: %s",
http->src.ip, tls_error(http->ctx));
goto err;
}
if (0 != tls_connect_socket
(http->ctx, http->fd, http->host)) {
warnx("%s: tls_connect_socket: %s, %s",
http->src.ip, http->host,
tls_error(http->ctx));
goto err;
}
return (http);
err:
http_free(http);
return (NULL);
}
struct httpxfer *
http_open(const struct http *http, const void *p, size_t psz)
{
char *req;
int c;
struct httpxfer *trans;
if (NULL == p) {
c = asprintf(&req,
"GET %s HTTP/1.0\r\n"
"Host: %s\r\n"
"\r\n",
http->path, http->host);
} else {
c = asprintf(&req,
"POST %s HTTP/1.0\r\n"
"Host: %s\r\n"
"Content-Length: %zu\r\n"
"\r\n",
http->path, http->host, psz);
}
if (-1 == c) {
warn("asprintf");
return (NULL);
} else if ( ! http_write(req, c, http)) {
free(req);
return (NULL);
} else if (NULL != p && ! http_write(p, psz, http)) {
free(req);
return (NULL);
}
free(req);
trans = calloc(1, sizeof(struct httpxfer));
if (NULL == trans)
warn("calloc");
return (trans);
}
void
http_close(struct httpxfer *x)
{
if (NULL == x)
return;
free(x->hbuf);
free(x->bbuf);
free(x->headbuf);
free(x->head);
free(x);
}
/*
* Read the HTTP body from the wire.
* If invoked multiple times, this will return the same pointer with the
* same data (or NULL, if the original invocation returned NULL).
* Returns NULL if read or allocation errors occur.
* You must not free the returned pointer.
*/
char *
http_body_read(const struct http *http,
struct httpxfer *trans, size_t *sz)
{
char buf[BUFSIZ];
ssize_t ssz;
void *pp;
size_t szp;
if (NULL == sz)
sz = &szp;
/* Have we already parsed this? */
if (trans->bodyok > 0) {
*sz = trans->bbufsz;
return (trans->bbuf);
} else if (trans->bodyok < 0)
return (NULL);
*sz = 0;
trans->bodyok = -1;
do {
/* If less than sizeof(buf), at EOF. */
if ((ssz = http_read(buf, sizeof(buf), http)) < 0)
return (NULL);
else if (0 == ssz)
break;
pp = realloc(trans->bbuf, trans->bbufsz + ssz);
if (NULL == pp) {
warn("realloc");
return (NULL);
}
trans->bbuf = pp;
memcpy(trans->bbuf + trans->bbufsz, buf, ssz);
trans->bbufsz += ssz;
} while (sizeof(buf) == ssz);
trans->bodyok = 1;
*sz = trans->bbufsz;
return (trans->bbuf);
}
struct httphead *
http_head_get(const char *v, struct httphead *h, size_t hsz)
{
size_t i;
for (i = 0; i < hsz; i++) {
if (strcmp(h[i].key, v))
continue;
return (&h[i]);
}
return (NULL);
}
/*
* Look through the headers and determine our HTTP code.
* This will return -1 on failure, otherwise the code.
*/
int
http_head_status(const struct http *http,
struct httphead *h, size_t sz)
{
int rc;
unsigned int code;
struct httphead *st;
if (NULL == (st = http_head_get("Status", h, sz))) {
warnx("%s: no status header", http->src.ip);
return (-1);
}
rc = sscanf(st->val, "%*s %u %*s", &code);
if (rc < 0) {
warn("sscanf");
return (-1);
} else if (1 != rc) {
warnx("%s: cannot convert status header",
http->src.ip);
return (-1);
}
return (code);
}
/*
* Parse headers from the transfer.
* Malformed headers are skipped.
* A special "Status" header is added for the HTTP status line.
* This can only happen once http_head_read has been called with
* success.
* This can be invoked multiple times: it will only parse the headers
* once and after that it will just return the cache.
* You must not free the returned pointer.
* If the original header parse failed, or if memory allocation fails
* internally, this returns NULL.
*/
struct httphead *
http_head_parse(const struct http *http,
struct httpxfer *trans, size_t *sz)
{
size_t hsz, szp;
struct httphead *h;
char *cp, *ep, *ccp, *buf;
if (NULL == sz)
sz = &szp;
/*
* If we've already parsed the headers, return the
* previously-parsed buffer now.
* If we have errors on the stream, return NULL now.
*/
if (NULL != trans->head) {
*sz = trans->headsz;
return (trans->head);
} else if (trans->headok <= 0)
return (NULL);
if (NULL == (buf = strdup(trans->hbuf))) {
warn("strdup");
return (NULL);
}
hsz = 0;
cp = buf;
do {
if (NULL != (cp = strstr(cp, "\r\n")))
cp += 2;
hsz++;
} while (NULL != cp);
/*
* Allocate headers, then step through the data buffer, parsing
* out headers as we have them.
* We know at this point that the buffer is nil-terminated in
* the usual way.
*/
h = calloc(hsz, sizeof(struct httphead));
if (NULL == h) {
warn("calloc");
free(buf);
return (NULL);
}
*sz = hsz;
hsz = 0;
cp = buf;
do {
if (NULL != (ep = strstr(cp, "\r\n"))) {
*ep = '\0';
ep += 2;
}
if (0 == hsz) {
h[hsz].key = "Status";
h[hsz++].val = cp;
continue;
}
/* Skip bad headers. */
if (NULL == (ccp = strchr(cp, ':'))) {
warnx("%s: header without separator",
http->src.ip);
continue;
}
*ccp++ = '\0';
while (isspace((int)*ccp))
ccp++;
h[hsz].key = cp;
h[hsz++].val = ccp;
} while (NULL != (cp = ep));
trans->headbuf = buf;
trans->head = h;
trans->headsz = hsz;
return (h);
}
/*
* Read the HTTP headers from the wire.
* If invoked multiple times, this will return the same pointer with the
* same data (or NULL, if the original invocation returned NULL).
* Returns NULL if read or allocation errors occur.
* You must not free the returned pointer.
*/
char *
http_head_read(const struct http *http,
struct httpxfer *trans, size_t *sz)
{
char buf[BUFSIZ];
ssize_t ssz;
char *ep;
void *pp;
size_t szp;
if (NULL == sz)
sz = &szp;
/* Have we already parsed this? */
if (trans->headok > 0) {
*sz = trans->hbufsz;
return (trans->hbuf);
} else if (trans->headok < 0)
return (NULL);
*sz = 0;
ep = NULL;
trans->headok = -1;
/*
* Begin by reading by BUFSIZ blocks until we reach the header
* termination marker (two CRLFs).
* We might read into our body, but that's ok: we'll copy out
* the body parts into our body buffer afterward.
*/
do {
/* If less than sizeof(buf), at EOF. */
if ((ssz = http_read(buf, sizeof(buf), http)) < 0)
return (NULL);
else if (0 == ssz)
break;
pp = realloc(trans->hbuf, trans->hbufsz + ssz);
if (NULL == pp) {
warn("realloc");
return (NULL);
}
trans->hbuf = pp;
memcpy(trans->hbuf + trans->hbufsz, buf, ssz);
trans->hbufsz += ssz;
/* Search for end of headers marker. */
ep = memmem(trans->hbuf, trans->hbufsz, "\r\n\r\n", 4);
} while (NULL == ep && sizeof(buf) == ssz);
if (NULL == ep) {
warnx("%s: partial transfer", http->src.ip);
return (NULL);
}
*ep = '\0';
/*
* The header data is invalid if it has any binary characters in
* it: check that now.
* This is important because we want to guarantee that all
* header keys and pairs are properly nil-terminated.
*/
if (strlen(trans->hbuf) != (uintptr_t)(ep - trans->hbuf)) {
warnx("%s: binary data in header", http->src.ip);
return (NULL);
}
/*
* Copy remaining buffer into body buffer.
*/
ep += 4;
trans->bbufsz = (trans->hbuf + trans->hbufsz) - ep;
trans->bbuf = malloc(trans->bbufsz);
if (NULL == trans->bbuf) {
warn("malloc");
return (NULL);
}
memcpy(trans->bbuf, ep, trans->bbufsz);
trans->headok = 1;
*sz = trans->hbufsz;
return (trans->hbuf);
}
void
http_get_free(struct httpget *g)
{
if (NULL == g)
return;
http_close(g->xfer);
http_free(g->http);
free(g);
}
struct httpget *
http_get(struct httpcfg *cfg,
const struct source *addrs, size_t addrsz,
const char *domain, short port, const char *path,
const void *post, size_t postsz)
{
struct http *h;
struct httpxfer *x;
struct httpget *g;
struct httphead *head;
size_t headsz, bodsz, headrsz;
int code;
char *bod, *headr;
h = http_alloc(cfg, addrs, addrsz, domain, port, path);
if (NULL == h)
return (NULL);
if (NULL == (x = http_open(h, post, postsz))) {
http_free(h);
return (NULL);
} else if (NULL == (headr = http_head_read(h, x, &headrsz))) {
http_close(x);
http_free(h);
return (NULL);
} else if (NULL == (bod = http_body_read(h, x, &bodsz))) {
http_close(x);
http_free(h);
return (NULL);
}
http_disconnect(h);
if (NULL == (head = http_head_parse(h, x, &headsz))) {
http_close(x);
http_free(h);
return (NULL);
} else if ((code = http_head_status(h, head, headsz)) < 0) {
http_close(x);
http_free(h);
return (NULL);
}
if (NULL == (g = calloc(1, sizeof(struct httpget)))) {
warn("calloc");
http_close(x);
http_free(h);
return (NULL);
}
g->headpart = headr;
g->headpartsz = headrsz;
g->bodypart = bod;
g->bodypartsz = bodsz;
g->head = head;
g->headsz = headsz;
g->code = code;
g->xfer = x;
g->http = h;
return (g);
}
#if 0
int
main(void)
{
struct httpget *g;
struct httphead *httph;
size_t i, httphsz;
struct source addrs[2];
size_t addrsz;
#if 0
addrs[0].ip = "127.0.0.1";
addrs[0].family = 4;
addrsz = 1;
#else
addrs[0].ip = "2a00:1450:400a:806::2004";
addrs[0].family = 6;
addrs[1].ip = "193.135.3.123";
addrs[1].family = 4;
addrsz = 2;
#endif
#if 0
g = http_get(addrs, addrsz, "localhost", 80, "/index.html");
#else
g = http_get(addrs, addrsz, "www.google.ch", 80, "/index.html",
NULL, 0);
#endif
if (NULL == g)
errx(EXIT_FAILURE, "http_get");
httph = http_head_parse(g->http, g->xfer, &httphsz);
warnx("code: %d", g->code);
for (i = 0; i < httphsz; i++)
warnx("head: [%s]=[%s]", httph[i].key, httph[i].val);
http_get_free(g);
return (EXIT_SUCCESS);
}
#endif