1 /* $Id: http.c,v 1.42 2007-10-08 13:19:23 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>
26 #include <yaz/snprintf.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
45 #include <yaz/yaz-util.h>
46 #include <yaz/comstack.h>
54 #include "http_command.h"
56 #define MAX_HTTP_HEADER 4096
58 static void proxy_io(IOCHAN i, int event);
59 static struct http_channel *http_create(const char *addr);
60 static void http_destroy(IOCHAN i);
62 // If this is set, we proxy normal HTTP requests
63 static struct sockaddr_in *proxy_addr = 0;
64 static char proxy_url[256] = "";
65 static char myurl[256] = "";
66 static struct http_buf *http_buf_freelist = 0;
67 static struct http_channel *http_channel_freelist = 0;
69 struct http_channel_observer_s {
72 http_channel_destroy_t destroy;
73 struct http_channel_observer_s *next;
74 struct http_channel *chan;
78 static const char *http_lookup_header(struct http_header *header,
81 for (; header; header = header->next)
82 if (!strcasecmp(name, header->name))
87 static struct http_buf *http_buf_create()
91 if (http_buf_freelist)
93 r = http_buf_freelist;
94 http_buf_freelist = http_buf_freelist->next;
97 r = xmalloc(sizeof(struct http_buf));
104 static void http_buf_destroy(struct http_buf *b)
106 b->next = http_buf_freelist;
107 http_buf_freelist = b;
110 static void http_buf_destroy_queue(struct http_buf *b)
122 // Calculate length of chain
123 static int http_buf_len(struct http_buf *b)
126 for (; b; b = b->next)
132 static struct http_buf *http_buf_bybuf(char *b, int len)
134 struct http_buf *res = 0;
135 struct http_buf **p = &res;
140 if (tocopy > HTTP_BUF_SIZE)
141 tocopy = HTTP_BUF_SIZE;
142 *p = http_buf_create();
143 memcpy((*p)->buf, b, tocopy);
152 // Add a (chain of) buffers to the end of an existing queue.
153 static void http_buf_enqueue(struct http_buf **queue, struct http_buf *b)
156 queue = &(*queue)->next;
160 static struct http_buf *http_buf_bywrbuf(WRBUF wrbuf)
162 // Heavens to Betsy (buf)!
163 return http_buf_bybuf(wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
166 // Non-destructively collapse chain of buffers into a string (max *len)
168 static void http_buf_peek(struct http_buf *b, char *buf, int len)
171 while (b && rd < len)
173 int toread = len - rd;
176 memcpy(buf + rd, b->buf + b->offset, toread);
183 static int http_buf_size(struct http_buf *b)
186 for (; b; b = b->next)
191 // Ddestructively munch up to len from head of queue.
192 static int http_buf_read(struct http_buf **b, char *buf, int len)
195 while ((*b) && rd < len)
197 int toread = len - rd;
198 if (toread > (*b)->len)
200 memcpy(buf + rd, (*b)->buf + (*b)->offset, toread);
202 if (toread < (*b)->len)
205 (*b)->offset += toread;
210 struct http_buf *n = (*b)->next;
211 http_buf_destroy(*b);
219 // Buffers may overlap.
220 static void urldecode(char *i, char *o)
232 sscanf(i, "%2hhx", o);
242 // Warning: Buffers may not overlap
243 void urlencode(const char *i, char *o)
247 if (strchr(" /:", *i))
249 sprintf(o, "%%%.2X", (int) *i);
259 void http_addheader(struct http_response *r, const char *name, const char *value)
261 struct http_channel *c = r->channel;
262 struct http_header *h = nmem_malloc(c->nmem, sizeof *h);
263 h->name = nmem_strdup(c->nmem, name);
264 h->value = nmem_strdup(c->nmem, value);
265 h->next = r->headers;
269 char *http_argbyname(struct http_request *r, char *name)
271 struct http_argument *p;
274 for (p = r->arguments; p; p = p->next)
275 if (!strcmp(p->name, name))
280 char *http_headerbyname(struct http_header *h, char *name)
282 for (; h; h = h->next)
283 if (!strcmp(h->name, name))
288 struct http_response *http_create_response(struct http_channel *c)
290 struct http_response *r = nmem_malloc(c->nmem, sizeof(*r));
291 strcpy(r->code, "200");
296 r->content_type = "text/xml";
301 static const char *next_crlf(const char *cp, size_t *skipped)
303 const char *next_cp = strchr(cp, '\n');
306 if (next_cp > cp && next_cp[-1] == '\r')
307 *skipped = next_cp - cp - 1;
309 *skipped = next_cp - cp;
315 // Check if buf contains a package (minus payload)
316 static int package_check(const char *buf, int sz)
324 const char *b = next_crlf(buf, &skipped);
328 // we did not find CRLF.. See if buffer is too large..
329 if (sz >= MAX_HTTP_HEADER-1)
330 return MAX_HTTP_HEADER-1; // yes. Return that (will fail later)
336 // CRLF CRLF , i.e. end of header
337 if (len + content_len <= sz)
338 return len + content_len;
342 // following first skip of \r\n so that we don't consider Method
343 if (!strncasecmp(buf, "Content-Length:", 15))
345 const char *cp = buf+15;
349 while (*cp && isdigit(*cp))
350 content_len = content_len*10 + (*cp++ - '0');
351 if (content_len < 0) /* prevent negative offsets */
355 return 0; // incomplete request
358 // Check if we have a request. Return 0 or length
359 static int request_check(struct http_buf *queue)
361 char tmp[MAX_HTTP_HEADER];
363 // only peek at the header..
364 http_buf_peek(queue, tmp, MAX_HTTP_HEADER-1);
365 // still we only return non-zero if the complete request is received..
366 return package_check(tmp, http_buf_size(queue));
369 struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len)
371 char tmp[MAX_HTTP_HEADER];
372 struct http_response *r = http_create_response(c);
374 struct http_header **hp = &r->headers;
376 if (len >= MAX_HTTP_HEADER)
378 memcpy(tmp, buf, len);
379 for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version
383 for (p2 = p; *p2 && *p2 != ' ' && p2 - p < 3; p2++)
384 r->code[p2 - p] = *p2;
385 if (!(p = strstr(tmp, "\r\n")))
390 if (!(p2 = strstr(p, "\r\n")))
392 if (p == p2) // End of headers
396 struct http_header *h = *hp = nmem_malloc(c->nmem, sizeof(*h));
397 char *value = strchr(p, ':');
401 h->name = nmem_strdup(c->nmem, p);
402 while (isspace(*value))
404 if (value >= p2) // Empty header;
411 h->value = nmem_strdup(c->nmem, value);
420 static int http_parse_arguments(struct http_request *r, NMEM nmem,
423 const char *p2 = args;
427 struct http_argument *a;
428 const char *equal = strchr(p2, '=');
429 const char *eoa = strchr(p2, '&');
432 yaz_log(YLOG_WARN, "Expected '=' in argument");
436 eoa = equal + strlen(equal); // last argument
437 else if (equal > eoa)
439 yaz_log(YLOG_WARN, "Missing '&' in argument");
442 a = nmem_malloc(nmem, sizeof(struct http_argument));
443 a->name = nmem_strdupn(nmem, p2, equal - p2);
444 a->value = nmem_strdupn(nmem, equal+1, eoa - equal - 1);
445 urldecode(a->name, a->name);
446 urldecode(a->value, a->value);
447 a->next = r->arguments;
456 struct http_request *http_parse_request(struct http_channel *c,
457 struct http_buf **queue,
460 struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
462 char *start = nmem_malloc(c->nmem, len+1);
465 if (http_buf_read(queue, buf, len) < len)
467 yaz_log(YLOG_WARN, "http_buf_read < len (%d)", len);
477 for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
481 yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
486 if (!(buf = strchr(buf, ' ')))
488 yaz_log(YLOG_WARN, "Missing Request-URI in HTTP request");
492 if (!(p = strchr(buf, ' ')))
494 yaz_log(YLOG_WARN, "HTTP Request-URI not terminated (too long?)");
498 if ((p2 = strchr(buf, '?'))) // Do we have arguments?
500 r->path = nmem_strdup(c->nmem, buf);
503 r->search = nmem_strdup(c->nmem, p2);
505 http_parse_arguments(r, c->nmem, p2);
509 if (strncmp(buf, "HTTP/", 5))
510 strcpy(r->http_version, "1.0");
514 buf += 5; // strlen("HTTP/")
516 p = (char*) next_crlf(buf, &skipped);
517 if (!p || skipped < 3 || skipped > 5)
520 memcpy(r->http_version, buf, skipped);
521 r->http_version[skipped] = '\0';
524 strcpy(c->version, r->http_version);
531 p = (char *) next_crlf(buf, &skipped);
536 else if (skipped == 0)
544 char *n_v = nmem_malloc(c->nmem, skipped+1);
545 struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
547 memcpy(n_v, buf, skipped);
550 if (!(cp = strchr(n_v, ':')))
552 h->name = nmem_strdupn(c->nmem, n_v, cp - n_v);
556 h->value = nmem_strdup(c->nmem, cp);
557 h->next = r->headers;
563 // determine if we do keep alive
564 if (!strcmp(c->version, "1.0"))
566 const char *v = http_lookup_header(r->headers, "Connection");
567 if (v && !strcmp(v, "Keep-Alive"))
574 const char *v = http_lookup_header(r->headers, "Connection");
575 if (v && !strcmp(v, "close"))
580 if (buf < start + len)
582 const char *content_type = http_lookup_header(r->headers,
584 r->content_len = start + len - buf;
585 r->content_buf = buf;
587 if (!strcmp(content_type, "application/x-www-form-urlencoded"))
589 http_parse_arguments(r, c->nmem, r->content_buf);
595 static struct http_buf *http_serialize_response(struct http_channel *c,
596 struct http_response *r)
598 struct http_header *h;
600 wrbuf_rewind(c->wrbuf);
601 wrbuf_printf(c->wrbuf, "HTTP/%s %s %s\r\n", c->version, r->code, r->msg);
602 for (h = r->headers; h; h = h->next)
603 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
606 wrbuf_printf(c->wrbuf, "Content-Length: %d\r\n", r->payload ?
607 (int) strlen(r->payload) : 0);
608 wrbuf_printf(c->wrbuf, "Content-Type: %s\r\n", r->content_type);
609 if (!strcmp(r->content_type, "text/xml"))
611 xmlDoc *doc = xmlParseMemory(r->payload, strlen(r->payload));
618 yaz_log(YLOG_WARN, "Sending non-wellformed "
619 "response (bug #1162");
620 yaz_log(YLOG_WARN, "payload: %s", r->payload);
624 wrbuf_puts(c->wrbuf, "\r\n");
627 wrbuf_puts(c->wrbuf, r->payload);
629 return http_buf_bywrbuf(c->wrbuf);
632 // Serialize a HTTP request
633 static struct http_buf *http_serialize_request(struct http_request *r)
635 struct http_channel *c = r->channel;
636 struct http_header *h;
638 wrbuf_rewind(c->wrbuf);
639 wrbuf_printf(c->wrbuf, "%s %s%s%s", r->method, r->path,
640 *r->search ? "?" : "", r->search);
642 wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
644 for (h = r->headers; h; h = h->next)
645 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
647 wrbuf_puts(c->wrbuf, "\r\n");
650 wrbuf_write(c->wrbuf, r->content_buf, r->content_len);
653 yaz_log(YLOG_LOG, "WRITING TO PROXY:\n%s\n----",
654 wrbuf_cstr(c->wrbuf));
656 return http_buf_bywrbuf(c->wrbuf);
660 static int http_weshouldproxy(struct http_request *rq)
662 if (proxy_addr && !strstr(rq->path, "search.pz2"))
668 struct http_header * http_header_append(struct http_channel *ch,
669 struct http_header * hp,
673 struct http_header *hpnew = 0;
678 while (hp && hp->next)
681 if(name && strlen(name)&& value && strlen(value)){
682 hpnew = nmem_malloc(ch->nmem, sizeof *hpnew);
683 hpnew->name = nmem_strdup(ch->nmem, name);
684 hpnew->value = nmem_strdup(ch->nmem, value);
698 static int http_proxy(struct http_request *rq)
700 struct http_channel *c = rq->channel;
701 struct http_proxy *p = c->proxy;
702 struct http_header *hp;
703 struct http_buf *requestbuf;
704 char server_via[128] = "";
705 char server_port[16] = "";
706 struct conf_server *ser = global_parameters.server;
708 if (!p) // This is a new connection. Create a proxy channel
715 if (!(pe = getprotobyname("tcp"))) {
718 if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
720 yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
723 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
724 &one, sizeof(one)) < 0)
726 if ((flags = fcntl(sock, F_GETFL, 0)) < 0)
727 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
728 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
729 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
730 if (connect(sock, (struct sockaddr *) proxy_addr,
731 sizeof(*proxy_addr)) < 0)
732 if (errno != EINPROGRESS)
734 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
738 p = xmalloc(sizeof(struct http_proxy));
741 p->first_response = 1;
743 // We will add EVENT_OUTPUT below
744 p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT);
745 iochan_setdata(p->iochan, p);
746 pazpar2_add_channel(p->iochan);
749 // Do _not_ modify Host: header, just checking it's existence
751 if (!http_lookup_header(rq->headers, "Host"))
753 yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
757 // Add new header about paraz2 version, host, remote client address, etc.
760 hp = http_header_append(c, hp,
761 "X-Pazpar2-Version", PACKAGE_VERSION);
762 hp = http_header_append(c, hp,
763 "X-Pazpar2-Server-Host", ser->host);
764 sprintf(server_port, "%d", ser->port);
765 hp = http_header_append(c, hp,
766 "X-Pazpar2-Server-Port", server_port);
767 sprintf(server_via, "1.1 %s:%s (%s/%s)",
768 ser->host, server_port, PACKAGE_NAME, PACKAGE_VERSION);
769 hp = http_header_append(c, hp, "Via" , server_via);
770 hp = http_header_append(c, hp, "X-Forwarded-For", c->addr);
773 requestbuf = http_serialize_request(rq);
775 http_buf_enqueue(&p->oqueue, requestbuf);
776 iochan_setflag(p->iochan, EVENT_OUTPUT);
780 void http_send_response(struct http_channel *ch)
782 struct http_response *rs = ch->response;
786 hb = http_serialize_response(ch, rs);
789 yaz_log(YLOG_WARN, "Failed to serialize HTTP response");
790 http_destroy(ch->iochan);
794 http_buf_enqueue(&ch->oqueue, hb);
795 iochan_setflag(ch->iochan, EVENT_OUTPUT);
796 ch->state = Http_Idle;
800 static void http_error(struct http_channel *hc, int no, const char *msg)
802 struct http_response *rs = http_create_response(hc);
805 hc->keep_alive = 0; // not keeping this HTTP session alive
807 sprintf(rs->code, "%d", no);
809 rs->msg = nmem_strdup(hc->nmem, msg);
810 rs->payload = nmem_malloc(hc->nmem, 100);
811 yaz_snprintf(rs->payload, 99, "<error>HTTP Error %d: %s</error>\n",
813 http_send_response(hc);
816 static void http_io(IOCHAN i, int event)
818 struct http_channel *hc = iochan_getdata(i);
823 struct http_buf *htbuf;
826 htbuf = http_buf_create();
827 res = read(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1);
828 if (res == -1 && errno == EAGAIN)
830 http_buf_destroy(htbuf);
835 http_buf_destroy(htbuf);
839 htbuf->buf[res] = '\0';
841 http_buf_enqueue(&hc->iqueue, htbuf);
845 if (hc->state == Http_Busy)
847 reqlen = request_check(hc->iqueue);
850 // we have a complete HTTP request
851 nmem_reset(hc->nmem);
852 if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
854 yaz_log(YLOG_WARN, "Failed to parse request");
855 http_error(hc, 400, "Bad Request");
859 yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
861 *hc->request->search ? "?" : "",
862 hc->request->search);
863 if (http_weshouldproxy(hc->request))
864 http_proxy(hc->request);
867 // Execute our business logic!
868 hc->state = Http_Busy;
876 struct http_buf *wb = hc->oqueue;
877 res = write(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len);
880 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
886 hc->oqueue = hc->oqueue->next;
887 http_buf_destroy(wb);
902 iochan_clearflag(i, EVENT_OUTPUT);
904 iochan_setevent(hc->iochan, EVENT_INPUT);
909 if (!hc->oqueue && hc->proxy && !hc->proxy->iochan)
910 http_destroy(i); // Server closed; we're done
913 yaz_log(YLOG_WARN, "Unexpected event on connection");
919 // If this hostname contains our proxy host as a prefix, replace with myurl
920 static char *sub_hostname(struct http_channel *c, char *buf)
923 if (strlen(buf) > 1023)
925 if (strncmp(buf, "http://", 7))
927 if (!strncmp(buf + 7, proxy_url, strlen(proxy_url)))
930 strcat(tmp, buf + strlen(proxy_url) + 7);
931 return nmem_strdup(c->nmem, tmp);
937 // Handles I/O on a client connection to a backend web server (proxy mode)
938 static void proxy_io(IOCHAN pi, int event)
940 struct http_proxy *pc = iochan_getdata(pi);
941 struct http_channel *hc = pc->channel;
946 struct http_buf *htbuf;
949 htbuf = http_buf_create();
950 res = read(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1);
951 if (res == 0 || (res < 0 && errno != EINPROGRESS))
955 yaz_log(YLOG_WARN, "Proxy read came up short");
956 // Close channel and alert client HTTP channel that we're gone
957 http_buf_destroy(htbuf);
958 close(iochan_getfd(pi));
964 http_destroy(hc->iochan);
970 htbuf->buf[res] = '\0';
974 if (pc->first_response) // Check if this is a redirect
977 if ((len = package_check(htbuf->buf)))
979 struct http_response *res = http_parse_response_buf(hc, htbuf->buf, len);
982 const char *location = http_lookup_header(
983 res->header, "Location");
986 // We found a location header. Rewrite it.
987 struct http_buf *buf;
988 h->value = sub_hostname(hc, location);
989 buf = http_serialize_response(hc, res);
990 yaz_log(YLOG_LOG, "Proxy rewrite");
991 http_buf_enqueue(&hc->oqueue, buf);
997 pc->first_response = 0;
1000 // Write any remaining payload
1001 if (htbuf->len - htbuf->offset > 0)
1002 http_buf_enqueue(&hc->oqueue, htbuf);
1004 iochan_setflag(hc->iochan, EVENT_OUTPUT);
1007 if (!(htbuf = pc->oqueue))
1009 iochan_clearflag(pi, EVENT_OUTPUT);
1012 res = write(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len);
1015 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
1016 http_destroy(hc->iochan);
1019 if (res == htbuf->len)
1021 struct http_buf *np = htbuf->next;
1022 http_buf_destroy(htbuf);
1028 htbuf->offset += res;
1032 iochan_setflags(pi, EVENT_INPUT); // Turns off output flag
1036 yaz_log(YLOG_WARN, "Unexpected event on connection");
1037 http_destroy(hc->iochan);
1041 static void http_fire_observers(struct http_channel *c);
1042 static void http_destroy_observers(struct http_channel *c);
1045 static void http_destroy(IOCHAN i)
1047 struct http_channel *s = iochan_getdata(i);
1051 if (s->proxy->iochan)
1053 close(iochan_getfd(s->proxy->iochan));
1054 iochan_destroy(s->proxy->iochan);
1056 http_buf_destroy_queue(s->proxy->oqueue);
1059 http_buf_destroy_queue(s->iqueue);
1060 http_buf_destroy_queue(s->oqueue);
1061 http_fire_observers(s);
1062 http_destroy_observers(s);
1063 s->next = http_channel_freelist;
1064 http_channel_freelist = s;
1065 close(iochan_getfd(i));
1069 static struct http_channel *http_create(const char *addr)
1071 struct http_channel *r = http_channel_freelist;
1075 http_channel_freelist = r->next;
1076 nmem_reset(r->nmem);
1077 wrbuf_rewind(r->wrbuf);
1081 r = xmalloc(sizeof(struct http_channel));
1082 r->nmem = nmem_create();
1083 r->wrbuf = wrbuf_alloc();
1087 r->iqueue = r->oqueue = 0;
1088 r->state = Http_Idle;
1094 yaz_log(YLOG_WARN, "Invalid HTTP forward address");
1097 strcpy(r->addr, addr);
1103 /* Accept a new command connection */
1104 static void http_accept(IOCHAN i, int event)
1106 struct sockaddr_in addr;
1107 int fd = iochan_getfd(i);
1112 struct http_channel *ch;
1115 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
1117 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
1120 if ((flags = fcntl(s, F_GETFL, 0)) < 0)
1121 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
1122 if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
1123 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
1125 yaz_log(YLOG_DEBUG, "New command connection");
1126 c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT);
1128 ch = http_create(inet_ntoa(addr.sin_addr));
1130 iochan_setdata(c, ch);
1132 pazpar2_add_channel(c);
1135 /* Create a http-channel listener, syntax [host:]port */
1136 void http_init(const char *addr)
1141 struct sockaddr_in myaddr;
1146 yaz_log(YLOG_LOG, "HTTP listener %s", addr);
1148 memset(&myaddr, 0, sizeof myaddr);
1149 myaddr.sin_family = AF_INET;
1150 pp = strchr(addr, ':');
1153 int len = pp - addr;
1157 strncpy(hostname, addr, len);
1158 hostname[len] = '\0';
1159 if (!(he = gethostbyname(hostname))){
1160 yaz_log(YLOG_FATAL, "Unable to resolve '%s'", hostname);
1164 memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1165 port = atoi(pp + 1);
1170 myaddr.sin_addr.s_addr = INADDR_ANY;
1173 myaddr.sin_port = htons(port);
1175 if (!(p = getprotobyname("tcp"))) {
1178 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
1179 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
1180 if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
1181 &one, sizeof(one)) < 0)
1184 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
1186 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
1189 if (listen(l, SOMAXCONN) < 0)
1191 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
1195 c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT);
1196 pazpar2_add_channel(c);
1199 void http_set_proxyaddr(char *host, char *base_url)
1205 strcpy(myurl, base_url);
1206 strcpy(proxy_url, host);
1207 p = strchr(host, ':');
1208 yaz_log(YLOG_DEBUG, "Proxying for %s", host);
1209 yaz_log(YLOG_LOG, "HTTP backend %s", proxy_url);
1216 if (!(he = gethostbyname(host)))
1218 fprintf(stderr, "Failed to lookup '%s'\n", host);
1221 proxy_addr = xmalloc(sizeof(struct sockaddr_in));
1222 proxy_addr->sin_family = he->h_addrtype;
1223 memcpy(&proxy_addr->sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1224 proxy_addr->sin_port = htons(port);
1227 static void http_fire_observers(struct http_channel *c)
1229 http_channel_observer_t p = c->observers;
1232 p->destroy(p->data, c, p->data2);
1237 static void http_destroy_observers(struct http_channel *c)
1239 while (c->observers)
1241 http_channel_observer_t obs = c->observers;
1242 c->observers = obs->next;
1247 http_channel_observer_t http_add_observer(struct http_channel *c, void *data,
1248 http_channel_destroy_t des)
1250 http_channel_observer_t obs = xmalloc(sizeof(*obs));
1255 obs->next = c->observers;
1260 void http_remove_observer(http_channel_observer_t obs)
1262 struct http_channel *c = obs->chan;
1263 http_channel_observer_t found, *p = &c->observers;
1272 struct http_channel *http_channel_observer_chan(http_channel_observer_t obs)
1277 void http_observer_set_data2(http_channel_observer_t obs, void *data2)
1286 * indent-tabs-mode: nil
1288 * vim: shiftwidth=4 tabstop=8 expandtab