1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2008 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <sys/socket.h>
22 #include <sys/types.h>
24 #include <yaz/snprintf.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
43 #include <yaz/yaz-util.h>
44 #include <yaz/comstack.h>
52 #include "http_command.h"
54 #define MAX_HTTP_HEADER 4096
56 static void proxy_io(IOCHAN i, int event);
57 static struct http_channel *http_create(const char *addr);
58 static void http_destroy(IOCHAN i);
60 // If this is set, we proxy normal HTTP requests
61 static struct sockaddr_in *proxy_addr = 0;
62 static char proxy_url[256] = "";
63 static char myurl[256] = "";
64 static struct http_buf *http_buf_freelist = 0;
65 static struct http_channel *http_channel_freelist = 0;
67 struct http_channel_observer_s {
70 http_channel_destroy_t destroy;
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)
119 static struct http_buf *http_buf_bybuf(char *b, int len)
121 struct http_buf *res = 0;
122 struct http_buf **p = &res;
127 if (tocopy > HTTP_BUF_SIZE)
128 tocopy = HTTP_BUF_SIZE;
129 *p = http_buf_create();
130 memcpy((*p)->buf, b, tocopy);
139 // Add a (chain of) buffers to the end of an existing queue.
140 static void http_buf_enqueue(struct http_buf **queue, struct http_buf *b)
143 queue = &(*queue)->next;
147 static struct http_buf *http_buf_bywrbuf(WRBUF wrbuf)
149 // Heavens to Betsy (buf)!
150 return http_buf_bybuf(wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
153 // Non-destructively collapse chain of buffers into a string (max *len)
155 static void http_buf_peek(struct http_buf *b, char *buf, int len)
158 while (b && rd < len)
160 int toread = len - rd;
163 memcpy(buf + rd, b->buf + b->offset, toread);
170 static int http_buf_size(struct http_buf *b)
173 for (; b; b = b->next)
178 // Ddestructively munch up to len from head of queue.
179 static int http_buf_read(struct http_buf **b, char *buf, int len)
182 while ((*b) && rd < len)
184 int toread = len - rd;
185 if (toread > (*b)->len)
187 memcpy(buf + rd, (*b)->buf + (*b)->offset, toread);
189 if (toread < (*b)->len)
192 (*b)->offset += toread;
197 struct http_buf *n = (*b)->next;
198 http_buf_destroy(*b);
206 // Buffers may overlap.
207 static void urldecode(char *i, char *o)
219 sscanf(i, "%2hhx", o);
229 // Warning: Buffers may not overlap
230 void urlencode(const char *i, char *o)
234 if (strchr(" /:", *i))
236 sprintf(o, "%%%.2X", (int) *i);
246 void http_addheader(struct http_response *r, const char *name, const char *value)
248 struct http_channel *c = r->channel;
249 struct http_header *h = nmem_malloc(c->nmem, sizeof *h);
250 h->name = nmem_strdup(c->nmem, name);
251 h->value = nmem_strdup(c->nmem, value);
252 h->next = r->headers;
256 char *http_argbyname(struct http_request *r, char *name)
258 struct http_argument *p;
261 for (p = r->arguments; p; p = p->next)
262 if (!strcmp(p->name, name))
267 char *http_headerbyname(struct http_header *h, char *name)
269 for (; h; h = h->next)
270 if (!strcmp(h->name, name))
275 struct http_response *http_create_response(struct http_channel *c)
277 struct http_response *r = nmem_malloc(c->nmem, sizeof(*r));
278 strcpy(r->code, "200");
283 r->content_type = "text/xml";
288 static const char *next_crlf(const char *cp, size_t *skipped)
290 const char *next_cp = strchr(cp, '\n');
293 if (next_cp > cp && next_cp[-1] == '\r')
294 *skipped = next_cp - cp - 1;
296 *skipped = next_cp - cp;
302 // Check if buf contains a package (minus payload)
303 static int package_check(const char *buf, int sz)
311 const char *b = next_crlf(buf, &skipped);
315 // we did not find CRLF.. See if buffer is too large..
316 if (sz >= MAX_HTTP_HEADER-1)
317 return MAX_HTTP_HEADER-1; // yes. Return that (will fail later)
323 // CRLF CRLF , i.e. end of header
324 if (len + content_len <= sz)
325 return len + content_len;
329 // following first skip of \r\n so that we don't consider Method
330 if (!strncasecmp(buf, "Content-Length:", 15))
332 const char *cp = buf+15;
336 while (*cp && isdigit(*cp))
337 content_len = content_len*10 + (*cp++ - '0');
338 if (content_len < 0) /* prevent negative offsets */
342 return 0; // incomplete request
345 // Check if we have a request. Return 0 or length
346 static int request_check(struct http_buf *queue)
348 char tmp[MAX_HTTP_HEADER];
350 // only peek at the header..
351 http_buf_peek(queue, tmp, MAX_HTTP_HEADER-1);
352 // still we only return non-zero if the complete request is received..
353 return package_check(tmp, http_buf_size(queue));
356 struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len)
358 char tmp[MAX_HTTP_HEADER];
359 struct http_response *r = http_create_response(c);
361 struct http_header **hp = &r->headers;
363 if (len >= MAX_HTTP_HEADER)
365 memcpy(tmp, buf, len);
366 for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version
370 for (p2 = p; *p2 && *p2 != ' ' && p2 - p < 3; p2++)
371 r->code[p2 - p] = *p2;
372 if (!(p = strstr(tmp, "\r\n")))
377 if (!(p2 = strstr(p, "\r\n")))
379 if (p == p2) // End of headers
383 struct http_header *h = *hp = nmem_malloc(c->nmem, sizeof(*h));
384 char *value = strchr(p, ':');
388 h->name = nmem_strdup(c->nmem, p);
389 while (isspace(*value))
391 if (value >= p2) // Empty header;
398 h->value = nmem_strdup(c->nmem, value);
407 static int http_parse_arguments(struct http_request *r, NMEM nmem,
410 const char *p2 = args;
414 struct http_argument *a;
415 const char *equal = strchr(p2, '=');
416 const char *eoa = strchr(p2, '&');
419 yaz_log(YLOG_WARN, "Expected '=' in argument");
423 eoa = equal + strlen(equal); // last argument
424 else if (equal > eoa)
426 yaz_log(YLOG_WARN, "Missing '&' in argument");
429 a = nmem_malloc(nmem, sizeof(struct http_argument));
430 a->name = nmem_strdupn(nmem, p2, equal - p2);
431 a->value = nmem_strdupn(nmem, equal+1, eoa - equal - 1);
432 urldecode(a->name, a->name);
433 urldecode(a->value, a->value);
434 a->next = r->arguments;
443 struct http_request *http_parse_request(struct http_channel *c,
444 struct http_buf **queue,
447 struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
449 char *start = nmem_malloc(c->nmem, len+1);
452 if (http_buf_read(queue, buf, len) < len)
454 yaz_log(YLOG_WARN, "http_buf_read < len (%d)", len);
464 for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
468 yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
473 if (!(buf = strchr(buf, ' ')))
475 yaz_log(YLOG_WARN, "Missing Request-URI in HTTP request");
479 if (!(p = strchr(buf, ' ')))
481 yaz_log(YLOG_WARN, "HTTP Request-URI not terminated (too long?)");
485 if ((p2 = strchr(buf, '?'))) // Do we have arguments?
487 r->path = nmem_strdup(c->nmem, buf);
490 r->search = nmem_strdup(c->nmem, p2);
492 http_parse_arguments(r, c->nmem, p2);
496 if (strncmp(buf, "HTTP/", 5))
497 strcpy(r->http_version, "1.0");
501 buf += 5; // strlen("HTTP/")
503 p = (char*) next_crlf(buf, &skipped);
504 if (!p || skipped < 3 || skipped > 5)
507 memcpy(r->http_version, buf, skipped);
508 r->http_version[skipped] = '\0';
511 strcpy(c->version, r->http_version);
518 p = (char *) next_crlf(buf, &skipped);
523 else if (skipped == 0)
531 char *n_v = nmem_malloc(c->nmem, skipped+1);
532 struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
534 memcpy(n_v, buf, skipped);
537 if (!(cp = strchr(n_v, ':')))
539 h->name = nmem_strdupn(c->nmem, n_v, cp - n_v);
543 h->value = nmem_strdup(c->nmem, cp);
544 h->next = r->headers;
550 // determine if we do keep alive
551 if (!strcmp(c->version, "1.0"))
553 const char *v = http_lookup_header(r->headers, "Connection");
554 if (v && !strcmp(v, "Keep-Alive"))
561 const char *v = http_lookup_header(r->headers, "Connection");
562 if (v && !strcmp(v, "close"))
567 if (buf < start + len)
569 const char *content_type = http_lookup_header(r->headers,
571 r->content_len = start + len - buf;
572 r->content_buf = buf;
574 if (!strcmp(content_type, "application/x-www-form-urlencoded"))
576 http_parse_arguments(r, c->nmem, r->content_buf);
582 static struct http_buf *http_serialize_response(struct http_channel *c,
583 struct http_response *r)
585 struct http_header *h;
587 wrbuf_rewind(c->wrbuf);
588 wrbuf_printf(c->wrbuf, "HTTP/%s %s %s\r\n", c->version, r->code, r->msg);
589 for (h = r->headers; h; h = h->next)
590 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
593 wrbuf_printf(c->wrbuf, "Content-Length: %d\r\n", r->payload ?
594 (int) strlen(r->payload) : 0);
595 wrbuf_printf(c->wrbuf, "Content-Type: %s\r\n", r->content_type);
596 if (!strcmp(r->content_type, "text/xml"))
598 xmlDoc *doc = xmlParseMemory(r->payload, strlen(r->payload));
605 yaz_log(YLOG_WARN, "Sending non-wellformed "
606 "response (bug #1162");
607 yaz_log(YLOG_WARN, "payload: %s", r->payload);
611 wrbuf_puts(c->wrbuf, "\r\n");
614 wrbuf_puts(c->wrbuf, r->payload);
616 return http_buf_bywrbuf(c->wrbuf);
619 // Serialize a HTTP request
620 static struct http_buf *http_serialize_request(struct http_request *r)
622 struct http_channel *c = r->channel;
623 struct http_header *h;
625 wrbuf_rewind(c->wrbuf);
626 wrbuf_printf(c->wrbuf, "%s %s%s%s", r->method, r->path,
627 *r->search ? "?" : "", r->search);
629 wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
631 for (h = r->headers; h; h = h->next)
632 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
634 wrbuf_puts(c->wrbuf, "\r\n");
637 wrbuf_write(c->wrbuf, r->content_buf, r->content_len);
640 yaz_log(YLOG_LOG, "WRITING TO PROXY:\n%s\n----",
641 wrbuf_cstr(c->wrbuf));
643 return http_buf_bywrbuf(c->wrbuf);
647 static int http_weshouldproxy(struct http_request *rq)
649 if (proxy_addr && !strstr(rq->path, "search.pz2"))
655 struct http_header * http_header_append(struct http_channel *ch,
656 struct http_header * hp,
660 struct http_header *hpnew = 0;
665 while (hp && hp->next)
668 if(name && strlen(name)&& value && strlen(value)){
669 hpnew = nmem_malloc(ch->nmem, sizeof *hpnew);
670 hpnew->name = nmem_strdup(ch->nmem, name);
671 hpnew->value = nmem_strdup(ch->nmem, value);
685 static int http_proxy(struct http_request *rq)
687 struct http_channel *c = rq->channel;
688 struct http_proxy *p = c->proxy;
689 struct http_header *hp;
690 struct http_buf *requestbuf;
691 char server_via[128] = "";
692 char server_port[16] = "";
693 struct conf_server *ser = global_parameters.server;
695 if (!p) // This is a new connection. Create a proxy channel
702 if (!(pe = getprotobyname("tcp"))) {
705 if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
707 yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
710 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
711 &one, sizeof(one)) < 0)
713 if ((flags = fcntl(sock, F_GETFL, 0)) < 0)
714 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
715 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
716 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
717 if (connect(sock, (struct sockaddr *) proxy_addr,
718 sizeof(*proxy_addr)) < 0)
719 if (errno != EINPROGRESS)
721 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
725 p = xmalloc(sizeof(struct http_proxy));
728 p->first_response = 1;
730 // We will add EVENT_OUTPUT below
731 p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT);
732 iochan_setdata(p->iochan, p);
733 pazpar2_add_channel(p->iochan);
736 // Do _not_ modify Host: header, just checking it's existence
738 if (!http_lookup_header(rq->headers, "Host"))
740 yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
744 // Add new header about paraz2 version, host, remote client address, etc.
747 hp = http_header_append(c, hp,
748 "X-Pazpar2-Version", PACKAGE_VERSION);
749 hp = http_header_append(c, hp,
750 "X-Pazpar2-Server-Host", ser->host);
751 sprintf(server_port, "%d", ser->port);
752 hp = http_header_append(c, hp,
753 "X-Pazpar2-Server-Port", server_port);
754 sprintf(server_via, "1.1 %s:%s (%s/%s)",
755 ser->host, server_port, PACKAGE_NAME, PACKAGE_VERSION);
756 hp = http_header_append(c, hp, "Via" , server_via);
757 hp = http_header_append(c, hp, "X-Forwarded-For", c->addr);
760 requestbuf = http_serialize_request(rq);
762 http_buf_enqueue(&p->oqueue, requestbuf);
763 iochan_setflag(p->iochan, EVENT_OUTPUT);
767 void http_send_response(struct http_channel *ch)
769 struct http_response *rs = ch->response;
773 hb = http_serialize_response(ch, rs);
776 yaz_log(YLOG_WARN, "Failed to serialize HTTP response");
777 http_destroy(ch->iochan);
781 http_buf_enqueue(&ch->oqueue, hb);
782 iochan_setflag(ch->iochan, EVENT_OUTPUT);
783 ch->state = Http_Idle;
787 static void http_error(struct http_channel *hc, int no, const char *msg)
789 struct http_response *rs = http_create_response(hc);
792 hc->keep_alive = 0; // not keeping this HTTP session alive
794 sprintf(rs->code, "%d", no);
796 rs->msg = nmem_strdup(hc->nmem, msg);
797 rs->payload = nmem_malloc(hc->nmem, 100);
798 yaz_snprintf(rs->payload, 99, "<error>HTTP Error %d: %s</error>\n",
800 http_send_response(hc);
803 static void http_io(IOCHAN i, int event)
805 struct http_channel *hc = iochan_getdata(i);
810 struct http_buf *htbuf;
813 htbuf = http_buf_create();
814 res = read(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1);
815 if (res == -1 && errno == EAGAIN)
817 http_buf_destroy(htbuf);
822 http_buf_destroy(htbuf);
826 htbuf->buf[res] = '\0';
828 http_buf_enqueue(&hc->iqueue, htbuf);
832 if (hc->state == Http_Busy)
834 reqlen = request_check(hc->iqueue);
837 // we have a complete HTTP request
838 nmem_reset(hc->nmem);
839 if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
841 yaz_log(YLOG_WARN, "Failed to parse request");
842 http_error(hc, 400, "Bad Request");
846 yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
848 *hc->request->search ? "?" : "",
849 hc->request->search);
850 if (http_weshouldproxy(hc->request))
851 http_proxy(hc->request);
854 // Execute our business logic!
855 hc->state = Http_Busy;
863 struct http_buf *wb = hc->oqueue;
864 res = write(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len);
867 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
873 hc->oqueue = hc->oqueue->next;
874 http_buf_destroy(wb);
889 iochan_clearflag(i, EVENT_OUTPUT);
891 iochan_setevent(hc->iochan, EVENT_INPUT);
896 if (!hc->oqueue && hc->proxy && !hc->proxy->iochan)
897 http_destroy(i); // Server closed; we're done
900 yaz_log(YLOG_WARN, "Unexpected event on connection");
905 // Handles I/O on a client connection to a backend web server (proxy mode)
906 static void proxy_io(IOCHAN pi, int event)
908 struct http_proxy *pc = iochan_getdata(pi);
909 struct http_channel *hc = pc->channel;
914 struct http_buf *htbuf;
917 htbuf = http_buf_create();
918 res = read(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1);
919 if (res == 0 || (res < 0 && errno != EINPROGRESS))
923 yaz_log(YLOG_WARN, "Proxy read came up short");
924 // Close channel and alert client HTTP channel that we're gone
925 http_buf_destroy(htbuf);
926 close(iochan_getfd(pi));
932 http_destroy(hc->iochan);
938 htbuf->buf[res] = '\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;
1035 yaz_log(YLOG_WARN, "Invalid HTTP forward address");
1038 strcpy(r->addr, addr);
1044 /* Accept a new command connection */
1045 static void http_accept(IOCHAN i, int event)
1047 struct sockaddr_in addr;
1048 int fd = iochan_getfd(i);
1053 struct http_channel *ch;
1056 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
1058 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
1061 if ((flags = fcntl(s, F_GETFL, 0)) < 0)
1062 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
1063 if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
1064 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
1066 yaz_log(YLOG_DEBUG, "New command connection");
1067 c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT);
1069 ch = http_create(inet_ntoa(addr.sin_addr));
1071 iochan_setdata(c, ch);
1073 pazpar2_add_channel(c);
1076 /* Create a http-channel listener, syntax [host:]port */
1077 void http_init(const char *addr)
1082 struct sockaddr_in myaddr;
1087 yaz_log(YLOG_LOG, "HTTP listener %s", addr);
1089 memset(&myaddr, 0, sizeof myaddr);
1090 myaddr.sin_family = AF_INET;
1091 pp = strchr(addr, ':');
1094 int len = pp - addr;
1098 strncpy(hostname, addr, len);
1099 hostname[len] = '\0';
1100 if (!(he = gethostbyname(hostname))){
1101 yaz_log(YLOG_FATAL, "Unable to resolve '%s'", hostname);
1105 memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1106 port = atoi(pp + 1);
1111 myaddr.sin_addr.s_addr = INADDR_ANY;
1114 myaddr.sin_port = htons(port);
1116 if (!(p = getprotobyname("tcp"))) {
1119 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
1120 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
1121 if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
1122 &one, sizeof(one)) < 0)
1125 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
1127 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
1130 if (listen(l, SOMAXCONN) < 0)
1132 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
1136 c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT);
1137 pazpar2_add_channel(c);
1140 void http_set_proxyaddr(char *host, char *base_url)
1146 strcpy(myurl, base_url);
1147 strcpy(proxy_url, host);
1148 p = strchr(host, ':');
1149 yaz_log(YLOG_DEBUG, "Proxying for %s", host);
1150 yaz_log(YLOG_LOG, "HTTP backend %s", proxy_url);
1157 if (!(he = gethostbyname(host)))
1159 fprintf(stderr, "Failed to lookup '%s'\n", host);
1162 proxy_addr = xmalloc(sizeof(struct sockaddr_in));
1163 proxy_addr->sin_family = he->h_addrtype;
1164 memcpy(&proxy_addr->sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1165 proxy_addr->sin_port = htons(port);
1168 static void http_fire_observers(struct http_channel *c)
1170 http_channel_observer_t p = c->observers;
1173 p->destroy(p->data, c, p->data2);
1178 static void http_destroy_observers(struct http_channel *c)
1180 while (c->observers)
1182 http_channel_observer_t obs = c->observers;
1183 c->observers = obs->next;
1188 http_channel_observer_t http_add_observer(struct http_channel *c, void *data,
1189 http_channel_destroy_t des)
1191 http_channel_observer_t obs = xmalloc(sizeof(*obs));
1196 obs->next = c->observers;
1201 void http_remove_observer(http_channel_observer_t obs)
1203 struct http_channel *c = obs->chan;
1204 http_channel_observer_t found, *p = &c->observers;
1213 struct http_channel *http_channel_observer_chan(http_channel_observer_t obs)
1218 void http_observer_set_data2(http_channel_observer_t obs, void *data2)
1227 * indent-tabs-mode: nil
1229 * vim: shiftwidth=4 tabstop=8 expandtab