1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 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
27 typedef int socklen_t;
31 #include <sys/socket.h>
34 #include <sys/types.h>
36 #include <yaz/snprintf.h>
54 #include <netinet/in.h>
58 #include <arpa/inet.h>
61 #include <yaz/yaz-util.h>
62 #include <yaz/comstack.h>
69 #include "http_command.h"
71 #define MAX_HTTP_HEADER 4096
73 static void proxy_io(IOCHAN i, int event);
74 static struct http_channel *http_create(const char *addr);
75 static void http_destroy(IOCHAN i);
77 // If this is set, we proxy normal HTTP requests
78 static struct sockaddr_in *proxy_addr = 0;
79 static char proxy_url[256] = "";
80 static char myurl[256] = "";
81 static struct http_buf *http_buf_freelist = 0;
82 static struct http_channel *http_channel_freelist = 0;
84 struct http_channel_observer_s {
87 http_channel_destroy_t destroy;
88 struct http_channel_observer_s *next;
89 struct http_channel *chan;
93 static const char *http_lookup_header(struct http_header *header,
96 for (; header; header = header->next)
97 if (!strcasecmp(name, header->name))
102 static struct http_buf *http_buf_create(void)
106 if (http_buf_freelist)
108 r = http_buf_freelist;
109 http_buf_freelist = http_buf_freelist->next;
112 r = xmalloc(sizeof(struct http_buf));
119 static void http_buf_destroy(struct http_buf *b)
121 b->next = http_buf_freelist;
122 http_buf_freelist = b;
125 static void http_buf_destroy_queue(struct http_buf *b)
136 static struct http_buf *http_buf_bybuf(char *b, int len)
138 struct http_buf *res = 0;
139 struct http_buf **p = &res;
144 if (tocopy > HTTP_BUF_SIZE)
145 tocopy = HTTP_BUF_SIZE;
146 *p = http_buf_create();
147 memcpy((*p)->buf, b, tocopy);
156 // Add a (chain of) buffers to the end of an existing queue.
157 static void http_buf_enqueue(struct http_buf **queue, struct http_buf *b)
160 queue = &(*queue)->next;
164 static struct http_buf *http_buf_bywrbuf(WRBUF wrbuf)
166 // Heavens to Betsy (buf)!
167 return http_buf_bybuf(wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
170 // Non-destructively collapse chain of buffers into a string (max *len)
172 static void http_buf_peek(struct http_buf *b, char *buf, int len)
175 while (b && rd < len)
177 int toread = len - rd;
180 memcpy(buf + rd, b->buf + b->offset, toread);
187 static int http_buf_size(struct http_buf *b)
190 for (; b; b = b->next)
195 // Ddestructively munch up to len from head of queue.
196 static int http_buf_read(struct http_buf **b, char *buf, int len)
199 while ((*b) && rd < len)
201 int toread = len - rd;
202 if (toread > (*b)->len)
204 memcpy(buf + rd, (*b)->buf + (*b)->offset, toread);
206 if (toread < (*b)->len)
209 (*b)->offset += toread;
214 struct http_buf *n = (*b)->next;
215 http_buf_destroy(*b);
223 // Buffers may overlap.
224 static void urldecode(char *i, char *o)
233 else if (*i == '%' && i[1] && i[2])
237 sscanf(i, "%2x", &v);
247 // Warning: Buffers may not overlap
248 void urlencode(const char *i, char *o)
252 if (strchr(" /:", *i))
254 sprintf(o, "%%%.2X", (int) *i);
264 void http_addheader(struct http_response *r, const char *name, const char *value)
266 struct http_channel *c = r->channel;
267 struct http_header *h = nmem_malloc(c->nmem, sizeof *h);
268 h->name = nmem_strdup(c->nmem, name);
269 h->value = nmem_strdup(c->nmem, value);
270 h->next = r->headers;
274 char *http_argbyname(struct http_request *r, char *name)
276 struct http_argument *p;
279 for (p = r->arguments; p; p = p->next)
280 if (!strcmp(p->name, name))
285 char *http_headerbyname(struct http_header *h, char *name)
287 for (; h; h = h->next)
288 if (!strcmp(h->name, name))
293 struct http_response *http_create_response(struct http_channel *c)
295 struct http_response *r = nmem_malloc(c->nmem, sizeof(*r));
296 strcpy(r->code, "200");
301 r->content_type = "text/xml";
306 static const char *next_crlf(const char *cp, size_t *skipped)
308 const char *next_cp = strchr(cp, '\n');
311 if (next_cp > cp && next_cp[-1] == '\r')
312 *skipped = next_cp - cp - 1;
314 *skipped = next_cp - cp;
320 // Check if buf contains a package (minus payload)
321 static int package_check(const char *buf, int sz)
329 const char *b = next_crlf(buf, &skipped);
333 // we did not find CRLF.. See if buffer is too large..
334 if (sz >= MAX_HTTP_HEADER-1)
335 return MAX_HTTP_HEADER-1; // yes. Return that (will fail later)
341 // CRLF CRLF , i.e. end of header
342 if (len + content_len <= sz)
343 return len + content_len;
347 // following first skip of \r\n so that we don't consider Method
348 if (!strncasecmp(buf, "Content-Length:", 15))
350 const char *cp = buf+15;
354 while (*cp && isdigit(*(const unsigned char *)cp))
355 content_len = content_len*10 + (*cp++ - '0');
356 if (content_len < 0) /* prevent negative offsets */
360 return 0; // incomplete request
363 // Check if we have a request. Return 0 or length
364 static int request_check(struct http_buf *queue)
366 char tmp[MAX_HTTP_HEADER];
368 // only peek at the header..
369 http_buf_peek(queue, tmp, MAX_HTTP_HEADER-1);
370 // still we only return non-zero if the complete request is received..
371 return package_check(tmp, http_buf_size(queue));
374 struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len)
376 char tmp[MAX_HTTP_HEADER];
377 struct http_response *r = http_create_response(c);
379 struct http_header **hp = &r->headers;
381 if (len >= MAX_HTTP_HEADER)
383 memcpy(tmp, buf, len);
384 for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version
388 for (p2 = p; *p2 && *p2 != ' ' && p2 - p < 3; p2++)
389 r->code[p2 - p] = *p2;
390 if (!(p = strstr(tmp, "\r\n")))
395 if (!(p2 = strstr(p, "\r\n")))
397 if (p == p2) // End of headers
401 struct http_header *h = *hp = nmem_malloc(c->nmem, sizeof(*h));
402 char *value = strchr(p, ':');
406 h->name = nmem_strdup(c->nmem, p);
407 while (isspace(*(const unsigned char *) value))
409 if (value >= p2) // Empty header;
416 h->value = nmem_strdup(c->nmem, value);
425 static int http_parse_arguments(struct http_request *r, NMEM nmem,
428 const char *p2 = args;
432 struct http_argument *a;
433 const char *equal = strchr(p2, '=');
434 const char *eoa = strchr(p2, '&');
437 yaz_log(YLOG_WARN, "Expected '=' in argument");
441 eoa = equal + strlen(equal); // last argument
442 else if (equal > eoa)
444 yaz_log(YLOG_WARN, "Missing '&' in argument");
447 a = nmem_malloc(nmem, sizeof(struct http_argument));
448 a->name = nmem_strdupn(nmem, p2, equal - p2);
449 a->value = nmem_strdupn(nmem, equal+1, eoa - equal - 1);
450 urldecode(a->name, a->name);
451 urldecode(a->value, a->value);
452 a->next = r->arguments;
461 struct http_request *http_parse_request(struct http_channel *c,
462 struct http_buf **queue,
465 struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
467 char *start = nmem_malloc(c->nmem, len+1);
470 if (http_buf_read(queue, buf, len) < len)
472 yaz_log(YLOG_WARN, "http_buf_read < len (%d)", len);
482 for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
486 yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
491 if (!(buf = strchr(buf, ' ')))
493 yaz_log(YLOG_WARN, "Missing Request-URI in HTTP request");
497 if (!(p = strchr(buf, ' ')))
499 yaz_log(YLOG_WARN, "HTTP Request-URI not terminated (too long?)");
503 if ((p2 = strchr(buf, '?'))) // Do we have arguments?
505 r->path = nmem_strdup(c->nmem, buf);
508 r->search = nmem_strdup(c->nmem, p2);
510 http_parse_arguments(r, c->nmem, p2);
514 if (strncmp(buf, "HTTP/", 5))
515 strcpy(r->http_version, "1.0");
519 buf += 5; // strlen("HTTP/")
521 p = (char*) next_crlf(buf, &skipped);
522 if (!p || skipped < 3 || skipped > 5)
525 memcpy(r->http_version, buf, skipped);
526 r->http_version[skipped] = '\0';
529 strcpy(c->version, r->http_version);
536 p = (char *) next_crlf(buf, &skipped);
541 else if (skipped == 0)
549 char *n_v = nmem_malloc(c->nmem, skipped+1);
550 struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
552 memcpy(n_v, buf, skipped);
555 if (!(cp = strchr(n_v, ':')))
557 h->name = nmem_strdupn(c->nmem, n_v, cp - n_v);
561 h->value = nmem_strdup(c->nmem, cp);
562 h->next = r->headers;
568 // determine if we do keep alive
569 if (!strcmp(c->version, "1.0"))
571 const char *v = http_lookup_header(r->headers, "Connection");
572 if (v && !strcmp(v, "Keep-Alive"))
579 const char *v = http_lookup_header(r->headers, "Connection");
580 if (v && !strcmp(v, "close"))
585 if (buf < start + len)
587 const char *content_type = http_lookup_header(r->headers,
589 r->content_len = start + len - buf;
590 r->content_buf = buf;
592 if (!yaz_strcmp_del("application/x-www-form-urlencoded",
595 http_parse_arguments(r, c->nmem, r->content_buf);
601 static struct http_buf *http_serialize_response(struct http_channel *c,
602 struct http_response *r)
604 struct http_header *h;
606 wrbuf_rewind(c->wrbuf);
607 wrbuf_printf(c->wrbuf, "HTTP/%s %s %s\r\n", c->version, r->code, r->msg);
608 for (h = r->headers; h; h = h->next)
609 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
612 wrbuf_printf(c->wrbuf, "Content-Length: %d\r\n", r->payload ?
613 (int) strlen(r->payload) : 0);
614 wrbuf_printf(c->wrbuf, "Content-Type: %s\r\n", r->content_type);
615 if (!strcmp(r->content_type, "text/xml"))
617 xmlDoc *doc = xmlParseMemory(r->payload, strlen(r->payload));
624 yaz_log(YLOG_WARN, "Sending non-wellformed "
625 "response (bug #1162");
626 yaz_log(YLOG_WARN, "payload: %s", r->payload);
630 wrbuf_puts(c->wrbuf, "\r\n");
633 wrbuf_puts(c->wrbuf, r->payload);
635 return http_buf_bywrbuf(c->wrbuf);
638 // Serialize a HTTP request
639 static struct http_buf *http_serialize_request(struct http_request *r)
641 struct http_channel *c = r->channel;
642 struct http_header *h;
644 wrbuf_rewind(c->wrbuf);
645 wrbuf_printf(c->wrbuf, "%s %s%s%s", r->method, r->path,
646 *r->search ? "?" : "", r->search);
648 wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
650 for (h = r->headers; h; h = h->next)
651 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
653 wrbuf_puts(c->wrbuf, "\r\n");
656 wrbuf_write(c->wrbuf, r->content_buf, r->content_len);
659 yaz_log(YLOG_LOG, "WRITING TO PROXY:\n%s\n----",
660 wrbuf_cstr(c->wrbuf));
662 return http_buf_bywrbuf(c->wrbuf);
666 static int http_weshouldproxy(struct http_request *rq)
668 if (proxy_addr && !strstr(rq->path, "search.pz2"))
674 struct http_header * http_header_append(struct http_channel *ch,
675 struct http_header * hp,
679 struct http_header *hpnew = 0;
684 while (hp && hp->next)
687 if(name && strlen(name)&& value && strlen(value)){
688 hpnew = nmem_malloc(ch->nmem, sizeof *hpnew);
689 hpnew->name = nmem_strdup(ch->nmem, name);
690 hpnew->value = nmem_strdup(ch->nmem, value);
703 static int is_inprogress(void)
706 if (WSAGetLastError() == WSAEWOULDBLOCK)
709 if (errno == EINPROGRESS)
715 static void enable_nonblock(int sock)
719 flags = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
720 if (ioctlsocket(sock, FIONBIO, &flags) < 0)
721 yaz_log(YLOG_FATAL|YLOG_ERRNO, "ioctlsocket");
723 if ((flags = fcntl(sock, F_GETFL, 0)) < 0)
724 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
725 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
726 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
730 static int http_proxy(struct http_request *rq)
732 struct http_channel *c = rq->channel;
733 struct http_proxy *p = c->proxy;
734 struct http_header *hp;
735 struct http_buf *requestbuf;
736 char server_via[128] = "";
737 char server_port[16] = "";
738 struct conf_server *ser = global_parameters.server;
740 if (!p) // This is a new connection. Create a proxy channel
746 if (!(pe = getprotobyname("tcp"))) {
749 if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
751 yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
754 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
755 &one, sizeof(one)) < 0)
757 enable_nonblock(sock);
758 if (connect(sock, (struct sockaddr *) proxy_addr,
759 sizeof(*proxy_addr)) < 0)
761 if (!is_inprogress())
763 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
767 p = xmalloc(sizeof(struct http_proxy));
770 p->first_response = 1;
772 // We will add EVENT_OUTPUT below
773 p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT);
774 iochan_setdata(p->iochan, p);
775 pazpar2_add_channel(p->iochan);
778 // Do _not_ modify Host: header, just checking it's existence
780 if (!http_lookup_header(rq->headers, "Host"))
782 yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
786 // Add new header about paraz2 version, host, remote client address, etc.
789 hp = http_header_append(c, hp,
790 "X-Pazpar2-Version", PACKAGE_VERSION);
791 hp = http_header_append(c, hp,
792 "X-Pazpar2-Server-Host", ser->host);
793 sprintf(server_port, "%d", ser->port);
794 hp = http_header_append(c, hp,
795 "X-Pazpar2-Server-Port", server_port);
796 sprintf(server_via, "1.1 %s:%s (%s/%s)",
797 ser->host ? ser->host : "@",
798 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 (hc->request->content_buf)
894 yaz_log(YLOG_LOG, "%s", hc->request->content_buf);
895 if (http_weshouldproxy(hc->request))
896 http_proxy(hc->request);
899 // Execute our business logic!
900 hc->state = Http_Busy;
908 struct http_buf *wb = hc->oqueue;
909 res = send(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len, 0);
912 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
918 hc->oqueue = hc->oqueue->next;
919 http_buf_destroy(wb);
934 iochan_clearflag(i, EVENT_OUTPUT);
936 iochan_setevent(hc->iochan, EVENT_INPUT);
941 if (!hc->oqueue && hc->proxy && !hc->proxy->iochan)
942 http_destroy(i); // Server closed; we're done
945 yaz_log(YLOG_WARN, "Unexpected event on connection");
950 // Handles I/O on a client connection to a backend web server (proxy mode)
951 static void proxy_io(IOCHAN pi, int event)
953 struct http_proxy *pc = iochan_getdata(pi);
954 struct http_channel *hc = pc->channel;
959 struct http_buf *htbuf;
962 htbuf = http_buf_create();
963 res = recv(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1, 0);
964 if (res == 0 || (res < 0 && !is_inprogress()))
968 yaz_log(YLOG_WARN, "Proxy read came up short");
969 // Close channel and alert client HTTP channel that we're gone
970 http_buf_destroy(htbuf);
972 closesocket(iochan_getfd(pi));
974 close(iochan_getfd(pi));
981 http_destroy(hc->iochan);
987 htbuf->buf[res] = '\0';
990 // Write any remaining payload
991 if (htbuf->len - htbuf->offset > 0)
992 http_buf_enqueue(&hc->oqueue, htbuf);
994 iochan_setflag(hc->iochan, EVENT_OUTPUT);
997 if (!(htbuf = pc->oqueue))
999 iochan_clearflag(pi, EVENT_OUTPUT);
1002 res = send(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len, 0);
1005 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
1006 http_destroy(hc->iochan);
1009 if (res == htbuf->len)
1011 struct http_buf *np = htbuf->next;
1012 http_buf_destroy(htbuf);
1018 htbuf->offset += res;
1022 iochan_setflags(pi, EVENT_INPUT); // Turns off output flag
1026 yaz_log(YLOG_WARN, "Unexpected event on connection");
1027 http_destroy(hc->iochan);
1031 static void http_fire_observers(struct http_channel *c);
1032 static void http_destroy_observers(struct http_channel *c);
1035 static void http_destroy(IOCHAN i)
1037 struct http_channel *s = iochan_getdata(i);
1041 if (s->proxy->iochan)
1044 closesocket(iochan_getfd(s->proxy->iochan));
1046 close(iochan_getfd(s->proxy->iochan));
1048 iochan_destroy(s->proxy->iochan);
1050 http_buf_destroy_queue(s->proxy->oqueue);
1053 http_buf_destroy_queue(s->iqueue);
1054 http_buf_destroy_queue(s->oqueue);
1055 http_fire_observers(s);
1056 http_destroy_observers(s);
1057 s->next = http_channel_freelist;
1058 http_channel_freelist = s;
1060 closesocket(iochan_getfd(i));
1062 close(iochan_getfd(i));
1067 static struct http_channel *http_create(const char *addr)
1069 struct http_channel *r = http_channel_freelist;
1073 http_channel_freelist = r->next;
1074 nmem_reset(r->nmem);
1075 wrbuf_rewind(r->wrbuf);
1079 r = xmalloc(sizeof(struct http_channel));
1080 r->nmem = nmem_create();
1081 r->wrbuf = wrbuf_alloc();
1085 r->iqueue = r->oqueue = 0;
1086 r->state = Http_Idle;
1092 yaz_log(YLOG_WARN, "Invalid HTTP forward address");
1095 strcpy(r->addr, addr);
1101 /* Accept a new command connection */
1102 static void http_accept(IOCHAN i, int event)
1104 struct sockaddr_in addr;
1105 int fd = iochan_getfd(i);
1109 struct http_channel *ch;
1112 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
1114 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
1119 yaz_log(YLOG_DEBUG, "New command connection");
1120 c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT);
1122 ch = http_create(inet_ntoa(addr.sin_addr));
1124 iochan_setdata(c, ch);
1126 pazpar2_add_channel(c);
1129 static int listener_socket = 0;
1131 /* Create a http-channel listener, syntax [host:]port */
1132 int http_init(const char *addr)
1137 struct sockaddr_in myaddr;
1142 yaz_log(YLOG_LOG, "HTTP listener %s", addr);
1144 memset(&myaddr, 0, sizeof myaddr);
1145 myaddr.sin_family = AF_INET;
1146 pp = strchr(addr, ':');
1149 int len = pp - addr;
1153 strncpy(hostname, addr, len);
1154 hostname[len] = '\0';
1155 if (!(he = gethostbyname(hostname))){
1156 yaz_log(YLOG_FATAL, "Unable to resolve '%s'", hostname);
1160 memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1161 port = atoi(pp + 1);
1166 myaddr.sin_addr.s_addr = INADDR_ANY;
1169 myaddr.sin_port = htons(port);
1171 if (!(p = getprotobyname("tcp"))) {
1174 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
1175 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
1176 if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
1177 &one, sizeof(one)) < 0)
1180 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
1182 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
1185 if (listen(l, SOMAXCONN) < 0)
1187 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
1191 listener_socket = l;
1193 c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT);
1194 pazpar2_add_channel(c);
1198 void http_close_server(void)
1200 /* break the event_loop (select) by closing down the HTTP listener sock */
1201 if (listener_socket)
1204 closesocket(listener_socket);
1206 close(listener_socket);
1211 void http_set_proxyaddr(char *host, char *base_url)
1217 strcpy(myurl, base_url);
1218 strcpy(proxy_url, host);
1219 p = strchr(host, ':');
1220 yaz_log(YLOG_DEBUG, "Proxying for %s", host);
1221 yaz_log(YLOG_LOG, "HTTP backend %s", proxy_url);
1228 if (!(he = gethostbyname(host)))
1230 fprintf(stderr, "Failed to lookup '%s'\n", host);
1233 proxy_addr = xmalloc(sizeof(struct sockaddr_in));
1234 proxy_addr->sin_family = he->h_addrtype;
1235 memcpy(&proxy_addr->sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1236 proxy_addr->sin_port = htons(port);
1239 static void http_fire_observers(struct http_channel *c)
1241 http_channel_observer_t p = c->observers;
1244 p->destroy(p->data, c, p->data2);
1249 static void http_destroy_observers(struct http_channel *c)
1251 while (c->observers)
1253 http_channel_observer_t obs = c->observers;
1254 c->observers = obs->next;
1259 http_channel_observer_t http_add_observer(struct http_channel *c, void *data,
1260 http_channel_destroy_t des)
1262 http_channel_observer_t obs = xmalloc(sizeof(*obs));
1267 obs->next = c->observers;
1272 void http_remove_observer(http_channel_observer_t obs)
1274 struct http_channel *c = obs->chan;
1275 http_channel_observer_t found, *p = &c->observers;
1284 struct http_channel *http_channel_observer_chan(http_channel_observer_t obs)
1289 void http_observer_set_data2(http_channel_observer_t obs, void *data2)
1298 * c-file-style: "Stroustrup"
1299 * indent-tabs-mode: nil
1301 * vim: shiftwidth=4 tabstop=8 expandtab