1 /* $Id: http.c,v 1.41 2007-10-02 12:11:14 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");
300 static const char *next_crlf(const char *cp, size_t *skipped)
302 const char *next_cp = strchr(cp, '\n');
305 if (next_cp > cp && next_cp[-1] == '\r')
306 *skipped = next_cp - cp - 1;
308 *skipped = next_cp - cp;
314 // Check if buf contains a package (minus payload)
315 static int package_check(const char *buf, int sz)
323 const char *b = next_crlf(buf, &skipped);
327 // we did not find CRLF.. See if buffer is too large..
328 if (sz >= MAX_HTTP_HEADER-1)
329 return MAX_HTTP_HEADER-1; // yes. Return that (will fail later)
335 // CRLF CRLF , i.e. end of header
336 if (len + content_len <= sz)
337 return len + content_len;
341 // following first skip of \r\n so that we don't consider Method
342 if (!strncasecmp(buf, "Content-Length:", 15))
344 const char *cp = buf+15;
348 while (*cp && isdigit(*cp))
349 content_len = content_len*10 + (*cp++ - '0');
350 if (content_len < 0) /* prevent negative offsets */
354 return 0; // incomplete request
357 // Check if we have a request. Return 0 or length
358 static int request_check(struct http_buf *queue)
360 char tmp[MAX_HTTP_HEADER];
362 // only peek at the header..
363 http_buf_peek(queue, tmp, MAX_HTTP_HEADER-1);
364 // still we only return non-zero if the complete request is received..
365 return package_check(tmp, http_buf_size(queue));
368 struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len)
370 char tmp[MAX_HTTP_HEADER];
371 struct http_response *r = http_create_response(c);
373 struct http_header **hp = &r->headers;
375 if (len >= MAX_HTTP_HEADER)
377 memcpy(tmp, buf, len);
378 for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version
382 for (p2 = p; *p2 && *p2 != ' ' && p2 - p < 3; p2++)
383 r->code[p2 - p] = *p2;
384 if (!(p = strstr(tmp, "\r\n")))
389 if (!(p2 = strstr(p, "\r\n")))
391 if (p == p2) // End of headers
395 struct http_header *h = *hp = nmem_malloc(c->nmem, sizeof(*h));
396 char *value = strchr(p, ':');
400 h->name = nmem_strdup(c->nmem, p);
401 while (isspace(*value))
403 if (value >= p2) // Empty header;
410 h->value = nmem_strdup(c->nmem, value);
419 static int http_parse_arguments(struct http_request *r, NMEM nmem,
422 const char *p2 = args;
426 struct http_argument *a;
427 const char *equal = strchr(p2, '=');
428 const char *eoa = strchr(p2, '&');
431 yaz_log(YLOG_WARN, "Expected '=' in argument");
435 eoa = equal + strlen(equal); // last argument
436 else if (equal > eoa)
438 yaz_log(YLOG_WARN, "Missing '&' in argument");
441 a = nmem_malloc(nmem, sizeof(struct http_argument));
442 a->name = nmem_strdupn(nmem, p2, equal - p2);
443 a->value = nmem_strdupn(nmem, equal+1, eoa - equal - 1);
444 urldecode(a->name, a->name);
445 urldecode(a->value, a->value);
446 a->next = r->arguments;
455 struct http_request *http_parse_request(struct http_channel *c,
456 struct http_buf **queue,
459 struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
461 char *start = nmem_malloc(c->nmem, len+1);
464 if (http_buf_read(queue, buf, len) < len)
466 yaz_log(YLOG_WARN, "http_buf_read < len (%d)", len);
476 for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
480 yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
485 if (!(buf = strchr(buf, ' ')))
487 yaz_log(YLOG_WARN, "Missing Request-URI in HTTP request");
491 if (!(p = strchr(buf, ' ')))
493 yaz_log(YLOG_WARN, "HTTP Request-URI not terminated (too long?)");
497 if ((p2 = strchr(buf, '?'))) // Do we have arguments?
499 r->path = nmem_strdup(c->nmem, buf);
502 r->search = nmem_strdup(c->nmem, p2);
504 http_parse_arguments(r, c->nmem, p2);
508 if (strncmp(buf, "HTTP/", 5))
509 strcpy(r->http_version, "1.0");
513 buf += 5; // strlen("HTTP/")
515 p = (char*) next_crlf(buf, &skipped);
516 if (!p || skipped < 3 || skipped > 5)
519 memcpy(r->http_version, buf, skipped);
520 r->http_version[skipped] = '\0';
523 strcpy(c->version, r->http_version);
530 p = (char *) next_crlf(buf, &skipped);
535 else if (skipped == 0)
543 char *n_v = nmem_malloc(c->nmem, skipped+1);
544 struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
546 memcpy(n_v, buf, skipped);
549 if (!(cp = strchr(n_v, ':')))
551 h->name = nmem_strdupn(c->nmem, n_v, cp - n_v);
555 h->value = nmem_strdup(c->nmem, cp);
556 h->next = r->headers;
562 // determine if we do keep alive
563 if (!strcmp(c->version, "1.0"))
565 const char *v = http_lookup_header(r->headers, "Connection");
566 if (v && !strcmp(v, "Keep-Alive"))
573 const char *v = http_lookup_header(r->headers, "Connection");
574 if (v && !strcmp(v, "close"))
579 if (buf < start + len)
581 const char *content_type = http_lookup_header(r->headers,
583 r->content_len = start + len - buf;
584 r->content_buf = buf;
586 if (!strcmp(content_type, "application/x-www-form-urlencoded"))
588 http_parse_arguments(r, c->nmem, r->content_buf);
594 static struct http_buf *http_serialize_response(struct http_channel *c,
595 struct http_response *r)
597 struct http_header *h;
599 wrbuf_rewind(c->wrbuf);
600 wrbuf_printf(c->wrbuf, "HTTP/%s %s %s\r\n", c->version, r->code, r->msg);
601 for (h = r->headers; h; h = h->next)
602 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
605 wrbuf_printf(c->wrbuf, "Content-Length: %d\r\n", r->payload ?
606 (int) strlen(r->payload) : 0);
607 wrbuf_printf(c->wrbuf, "Content-Type: text/xml\r\n");
610 xmlDoc *doc = xmlParseMemory(r->payload, strlen(r->payload));
617 yaz_log(YLOG_WARN, "Sending non-wellformed "
618 "response (bug #1162");
619 yaz_log(YLOG_WARN, "payload: %s", r->payload);
623 wrbuf_puts(c->wrbuf, "\r\n");
626 wrbuf_puts(c->wrbuf, r->payload);
628 return http_buf_bywrbuf(c->wrbuf);
631 // Serialize a HTTP request
632 static struct http_buf *http_serialize_request(struct http_request *r)
634 struct http_channel *c = r->channel;
635 struct http_header *h;
637 wrbuf_rewind(c->wrbuf);
638 wrbuf_printf(c->wrbuf, "%s %s%s%s", r->method, r->path,
639 *r->search ? "?" : "", r->search);
641 wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
643 for (h = r->headers; h; h = h->next)
644 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
646 wrbuf_puts(c->wrbuf, "\r\n");
649 wrbuf_write(c->wrbuf, r->content_buf, r->content_len);
652 yaz_log(YLOG_LOG, "WRITING TO PROXY:\n%s\n----",
653 wrbuf_cstr(c->wrbuf));
655 return http_buf_bywrbuf(c->wrbuf);
659 static int http_weshouldproxy(struct http_request *rq)
661 if (proxy_addr && !strstr(rq->path, "search.pz2"))
667 struct http_header * http_header_append(struct http_channel *ch,
668 struct http_header * hp,
672 struct http_header *hpnew = 0;
677 while (hp && hp->next)
680 if(name && strlen(name)&& value && strlen(value)){
681 hpnew = nmem_malloc(ch->nmem, sizeof *hpnew);
682 hpnew->name = nmem_strdup(ch->nmem, name);
683 hpnew->value = nmem_strdup(ch->nmem, value);
697 static int http_proxy(struct http_request *rq)
699 struct http_channel *c = rq->channel;
700 struct http_proxy *p = c->proxy;
701 struct http_header *hp;
702 struct http_buf *requestbuf;
703 char server_via[128] = "";
704 char server_port[16] = "";
705 struct conf_server *ser = global_parameters.server;
707 if (!p) // This is a new connection. Create a proxy channel
714 if (!(pe = getprotobyname("tcp"))) {
717 if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
719 yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
722 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
723 &one, sizeof(one)) < 0)
725 if ((flags = fcntl(sock, F_GETFL, 0)) < 0)
726 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
727 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
728 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
729 if (connect(sock, (struct sockaddr *) proxy_addr,
730 sizeof(*proxy_addr)) < 0)
731 if (errno != EINPROGRESS)
733 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
737 p = xmalloc(sizeof(struct http_proxy));
740 p->first_response = 1;
742 // We will add EVENT_OUTPUT below
743 p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT);
744 iochan_setdata(p->iochan, p);
745 pazpar2_add_channel(p->iochan);
748 // Do _not_ modify Host: header, just checking it's existence
750 if (!http_lookup_header(rq->headers, "Host"))
752 yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
756 // Add new header about paraz2 version, host, remote client address, etc.
759 hp = http_header_append(c, hp,
760 "X-Pazpar2-Version", PACKAGE_VERSION);
761 hp = http_header_append(c, hp,
762 "X-Pazpar2-Server-Host", ser->host);
763 sprintf(server_port, "%d", ser->port);
764 hp = http_header_append(c, hp,
765 "X-Pazpar2-Server-Port", server_port);
766 sprintf(server_via, "1.1 %s:%s (%s/%s)",
767 ser->host, server_port, PACKAGE_NAME, PACKAGE_VERSION);
768 hp = http_header_append(c, hp, "Via" , server_via);
769 hp = http_header_append(c, hp, "X-Forwarded-For", c->addr);
772 requestbuf = http_serialize_request(rq);
774 http_buf_enqueue(&p->oqueue, requestbuf);
775 iochan_setflag(p->iochan, EVENT_OUTPUT);
779 void http_send_response(struct http_channel *ch)
781 struct http_response *rs = ch->response;
785 hb = http_serialize_response(ch, rs);
788 yaz_log(YLOG_WARN, "Failed to serialize HTTP response");
789 http_destroy(ch->iochan);
793 http_buf_enqueue(&ch->oqueue, hb);
794 iochan_setflag(ch->iochan, EVENT_OUTPUT);
795 ch->state = Http_Idle;
799 static void http_error(struct http_channel *hc, int no, const char *msg)
801 struct http_response *rs = http_create_response(hc);
804 hc->keep_alive = 0; // not keeping this HTTP session alive
806 sprintf(rs->code, "%d", no);
808 rs->msg = nmem_strdup(hc->nmem, msg);
809 rs->payload = nmem_malloc(hc->nmem, 100);
810 yaz_snprintf(rs->payload, 99, "<error>HTTP Error %d: %s</error>\n",
812 http_send_response(hc);
815 static void http_io(IOCHAN i, int event)
817 struct http_channel *hc = iochan_getdata(i);
822 struct http_buf *htbuf;
825 htbuf = http_buf_create();
826 res = read(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1);
827 if (res == -1 && errno == EAGAIN)
829 http_buf_destroy(htbuf);
834 http_buf_destroy(htbuf);
838 htbuf->buf[res] = '\0';
840 http_buf_enqueue(&hc->iqueue, htbuf);
844 if (hc->state == Http_Busy)
846 reqlen = request_check(hc->iqueue);
849 // we have a complete HTTP request
850 nmem_reset(hc->nmem);
851 if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
853 yaz_log(YLOG_WARN, "Failed to parse request");
854 http_error(hc, 400, "Bad Request");
858 yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
860 *hc->request->search ? "?" : "",
861 hc->request->search);
862 if (http_weshouldproxy(hc->request))
863 http_proxy(hc->request);
866 // Execute our business logic!
867 hc->state = Http_Busy;
875 struct http_buf *wb = hc->oqueue;
876 res = write(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len);
879 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
885 hc->oqueue = hc->oqueue->next;
886 http_buf_destroy(wb);
901 iochan_clearflag(i, EVENT_OUTPUT);
903 iochan_setevent(hc->iochan, EVENT_INPUT);
908 if (!hc->oqueue && hc->proxy && !hc->proxy->iochan)
909 http_destroy(i); // Server closed; we're done
912 yaz_log(YLOG_WARN, "Unexpected event on connection");
918 // If this hostname contains our proxy host as a prefix, replace with myurl
919 static char *sub_hostname(struct http_channel *c, char *buf)
922 if (strlen(buf) > 1023)
924 if (strncmp(buf, "http://", 7))
926 if (!strncmp(buf + 7, proxy_url, strlen(proxy_url)))
929 strcat(tmp, buf + strlen(proxy_url) + 7);
930 return nmem_strdup(c->nmem, tmp);
936 // Handles I/O on a client connection to a backend web server (proxy mode)
937 static void proxy_io(IOCHAN pi, int event)
939 struct http_proxy *pc = iochan_getdata(pi);
940 struct http_channel *hc = pc->channel;
945 struct http_buf *htbuf;
948 htbuf = http_buf_create();
949 res = read(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1);
950 if (res == 0 || (res < 0 && errno != EINPROGRESS))
954 yaz_log(YLOG_WARN, "Proxy read came up short");
955 // Close channel and alert client HTTP channel that we're gone
956 http_buf_destroy(htbuf);
957 close(iochan_getfd(pi));
963 http_destroy(hc->iochan);
969 htbuf->buf[res] = '\0';
973 if (pc->first_response) // Check if this is a redirect
976 if ((len = package_check(htbuf->buf)))
978 struct http_response *res = http_parse_response_buf(hc, htbuf->buf, len);
981 const char *location = http_lookup_header(
982 res->header, "Location");
985 // We found a location header. Rewrite it.
986 struct http_buf *buf;
987 h->value = sub_hostname(hc, location);
988 buf = http_serialize_response(hc, res);
989 yaz_log(YLOG_LOG, "Proxy rewrite");
990 http_buf_enqueue(&hc->oqueue, buf);
996 pc->first_response = 0;
999 // Write any remaining payload
1000 if (htbuf->len - htbuf->offset > 0)
1001 http_buf_enqueue(&hc->oqueue, htbuf);
1003 iochan_setflag(hc->iochan, EVENT_OUTPUT);
1006 if (!(htbuf = pc->oqueue))
1008 iochan_clearflag(pi, EVENT_OUTPUT);
1011 res = write(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len);
1014 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
1015 http_destroy(hc->iochan);
1018 if (res == htbuf->len)
1020 struct http_buf *np = htbuf->next;
1021 http_buf_destroy(htbuf);
1027 htbuf->offset += res;
1031 iochan_setflags(pi, EVENT_INPUT); // Turns off output flag
1035 yaz_log(YLOG_WARN, "Unexpected event on connection");
1036 http_destroy(hc->iochan);
1040 static void http_fire_observers(struct http_channel *c);
1041 static void http_destroy_observers(struct http_channel *c);
1044 static void http_destroy(IOCHAN i)
1046 struct http_channel *s = iochan_getdata(i);
1050 if (s->proxy->iochan)
1052 close(iochan_getfd(s->proxy->iochan));
1053 iochan_destroy(s->proxy->iochan);
1055 http_buf_destroy_queue(s->proxy->oqueue);
1058 http_buf_destroy_queue(s->iqueue);
1059 http_buf_destroy_queue(s->oqueue);
1060 http_fire_observers(s);
1061 http_destroy_observers(s);
1062 s->next = http_channel_freelist;
1063 http_channel_freelist = s;
1064 close(iochan_getfd(i));
1068 static struct http_channel *http_create(const char *addr)
1070 struct http_channel *r = http_channel_freelist;
1074 http_channel_freelist = r->next;
1075 nmem_reset(r->nmem);
1076 wrbuf_rewind(r->wrbuf);
1080 r = xmalloc(sizeof(struct http_channel));
1081 r->nmem = nmem_create();
1082 r->wrbuf = wrbuf_alloc();
1086 r->iqueue = r->oqueue = 0;
1087 r->state = Http_Idle;
1093 yaz_log(YLOG_WARN, "Invalid HTTP forward address");
1096 strcpy(r->addr, addr);
1102 /* Accept a new command connection */
1103 static void http_accept(IOCHAN i, int event)
1105 struct sockaddr_in addr;
1106 int fd = iochan_getfd(i);
1111 struct http_channel *ch;
1114 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
1116 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
1119 if ((flags = fcntl(s, F_GETFL, 0)) < 0)
1120 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
1121 if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
1122 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
1124 yaz_log(YLOG_DEBUG, "New command connection");
1125 c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT);
1127 ch = http_create(inet_ntoa(addr.sin_addr));
1129 iochan_setdata(c, ch);
1131 pazpar2_add_channel(c);
1134 /* Create a http-channel listener, syntax [host:]port */
1135 void http_init(const char *addr)
1140 struct sockaddr_in myaddr;
1145 yaz_log(YLOG_LOG, "HTTP listener %s", addr);
1147 memset(&myaddr, 0, sizeof myaddr);
1148 myaddr.sin_family = AF_INET;
1149 pp = strchr(addr, ':');
1152 int len = pp - addr;
1156 strncpy(hostname, addr, len);
1157 hostname[len] = '\0';
1158 if (!(he = gethostbyname(hostname))){
1159 yaz_log(YLOG_FATAL, "Unable to resolve '%s'", hostname);
1163 memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1164 port = atoi(pp + 1);
1169 myaddr.sin_addr.s_addr = INADDR_ANY;
1172 myaddr.sin_port = htons(port);
1174 if (!(p = getprotobyname("tcp"))) {
1177 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
1178 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
1179 if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
1180 &one, sizeof(one)) < 0)
1183 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
1185 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
1188 if (listen(l, SOMAXCONN) < 0)
1190 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
1194 c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT);
1195 pazpar2_add_channel(c);
1198 void http_set_proxyaddr(char *host, char *base_url)
1204 strcpy(myurl, base_url);
1205 strcpy(proxy_url, host);
1206 p = strchr(host, ':');
1207 yaz_log(YLOG_DEBUG, "Proxying for %s", host);
1208 yaz_log(YLOG_LOG, "HTTP backend %s", proxy_url);
1215 if (!(he = gethostbyname(host)))
1217 fprintf(stderr, "Failed to lookup '%s'\n", host);
1220 proxy_addr = xmalloc(sizeof(struct sockaddr_in));
1221 proxy_addr->sin_family = he->h_addrtype;
1222 memcpy(&proxy_addr->sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1223 proxy_addr->sin_port = htons(port);
1226 static void http_fire_observers(struct http_channel *c)
1228 http_channel_observer_t p = c->observers;
1231 p->destroy(p->data, c, p->data2);
1236 static void http_destroy_observers(struct http_channel *c)
1238 while (c->observers)
1240 http_channel_observer_t obs = c->observers;
1241 c->observers = obs->next;
1246 http_channel_observer_t http_add_observer(struct http_channel *c, void *data,
1247 http_channel_destroy_t des)
1249 http_channel_observer_t obs = xmalloc(sizeof(*obs));
1254 obs->next = c->observers;
1259 void http_remove_observer(http_channel_observer_t obs)
1261 struct http_channel *c = obs->chan;
1262 http_channel_observer_t found, *p = &c->observers;
1271 struct http_channel *http_channel_observer_chan(http_channel_observer_t obs)
1276 void http_observer_set_data2(http_channel_observer_t obs, void *data2)
1285 * indent-tabs-mode: nil
1287 * vim: shiftwidth=4 tabstop=8 expandtab