Include netinet/in.h for IP resolve types. Makes pazpar2 run happily
[pazpar2-moved-to-github.git] / src / http.c
1 /*
2  * $Id: http.c,v 1.9 2007-01-10 11:56:10 adam Exp $
3  */
4
5 #include <stdio.h>
6 #include <sys/socket.h>
7 #include <sys/types.h>
8 #include <sys/uio.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <strings.h>
12 #include <ctype.h>
13 #include <fcntl.h>
14 #include <netdb.h>
15 #include <errno.h>
16 #include <assert.h>
17
18 #if HAVE_CONFIG_H
19 #include <cconfig.h>
20 #endif
21
22 #include <netinet/in.h>
23
24 #include <yaz/yaz-util.h>
25 #include <yaz/comstack.h>
26 #include <netdb.h>
27
28 #include "util.h"
29 #include "eventl.h"
30 #include "pazpar2.h"
31 #include "http.h"
32 #include "http_command.h"
33
34 static void proxy_io(IOCHAN i, int event);
35 static struct http_channel *http_create(void);
36 static void http_destroy(IOCHAN i);
37
38 extern IOCHAN channel_list;
39
40 static struct sockaddr_in *proxy_addr = 0; // If this is set, we proxy normal HTTP requests
41 static char proxy_url[256] = "";
42 static struct http_buf *http_buf_freelist = 0;
43 static struct http_channel *http_channel_freelist = 0;
44
45 static struct http_buf *http_buf_create()
46 {
47     struct http_buf *r;
48
49     if (http_buf_freelist)
50     {
51         r = http_buf_freelist;
52         http_buf_freelist = http_buf_freelist->next;
53     }
54     else
55         r = xmalloc(sizeof(struct http_buf));
56     r->offset = 0;
57     r->len = 0;
58     r->next = 0;
59     return r;
60 }
61
62 static void http_buf_destroy(struct http_buf *b)
63 {
64     b->next = http_buf_freelist;
65     http_buf_freelist = b;
66 }
67
68 static void http_buf_destroy_queue(struct http_buf *b)
69 {
70     struct http_buf *p;
71     while (b)
72     {
73         p = b->next;
74         http_buf_destroy(b);
75         b = p;
76     }
77 }
78
79 #ifdef GAGA
80 // Calculate length of chain
81 static int http_buf_len(struct http_buf *b)
82 {
83     int sum = 0;
84     for (; b; b = b->next)
85         sum += b->len;
86     return sum;
87 }
88 #endif
89
90 static struct http_buf *http_buf_bybuf(char *b, int len)
91 {
92     struct http_buf *res = 0;
93     struct http_buf **p = &res;
94
95     while (len)
96     {
97         int tocopy = len;
98         if (tocopy > HTTP_BUF_SIZE)
99             tocopy = HTTP_BUF_SIZE;
100         *p = http_buf_create();
101         memcpy((*p)->buf, b, tocopy);
102         (*p)->len = tocopy;
103         len -= tocopy;
104         b += tocopy;
105         p = &(*p)->next;
106     }
107     return res;
108 }
109
110 // Add a (chain of) buffers to the end of an existing queue.
111 static void http_buf_enqueue(struct http_buf **queue, struct http_buf *b)
112 {
113     while (*queue)
114         queue = &(*queue)->next;
115     *queue = b;
116 }
117
118 static struct http_buf *http_buf_bywrbuf(WRBUF wrbuf)
119 {
120     // Heavens to Betsy (buf)!
121     return http_buf_bybuf(wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
122 }
123
124 // Non-destructively collapse chain of buffers into a string (max *len)
125 // Return
126 static int http_buf_peek(struct http_buf *b, char *buf, int len)
127 {
128     int rd = 0;
129     while (b && rd < len)
130     {
131         int toread = len - rd;
132         if (toread > b->len)
133             toread = b->len;
134         memcpy(buf + rd, b->buf + b->offset, toread);
135         rd += toread;
136         b = b->next;
137     }
138     buf[rd] = '\0';
139     return rd;
140 }
141
142 // Ddestructively munch up to len  from head of queue.
143 static int http_buf_read(struct http_buf **b, char *buf, int len)
144 {
145     int rd = 0;
146     while ((*b) && rd < len)
147     {
148         int toread = len - rd;
149         if (toread > (*b)->len)
150             toread = (*b)->len;
151         memcpy(buf + rd, (*b)->buf + (*b)->offset, toread);
152         rd += toread;
153         if (toread < (*b)->len)
154         {
155             (*b)->len -= toread;
156             (*b)->offset += toread;
157             break;
158         }
159         else
160         {
161             struct http_buf *n = (*b)->next;
162             http_buf_destroy(*b);
163             *b = n;
164         }
165     }
166     buf[rd] = '\0';
167     return rd;
168 }
169
170 void static urldecode(char *i, char *o)
171 {
172     while (*i)
173     {
174         if (*i == '+')
175         {
176             *(o++) = ' ';
177             i++;
178         }
179         else if (*i == '%')
180         {
181             i++;
182             sscanf(i, "%2hhx", o);
183             i += 2;
184             o++;
185         }
186         else
187             *(o++) = *(i++);
188     }
189     *o = '\0';
190 }
191
192 void http_addheader(struct http_response *r, const char *name, const char *value)
193 {
194     struct http_channel *c = r->channel;
195     struct http_header *h = nmem_malloc(c->nmem, sizeof *h);
196     h->name = nmem_strdup(c->nmem, name);
197     h->value = nmem_strdup(c->nmem, value);
198     h->next = r->headers;
199     r->headers = h;
200 }
201
202 char *http_argbyname(struct http_request *r, char *name)
203 {
204     struct http_argument *p;
205     if (!name)
206         return 0;
207     for (p = r->arguments; p; p = p->next)
208         if (!strcmp(p->name, name))
209             return p->value;
210     return 0;
211 }
212
213 char *http_headerbyname(struct http_request *r, char *name)
214 {
215     struct http_header *p;
216     for (p = r->headers; p; p = p->next)
217         if (!strcmp(p->name, name))
218             return p->value;
219     return 0;
220 }
221
222 struct http_response *http_create_response(struct http_channel *c)
223 {
224     struct http_response *r = nmem_malloc(c->nmem, sizeof(*r));
225     strcpy(r->code, "200");
226     r->msg = "OK";
227     r->channel = c;
228     r->headers = 0;
229     r->payload = 0;
230     return r;
231 }
232
233 // Check if we have a complete request. Return 0 or length (including trailing newline)
234 // FIXME: Does not deal gracefully with requests carrying payload
235 // but this is kind of OK since we will reject anything other than an empty GET
236 static int request_check(struct http_buf *queue)
237 {
238     char tmp[4096];
239     int len = 0;
240     char *buf = tmp;
241
242     http_buf_peek(queue, tmp, 4096);
243     while (*buf) // Check if we have a sequence of lines terminated by an empty line
244     {
245         char *b = strstr(buf, "\r\n");
246
247         if (!b)
248             return 0;
249
250         len += (b - buf) + 2;
251         if (b == buf)
252             return len;
253         buf = b + 2;
254     }
255     return 0;
256 }
257
258 struct http_request *http_parse_request(struct http_channel *c, struct http_buf **queue,
259         int len)
260 {
261     struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
262     char *p, *p2;
263     char tmp[4096];
264     char *buf = tmp;
265
266     if (len > 4096)
267         return 0;
268     if (http_buf_read(queue, buf, len) < len)
269         return 0;
270
271     r->search = "";
272     r->channel = c;
273     r->arguments = 0;
274     r->headers = 0;
275     // Parse first line
276     for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
277         *(p2++) = *p;
278     if (*p != ' ')
279     {
280         yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
281         return 0;
282     }
283     *p2 = '\0';
284
285     if (!(buf = strchr(buf, ' ')))
286     {
287         yaz_log(YLOG_WARN, "Syntax error in request (1)");
288         return 0;
289     }
290     buf++;
291     if (!(p = strchr(buf, ' ')))
292     {
293         yaz_log(YLOG_WARN, "Syntax error in request (2)");
294         return 0;
295     }
296     *(p++) = '\0';
297     if ((p2 = strchr(buf, '?'))) // Do we have arguments?
298         *(p2++) = '\0';
299     r->path = nmem_strdup(c->nmem, buf);
300     if (p2)
301     {
302         r->search = nmem_strdup(c->nmem, p2);
303         // Parse Arguments
304         while (*p2)
305         {
306             struct http_argument *a;
307             char *equal = strchr(p2, '=');
308             char *eoa = strchr(p2, '&');
309             if (!equal)
310             {
311                 yaz_log(YLOG_WARN, "Expected '=' in argument");
312                 return 0;
313             }
314             if (!eoa)
315                 eoa = equal + strlen(equal); // last argument
316             else
317                 *(eoa++) = '\0';
318             a = nmem_malloc(c->nmem, sizeof(struct http_argument));
319             *(equal++) = '\0';
320             a->name = nmem_strdup(c->nmem, p2);
321             urldecode(equal, equal);
322             a->value = nmem_strdup(c->nmem, equal);
323             a->next = r->arguments;
324             r->arguments = a;
325             p2 = eoa;
326         }
327     }
328     buf = p;
329
330     if (strncmp(buf, "HTTP/", 5))
331         strcpy(r->http_version, "1.0");
332     else
333     {
334         buf += 5;
335         if (!(p = strstr(buf, "\r\n")))
336             return 0;
337         *(p++) = '\0';
338         p++;
339         strcpy(r->http_version, buf);
340         buf = p;
341     }
342     strcpy(c->version, r->http_version);
343
344     r->headers = 0;
345     while (*buf)
346     {
347         if (!(p = strstr(buf, "\r\n")))
348             return 0;
349         if (p == buf)
350             break;
351         else
352         {
353             struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
354             if (!(p2 = strchr(buf, ':')))
355                 return 0;
356             *(p2++) = '\0';
357             h->name = nmem_strdup(c->nmem, buf);
358             while (isspace(*p2))
359                 p2++;
360             if (p2 >= p) // Empty header?
361             {
362                 buf = p + 2;
363                 continue;
364             }
365             *p = '\0';
366             h->value = nmem_strdup(c->nmem, p2);
367             h->next = r->headers;
368             r->headers = h;
369             buf = p + 2;
370         }
371     }
372
373     return r;
374 }
375
376
377 static struct http_buf *http_serialize_response(struct http_channel *c,
378         struct http_response *r)
379 {
380     struct http_header *h;
381
382     wrbuf_rewind(c->wrbuf);
383     wrbuf_printf(c->wrbuf, "HTTP/1.1 %s %s\r\n", r->code, r->msg);
384     for (h = r->headers; h; h = h->next)
385         wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
386     wrbuf_printf(c->wrbuf, "Content-length: %d\r\n", r->payload ? (int) strlen(r->payload) : 0);
387     wrbuf_printf(c->wrbuf, "Content-type: text/xml\r\n");
388     wrbuf_puts(c->wrbuf, "\r\n");
389
390     if (r->payload)
391         wrbuf_puts(c->wrbuf, r->payload);
392
393     return http_buf_bywrbuf(c->wrbuf);
394 }
395
396 // Serialize a HTTP request
397 static struct http_buf *http_serialize_request(struct http_request *r)
398 {
399     struct http_channel *c = r->channel;
400     struct http_header *h;
401     struct http_argument *a;
402
403     wrbuf_rewind(c->wrbuf);
404     wrbuf_printf(c->wrbuf, "%s %s", r->method, r->path);
405
406     if (r->arguments)
407     {
408         wrbuf_putc(c->wrbuf, '?');
409         for (a = r->arguments; a; a = a->next) {
410             if (a != r->arguments)
411                 wrbuf_putc(c->wrbuf, '&');
412             wrbuf_printf(c->wrbuf, "%s=%s", a->name, a->value);
413         }
414     }
415
416     wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
417
418     for (h = r->headers; h; h = h->next)
419         wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
420
421     wrbuf_puts(c->wrbuf, "\r\n");
422     
423     return http_buf_bywrbuf(c->wrbuf);
424 }
425
426
427 static int http_weshouldproxy(struct http_request *rq)
428 {
429     if (proxy_addr && !strstr(rq->path, "search.pz2"))
430         return 1;
431     return 0;
432 }
433
434 static int http_proxy(struct http_request *rq)
435 {
436     struct http_channel *c = rq->channel;
437     struct http_proxy *p = c->proxy;
438     struct http_header *hp;
439     struct http_buf *requestbuf;
440
441     if (!p) // This is a new connection. Create a proxy channel
442     {
443         int sock;
444         struct protoent *pe;
445         int one = 1;
446         int flags;
447
448         if (!(pe = getprotobyname("tcp"))) {
449             abort();
450         }
451         if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
452         {
453             yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
454             return -1;
455         }
456         if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
457                         &one, sizeof(one)) < 0)
458             abort();
459         if ((flags = fcntl(sock, F_GETFL, 0)) < 0) 
460             yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
461         if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
462             yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
463         if (connect(sock, (struct sockaddr *) proxy_addr, sizeof(*proxy_addr)) < 0)
464             if (errno != EINPROGRESS)
465             {
466                 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
467                 return -1;
468             }
469
470         p = xmalloc(sizeof(struct http_proxy));
471         p->oqueue = 0;
472         p->channel = c;
473         c->proxy = p;
474         // We will add EVENT_OUTPUT below
475         p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT);
476         iochan_setdata(p->iochan, p);
477         p->iochan->next = channel_list;
478         channel_list = p->iochan;
479     }
480
481     // Modify Host: header
482     for (hp = rq->headers; hp; hp = hp->next)
483         if (!strcmp(hp->name, "Host"))
484             break;
485     if (!hp)
486     {
487         yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
488         return -1;
489     }
490     hp->value = nmem_strdup(c->nmem, proxy_url);
491     requestbuf = http_serialize_request(rq);
492     http_buf_enqueue(&p->oqueue, requestbuf);
493     iochan_setflag(p->iochan, EVENT_OUTPUT);
494     return 0;
495 }
496
497 void http_send_response(struct http_channel *ch)
498 {
499     struct http_response *rs = ch->response;
500     struct http_buf *hb;
501
502     assert(rs);
503     hb = http_serialize_response(ch, rs);
504     if (!hb)
505     {
506         yaz_log(YLOG_WARN, "Failed to serialize HTTP response");
507         http_destroy(ch->iochan);
508     }
509     else
510     {
511         http_buf_enqueue(&ch->oqueue, hb);
512         iochan_setflag(ch->iochan, EVENT_OUTPUT);
513         ch->state = Http_Idle;
514     }
515 }
516
517 static void http_io(IOCHAN i, int event)
518 {
519     struct http_channel *hc = iochan_getdata(i);
520
521     switch (event)
522     {
523         int res, reqlen;
524         struct http_buf *htbuf;
525
526         case EVENT_INPUT:
527             htbuf = http_buf_create();
528             res = read(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1);
529             if (res == -1 && errno == EAGAIN)
530             {
531                 http_buf_destroy(htbuf);
532                 return;
533             }
534             if (res <= 0)
535             {
536                 http_buf_destroy(htbuf);
537                 http_destroy(i);
538                 return;
539             }
540             if (res > 0)
541             {
542                 htbuf->buf[res] = '\0';
543                 htbuf->len = res;
544                 http_buf_enqueue(&hc->iqueue, htbuf);
545             }
546
547             if (hc->state == Http_Busy)
548                 return;
549             if ((reqlen = request_check(hc->iqueue)) <= 2)
550                 return;
551
552             nmem_reset(hc->nmem);
553             if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
554             {
555                 yaz_log(YLOG_WARN, "Failed to parse request");
556                 http_destroy(i);
557                 return;
558             }
559             hc->response = 0;
560             yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
561                     hc->request->path,
562                     *hc->request->search ? "?" : "",
563                     hc->request->search);
564             if (http_weshouldproxy(hc->request))
565                 http_proxy(hc->request);
566             else
567             {
568                 // Execute our business logic!
569                 hc->state = Http_Busy;
570                 http_command(hc);
571             }
572             if (hc->iqueue)
573             {
574                 yaz_log(YLOG_DEBUG, "We think we have more input to read. Forcing event");
575                 iochan_setevent(i, EVENT_INPUT);
576             }
577
578             break;
579
580         case EVENT_OUTPUT:
581             if (hc->oqueue)
582             {
583                 struct http_buf *wb = hc->oqueue;
584                 res = write(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len);
585                 if (res <= 0)
586                 {
587                     yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
588                     http_destroy(i);
589                     return;
590                 }
591                 if (res == wb->len)
592                 {
593                     hc->oqueue = hc->oqueue->next;
594                     http_buf_destroy(wb);
595                 }
596                 else
597                 {
598                     wb->len -= res;
599                     wb->offset += res;
600                 }
601                 if (!hc->oqueue) {
602                     if (!strcmp(hc->version, "1.0"))
603                     {
604                         http_destroy(i);
605                         return;
606                     }
607                     else
608                     {
609                         iochan_clearflag(i, EVENT_OUTPUT);
610                         if (hc->iqueue)
611                             iochan_setevent(hc->iochan, EVENT_INPUT);
612                     }
613                 }
614             }
615
616             if (!hc->oqueue && hc->proxy && !hc->proxy->iochan) 
617                 http_destroy(i); // Server closed; we're done
618             break;
619         default:
620             yaz_log(YLOG_WARN, "Unexpected event on connection");
621             http_destroy(i);
622     }
623 }
624
625 // Handles I/O on a client connection to a backend web server (proxy mode)
626 static void proxy_io(IOCHAN pi, int event)
627 {
628     struct http_proxy *pc = iochan_getdata(pi);
629     struct http_channel *hc = pc->channel;
630
631     switch (event)
632     {
633         int res;
634         struct http_buf *htbuf;
635
636         case EVENT_INPUT:
637             htbuf = http_buf_create();
638             res = read(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1);
639             if (res == 0 || (res < 0 && errno != EINPROGRESS))
640             {
641                 if (hc->oqueue)
642                 {
643                     yaz_log(YLOG_WARN, "Proxy read came up short");
644                     // Close channel and alert client HTTP channel that we're gone
645                     http_buf_destroy(htbuf);
646                     close(iochan_getfd(pi));
647                     iochan_destroy(pi);
648                     pc->iochan = 0;
649                 }
650                 else
651                 {
652                     http_destroy(hc->iochan);
653                     return;
654                 }
655             }
656             else
657             {
658                 htbuf->buf[res] = '\0';
659                 htbuf->len = res;
660                 http_buf_enqueue(&hc->oqueue, htbuf);
661             }
662             iochan_setflag(hc->iochan, EVENT_OUTPUT);
663             break;
664         case EVENT_OUTPUT:
665             if (!(htbuf = pc->oqueue))
666             {
667                 iochan_clearflag(pi, EVENT_OUTPUT);
668                 return;
669             }
670             res = write(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len);
671             if (res <= 0)
672             {
673                 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
674                 http_destroy(hc->iochan);
675                 return;
676             }
677             if (res == htbuf->len)
678             {
679                 struct http_buf *np = htbuf->next;
680                 http_buf_destroy(htbuf);
681                 pc->oqueue = np;
682             }
683             else
684             {
685                 htbuf->len -= res;
686                 htbuf->offset += res;
687             }
688
689             if (!pc->oqueue) {
690                 iochan_setflags(pi, EVENT_INPUT); // Turns off output flag
691             }
692             break;
693         default:
694             yaz_log(YLOG_WARN, "Unexpected event on connection");
695             http_destroy(hc->iochan);
696     }
697 }
698
699 // Cleanup channel
700 static void http_destroy(IOCHAN i)
701 {
702     struct http_channel *s = iochan_getdata(i);
703
704     if (s->proxy)
705     {
706         if (s->proxy->iochan)
707         {
708             close(iochan_getfd(s->proxy->iochan));
709             iochan_destroy(s->proxy->iochan);
710         }
711         http_buf_destroy_queue(s->proxy->oqueue);
712         xfree(s->proxy);
713     }
714     s->next = http_channel_freelist;
715     http_channel_freelist = s;
716     close(iochan_getfd(i));
717     iochan_destroy(i);
718 }
719
720 static struct http_channel *http_create(void)
721 {
722     struct http_channel *r = http_channel_freelist;
723
724     if (r)
725     {
726         http_channel_freelist = r->next;
727         nmem_reset(r->nmem);
728         wrbuf_rewind(r->wrbuf);
729     }
730     else
731     {
732         r = xmalloc(sizeof(struct http_channel));
733         r->nmem = nmem_create();
734         r->wrbuf = wrbuf_alloc();
735     }
736     r->proxy = 0;
737     r->iochan = 0;
738     r->iqueue = r->oqueue = 0;
739     r->state = Http_Idle;
740     r->request = 0;
741     r->response = 0;
742     return r;
743 }
744
745
746 /* Accept a new command connection */
747 static void http_accept(IOCHAN i, int event)
748 {
749     struct sockaddr_in addr;
750     int fd = iochan_getfd(i);
751     socklen_t len;
752     int s;
753     IOCHAN c;
754     int flags;
755     struct http_channel *ch;
756
757     len = sizeof addr;
758     if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
759     {
760         yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
761         return;
762     }
763     if ((flags = fcntl(s, F_GETFL, 0)) < 0) 
764         yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
765     if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
766         yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
767
768     yaz_log(YLOG_DEBUG, "New command connection");
769     c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT);
770
771     ch = http_create();
772     ch->iochan = c;
773     iochan_setdata(c, ch);
774
775     c->next = channel_list;
776     channel_list = c;
777 }
778
779 /* Create a http-channel listener, syntax [host:]port */
780 void http_init(const char *addr)
781 {
782     IOCHAN c;
783     int l;
784     struct protoent *p;
785     struct sockaddr_in myaddr;
786     int one = 1;
787     const char *pp;
788     int port;
789
790     yaz_log(YLOG_LOG, "HTTP listener is %s", addr);
791
792     memset(&myaddr, 0, sizeof myaddr);
793     myaddr.sin_family = AF_INET;
794     pp = strchr(addr, ':');
795     if (pp)
796     {
797         int len = pp - addr;
798         char hostname[128];
799         struct hostent *he;
800
801         strncpy(hostname, addr, len);
802         hostname[len] = '\0';
803         if (!(he = gethostbyname(hostname)))
804         {
805             yaz_log(YLOG_FATAL, "Unable to resolve '%s'", hostname);
806             exit(1);
807         }
808         memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
809         port = atoi(pp + 1);
810     }
811     else
812     {
813         port = atoi(addr);
814         myaddr.sin_addr.s_addr = INADDR_ANY;
815     }
816     myaddr.sin_port = htons(port);
817
818     if (!(p = getprotobyname("tcp"))) {
819         abort();
820     }
821     if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
822         yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
823     if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
824                     &one, sizeof(one)) < 0)
825         abort();
826
827     if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0) 
828         yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
829     if (listen(l, SOMAXCONN) < 0) 
830         yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
831
832     c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT);
833     c->next = channel_list;
834     channel_list = c;
835 }
836
837 void http_set_proxyaddr(char *host)
838 {
839     char *p;
840     int port;
841     struct hostent *he;
842
843     strcpy(proxy_url, host);
844     p = strchr(host, ':');
845     yaz_log(YLOG_DEBUG, "Proxying for %s", host);
846     if (p) {
847         port = atoi(p + 1);
848         *p = '\0';
849     }
850     else
851         port = 80;
852     if (!(he = gethostbyname(host))) 
853     {
854         fprintf(stderr, "Failed to lookup '%s'\n", host);
855         exit(1);
856     }
857     proxy_addr = xmalloc(sizeof(struct sockaddr_in));
858     proxy_addr->sin_family = he->h_addrtype;
859     memcpy(&proxy_addr->sin_addr.s_addr, he->h_addr_list[0], he->h_length);
860     proxy_addr->sin_port = htons(port);
861 }
862
863 /*
864  * Local variables:
865  * c-basic-offset: 4
866  * indent-tabs-mode: nil
867  * End:
868  * vim: shiftwidth=4 tabstop=8 expandtab
869  */