1 /* $Id: http.c,v 1.40 2007-09-26 09:09:15 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 {
71 void (*destroy)(void *data, struct http_channel *chan);
72 struct http_channel_observer_s *next;
73 struct http_channel *chan;
77 static const char *http_lookup_header(struct http_header *header,
80 for (; header; header = header->next)
81 if (!strcasecmp(name, header->name))
86 static struct http_buf *http_buf_create()
90 if (http_buf_freelist)
92 r = http_buf_freelist;
93 http_buf_freelist = http_buf_freelist->next;
96 r = xmalloc(sizeof(struct http_buf));
103 static void http_buf_destroy(struct http_buf *b)
105 b->next = http_buf_freelist;
106 http_buf_freelist = b;
109 static void http_buf_destroy_queue(struct http_buf *b)
121 // Calculate length of chain
122 static int http_buf_len(struct http_buf *b)
125 for (; b; b = b->next)
131 static struct http_buf *http_buf_bybuf(char *b, int len)
133 struct http_buf *res = 0;
134 struct http_buf **p = &res;
139 if (tocopy > HTTP_BUF_SIZE)
140 tocopy = HTTP_BUF_SIZE;
141 *p = http_buf_create();
142 memcpy((*p)->buf, b, tocopy);
151 // Add a (chain of) buffers to the end of an existing queue.
152 static void http_buf_enqueue(struct http_buf **queue, struct http_buf *b)
155 queue = &(*queue)->next;
159 static struct http_buf *http_buf_bywrbuf(WRBUF wrbuf)
161 // Heavens to Betsy (buf)!
162 return http_buf_bybuf(wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
165 // Non-destructively collapse chain of buffers into a string (max *len)
167 static void http_buf_peek(struct http_buf *b, char *buf, int len)
170 while (b && rd < len)
172 int toread = len - rd;
175 memcpy(buf + rd, b->buf + b->offset, toread);
182 static int http_buf_size(struct http_buf *b)
185 for (; b; b = b->next)
190 // Ddestructively munch up to len from head of queue.
191 static int http_buf_read(struct http_buf **b, char *buf, int len)
194 while ((*b) && rd < len)
196 int toread = len - rd;
197 if (toread > (*b)->len)
199 memcpy(buf + rd, (*b)->buf + (*b)->offset, toread);
201 if (toread < (*b)->len)
204 (*b)->offset += toread;
209 struct http_buf *n = (*b)->next;
210 http_buf_destroy(*b);
218 // Buffers may overlap.
219 static void urldecode(char *i, char *o)
231 sscanf(i, "%2hhx", o);
241 // Warning: Buffers may not overlap
242 void urlencode(const char *i, char *o)
246 if (strchr(" /:", *i))
248 sprintf(o, "%%%.2X", (int) *i);
258 void http_addheader(struct http_response *r, const char *name, const char *value)
260 struct http_channel *c = r->channel;
261 struct http_header *h = nmem_malloc(c->nmem, sizeof *h);
262 h->name = nmem_strdup(c->nmem, name);
263 h->value = nmem_strdup(c->nmem, value);
264 h->next = r->headers;
268 char *http_argbyname(struct http_request *r, char *name)
270 struct http_argument *p;
273 for (p = r->arguments; p; p = p->next)
274 if (!strcmp(p->name, name))
279 char *http_headerbyname(struct http_header *h, char *name)
281 for (; h; h = h->next)
282 if (!strcmp(h->name, name))
287 struct http_response *http_create_response(struct http_channel *c)
289 struct http_response *r = nmem_malloc(c->nmem, sizeof(*r));
290 strcpy(r->code, "200");
299 static const char *next_crlf(const char *cp, size_t *skipped)
301 const char *next_cp = strchr(cp, '\n');
304 if (next_cp > cp && next_cp[-1] == '\r')
305 *skipped = next_cp - cp - 1;
307 *skipped = next_cp - cp;
313 // Check if buf contains a package (minus payload)
314 static int package_check(const char *buf, int sz)
322 const char *b = next_crlf(buf, &skipped);
326 // we did not find CRLF.. See if buffer is too large..
327 if (sz >= MAX_HTTP_HEADER-1)
328 return MAX_HTTP_HEADER-1; // yes. Return that (will fail later)
334 // CRLF CRLF , i.e. end of header
335 if (len + content_len <= sz)
336 return len + content_len;
340 // following first skip of \r\n so that we don't consider Method
341 if (!strncasecmp(buf, "Content-Length:", 15))
343 const char *cp = buf+15;
347 while (*cp && isdigit(*cp))
348 content_len = content_len*10 + (*cp++ - '0');
349 if (content_len < 0) /* prevent negative offsets */
353 return 0; // incomplete request
356 // Check if we have a request. Return 0 or length
357 static int request_check(struct http_buf *queue)
359 char tmp[MAX_HTTP_HEADER];
361 // only peek at the header..
362 http_buf_peek(queue, tmp, MAX_HTTP_HEADER-1);
363 // still we only return non-zero if the complete request is received..
364 return package_check(tmp, http_buf_size(queue));
367 struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len)
369 char tmp[MAX_HTTP_HEADER];
370 struct http_response *r = http_create_response(c);
372 struct http_header **hp = &r->headers;
374 if (len >= MAX_HTTP_HEADER)
376 memcpy(tmp, buf, len);
377 for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version
381 for (p2 = p; *p2 && *p2 != ' ' && p2 - p < 3; p2++)
382 r->code[p2 - p] = *p2;
383 if (!(p = strstr(tmp, "\r\n")))
388 if (!(p2 = strstr(p, "\r\n")))
390 if (p == p2) // End of headers
394 struct http_header *h = *hp = nmem_malloc(c->nmem, sizeof(*h));
395 char *value = strchr(p, ':');
399 h->name = nmem_strdup(c->nmem, p);
400 while (isspace(*value))
402 if (value >= p2) // Empty header;
409 h->value = nmem_strdup(c->nmem, value);
418 static int http_parse_arguments(struct http_request *r, NMEM nmem,
421 const char *p2 = args;
425 struct http_argument *a;
426 const char *equal = strchr(p2, '=');
427 const char *eoa = strchr(p2, '&');
430 yaz_log(YLOG_WARN, "Expected '=' in argument");
434 eoa = equal + strlen(equal); // last argument
435 else if (equal > eoa)
437 yaz_log(YLOG_WARN, "Missing '&' in argument");
440 a = nmem_malloc(nmem, sizeof(struct http_argument));
441 a->name = nmem_strdupn(nmem, p2, equal - p2);
442 a->value = nmem_strdupn(nmem, equal+1, eoa - equal - 1);
443 urldecode(a->name, a->name);
444 urldecode(a->value, a->value);
445 a->next = r->arguments;
454 struct http_request *http_parse_request(struct http_channel *c,
455 struct http_buf **queue,
458 struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
460 char *start = nmem_malloc(c->nmem, len+1);
463 if (http_buf_read(queue, buf, len) < len)
465 yaz_log(YLOG_WARN, "http_buf_read < len (%d)", len);
475 for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
479 yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
484 if (!(buf = strchr(buf, ' ')))
486 yaz_log(YLOG_WARN, "Missing Request-URI in HTTP request");
490 if (!(p = strchr(buf, ' ')))
492 yaz_log(YLOG_WARN, "HTTP Request-URI not terminated (too long?)");
496 if ((p2 = strchr(buf, '?'))) // Do we have arguments?
498 r->path = nmem_strdup(c->nmem, buf);
501 r->search = nmem_strdup(c->nmem, p2);
503 http_parse_arguments(r, c->nmem, p2);
507 if (strncmp(buf, "HTTP/", 5))
508 strcpy(r->http_version, "1.0");
512 buf += 5; // strlen("HTTP/")
514 p = (char*) next_crlf(buf, &skipped);
515 if (!p || skipped < 3 || skipped > 5)
518 memcpy(r->http_version, buf, skipped);
519 r->http_version[skipped] = '\0';
522 strcpy(c->version, r->http_version);
529 p = (char *) next_crlf(buf, &skipped);
534 else if (skipped == 0)
542 char *n_v = nmem_malloc(c->nmem, skipped+1);
543 struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
545 memcpy(n_v, buf, skipped);
548 if (!(cp = strchr(n_v, ':')))
550 h->name = nmem_strdupn(c->nmem, n_v, cp - n_v);
554 h->value = nmem_strdup(c->nmem, cp);
555 h->next = r->headers;
561 // determine if we do keep alive
562 if (!strcmp(c->version, "1.0"))
564 const char *v = http_lookup_header(r->headers, "Connection");
565 if (v && !strcmp(v, "Keep-Alive"))
572 const char *v = http_lookup_header(r->headers, "Connection");
573 if (v && !strcmp(v, "close"))
578 if (buf < start + len)
580 const char *content_type = http_lookup_header(r->headers,
582 r->content_len = start + len - buf;
583 r->content_buf = buf;
585 if (!strcmp(content_type, "application/x-www-form-urlencoded"))
587 http_parse_arguments(r, c->nmem, r->content_buf);
593 static struct http_buf *http_serialize_response(struct http_channel *c,
594 struct http_response *r)
596 struct http_header *h;
598 wrbuf_rewind(c->wrbuf);
599 wrbuf_printf(c->wrbuf, "HTTP/%s %s %s\r\n", c->version, r->code, r->msg);
600 for (h = r->headers; h; h = h->next)
601 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
604 wrbuf_printf(c->wrbuf, "Content-Length: %d\r\n", r->payload ?
605 (int) strlen(r->payload) : 0);
606 wrbuf_printf(c->wrbuf, "Content-Type: text/xml\r\n");
609 xmlDoc *doc = xmlParseMemory(r->payload, strlen(r->payload));
616 yaz_log(YLOG_WARN, "Sending non-wellformed "
617 "response (bug #1162");
618 yaz_log(YLOG_WARN, "payload: %s", r->payload);
622 wrbuf_puts(c->wrbuf, "\r\n");
625 wrbuf_puts(c->wrbuf, r->payload);
627 return http_buf_bywrbuf(c->wrbuf);
630 // Serialize a HTTP request
631 static struct http_buf *http_serialize_request(struct http_request *r)
633 struct http_channel *c = r->channel;
634 struct http_header *h;
636 wrbuf_rewind(c->wrbuf);
637 wrbuf_printf(c->wrbuf, "%s %s%s%s", r->method, r->path,
638 *r->search ? "?" : "", r->search);
640 wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
642 for (h = r->headers; h; h = h->next)
643 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
645 wrbuf_puts(c->wrbuf, "\r\n");
648 wrbuf_write(c->wrbuf, r->content_buf, r->content_len);
651 yaz_log(YLOG_LOG, "WRITING TO PROXY:\n%s\n----",
652 wrbuf_cstr(c->wrbuf));
654 return http_buf_bywrbuf(c->wrbuf);
658 static int http_weshouldproxy(struct http_request *rq)
660 if (proxy_addr && !strstr(rq->path, "search.pz2"))
666 struct http_header * http_header_append(struct http_channel *ch,
667 struct http_header * hp,
671 struct http_header *hpnew = 0;
676 while (hp && hp->next)
679 if(name && strlen(name)&& value && strlen(value)){
680 hpnew = nmem_malloc(ch->nmem, sizeof *hpnew);
681 hpnew->name = nmem_strdup(ch->nmem, name);
682 hpnew->value = nmem_strdup(ch->nmem, value);
696 static int http_proxy(struct http_request *rq)
698 struct http_channel *c = rq->channel;
699 struct http_proxy *p = c->proxy;
700 struct http_header *hp;
701 struct http_buf *requestbuf;
702 char server_via[128] = "";
703 char server_port[16] = "";
704 struct conf_server *ser = global_parameters.server;
706 if (!p) // This is a new connection. Create a proxy channel
713 if (!(pe = getprotobyname("tcp"))) {
716 if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
718 yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
721 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
722 &one, sizeof(one)) < 0)
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");
728 if (connect(sock, (struct sockaddr *) proxy_addr,
729 sizeof(*proxy_addr)) < 0)
730 if (errno != EINPROGRESS)
732 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
736 p = xmalloc(sizeof(struct http_proxy));
739 p->first_response = 1;
741 // We will add EVENT_OUTPUT below
742 p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT);
743 iochan_setdata(p->iochan, p);
744 pazpar2_add_channel(p->iochan);
747 // Do _not_ modify Host: header, just checking it's existence
749 if (!http_lookup_header(rq->headers, "Host"))
751 yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
755 // Add new header about paraz2 version, host, remote client address, etc.
758 hp = http_header_append(c, hp,
759 "X-Pazpar2-Version", PACKAGE_VERSION);
760 hp = http_header_append(c, hp,
761 "X-Pazpar2-Server-Host", ser->host);
762 sprintf(server_port, "%d", ser->port);
763 hp = http_header_append(c, hp,
764 "X-Pazpar2-Server-Port", server_port);
765 sprintf(server_via, "1.1 %s:%s (%s/%s)",
766 ser->host, server_port, PACKAGE_NAME, PACKAGE_VERSION);
767 hp = http_header_append(c, hp, "Via" , server_via);
768 hp = http_header_append(c, hp, "X-Forwarded-For", c->addr);
771 requestbuf = http_serialize_request(rq);
773 http_buf_enqueue(&p->oqueue, requestbuf);
774 iochan_setflag(p->iochan, EVENT_OUTPUT);
778 void http_send_response(struct http_channel *ch)
780 struct http_response *rs = ch->response;
784 hb = http_serialize_response(ch, rs);
787 yaz_log(YLOG_WARN, "Failed to serialize HTTP response");
788 http_destroy(ch->iochan);
792 http_buf_enqueue(&ch->oqueue, hb);
793 iochan_setflag(ch->iochan, EVENT_OUTPUT);
794 ch->state = Http_Idle;
798 static void http_error(struct http_channel *hc, int no, const char *msg)
800 struct http_response *rs = http_create_response(hc);
803 hc->keep_alive = 0; // not keeping this HTTP session alive
805 sprintf(rs->code, "%d", no);
807 rs->msg = nmem_strdup(hc->nmem, msg);
808 rs->payload = nmem_malloc(hc->nmem, 100);
809 yaz_snprintf(rs->payload, 99, "<error>HTTP Error %d: %s</error>\n",
811 http_send_response(hc);
814 static void http_io(IOCHAN i, int event)
816 struct http_channel *hc = iochan_getdata(i);
821 struct http_buf *htbuf;
824 htbuf = http_buf_create();
825 res = read(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1);
826 if (res == -1 && errno == EAGAIN)
828 http_buf_destroy(htbuf);
833 http_buf_destroy(htbuf);
837 htbuf->buf[res] = '\0';
839 http_buf_enqueue(&hc->iqueue, htbuf);
843 if (hc->state == Http_Busy)
845 reqlen = request_check(hc->iqueue);
848 // we have a complete HTTP request
849 nmem_reset(hc->nmem);
850 if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
852 yaz_log(YLOG_WARN, "Failed to parse request");
853 http_error(hc, 400, "Bad Request");
857 yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
859 *hc->request->search ? "?" : "",
860 hc->request->search);
861 if (http_weshouldproxy(hc->request))
862 http_proxy(hc->request);
865 // Execute our business logic!
866 hc->state = Http_Busy;
874 struct http_buf *wb = hc->oqueue;
875 res = write(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len);
878 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
884 hc->oqueue = hc->oqueue->next;
885 http_buf_destroy(wb);
900 iochan_clearflag(i, EVENT_OUTPUT);
902 iochan_setevent(hc->iochan, EVENT_INPUT);
907 if (!hc->oqueue && hc->proxy && !hc->proxy->iochan)
908 http_destroy(i); // Server closed; we're done
911 yaz_log(YLOG_WARN, "Unexpected event on connection");
917 // If this hostname contains our proxy host as a prefix, replace with myurl
918 static char *sub_hostname(struct http_channel *c, char *buf)
921 if (strlen(buf) > 1023)
923 if (strncmp(buf, "http://", 7))
925 if (!strncmp(buf + 7, proxy_url, strlen(proxy_url)))
928 strcat(tmp, buf + strlen(proxy_url) + 7);
929 return nmem_strdup(c->nmem, tmp);
935 // Handles I/O on a client connection to a backend web server (proxy mode)
936 static void proxy_io(IOCHAN pi, int event)
938 struct http_proxy *pc = iochan_getdata(pi);
939 struct http_channel *hc = pc->channel;
944 struct http_buf *htbuf;
947 htbuf = http_buf_create();
948 res = read(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1);
949 if (res == 0 || (res < 0 && errno != EINPROGRESS))
953 yaz_log(YLOG_WARN, "Proxy read came up short");
954 // Close channel and alert client HTTP channel that we're gone
955 http_buf_destroy(htbuf);
956 close(iochan_getfd(pi));
962 http_destroy(hc->iochan);
968 htbuf->buf[res] = '\0';
972 if (pc->first_response) // Check if this is a redirect
975 if ((len = package_check(htbuf->buf)))
977 struct http_response *res = http_parse_response_buf(hc, htbuf->buf, len);
980 const char *location = http_lookup_header(
981 res->header, "Location");
984 // We found a location header. Rewrite it.
985 struct http_buf *buf;
986 h->value = sub_hostname(hc, location);
987 buf = http_serialize_response(hc, res);
988 yaz_log(YLOG_LOG, "Proxy rewrite");
989 http_buf_enqueue(&hc->oqueue, buf);
995 pc->first_response = 0;
998 // Write any remaining payload
999 if (htbuf->len - htbuf->offset > 0)
1000 http_buf_enqueue(&hc->oqueue, htbuf);
1002 iochan_setflag(hc->iochan, EVENT_OUTPUT);
1005 if (!(htbuf = pc->oqueue))
1007 iochan_clearflag(pi, EVENT_OUTPUT);
1010 res = write(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len);
1013 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
1014 http_destroy(hc->iochan);
1017 if (res == htbuf->len)
1019 struct http_buf *np = htbuf->next;
1020 http_buf_destroy(htbuf);
1026 htbuf->offset += res;
1030 iochan_setflags(pi, EVENT_INPUT); // Turns off output flag
1034 yaz_log(YLOG_WARN, "Unexpected event on connection");
1035 http_destroy(hc->iochan);
1039 static void http_fire_observers(struct http_channel *c);
1040 static void http_destroy_observers(struct http_channel *c);
1043 static void http_destroy(IOCHAN i)
1045 struct http_channel *s = iochan_getdata(i);
1049 if (s->proxy->iochan)
1051 close(iochan_getfd(s->proxy->iochan));
1052 iochan_destroy(s->proxy->iochan);
1054 http_buf_destroy_queue(s->proxy->oqueue);
1057 http_buf_destroy_queue(s->iqueue);
1058 http_buf_destroy_queue(s->oqueue);
1059 http_fire_observers(s);
1060 http_destroy_observers(s);
1061 s->next = http_channel_freelist;
1062 http_channel_freelist = s;
1063 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);
1110 struct http_channel *ch;
1113 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
1115 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
1118 if ((flags = fcntl(s, F_GETFL, 0)) < 0)
1119 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
1120 if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
1121 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
1123 yaz_log(YLOG_DEBUG, "New command connection");
1124 c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT);
1126 ch = http_create(inet_ntoa(addr.sin_addr));
1128 iochan_setdata(c, ch);
1130 pazpar2_add_channel(c);
1133 /* Create a http-channel listener, syntax [host:]port */
1134 void http_init(const char *addr)
1139 struct sockaddr_in myaddr;
1144 yaz_log(YLOG_LOG, "HTTP listener %s", addr);
1146 memset(&myaddr, 0, sizeof myaddr);
1147 myaddr.sin_family = AF_INET;
1148 pp = strchr(addr, ':');
1151 int len = pp - addr;
1155 strncpy(hostname, addr, len);
1156 hostname[len] = '\0';
1157 if (!(he = gethostbyname(hostname))){
1158 yaz_log(YLOG_FATAL, "Unable to resolve '%s'", hostname);
1162 memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1163 port = atoi(pp + 1);
1168 myaddr.sin_addr.s_addr = INADDR_ANY;
1171 myaddr.sin_port = htons(port);
1173 if (!(p = getprotobyname("tcp"))) {
1176 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
1177 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
1178 if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
1179 &one, sizeof(one)) < 0)
1182 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
1184 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
1187 if (listen(l, SOMAXCONN) < 0)
1189 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
1193 c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT);
1194 pazpar2_add_channel(c);
1197 void http_set_proxyaddr(char *host, char *base_url)
1203 strcpy(myurl, base_url);
1204 strcpy(proxy_url, host);
1205 p = strchr(host, ':');
1206 yaz_log(YLOG_DEBUG, "Proxying for %s", host);
1207 yaz_log(YLOG_LOG, "HTTP backend %s", proxy_url);
1214 if (!(he = gethostbyname(host)))
1216 fprintf(stderr, "Failed to lookup '%s'\n", host);
1219 proxy_addr = xmalloc(sizeof(struct sockaddr_in));
1220 proxy_addr->sin_family = he->h_addrtype;
1221 memcpy(&proxy_addr->sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1222 proxy_addr->sin_port = htons(port);
1225 static void http_fire_observers(struct http_channel *c)
1227 http_channel_observer_t p = c->observers;
1230 p->destroy(p->data, c);
1235 static void http_destroy_observers(struct http_channel *c)
1237 while (c->observers)
1239 http_channel_observer_t obs = c->observers;
1240 c->observers = obs->next;
1245 http_channel_observer_t http_add_observer(struct http_channel *c, void *data,
1246 http_channel_destroy_t des)
1248 http_channel_observer_t obs = xmalloc(sizeof(*obs));
1252 obs->next = c->observers;
1257 void http_remove_observer(http_channel_observer_t obs)
1259 struct http_channel *c = obs->chan;
1260 http_channel_observer_t found, *p = &c->observers;
1269 struct http_channel *http_channel_observer_chan(http_channel_observer_t obs)
1277 * indent-tabs-mode: nil
1279 * vim: shiftwidth=4 tabstop=8 expandtab