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
23 typedef int socklen_t;
27 #include <sys/socket.h>
30 #include <sys/types.h>
35 #include <yaz/snprintf.h>
57 #include <netinet/in.h>
61 #include <arpa/inet.h>
64 #include <yaz/yaz-util.h>
65 #include <yaz/comstack.h>
72 #include "http_command.h"
74 #define MAX_HTTP_HEADER 4096
76 static void proxy_io(IOCHAN i, int event);
77 static struct http_channel *http_create(const char *addr);
78 static void http_destroy(IOCHAN i);
80 // If this is set, we proxy normal HTTP requests
81 static struct sockaddr_in *proxy_addr = 0;
82 static char proxy_url[256] = "";
83 static char myurl[256] = "";
84 static struct http_buf *http_buf_freelist = 0;
85 static struct http_channel *http_channel_freelist = 0;
87 struct http_channel_observer_s {
90 http_channel_destroy_t destroy;
91 struct http_channel_observer_s *next;
92 struct http_channel *chan;
96 static const char *http_lookup_header(struct http_header *header,
99 for (; header; header = header->next)
100 if (!strcasecmp(name, header->name))
101 return header->value;
105 static struct http_buf *http_buf_create()
109 if (http_buf_freelist)
111 r = http_buf_freelist;
112 http_buf_freelist = http_buf_freelist->next;
115 r = xmalloc(sizeof(struct http_buf));
122 static void http_buf_destroy(struct http_buf *b)
124 b->next = http_buf_freelist;
125 http_buf_freelist = b;
128 static void http_buf_destroy_queue(struct http_buf *b)
139 static struct http_buf *http_buf_bybuf(char *b, int len)
141 struct http_buf *res = 0;
142 struct http_buf **p = &res;
147 if (tocopy > HTTP_BUF_SIZE)
148 tocopy = HTTP_BUF_SIZE;
149 *p = http_buf_create();
150 memcpy((*p)->buf, b, tocopy);
159 // Add a (chain of) buffers to the end of an existing queue.
160 static void http_buf_enqueue(struct http_buf **queue, struct http_buf *b)
163 queue = &(*queue)->next;
167 static struct http_buf *http_buf_bywrbuf(WRBUF wrbuf)
169 // Heavens to Betsy (buf)!
170 return http_buf_bybuf(wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
173 // Non-destructively collapse chain of buffers into a string (max *len)
175 static void http_buf_peek(struct http_buf *b, char *buf, int len)
178 while (b && rd < len)
180 int toread = len - rd;
183 memcpy(buf + rd, b->buf + b->offset, toread);
190 static int http_buf_size(struct http_buf *b)
193 for (; b; b = b->next)
198 // Ddestructively munch up to len from head of queue.
199 static int http_buf_read(struct http_buf **b, char *buf, int len)
202 while ((*b) && rd < len)
204 int toread = len - rd;
205 if (toread > (*b)->len)
207 memcpy(buf + rd, (*b)->buf + (*b)->offset, toread);
209 if (toread < (*b)->len)
212 (*b)->offset += toread;
217 struct http_buf *n = (*b)->next;
218 http_buf_destroy(*b);
226 // Buffers may overlap.
227 static void urldecode(char *i, char *o)
239 sscanf(i, "%2hhx", o);
249 // Warning: Buffers may not overlap
250 void urlencode(const char *i, char *o)
254 if (strchr(" /:", *i))
256 sprintf(o, "%%%.2X", (int) *i);
266 void http_addheader(struct http_response *r, const char *name, const char *value)
268 struct http_channel *c = r->channel;
269 struct http_header *h = nmem_malloc(c->nmem, sizeof *h);
270 h->name = nmem_strdup(c->nmem, name);
271 h->value = nmem_strdup(c->nmem, value);
272 h->next = r->headers;
276 char *http_argbyname(struct http_request *r, char *name)
278 struct http_argument *p;
281 for (p = r->arguments; p; p = p->next)
282 if (!strcmp(p->name, name))
287 char *http_headerbyname(struct http_header *h, char *name)
289 for (; h; h = h->next)
290 if (!strcmp(h->name, name))
295 struct http_response *http_create_response(struct http_channel *c)
297 struct http_response *r = nmem_malloc(c->nmem, sizeof(*r));
298 strcpy(r->code, "200");
303 r->content_type = "text/xml";
308 static const char *next_crlf(const char *cp, size_t *skipped)
310 const char *next_cp = strchr(cp, '\n');
313 if (next_cp > cp && next_cp[-1] == '\r')
314 *skipped = next_cp - cp - 1;
316 *skipped = next_cp - cp;
322 // Check if buf contains a package (minus payload)
323 static int package_check(const char *buf, int sz)
331 const char *b = next_crlf(buf, &skipped);
335 // we did not find CRLF.. See if buffer is too large..
336 if (sz >= MAX_HTTP_HEADER-1)
337 return MAX_HTTP_HEADER-1; // yes. Return that (will fail later)
343 // CRLF CRLF , i.e. end of header
344 if (len + content_len <= sz)
345 return len + content_len;
349 // following first skip of \r\n so that we don't consider Method
350 if (!strncasecmp(buf, "Content-Length:", 15))
352 const char *cp = buf+15;
356 while (*cp && isdigit(*cp))
357 content_len = content_len*10 + (*cp++ - '0');
358 if (content_len < 0) /* prevent negative offsets */
362 return 0; // incomplete request
365 // Check if we have a request. Return 0 or length
366 static int request_check(struct http_buf *queue)
368 char tmp[MAX_HTTP_HEADER];
370 // only peek at the header..
371 http_buf_peek(queue, tmp, MAX_HTTP_HEADER-1);
372 // still we only return non-zero if the complete request is received..
373 return package_check(tmp, http_buf_size(queue));
376 struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len)
378 char tmp[MAX_HTTP_HEADER];
379 struct http_response *r = http_create_response(c);
381 struct http_header **hp = &r->headers;
383 if (len >= MAX_HTTP_HEADER)
385 memcpy(tmp, buf, len);
386 for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version
390 for (p2 = p; *p2 && *p2 != ' ' && p2 - p < 3; p2++)
391 r->code[p2 - p] = *p2;
392 if (!(p = strstr(tmp, "\r\n")))
397 if (!(p2 = strstr(p, "\r\n")))
399 if (p == p2) // End of headers
403 struct http_header *h = *hp = nmem_malloc(c->nmem, sizeof(*h));
404 char *value = strchr(p, ':');
408 h->name = nmem_strdup(c->nmem, p);
409 while (isspace(*value))
411 if (value >= p2) // Empty header;
418 h->value = nmem_strdup(c->nmem, value);
427 static int http_parse_arguments(struct http_request *r, NMEM nmem,
430 const char *p2 = args;
434 struct http_argument *a;
435 const char *equal = strchr(p2, '=');
436 const char *eoa = strchr(p2, '&');
439 yaz_log(YLOG_WARN, "Expected '=' in argument");
443 eoa = equal + strlen(equal); // last argument
444 else if (equal > eoa)
446 yaz_log(YLOG_WARN, "Missing '&' in argument");
449 a = nmem_malloc(nmem, sizeof(struct http_argument));
450 a->name = nmem_strdupn(nmem, p2, equal - p2);
451 a->value = nmem_strdupn(nmem, equal+1, eoa - equal - 1);
452 urldecode(a->name, a->name);
453 urldecode(a->value, a->value);
454 a->next = r->arguments;
463 struct http_request *http_parse_request(struct http_channel *c,
464 struct http_buf **queue,
467 struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
469 char *start = nmem_malloc(c->nmem, len+1);
472 if (http_buf_read(queue, buf, len) < len)
474 yaz_log(YLOG_WARN, "http_buf_read < len (%d)", len);
484 for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
488 yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
493 if (!(buf = strchr(buf, ' ')))
495 yaz_log(YLOG_WARN, "Missing Request-URI in HTTP request");
499 if (!(p = strchr(buf, ' ')))
501 yaz_log(YLOG_WARN, "HTTP Request-URI not terminated (too long?)");
505 if ((p2 = strchr(buf, '?'))) // Do we have arguments?
507 r->path = nmem_strdup(c->nmem, buf);
510 r->search = nmem_strdup(c->nmem, p2);
512 http_parse_arguments(r, c->nmem, p2);
516 if (strncmp(buf, "HTTP/", 5))
517 strcpy(r->http_version, "1.0");
521 buf += 5; // strlen("HTTP/")
523 p = (char*) next_crlf(buf, &skipped);
524 if (!p || skipped < 3 || skipped > 5)
527 memcpy(r->http_version, buf, skipped);
528 r->http_version[skipped] = '\0';
531 strcpy(c->version, r->http_version);
538 p = (char *) next_crlf(buf, &skipped);
543 else if (skipped == 0)
551 char *n_v = nmem_malloc(c->nmem, skipped+1);
552 struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
554 memcpy(n_v, buf, skipped);
557 if (!(cp = strchr(n_v, ':')))
559 h->name = nmem_strdupn(c->nmem, n_v, cp - n_v);
563 h->value = nmem_strdup(c->nmem, cp);
564 h->next = r->headers;
570 // determine if we do keep alive
571 if (!strcmp(c->version, "1.0"))
573 const char *v = http_lookup_header(r->headers, "Connection");
574 if (v && !strcmp(v, "Keep-Alive"))
581 const char *v = http_lookup_header(r->headers, "Connection");
582 if (v && !strcmp(v, "close"))
587 if (buf < start + len)
589 const char *content_type = http_lookup_header(r->headers,
591 r->content_len = start + len - buf;
592 r->content_buf = buf;
594 if (!strcmp(content_type, "application/x-www-form-urlencoded"))
596 http_parse_arguments(r, c->nmem, r->content_buf);
602 static struct http_buf *http_serialize_response(struct http_channel *c,
603 struct http_response *r)
605 struct http_header *h;
607 wrbuf_rewind(c->wrbuf);
608 wrbuf_printf(c->wrbuf, "HTTP/%s %s %s\r\n", c->version, r->code, r->msg);
609 for (h = r->headers; h; h = h->next)
610 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
613 wrbuf_printf(c->wrbuf, "Content-Length: %d\r\n", r->payload ?
614 (int) strlen(r->payload) : 0);
615 wrbuf_printf(c->wrbuf, "Content-Type: %s\r\n", r->content_type);
616 if (!strcmp(r->content_type, "text/xml"))
618 xmlDoc *doc = xmlParseMemory(r->payload, strlen(r->payload));
625 yaz_log(YLOG_WARN, "Sending non-wellformed "
626 "response (bug #1162");
627 yaz_log(YLOG_WARN, "payload: %s", r->payload);
631 wrbuf_puts(c->wrbuf, "\r\n");
634 wrbuf_puts(c->wrbuf, r->payload);
636 return http_buf_bywrbuf(c->wrbuf);
639 // Serialize a HTTP request
640 static struct http_buf *http_serialize_request(struct http_request *r)
642 struct http_channel *c = r->channel;
643 struct http_header *h;
645 wrbuf_rewind(c->wrbuf);
646 wrbuf_printf(c->wrbuf, "%s %s%s%s", r->method, r->path,
647 *r->search ? "?" : "", r->search);
649 wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
651 for (h = r->headers; h; h = h->next)
652 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
654 wrbuf_puts(c->wrbuf, "\r\n");
657 wrbuf_write(c->wrbuf, r->content_buf, r->content_len);
660 yaz_log(YLOG_LOG, "WRITING TO PROXY:\n%s\n----",
661 wrbuf_cstr(c->wrbuf));
663 return http_buf_bywrbuf(c->wrbuf);
667 static int http_weshouldproxy(struct http_request *rq)
669 if (proxy_addr && !strstr(rq->path, "search.pz2"))
675 struct http_header * http_header_append(struct http_channel *ch,
676 struct http_header * hp,
680 struct http_header *hpnew = 0;
685 while (hp && hp->next)
688 if(name && strlen(name)&& value && strlen(value)){
689 hpnew = nmem_malloc(ch->nmem, sizeof *hpnew);
690 hpnew->name = nmem_strdup(ch->nmem, name);
691 hpnew->value = nmem_strdup(ch->nmem, value);
704 static int is_inprogress(void)
707 if (WSAGetLastError() != WSAEWOULDBLOCK)
710 if (errno != EINPROGRESS)
716 static void enable_nonblock(int sock)
720 flags = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
721 if (ioctlsocket(sock, FIONBIO, &flags) < 0)
722 yaz_log(YLOG_FATAL|YLOG_ERRNO, "ioctlsocket");
724 if ((flags = fcntl(sock, F_GETFL, 0)) < 0)
725 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
726 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
727 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
731 static int http_proxy(struct http_request *rq)
733 struct http_channel *c = rq->channel;
734 struct http_proxy *p = c->proxy;
735 struct http_header *hp;
736 struct http_buf *requestbuf;
737 char server_via[128] = "";
738 char server_port[16] = "";
739 struct conf_server *ser = global_parameters.server;
741 if (!p) // This is a new connection. Create a proxy channel
747 if (!(pe = getprotobyname("tcp"))) {
750 if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
752 yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
755 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
756 &one, sizeof(one)) < 0)
758 enable_nonblock(sock);
759 if (connect(sock, (struct sockaddr *) proxy_addr,
760 sizeof(*proxy_addr)) < 0)
762 if (!is_inprogress())
764 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
768 p = xmalloc(sizeof(struct http_proxy));
771 p->first_response = 1;
773 // We will add EVENT_OUTPUT below
774 p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT);
775 iochan_setdata(p->iochan, p);
776 pazpar2_add_channel(p->iochan);
779 // Do _not_ modify Host: header, just checking it's existence
781 if (!http_lookup_header(rq->headers, "Host"))
783 yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
787 // Add new header about paraz2 version, host, remote client address, etc.
790 hp = http_header_append(c, hp,
791 "X-Pazpar2-Version", PACKAGE_VERSION);
792 hp = http_header_append(c, hp,
793 "X-Pazpar2-Server-Host", ser->host);
794 sprintf(server_port, "%d", ser->port);
795 hp = http_header_append(c, hp,
796 "X-Pazpar2-Server-Port", server_port);
797 sprintf(server_via, "1.1 %s:%s (%s/%s)",
798 ser->host, server_port, PACKAGE_NAME, PACKAGE_VERSION);
799 hp = http_header_append(c, hp, "Via" , server_via);
800 hp = http_header_append(c, hp, "X-Forwarded-For", c->addr);
803 requestbuf = http_serialize_request(rq);
805 http_buf_enqueue(&p->oqueue, requestbuf);
806 iochan_setflag(p->iochan, EVENT_OUTPUT);
810 void http_send_response(struct http_channel *ch)
812 struct http_response *rs = ch->response;
816 hb = http_serialize_response(ch, rs);
819 yaz_log(YLOG_WARN, "Failed to serialize HTTP response");
820 http_destroy(ch->iochan);
824 http_buf_enqueue(&ch->oqueue, hb);
825 iochan_setflag(ch->iochan, EVENT_OUTPUT);
826 ch->state = Http_Idle;
830 static void http_error(struct http_channel *hc, int no, const char *msg)
832 struct http_response *rs = http_create_response(hc);
835 hc->keep_alive = 0; // not keeping this HTTP session alive
837 sprintf(rs->code, "%d", no);
839 rs->msg = nmem_strdup(hc->nmem, msg);
840 rs->payload = nmem_malloc(hc->nmem, 100);
841 yaz_snprintf(rs->payload, 99, "<error>HTTP Error %d: %s</error>\n",
843 http_send_response(hc);
846 static void http_io(IOCHAN i, int event)
848 struct http_channel *hc = iochan_getdata(i);
853 struct http_buf *htbuf;
856 htbuf = http_buf_create();
857 res = recv(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1, 0);
858 if (res == -1 && errno == EAGAIN)
860 http_buf_destroy(htbuf);
865 http_buf_destroy(htbuf);
869 htbuf->buf[res] = '\0';
871 http_buf_enqueue(&hc->iqueue, htbuf);
875 if (hc->state == Http_Busy)
877 reqlen = request_check(hc->iqueue);
880 // we have a complete HTTP request
881 nmem_reset(hc->nmem);
882 if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
884 yaz_log(YLOG_WARN, "Failed to parse request");
885 http_error(hc, 400, "Bad Request");
889 yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
891 *hc->request->search ? "?" : "",
892 hc->request->search);
893 if (http_weshouldproxy(hc->request))
894 http_proxy(hc->request);
897 // Execute our business logic!
898 hc->state = Http_Busy;
906 struct http_buf *wb = hc->oqueue;
907 res = send(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len, 0);
910 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
916 hc->oqueue = hc->oqueue->next;
917 http_buf_destroy(wb);
932 iochan_clearflag(i, EVENT_OUTPUT);
934 iochan_setevent(hc->iochan, EVENT_INPUT);
939 if (!hc->oqueue && hc->proxy && !hc->proxy->iochan)
940 http_destroy(i); // Server closed; we're done
943 yaz_log(YLOG_WARN, "Unexpected event on connection");
948 // Handles I/O on a client connection to a backend web server (proxy mode)
949 static void proxy_io(IOCHAN pi, int event)
951 struct http_proxy *pc = iochan_getdata(pi);
952 struct http_channel *hc = pc->channel;
957 struct http_buf *htbuf;
960 htbuf = http_buf_create();
961 res = recv(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1, 0);
962 if (res == 0 || (res < 0 && !is_inprogress()))
966 yaz_log(YLOG_WARN, "Proxy read came up short");
967 // Close channel and alert client HTTP channel that we're gone
968 http_buf_destroy(htbuf);
970 closesocket(iochan_getfd(pi));
972 close(iochan_getfd(pi));
979 http_destroy(hc->iochan);
985 htbuf->buf[res] = '\0';
988 // Write any remaining payload
989 if (htbuf->len - htbuf->offset > 0)
990 http_buf_enqueue(&hc->oqueue, htbuf);
992 iochan_setflag(hc->iochan, EVENT_OUTPUT);
995 if (!(htbuf = pc->oqueue))
997 iochan_clearflag(pi, EVENT_OUTPUT);
1000 res = send(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len, 0);
1003 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
1004 http_destroy(hc->iochan);
1007 if (res == htbuf->len)
1009 struct http_buf *np = htbuf->next;
1010 http_buf_destroy(htbuf);
1016 htbuf->offset += res;
1020 iochan_setflags(pi, EVENT_INPUT); // Turns off output flag
1024 yaz_log(YLOG_WARN, "Unexpected event on connection");
1025 http_destroy(hc->iochan);
1029 static void http_fire_observers(struct http_channel *c);
1030 static void http_destroy_observers(struct http_channel *c);
1033 static void http_destroy(IOCHAN i)
1035 struct http_channel *s = iochan_getdata(i);
1039 if (s->proxy->iochan)
1042 closesocket(iochan_getfd(s->proxy->iochan));
1044 close(iochan_getfd(s->proxy->iochan));
1046 iochan_destroy(s->proxy->iochan);
1048 http_buf_destroy_queue(s->proxy->oqueue);
1051 http_buf_destroy_queue(s->iqueue);
1052 http_buf_destroy_queue(s->oqueue);
1053 http_fire_observers(s);
1054 http_destroy_observers(s);
1055 s->next = http_channel_freelist;
1056 http_channel_freelist = s;
1058 closesocket(iochan_getfd(i));
1060 close(iochan_getfd(i));
1065 static struct http_channel *http_create(const char *addr)
1067 struct http_channel *r = http_channel_freelist;
1071 http_channel_freelist = r->next;
1072 nmem_reset(r->nmem);
1073 wrbuf_rewind(r->wrbuf);
1077 r = xmalloc(sizeof(struct http_channel));
1078 r->nmem = nmem_create();
1079 r->wrbuf = wrbuf_alloc();
1083 r->iqueue = r->oqueue = 0;
1084 r->state = Http_Idle;
1090 yaz_log(YLOG_WARN, "Invalid HTTP forward address");
1093 strcpy(r->addr, addr);
1099 /* Accept a new command connection */
1100 static void http_accept(IOCHAN i, int event)
1102 struct sockaddr_in addr;
1103 int fd = iochan_getfd(i);
1107 struct http_channel *ch;
1110 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
1112 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
1117 yaz_log(YLOG_DEBUG, "New command connection");
1118 c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT);
1120 ch = http_create(inet_ntoa(addr.sin_addr));
1122 iochan_setdata(c, ch);
1124 pazpar2_add_channel(c);
1127 /* Create a http-channel listener, syntax [host:]port */
1128 void http_init(const char *addr)
1133 struct sockaddr_in myaddr;
1138 yaz_log(YLOG_LOG, "HTTP listener %s", addr);
1140 memset(&myaddr, 0, sizeof myaddr);
1141 myaddr.sin_family = AF_INET;
1142 pp = strchr(addr, ':');
1145 int len = pp - addr;
1149 strncpy(hostname, addr, len);
1150 hostname[len] = '\0';
1151 if (!(he = gethostbyname(hostname))){
1152 yaz_log(YLOG_FATAL, "Unable to resolve '%s'", hostname);
1156 memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1157 port = atoi(pp + 1);
1162 myaddr.sin_addr.s_addr = INADDR_ANY;
1165 myaddr.sin_port = htons(port);
1167 if (!(p = getprotobyname("tcp"))) {
1170 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
1171 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
1172 if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
1173 &one, sizeof(one)) < 0)
1176 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
1178 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
1181 if (listen(l, SOMAXCONN) < 0)
1183 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
1187 c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT);
1188 pazpar2_add_channel(c);
1191 void http_set_proxyaddr(char *host, char *base_url)
1197 strcpy(myurl, base_url);
1198 strcpy(proxy_url, host);
1199 p = strchr(host, ':');
1200 yaz_log(YLOG_DEBUG, "Proxying for %s", host);
1201 yaz_log(YLOG_LOG, "HTTP backend %s", proxy_url);
1208 if (!(he = gethostbyname(host)))
1210 fprintf(stderr, "Failed to lookup '%s'\n", host);
1213 proxy_addr = xmalloc(sizeof(struct sockaddr_in));
1214 proxy_addr->sin_family = he->h_addrtype;
1215 memcpy(&proxy_addr->sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1216 proxy_addr->sin_port = htons(port);
1219 static void http_fire_observers(struct http_channel *c)
1221 http_channel_observer_t p = c->observers;
1224 p->destroy(p->data, c, p->data2);
1229 static void http_destroy_observers(struct http_channel *c)
1231 while (c->observers)
1233 http_channel_observer_t obs = c->observers;
1234 c->observers = obs->next;
1239 http_channel_observer_t http_add_observer(struct http_channel *c, void *data,
1240 http_channel_destroy_t des)
1242 http_channel_observer_t obs = xmalloc(sizeof(*obs));
1247 obs->next = c->observers;
1252 void http_remove_observer(http_channel_observer_t obs)
1254 struct http_channel *c = obs->chan;
1255 http_channel_observer_t found, *p = &c->observers;
1264 struct http_channel *http_channel_observer_chan(http_channel_observer_t obs)
1269 void http_observer_set_data2(http_channel_observer_t obs, void *data2)
1278 * indent-tabs-mode: nil
1280 * vim: shiftwidth=4 tabstop=8 expandtab