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