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>
51 #include "http_command.h"
53 #define MAX_HTTP_HEADER 4096
55 static void proxy_io(IOCHAN i, int event);
56 static struct http_channel *http_create(const char *addr);
57 static void http_destroy(IOCHAN i);
59 // If this is set, we proxy normal HTTP requests
60 static struct sockaddr_in *proxy_addr = 0;
61 static char proxy_url[256] = "";
62 static char myurl[256] = "";
63 static struct http_buf *http_buf_freelist = 0;
64 static struct http_channel *http_channel_freelist = 0;
66 struct http_channel_observer_s {
69 http_channel_destroy_t destroy;
70 struct http_channel_observer_s *next;
71 struct http_channel *chan;
75 static const char *http_lookup_header(struct http_header *header,
78 for (; header; header = header->next)
79 if (!strcasecmp(name, header->name))
84 static struct http_buf *http_buf_create()
88 if (http_buf_freelist)
90 r = http_buf_freelist;
91 http_buf_freelist = http_buf_freelist->next;
94 r = xmalloc(sizeof(struct http_buf));
101 static void http_buf_destroy(struct http_buf *b)
103 b->next = http_buf_freelist;
104 http_buf_freelist = b;
107 static void http_buf_destroy_queue(struct http_buf *b)
118 static struct http_buf *http_buf_bybuf(char *b, int len)
120 struct http_buf *res = 0;
121 struct http_buf **p = &res;
126 if (tocopy > HTTP_BUF_SIZE)
127 tocopy = HTTP_BUF_SIZE;
128 *p = http_buf_create();
129 memcpy((*p)->buf, b, tocopy);
138 // Add a (chain of) buffers to the end of an existing queue.
139 static void http_buf_enqueue(struct http_buf **queue, struct http_buf *b)
142 queue = &(*queue)->next;
146 static struct http_buf *http_buf_bywrbuf(WRBUF wrbuf)
148 // Heavens to Betsy (buf)!
149 return http_buf_bybuf(wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
152 // Non-destructively collapse chain of buffers into a string (max *len)
154 static void http_buf_peek(struct http_buf *b, char *buf, int len)
157 while (b && rd < len)
159 int toread = len - rd;
162 memcpy(buf + rd, b->buf + b->offset, toread);
169 static int http_buf_size(struct http_buf *b)
172 for (; b; b = b->next)
177 // Ddestructively munch up to len from head of queue.
178 static int http_buf_read(struct http_buf **b, char *buf, int len)
181 while ((*b) && rd < len)
183 int toread = len - rd;
184 if (toread > (*b)->len)
186 memcpy(buf + rd, (*b)->buf + (*b)->offset, toread);
188 if (toread < (*b)->len)
191 (*b)->offset += toread;
196 struct http_buf *n = (*b)->next;
197 http_buf_destroy(*b);
205 // Buffers may overlap.
206 static void urldecode(char *i, char *o)
218 sscanf(i, "%2hhx", o);
228 // Warning: Buffers may not overlap
229 void urlencode(const char *i, char *o)
233 if (strchr(" /:", *i))
235 sprintf(o, "%%%.2X", (int) *i);
245 void http_addheader(struct http_response *r, const char *name, const char *value)
247 struct http_channel *c = r->channel;
248 struct http_header *h = nmem_malloc(c->nmem, sizeof *h);
249 h->name = nmem_strdup(c->nmem, name);
250 h->value = nmem_strdup(c->nmem, value);
251 h->next = r->headers;
255 char *http_argbyname(struct http_request *r, char *name)
257 struct http_argument *p;
260 for (p = r->arguments; p; p = p->next)
261 if (!strcmp(p->name, name))
266 char *http_headerbyname(struct http_header *h, char *name)
268 for (; h; h = h->next)
269 if (!strcmp(h->name, name))
274 struct http_response *http_create_response(struct http_channel *c)
276 struct http_response *r = nmem_malloc(c->nmem, sizeof(*r));
277 strcpy(r->code, "200");
282 r->content_type = "text/xml";
287 static const char *next_crlf(const char *cp, size_t *skipped)
289 const char *next_cp = strchr(cp, '\n');
292 if (next_cp > cp && next_cp[-1] == '\r')
293 *skipped = next_cp - cp - 1;
295 *skipped = next_cp - cp;
301 // Check if buf contains a package (minus payload)
302 static int package_check(const char *buf, int sz)
310 const char *b = next_crlf(buf, &skipped);
314 // we did not find CRLF.. See if buffer is too large..
315 if (sz >= MAX_HTTP_HEADER-1)
316 return MAX_HTTP_HEADER-1; // yes. Return that (will fail later)
322 // CRLF CRLF , i.e. end of header
323 if (len + content_len <= sz)
324 return len + content_len;
328 // following first skip of \r\n so that we don't consider Method
329 if (!strncasecmp(buf, "Content-Length:", 15))
331 const char *cp = buf+15;
335 while (*cp && isdigit(*cp))
336 content_len = content_len*10 + (*cp++ - '0');
337 if (content_len < 0) /* prevent negative offsets */
341 return 0; // incomplete request
344 // Check if we have a request. Return 0 or length
345 static int request_check(struct http_buf *queue)
347 char tmp[MAX_HTTP_HEADER];
349 // only peek at the header..
350 http_buf_peek(queue, tmp, MAX_HTTP_HEADER-1);
351 // still we only return non-zero if the complete request is received..
352 return package_check(tmp, http_buf_size(queue));
355 struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len)
357 char tmp[MAX_HTTP_HEADER];
358 struct http_response *r = http_create_response(c);
360 struct http_header **hp = &r->headers;
362 if (len >= MAX_HTTP_HEADER)
364 memcpy(tmp, buf, len);
365 for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version
369 for (p2 = p; *p2 && *p2 != ' ' && p2 - p < 3; p2++)
370 r->code[p2 - p] = *p2;
371 if (!(p = strstr(tmp, "\r\n")))
376 if (!(p2 = strstr(p, "\r\n")))
378 if (p == p2) // End of headers
382 struct http_header *h = *hp = nmem_malloc(c->nmem, sizeof(*h));
383 char *value = strchr(p, ':');
387 h->name = nmem_strdup(c->nmem, p);
388 while (isspace(*value))
390 if (value >= p2) // Empty header;
397 h->value = nmem_strdup(c->nmem, value);
406 static int http_parse_arguments(struct http_request *r, NMEM nmem,
409 const char *p2 = args;
413 struct http_argument *a;
414 const char *equal = strchr(p2, '=');
415 const char *eoa = strchr(p2, '&');
418 yaz_log(YLOG_WARN, "Expected '=' in argument");
422 eoa = equal + strlen(equal); // last argument
423 else if (equal > eoa)
425 yaz_log(YLOG_WARN, "Missing '&' in argument");
428 a = nmem_malloc(nmem, sizeof(struct http_argument));
429 a->name = nmem_strdupn(nmem, p2, equal - p2);
430 a->value = nmem_strdupn(nmem, equal+1, eoa - equal - 1);
431 urldecode(a->name, a->name);
432 urldecode(a->value, a->value);
433 a->next = r->arguments;
442 struct http_request *http_parse_request(struct http_channel *c,
443 struct http_buf **queue,
446 struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
448 char *start = nmem_malloc(c->nmem, len+1);
451 if (http_buf_read(queue, buf, len) < len)
453 yaz_log(YLOG_WARN, "http_buf_read < len (%d)", len);
463 for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
467 yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
472 if (!(buf = strchr(buf, ' ')))
474 yaz_log(YLOG_WARN, "Missing Request-URI in HTTP request");
478 if (!(p = strchr(buf, ' ')))
480 yaz_log(YLOG_WARN, "HTTP Request-URI not terminated (too long?)");
484 if ((p2 = strchr(buf, '?'))) // Do we have arguments?
486 r->path = nmem_strdup(c->nmem, buf);
489 r->search = nmem_strdup(c->nmem, p2);
491 http_parse_arguments(r, c->nmem, p2);
495 if (strncmp(buf, "HTTP/", 5))
496 strcpy(r->http_version, "1.0");
500 buf += 5; // strlen("HTTP/")
502 p = (char*) next_crlf(buf, &skipped);
503 if (!p || skipped < 3 || skipped > 5)
506 memcpy(r->http_version, buf, skipped);
507 r->http_version[skipped] = '\0';
510 strcpy(c->version, r->http_version);
517 p = (char *) next_crlf(buf, &skipped);
522 else if (skipped == 0)
530 char *n_v = nmem_malloc(c->nmem, skipped+1);
531 struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
533 memcpy(n_v, buf, skipped);
536 if (!(cp = strchr(n_v, ':')))
538 h->name = nmem_strdupn(c->nmem, n_v, cp - n_v);
542 h->value = nmem_strdup(c->nmem, cp);
543 h->next = r->headers;
549 // determine if we do keep alive
550 if (!strcmp(c->version, "1.0"))
552 const char *v = http_lookup_header(r->headers, "Connection");
553 if (v && !strcmp(v, "Keep-Alive"))
560 const char *v = http_lookup_header(r->headers, "Connection");
561 if (v && !strcmp(v, "close"))
566 if (buf < start + len)
568 const char *content_type = http_lookup_header(r->headers,
570 r->content_len = start + len - buf;
571 r->content_buf = buf;
573 if (!strcmp(content_type, "application/x-www-form-urlencoded"))
575 http_parse_arguments(r, c->nmem, r->content_buf);
581 static struct http_buf *http_serialize_response(struct http_channel *c,
582 struct http_response *r)
584 struct http_header *h;
586 wrbuf_rewind(c->wrbuf);
587 wrbuf_printf(c->wrbuf, "HTTP/%s %s %s\r\n", c->version, r->code, r->msg);
588 for (h = r->headers; h; h = h->next)
589 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
592 wrbuf_printf(c->wrbuf, "Content-Length: %d\r\n", r->payload ?
593 (int) strlen(r->payload) : 0);
594 wrbuf_printf(c->wrbuf, "Content-Type: %s\r\n", r->content_type);
595 if (!strcmp(r->content_type, "text/xml"))
597 xmlDoc *doc = xmlParseMemory(r->payload, strlen(r->payload));
604 yaz_log(YLOG_WARN, "Sending non-wellformed "
605 "response (bug #1162");
606 yaz_log(YLOG_WARN, "payload: %s", r->payload);
610 wrbuf_puts(c->wrbuf, "\r\n");
613 wrbuf_puts(c->wrbuf, r->payload);
615 return http_buf_bywrbuf(c->wrbuf);
618 // Serialize a HTTP request
619 static struct http_buf *http_serialize_request(struct http_request *r)
621 struct http_channel *c = r->channel;
622 struct http_header *h;
624 wrbuf_rewind(c->wrbuf);
625 wrbuf_printf(c->wrbuf, "%s %s%s%s", r->method, r->path,
626 *r->search ? "?" : "", r->search);
628 wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
630 for (h = r->headers; h; h = h->next)
631 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
633 wrbuf_puts(c->wrbuf, "\r\n");
636 wrbuf_write(c->wrbuf, r->content_buf, r->content_len);
639 yaz_log(YLOG_LOG, "WRITING TO PROXY:\n%s\n----",
640 wrbuf_cstr(c->wrbuf));
642 return http_buf_bywrbuf(c->wrbuf);
646 static int http_weshouldproxy(struct http_request *rq)
648 if (proxy_addr && !strstr(rq->path, "search.pz2"))
654 struct http_header * http_header_append(struct http_channel *ch,
655 struct http_header * hp,
659 struct http_header *hpnew = 0;
664 while (hp && hp->next)
667 if(name && strlen(name)&& value && strlen(value)){
668 hpnew = nmem_malloc(ch->nmem, sizeof *hpnew);
669 hpnew->name = nmem_strdup(ch->nmem, name);
670 hpnew->value = nmem_strdup(ch->nmem, value);
684 static int http_proxy(struct http_request *rq)
686 struct http_channel *c = rq->channel;
687 struct http_proxy *p = c->proxy;
688 struct http_header *hp;
689 struct http_buf *requestbuf;
690 char server_via[128] = "";
691 char server_port[16] = "";
692 struct conf_server *ser = global_parameters.server;
694 if (!p) // This is a new connection. Create a proxy channel
701 if (!(pe = getprotobyname("tcp"))) {
704 if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
706 yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
709 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
710 &one, sizeof(one)) < 0)
712 if ((flags = fcntl(sock, F_GETFL, 0)) < 0)
713 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
714 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
715 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
716 if (connect(sock, (struct sockaddr *) proxy_addr,
717 sizeof(*proxy_addr)) < 0)
718 if (errno != EINPROGRESS)
720 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
724 p = xmalloc(sizeof(struct http_proxy));
727 p->first_response = 1;
729 // We will add EVENT_OUTPUT below
730 p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT);
731 iochan_setdata(p->iochan, p);
732 pazpar2_add_channel(p->iochan);
735 // Do _not_ modify Host: header, just checking it's existence
737 if (!http_lookup_header(rq->headers, "Host"))
739 yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
743 // Add new header about paraz2 version, host, remote client address, etc.
746 hp = http_header_append(c, hp,
747 "X-Pazpar2-Version", PACKAGE_VERSION);
748 hp = http_header_append(c, hp,
749 "X-Pazpar2-Server-Host", ser->host);
750 sprintf(server_port, "%d", ser->port);
751 hp = http_header_append(c, hp,
752 "X-Pazpar2-Server-Port", server_port);
753 sprintf(server_via, "1.1 %s:%s (%s/%s)",
754 ser->host, server_port, PACKAGE_NAME, PACKAGE_VERSION);
755 hp = http_header_append(c, hp, "Via" , server_via);
756 hp = http_header_append(c, hp, "X-Forwarded-For", c->addr);
759 requestbuf = http_serialize_request(rq);
761 http_buf_enqueue(&p->oqueue, requestbuf);
762 iochan_setflag(p->iochan, EVENT_OUTPUT);
766 void http_send_response(struct http_channel *ch)
768 struct http_response *rs = ch->response;
772 hb = http_serialize_response(ch, rs);
775 yaz_log(YLOG_WARN, "Failed to serialize HTTP response");
776 http_destroy(ch->iochan);
780 http_buf_enqueue(&ch->oqueue, hb);
781 iochan_setflag(ch->iochan, EVENT_OUTPUT);
782 ch->state = Http_Idle;
786 static void http_error(struct http_channel *hc, int no, const char *msg)
788 struct http_response *rs = http_create_response(hc);
791 hc->keep_alive = 0; // not keeping this HTTP session alive
793 sprintf(rs->code, "%d", no);
795 rs->msg = nmem_strdup(hc->nmem, msg);
796 rs->payload = nmem_malloc(hc->nmem, 100);
797 yaz_snprintf(rs->payload, 99, "<error>HTTP Error %d: %s</error>\n",
799 http_send_response(hc);
802 static void http_io(IOCHAN i, int event)
804 struct http_channel *hc = iochan_getdata(i);
809 struct http_buf *htbuf;
812 htbuf = http_buf_create();
813 res = read(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1);
814 if (res == -1 && errno == EAGAIN)
816 http_buf_destroy(htbuf);
821 http_buf_destroy(htbuf);
825 htbuf->buf[res] = '\0';
827 http_buf_enqueue(&hc->iqueue, htbuf);
831 if (hc->state == Http_Busy)
833 reqlen = request_check(hc->iqueue);
836 // we have a complete HTTP request
837 nmem_reset(hc->nmem);
838 if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
840 yaz_log(YLOG_WARN, "Failed to parse request");
841 http_error(hc, 400, "Bad Request");
845 yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
847 *hc->request->search ? "?" : "",
848 hc->request->search);
849 if (http_weshouldproxy(hc->request))
850 http_proxy(hc->request);
853 // Execute our business logic!
854 hc->state = Http_Busy;
862 struct http_buf *wb = hc->oqueue;
863 res = write(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len);
866 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
872 hc->oqueue = hc->oqueue->next;
873 http_buf_destroy(wb);
888 iochan_clearflag(i, EVENT_OUTPUT);
890 iochan_setevent(hc->iochan, EVENT_INPUT);
895 if (!hc->oqueue && hc->proxy && !hc->proxy->iochan)
896 http_destroy(i); // Server closed; we're done
899 yaz_log(YLOG_WARN, "Unexpected event on connection");
904 // Handles I/O on a client connection to a backend web server (proxy mode)
905 static void proxy_io(IOCHAN pi, int event)
907 struct http_proxy *pc = iochan_getdata(pi);
908 struct http_channel *hc = pc->channel;
913 struct http_buf *htbuf;
916 htbuf = http_buf_create();
917 res = read(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1);
918 if (res == 0 || (res < 0 && errno != EINPROGRESS))
922 yaz_log(YLOG_WARN, "Proxy read came up short");
923 // Close channel and alert client HTTP channel that we're gone
924 http_buf_destroy(htbuf);
925 close(iochan_getfd(pi));
931 http_destroy(hc->iochan);
937 htbuf->buf[res] = '\0';
940 // Write any remaining payload
941 if (htbuf->len - htbuf->offset > 0)
942 http_buf_enqueue(&hc->oqueue, htbuf);
944 iochan_setflag(hc->iochan, EVENT_OUTPUT);
947 if (!(htbuf = pc->oqueue))
949 iochan_clearflag(pi, EVENT_OUTPUT);
952 res = write(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len);
955 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
956 http_destroy(hc->iochan);
959 if (res == htbuf->len)
961 struct http_buf *np = htbuf->next;
962 http_buf_destroy(htbuf);
968 htbuf->offset += res;
972 iochan_setflags(pi, EVENT_INPUT); // Turns off output flag
976 yaz_log(YLOG_WARN, "Unexpected event on connection");
977 http_destroy(hc->iochan);
981 static void http_fire_observers(struct http_channel *c);
982 static void http_destroy_observers(struct http_channel *c);
985 static void http_destroy(IOCHAN i)
987 struct http_channel *s = iochan_getdata(i);
991 if (s->proxy->iochan)
993 close(iochan_getfd(s->proxy->iochan));
994 iochan_destroy(s->proxy->iochan);
996 http_buf_destroy_queue(s->proxy->oqueue);
999 http_buf_destroy_queue(s->iqueue);
1000 http_buf_destroy_queue(s->oqueue);
1001 http_fire_observers(s);
1002 http_destroy_observers(s);
1003 s->next = http_channel_freelist;
1004 http_channel_freelist = s;
1005 close(iochan_getfd(i));
1009 static struct http_channel *http_create(const char *addr)
1011 struct http_channel *r = http_channel_freelist;
1015 http_channel_freelist = r->next;
1016 nmem_reset(r->nmem);
1017 wrbuf_rewind(r->wrbuf);
1021 r = xmalloc(sizeof(struct http_channel));
1022 r->nmem = nmem_create();
1023 r->wrbuf = wrbuf_alloc();
1027 r->iqueue = r->oqueue = 0;
1028 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, p->data2);
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));
1195 obs->next = c->observers;
1200 void http_remove_observer(http_channel_observer_t obs)
1202 struct http_channel *c = obs->chan;
1203 http_channel_observer_t found, *p = &c->observers;
1212 struct http_channel *http_channel_observer_chan(http_channel_observer_t obs)
1217 void http_observer_set_data2(http_channel_observer_t obs, void *data2)
1226 * indent-tabs-mode: nil
1228 * vim: shiftwidth=4 tabstop=8 expandtab