1 /* $Id: http.c,v 1.38 2007-09-23 15:39:24 adam Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include <sys/socket.h>
24 #include <sys/types.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
44 #include <yaz/yaz-util.h>
45 #include <yaz/comstack.h>
53 #include "http_command.h"
55 #define MAX_HTTP_HEADER 4096
57 static void proxy_io(IOCHAN i, int event);
58 static struct http_channel *http_create(const char *addr);
59 static void http_destroy(IOCHAN i);
61 // If this is set, we proxy normal HTTP requests
62 static struct sockaddr_in *proxy_addr = 0;
63 static char proxy_url[256] = "";
64 static char myurl[256] = "";
65 static struct http_buf *http_buf_freelist = 0;
66 static struct http_channel *http_channel_freelist = 0;
68 struct http_channel_observer_s {
70 void (*destroy)(void *data, struct http_channel *chan);
71 struct http_channel_observer_s *next;
72 struct http_channel *chan;
76 static const char *http_lookup_header(struct http_header *header,
79 for (; header; header = header->next)
80 if (!strcasecmp(name, header->name))
85 static struct http_buf *http_buf_create()
89 if (http_buf_freelist)
91 r = http_buf_freelist;
92 http_buf_freelist = http_buf_freelist->next;
95 r = xmalloc(sizeof(struct http_buf));
102 static void http_buf_destroy(struct http_buf *b)
104 b->next = http_buf_freelist;
105 http_buf_freelist = b;
108 static void http_buf_destroy_queue(struct http_buf *b)
120 // Calculate length of chain
121 static int http_buf_len(struct http_buf *b)
124 for (; b; b = b->next)
130 static struct http_buf *http_buf_bybuf(char *b, int len)
132 struct http_buf *res = 0;
133 struct http_buf **p = &res;
138 if (tocopy > HTTP_BUF_SIZE)
139 tocopy = HTTP_BUF_SIZE;
140 *p = http_buf_create();
141 memcpy((*p)->buf, b, tocopy);
150 // Add a (chain of) buffers to the end of an existing queue.
151 static void http_buf_enqueue(struct http_buf **queue, struct http_buf *b)
154 queue = &(*queue)->next;
158 static struct http_buf *http_buf_bywrbuf(WRBUF wrbuf)
160 // Heavens to Betsy (buf)!
161 return http_buf_bybuf(wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
164 // Non-destructively collapse chain of buffers into a string (max *len)
166 static void http_buf_peek(struct http_buf *b, char *buf, int len)
169 while (b && rd < len)
171 int toread = len - rd;
174 memcpy(buf + rd, b->buf + b->offset, toread);
181 static int http_buf_size(struct http_buf *b)
184 for (; b; b = b->next)
189 // Ddestructively munch up to len from head of queue.
190 static int http_buf_read(struct http_buf **b, char *buf, int len)
193 while ((*b) && rd < len)
195 int toread = len - rd;
196 if (toread > (*b)->len)
198 memcpy(buf + rd, (*b)->buf + (*b)->offset, toread);
200 if (toread < (*b)->len)
203 (*b)->offset += toread;
208 struct http_buf *n = (*b)->next;
209 http_buf_destroy(*b);
217 // Buffers may overlap.
218 static void urldecode(char *i, char *o)
230 sscanf(i, "%2hhx", o);
240 // Warning: Buffers may not overlap
241 void urlencode(const char *i, char *o)
245 if (strchr(" /:", *i))
247 sprintf(o, "%%%.2X", (int) *i);
257 void http_addheader(struct http_response *r, const char *name, const char *value)
259 struct http_channel *c = r->channel;
260 struct http_header *h = nmem_malloc(c->nmem, sizeof *h);
261 h->name = nmem_strdup(c->nmem, name);
262 h->value = nmem_strdup(c->nmem, value);
263 h->next = r->headers;
267 char *http_argbyname(struct http_request *r, char *name)
269 struct http_argument *p;
272 for (p = r->arguments; p; p = p->next)
273 if (!strcmp(p->name, name))
278 char *http_headerbyname(struct http_header *h, char *name)
280 for (; h; h = h->next)
281 if (!strcmp(h->name, name))
286 struct http_response *http_create_response(struct http_channel *c)
288 struct http_response *r = nmem_malloc(c->nmem, sizeof(*r));
289 strcpy(r->code, "200");
297 // Check if buf contains a package (minus payload)
298 static int package_check(const char *buf, int sz)
303 while (*buf) // Check if we have a sequence of lines terminated by an empty line
305 const char *b = strstr(buf, "\r\n");
310 len += (b - buf) + 2;
313 if (len + content_len <= sz)
314 return len + content_len;
318 // following first skip of \r\n so that we don't consider Method
319 if (!strncasecmp(buf, "Content-Length:", 15))
321 const char *cp = buf+15;
325 while (*cp && isdigit(*cp))
326 content_len = content_len*10 + (*cp++ - '0');
327 if (content_len < 0) /* prevent negative offsets */
334 // Check if we have a request. Return 0 or length
335 // (including trailing CRNL) FIXME: Does not deal gracefully with requests
336 // carrying payload but this is kind of OK since we will reject anything
337 // other than an empty GET
338 static int request_check(struct http_buf *queue)
340 char tmp[MAX_HTTP_HEADER];
342 http_buf_peek(queue, tmp, MAX_HTTP_HEADER-1);
343 return package_check(tmp, http_buf_size(queue));
346 struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len)
348 char tmp[MAX_HTTP_HEADER];
349 struct http_response *r = http_create_response(c);
351 struct http_header **hp = &r->headers;
353 if (len >= MAX_HTTP_HEADER)
355 memcpy(tmp, buf, len);
356 for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version
360 for (p2 = p; *p2 && *p2 != ' ' && p2 - p < 3; p2++)
361 r->code[p2 - p] = *p2;
362 if (!(p = strstr(tmp, "\r\n")))
367 if (!(p2 = strstr(p, "\r\n")))
369 if (p == p2) // End of headers
373 struct http_header *h = *hp = nmem_malloc(c->nmem, sizeof(*h));
374 char *value = strchr(p, ':');
378 h->name = nmem_strdup(c->nmem, p);
379 while (isspace(*value))
381 if (value >= p2) // Empty header;
388 h->value = nmem_strdup(c->nmem, value);
397 static int http_parse_arguments(struct http_request *r, NMEM nmem,
400 const char *p2 = args;
404 struct http_argument *a;
405 const char *equal = strchr(p2, '=');
406 const char *eoa = strchr(p2, '&');
409 yaz_log(YLOG_WARN, "Expected '=' in argument");
413 eoa = equal + strlen(equal); // last argument
414 else if (equal > eoa)
416 yaz_log(YLOG_WARN, "Missing '&' in argument");
419 a = nmem_malloc(nmem, sizeof(struct http_argument));
420 a->name = nmem_strdupn(nmem, p2, equal - p2);
421 a->value = nmem_strdupn(nmem, equal+1, eoa - equal - 1);
422 urldecode(a->name, a->name);
423 urldecode(a->value, a->value);
424 a->next = r->arguments;
433 struct http_request *http_parse_request(struct http_channel *c,
434 struct http_buf **queue,
437 struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
439 char *start = nmem_malloc(c->nmem, len+1);
442 if (http_buf_read(queue, buf, len) < len)
444 yaz_log(YLOG_WARN, "http_buf_read < len (%d)", len);
454 for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
458 yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
463 if (!(buf = strchr(buf, ' ')))
465 yaz_log(YLOG_WARN, "Syntax error in request (1)");
469 if (!(p = strchr(buf, ' ')))
471 yaz_log(YLOG_WARN, "Syntax error in request (2)");
475 if ((p2 = strchr(buf, '?'))) // Do we have arguments?
477 r->path = nmem_strdup(c->nmem, buf);
480 r->search = nmem_strdup(c->nmem, p2);
482 http_parse_arguments(r, c->nmem, p2);
486 if (strncmp(buf, "HTTP/", 5))
487 strcpy(r->http_version, "1.0");
491 if (!(p = strstr(buf, "\r\n")))
493 yaz_log(YLOG_WARN, "Did not see \\r\\n (1)");
498 strcpy(r->http_version, buf);
501 strcpy(c->version, r->http_version);
506 if (!(p = strstr(buf, "\r\n")))
508 yaz_log(YLOG_WARN, "Did not see \\r\\n (2)");
518 struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
519 if (!(p2 = strchr(buf, ':')))
522 h->name = nmem_strdup(c->nmem, buf);
525 if (p2 >= p) // Empty header?
531 h->value = nmem_strdup(c->nmem, p2);
532 h->next = r->headers;
538 if (buf < start + len)
540 const char *content_type = http_lookup_header(r->headers,
542 r->content_len = start + len - buf;
543 r->content_buf = buf;
545 if (!strcmp(content_type, "application/x-www-form-urlencoded"))
547 http_parse_arguments(r, c->nmem, r->content_buf);
553 static struct http_buf *http_serialize_response(struct http_channel *c,
554 struct http_response *r)
556 struct http_header *h;
558 wrbuf_rewind(c->wrbuf);
559 wrbuf_printf(c->wrbuf, "HTTP/1.1 %s %s\r\n", r->code, r->msg);
560 for (h = r->headers; h; h = h->next)
561 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
564 wrbuf_printf(c->wrbuf, "Content-length: %d\r\n", r->payload ?
565 (int) strlen(r->payload) : 0);
566 wrbuf_printf(c->wrbuf, "Content-type: text/xml\r\n");
569 xmlDoc *doc = xmlParseMemory(r->payload, strlen(r->payload));
576 yaz_log(YLOG_WARN, "Sending non-wellformed "
577 "response (bug #1162");
578 yaz_log(YLOG_WARN, "payload: %s", r->payload);
582 wrbuf_puts(c->wrbuf, "\r\n");
585 wrbuf_puts(c->wrbuf, r->payload);
587 return http_buf_bywrbuf(c->wrbuf);
590 // Serialize a HTTP request
591 static struct http_buf *http_serialize_request(struct http_request *r)
593 struct http_channel *c = r->channel;
594 struct http_header *h;
596 wrbuf_rewind(c->wrbuf);
597 wrbuf_printf(c->wrbuf, "%s %s%s%s", r->method, r->path,
598 *r->search ? "?" : "", r->search);
600 wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
602 for (h = r->headers; h; h = h->next)
603 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
605 wrbuf_puts(c->wrbuf, "\r\n");
608 wrbuf_write(c->wrbuf, r->content_buf, r->content_len);
611 yaz_log(YLOG_LOG, "WRITING TO PROXY:\n%s\n----",
612 wrbuf_cstr(c->wrbuf));
614 return http_buf_bywrbuf(c->wrbuf);
618 static int http_weshouldproxy(struct http_request *rq)
620 if (proxy_addr && !strstr(rq->path, "search.pz2"))
626 struct http_header * http_header_append(struct http_channel *ch,
627 struct http_header * hp,
631 struct http_header *hpnew = 0;
636 while (hp && hp->next)
639 if(name && strlen(name)&& value && strlen(value)){
640 hpnew = nmem_malloc(ch->nmem, sizeof *hpnew);
641 hpnew->name = nmem_strdup(ch->nmem, name);
642 hpnew->value = nmem_strdup(ch->nmem, value);
656 static int http_proxy(struct http_request *rq)
658 struct http_channel *c = rq->channel;
659 struct http_proxy *p = c->proxy;
660 struct http_header *hp;
661 struct http_buf *requestbuf;
662 char server_via[128] = "";
663 char server_port[16] = "";
664 struct conf_server *ser = global_parameters.server;
666 if (!p) // This is a new connection. Create a proxy channel
673 if (!(pe = getprotobyname("tcp"))) {
676 if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
678 yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
681 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
682 &one, sizeof(one)) < 0)
684 if ((flags = fcntl(sock, F_GETFL, 0)) < 0)
685 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
686 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
687 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
688 if (connect(sock, (struct sockaddr *) proxy_addr,
689 sizeof(*proxy_addr)) < 0)
690 if (errno != EINPROGRESS)
692 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
696 p = xmalloc(sizeof(struct http_proxy));
699 p->first_response = 1;
701 // We will add EVENT_OUTPUT below
702 p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT);
703 iochan_setdata(p->iochan, p);
704 pazpar2_add_channel(p->iochan);
707 // Do _not_ modify Host: header, just checking it's existence
709 if (!http_lookup_header(rq->headers, "Host"))
711 yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
715 // Add new header about paraz2 version, host, remote client address, etc.
718 hp = http_header_append(c, hp,
719 "X-Pazpar2-Version", PACKAGE_VERSION);
720 hp = http_header_append(c, hp,
721 "X-Pazpar2-Server-Host", ser->host);
722 sprintf(server_port, "%d", ser->port);
723 hp = http_header_append(c, hp,
724 "X-Pazpar2-Server-Port", server_port);
725 sprintf(server_via, "1.1 %s:%s (%s/%s)",
726 ser->host, server_port, PACKAGE_NAME, PACKAGE_VERSION);
727 hp = http_header_append(c, hp, "Via" , server_via);
728 hp = http_header_append(c, hp, "X-Forwarded-For", c->addr);
731 requestbuf = http_serialize_request(rq);
733 http_buf_enqueue(&p->oqueue, requestbuf);
734 iochan_setflag(p->iochan, EVENT_OUTPUT);
738 void http_send_response(struct http_channel *ch)
740 struct http_response *rs = ch->response;
744 hb = http_serialize_response(ch, rs);
747 yaz_log(YLOG_WARN, "Failed to serialize HTTP response");
748 http_destroy(ch->iochan);
752 http_buf_enqueue(&ch->oqueue, hb);
753 iochan_setflag(ch->iochan, EVENT_OUTPUT);
754 ch->state = Http_Idle;
758 static void http_io(IOCHAN i, int event)
760 struct http_channel *hc = iochan_getdata(i);
765 struct http_buf *htbuf;
768 htbuf = http_buf_create();
769 res = read(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1);
770 if (res == -1 && errno == EAGAIN)
772 http_buf_destroy(htbuf);
777 http_buf_destroy(htbuf);
781 htbuf->buf[res] = '\0';
783 http_buf_enqueue(&hc->iqueue, htbuf);
787 if (hc->state == Http_Busy)
789 if ((reqlen = request_check(hc->iqueue)) <= 2)
791 // we have a complete HTTP request
792 nmem_reset(hc->nmem);
793 if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
795 yaz_log(YLOG_WARN, "Failed to parse request");
800 yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
802 *hc->request->search ? "?" : "",
803 hc->request->search);
804 if (http_weshouldproxy(hc->request))
805 http_proxy(hc->request);
808 // Execute our business logic!
809 hc->state = Http_Busy;
817 struct http_buf *wb = hc->oqueue;
818 res = write(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len);
821 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
827 hc->oqueue = hc->oqueue->next;
828 http_buf_destroy(wb);
836 if (!strcmp(hc->version, "1.0"))
843 iochan_clearflag(i, EVENT_OUTPUT);
845 iochan_setevent(hc->iochan, EVENT_INPUT);
850 if (!hc->oqueue && hc->proxy && !hc->proxy->iochan)
851 http_destroy(i); // Server closed; we're done
854 yaz_log(YLOG_WARN, "Unexpected event on connection");
860 // If this hostname contains our proxy host as a prefix, replace with myurl
861 static char *sub_hostname(struct http_channel *c, char *buf)
864 if (strlen(buf) > 1023)
866 if (strncmp(buf, "http://", 7))
868 if (!strncmp(buf + 7, proxy_url, strlen(proxy_url)))
871 strcat(tmp, buf + strlen(proxy_url) + 7);
872 return nmem_strdup(c->nmem, tmp);
878 // Handles I/O on a client connection to a backend web server (proxy mode)
879 static void proxy_io(IOCHAN pi, int event)
881 struct http_proxy *pc = iochan_getdata(pi);
882 struct http_channel *hc = pc->channel;
887 struct http_buf *htbuf;
890 htbuf = http_buf_create();
891 res = read(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1);
892 if (res == 0 || (res < 0 && errno != EINPROGRESS))
896 yaz_log(YLOG_WARN, "Proxy read came up short");
897 // Close channel and alert client HTTP channel that we're gone
898 http_buf_destroy(htbuf);
899 close(iochan_getfd(pi));
905 http_destroy(hc->iochan);
911 htbuf->buf[res] = '\0';
915 if (pc->first_response) // Check if this is a redirect
918 if ((len = package_check(htbuf->buf)))
920 struct http_response *res = http_parse_response_buf(hc, htbuf->buf, len);
923 const char *location = http_lookup_header(
924 res->header, "Location");
927 // We found a location header. Rewrite it.
928 struct http_buf *buf;
929 h->value = sub_hostname(hc, location);
930 buf = http_serialize_response(hc, res);
931 yaz_log(YLOG_LOG, "Proxy rewrite");
932 http_buf_enqueue(&hc->oqueue, buf);
938 pc->first_response = 0;
941 // Write any remaining payload
942 if (htbuf->len - htbuf->offset > 0)
943 http_buf_enqueue(&hc->oqueue, htbuf);
945 iochan_setflag(hc->iochan, EVENT_OUTPUT);
948 if (!(htbuf = pc->oqueue))
950 iochan_clearflag(pi, EVENT_OUTPUT);
953 res = write(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len);
956 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
957 http_destroy(hc->iochan);
960 if (res == htbuf->len)
962 struct http_buf *np = htbuf->next;
963 http_buf_destroy(htbuf);
969 htbuf->offset += res;
973 iochan_setflags(pi, EVENT_INPUT); // Turns off output flag
977 yaz_log(YLOG_WARN, "Unexpected event on connection");
978 http_destroy(hc->iochan);
982 static void http_fire_observers(struct http_channel *c);
983 static void http_destroy_observers(struct http_channel *c);
986 static void http_destroy(IOCHAN i)
988 struct http_channel *s = iochan_getdata(i);
992 if (s->proxy->iochan)
994 close(iochan_getfd(s->proxy->iochan));
995 iochan_destroy(s->proxy->iochan);
997 http_buf_destroy_queue(s->proxy->oqueue);
1000 http_buf_destroy_queue(s->iqueue);
1001 http_buf_destroy_queue(s->oqueue);
1002 http_fire_observers(s);
1003 http_destroy_observers(s);
1004 s->next = http_channel_freelist;
1005 http_channel_freelist = s;
1006 close(iochan_getfd(i));
1010 static struct http_channel *http_create(const char *addr)
1012 struct http_channel *r = http_channel_freelist;
1016 http_channel_freelist = r->next;
1017 nmem_reset(r->nmem);
1018 wrbuf_rewind(r->wrbuf);
1022 r = xmalloc(sizeof(struct http_channel));
1023 r->nmem = nmem_create();
1024 r->wrbuf = wrbuf_alloc();
1028 r->iqueue = r->oqueue = 0;
1029 r->state = Http_Idle;
1034 yaz_log(YLOG_WARN, "Invalid HTTP forward address");
1037 strcpy(r->addr, addr);
1043 /* Accept a new command connection */
1044 static void http_accept(IOCHAN i, int event)
1046 struct sockaddr_in addr;
1047 int fd = iochan_getfd(i);
1052 struct http_channel *ch;
1055 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
1057 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
1060 if ((flags = fcntl(s, F_GETFL, 0)) < 0)
1061 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
1062 if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
1063 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
1065 yaz_log(YLOG_DEBUG, "New command connection");
1066 c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT);
1068 ch = http_create(inet_ntoa(addr.sin_addr));
1070 iochan_setdata(c, ch);
1072 pazpar2_add_channel(c);
1075 /* Create a http-channel listener, syntax [host:]port */
1076 void http_init(const char *addr)
1081 struct sockaddr_in myaddr;
1086 yaz_log(YLOG_LOG, "HTTP listener %s", addr);
1088 memset(&myaddr, 0, sizeof myaddr);
1089 myaddr.sin_family = AF_INET;
1090 pp = strchr(addr, ':');
1093 int len = pp - addr;
1097 strncpy(hostname, addr, len);
1098 hostname[len] = '\0';
1099 if (!(he = gethostbyname(hostname))){
1100 yaz_log(YLOG_FATAL, "Unable to resolve '%s'", hostname);
1104 memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1105 port = atoi(pp + 1);
1110 myaddr.sin_addr.s_addr = INADDR_ANY;
1113 myaddr.sin_port = htons(port);
1115 if (!(p = getprotobyname("tcp"))) {
1118 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
1119 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
1120 if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
1121 &one, sizeof(one)) < 0)
1124 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
1126 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
1129 if (listen(l, SOMAXCONN) < 0)
1131 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
1135 c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT);
1136 pazpar2_add_channel(c);
1139 void http_set_proxyaddr(char *host, char *base_url)
1145 strcpy(myurl, base_url);
1146 strcpy(proxy_url, host);
1147 p = strchr(host, ':');
1148 yaz_log(YLOG_DEBUG, "Proxying for %s", host);
1149 yaz_log(YLOG_LOG, "HTTP backend %s", proxy_url);
1156 if (!(he = gethostbyname(host)))
1158 fprintf(stderr, "Failed to lookup '%s'\n", host);
1161 proxy_addr = xmalloc(sizeof(struct sockaddr_in));
1162 proxy_addr->sin_family = he->h_addrtype;
1163 memcpy(&proxy_addr->sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1164 proxy_addr->sin_port = htons(port);
1167 static void http_fire_observers(struct http_channel *c)
1169 http_channel_observer_t p = c->observers;
1172 p->destroy(p->data, c);
1177 static void http_destroy_observers(struct http_channel *c)
1179 while (c->observers)
1181 http_channel_observer_t obs = c->observers;
1182 c->observers = obs->next;
1187 http_channel_observer_t http_add_observer(struct http_channel *c, void *data,
1188 http_channel_destroy_t des)
1190 http_channel_observer_t obs = xmalloc(sizeof(*obs));
1194 obs->next = c->observers;
1199 void http_remove_observer(http_channel_observer_t obs)
1201 struct http_channel *c = obs->chan;
1202 http_channel_observer_t found, *p = &c->observers;
1211 struct http_channel *http_channel_observer_chan(http_channel_observer_t obs)
1219 * indent-tabs-mode: nil
1221 * vim: shiftwidth=4 tabstop=8 expandtab