1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2011 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>
64 #include <yaz/mutex.h>
70 #define MAX_HTTP_HEADER 4096
73 #define strncasecmp _strnicmp
74 #define strcasecmp _stricmp
79 #define HTTP_BUF_SIZE 4096
83 struct http_buf *next;
87 static void proxy_io(IOCHAN i, int event);
88 static struct http_channel *http_channel_create(http_server_t http_server,
90 struct conf_server *server);
91 static void http_channel_destroy(IOCHAN i);
92 static http_server_t http_server_create(void);
93 static void http_server_incref(http_server_t hs);
97 struct http_buf *http_buf_freelist;
98 struct http_channel *http_channel_freelist;
102 http_sessions_t http_sessions;
103 struct sockaddr_in *proxy_addr;
106 struct http_channel_observer_s {
109 http_channel_destroy_t destroy;
110 struct http_channel_observer_s *next;
111 struct http_channel *chan;
115 const char *http_lookup_header(struct http_header *header,
118 for (; header; header = header->next)
119 if (!strcasecmp(name, header->name))
120 return header->value;
124 static struct http_buf *http_buf_create(http_server_t hs)
126 struct http_buf *r = 0;
128 yaz_mutex_enter(hs->mutex);
129 if (hs->http_buf_freelist)
131 r = hs->http_buf_freelist;
132 hs->http_buf_freelist = hs->http_buf_freelist->next;
134 yaz_mutex_leave(hs->mutex);
136 r = xmalloc(sizeof(struct http_buf));
143 static void http_buf_destroy(http_server_t hs, struct http_buf *b)
145 yaz_mutex_enter(hs->mutex);
146 b->next = hs->http_buf_freelist;
147 hs->http_buf_freelist = b;
148 yaz_mutex_leave(hs->mutex);
151 static void http_buf_destroy_queue(http_server_t hs, struct http_buf *b)
157 http_buf_destroy(hs, b);
162 static struct http_buf *http_buf_bybuf(http_server_t hs, char *b, int len)
164 struct http_buf *res = 0;
165 struct http_buf **p = &res;
170 if (tocopy > HTTP_BUF_SIZE)
171 tocopy = HTTP_BUF_SIZE;
172 *p = http_buf_create(hs);
173 memcpy((*p)->buf, b, tocopy);
182 // Add a (chain of) buffers to the end of an existing queue.
183 static void http_buf_enqueue(struct http_buf **queue, struct http_buf *b)
186 queue = &(*queue)->next;
190 static struct http_buf *http_buf_bywrbuf(http_server_t hs, WRBUF wrbuf)
192 // Heavens to Betsy (buf)!
193 return http_buf_bybuf(hs, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
196 // Non-destructively collapse chain of buffers into a string (max *len)
198 static void http_buf_peek(struct http_buf *b, char *buf, int len)
201 while (b && rd < len)
203 int toread = len - rd;
206 memcpy(buf + rd, b->buf + b->offset, toread);
213 static int http_buf_size(struct http_buf *b)
216 for (; b; b = b->next)
221 // Ddestructively munch up to len from head of queue.
222 static int http_buf_read(http_server_t hs,
223 struct http_buf **b, char *buf, int len)
226 while ((*b) && rd < len)
228 int toread = len - rd;
229 if (toread > (*b)->len)
231 memcpy(buf + rd, (*b)->buf + (*b)->offset, toread);
233 if (toread < (*b)->len)
236 (*b)->offset += toread;
241 struct http_buf *n = (*b)->next;
242 http_buf_destroy(hs, *b);
250 // Buffers may overlap.
251 static void urldecode(char *i, char *o)
260 else if (*i == '%' && i[1] && i[2])
264 sscanf(i, "%2x", &v);
274 // Warning: Buffers may not overlap
275 void urlencode(const char *i, char *o)
279 if (strchr(" /:", *i))
281 sprintf(o, "%%%.2X", (int) *i);
291 void http_addheader(struct http_response *r, const char *name, const char *value)
293 struct http_channel *c = r->channel;
294 struct http_header *h = nmem_malloc(c->nmem, sizeof *h);
295 h->name = nmem_strdup(c->nmem, name);
296 h->value = nmem_strdup(c->nmem, value);
297 h->next = r->headers;
301 const char *http_argbyname(struct http_request *r, const char *name)
303 struct http_argument *p;
306 for (p = r->arguments; p; p = p->next)
307 if (!strcmp(p->name, name))
312 const char *http_headerbyname(struct http_header *h, const char *name)
314 for (; h; h = h->next)
315 if (!strcmp(h->name, name))
320 struct http_response *http_create_response(struct http_channel *c)
322 struct http_response *r = nmem_malloc(c->nmem, sizeof(*r));
323 strcpy(r->code, "200");
328 r->content_type = "text/xml";
333 static const char *next_crlf(const char *cp, size_t *skipped)
335 const char *next_cp = strchr(cp, '\n');
338 if (next_cp > cp && next_cp[-1] == '\r')
339 *skipped = next_cp - cp - 1;
341 *skipped = next_cp - cp;
347 // Check if buf contains a package (minus payload)
348 static int package_check(const char *buf, int sz)
356 const char *b = next_crlf(buf, &skipped);
360 // we did not find CRLF.. See if buffer is too large..
361 if (sz >= MAX_HTTP_HEADER-1)
362 return MAX_HTTP_HEADER-1; // yes. Return that (will fail later)
368 // CRLF CRLF , i.e. end of header
369 if (len + content_len <= sz)
370 return len + content_len;
374 // following first skip of \r\n so that we don't consider Method
375 if (!strncasecmp(buf, "Content-Length:", 15))
377 const char *cp = buf+15;
381 while (*cp && isdigit(*(const unsigned char *)cp))
382 content_len = content_len*10 + (*cp++ - '0');
383 if (content_len < 0) /* prevent negative offsets */
387 return 0; // incomplete request
390 // Check if we have a request. Return 0 or length
391 static int request_check(struct http_buf *queue)
393 char tmp[MAX_HTTP_HEADER];
395 // only peek at the header..
396 http_buf_peek(queue, tmp, MAX_HTTP_HEADER-1);
397 // still we only return non-zero if the complete request is received..
398 return package_check(tmp, http_buf_size(queue));
401 struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len)
403 char tmp[MAX_HTTP_HEADER];
404 struct http_response *r = http_create_response(c);
406 struct http_header **hp = &r->headers;
408 if (len >= MAX_HTTP_HEADER)
410 memcpy(tmp, buf, len);
411 for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version
415 for (p2 = p; *p2 && *p2 != ' ' && p2 - p < 3; p2++)
416 r->code[p2 - p] = *p2;
417 if (!(p = strstr(tmp, "\r\n")))
422 if (!(p2 = strstr(p, "\r\n")))
424 if (p == p2) // End of headers
428 struct http_header *h = *hp = nmem_malloc(c->nmem, sizeof(*h));
429 char *value = strchr(p, ':');
433 h->name = nmem_strdup(c->nmem, p);
434 while (isspace(*(const unsigned char *) value))
436 if (value >= p2) // Empty header;
443 h->value = nmem_strdup(c->nmem, value);
452 static int http_parse_arguments(struct http_request *r, NMEM nmem,
455 const char *p2 = args;
459 struct http_argument *a;
460 const char *equal = strchr(p2, '=');
461 const char *eoa = strchr(p2, '&');
464 yaz_log(YLOG_WARN, "Expected '=' in argument");
468 eoa = equal + strlen(equal); // last argument
469 else if (equal > eoa)
471 yaz_log(YLOG_WARN, "Missing '&' in argument");
474 a = nmem_malloc(nmem, sizeof(struct http_argument));
475 a->name = nmem_strdupn(nmem, p2, equal - p2);
476 a->value = nmem_strdupn(nmem, equal+1, eoa - equal - 1);
477 urldecode(a->name, a->name);
478 urldecode(a->value, a->value);
479 a->next = r->arguments;
488 struct http_request *http_parse_request(struct http_channel *c,
489 struct http_buf **queue,
492 struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
494 char *start = nmem_malloc(c->nmem, len+1);
497 if (http_buf_read(c->http_server, queue, buf, len) < len)
499 yaz_log(YLOG_WARN, "http_buf_read < len (%d)", len);
509 for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
513 yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
518 if (!(buf = strchr(buf, ' ')))
520 yaz_log(YLOG_WARN, "Missing Request-URI in HTTP request");
524 if (!(p = strchr(buf, ' ')))
526 yaz_log(YLOG_WARN, "HTTP Request-URI not terminated (too long?)");
530 if ((p2 = strchr(buf, '?'))) // Do we have arguments?
532 r->path = nmem_strdup(c->nmem, buf);
535 r->search = nmem_strdup(c->nmem, p2);
537 http_parse_arguments(r, c->nmem, p2);
541 if (strncmp(buf, "HTTP/", 5))
542 strcpy(r->http_version, "1.0");
546 buf += 5; // strlen("HTTP/")
548 p = (char*) next_crlf(buf, &skipped);
549 if (!p || skipped < 3 || skipped > 5)
552 memcpy(r->http_version, buf, skipped);
553 r->http_version[skipped] = '\0';
556 strcpy(c->version, r->http_version);
563 p = (char *) next_crlf(buf, &skipped);
568 else if (skipped == 0)
576 char *n_v = nmem_malloc(c->nmem, skipped+1);
577 struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
579 memcpy(n_v, buf, skipped);
582 if (!(cp = strchr(n_v, ':')))
584 h->name = nmem_strdupn(c->nmem, n_v, cp - n_v);
588 h->value = nmem_strdup(c->nmem, cp);
589 h->next = r->headers;
595 // determine if we do keep alive
596 if (!strcmp(c->version, "1.0"))
598 const char *v = http_lookup_header(r->headers, "Connection");
599 if (v && !strcmp(v, "Keep-Alive"))
606 const char *v = http_lookup_header(r->headers, "Connection");
607 if (v && !strcmp(v, "close"))
612 if (buf < start + len)
614 const char *content_type = http_lookup_header(r->headers,
616 r->content_len = start + len - buf;
617 r->content_buf = buf;
619 if (!yaz_strcmp_del("application/x-www-form-urlencoded",
622 http_parse_arguments(r, c->nmem, r->content_buf);
628 static struct http_buf *http_serialize_response(struct http_channel *c,
629 struct http_response *r)
631 struct http_header *h;
633 wrbuf_rewind(c->wrbuf);
634 wrbuf_printf(c->wrbuf, "HTTP/%s %s %s\r\n", c->version, r->code, r->msg);
635 for (h = r->headers; h; h = h->next)
636 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
639 wrbuf_printf(c->wrbuf, "Content-Length: %d\r\n", r->payload ?
640 (int) strlen(r->payload) : 0);
641 wrbuf_printf(c->wrbuf, "Content-Type: %s\r\n", r->content_type);
642 if (!strcmp(r->content_type, "text/xml"))
644 xmlDoc *doc = xmlParseMemory(r->payload, strlen(r->payload));
651 yaz_log(YLOG_WARN, "Sending non-wellformed "
652 "response (bug #1162");
653 yaz_log(YLOG_WARN, "payload: %s", r->payload);
657 wrbuf_puts(c->wrbuf, "\r\n");
660 wrbuf_puts(c->wrbuf, r->payload);
662 return http_buf_bywrbuf(c->http_server, c->wrbuf);
665 // Serialize a HTTP request
666 static struct http_buf *http_serialize_request(struct http_request *r)
668 struct http_channel *c = r->channel;
669 struct http_header *h;
671 wrbuf_rewind(c->wrbuf);
672 wrbuf_printf(c->wrbuf, "%s %s%s%s", r->method, r->path,
673 *r->search ? "?" : "", r->search);
675 wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
677 for (h = r->headers; h; h = h->next)
678 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
680 wrbuf_puts(c->wrbuf, "\r\n");
683 wrbuf_write(c->wrbuf, r->content_buf, r->content_len);
686 yaz_log(YLOG_LOG, "WRITING TO PROXY:\n%s\n----",
687 wrbuf_cstr(c->wrbuf));
689 return http_buf_bywrbuf(c->http_server, c->wrbuf);
693 static int http_weshouldproxy(struct http_request *rq)
695 struct http_channel *c = rq->channel;
696 if (c->server->http_server->proxy_addr && !strstr(rq->path, "search.pz2"))
702 struct http_header * http_header_append(struct http_channel *ch,
703 struct http_header * hp,
707 struct http_header *hpnew = 0;
712 while (hp && hp->next)
715 if(name && strlen(name)&& value && strlen(value)){
716 hpnew = nmem_malloc(ch->nmem, sizeof *hpnew);
717 hpnew->name = nmem_strdup(ch->nmem, name);
718 hpnew->value = nmem_strdup(ch->nmem, value);
731 static int is_inprogress(void)
734 if (WSAGetLastError() == WSAEWOULDBLOCK)
737 if (errno == EINPROGRESS)
743 static void enable_nonblock(int sock)
747 flags = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
748 if (ioctlsocket(sock, FIONBIO, &flags) < 0)
749 yaz_log(YLOG_FATAL|YLOG_ERRNO, "ioctlsocket");
751 if ((flags = fcntl(sock, F_GETFL, 0)) < 0)
752 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
753 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
754 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
758 static int http_proxy(struct http_request *rq)
760 struct http_channel *c = rq->channel;
761 struct http_proxy *p = c->proxy;
762 struct http_header *hp;
763 struct http_buf *requestbuf;
764 char server_port[16] = "";
765 struct conf_server *ser = c->server;
767 if (!p) // This is a new connection. Create a proxy channel
773 if (!(pe = getprotobyname("tcp"))) {
776 if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
778 yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
781 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
782 &one, sizeof(one)) < 0)
784 enable_nonblock(sock);
785 if (connect(sock, (struct sockaddr *)
786 c->server->http_server->proxy_addr,
787 sizeof(*c->server->http_server->proxy_addr)) < 0)
789 if (!is_inprogress())
791 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
795 p = xmalloc(sizeof(struct http_proxy));
798 p->first_response = 1;
800 // We will add EVENT_OUTPUT below
801 p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT, "http_proxy");
802 iochan_setdata(p->iochan, p);
804 iochan_add(ser->iochan_man, p->iochan);
807 // Do _not_ modify Host: header, just checking it's existence
809 if (!http_lookup_header(rq->headers, "Host"))
811 yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
815 // Add new header about paraz2 version, host, remote client address, etc.
817 char server_via[128];
820 hp = http_header_append(c, hp,
821 "X-Pazpar2-Version", PACKAGE_VERSION);
822 hp = http_header_append(c, hp,
823 "X-Pazpar2-Server-Host", ser->host);
824 sprintf(server_port, "%d", ser->port);
825 hp = http_header_append(c, hp,
826 "X-Pazpar2-Server-Port", server_port);
827 yaz_snprintf(server_via, sizeof(server_via),
829 ser->host ? ser->host : "@",
830 server_port, PACKAGE_NAME, PACKAGE_VERSION);
831 hp = http_header_append(c, hp, "Via" , server_via);
832 hp = http_header_append(c, hp, "X-Forwarded-For", c->addr);
835 requestbuf = http_serialize_request(rq);
837 http_buf_enqueue(&p->oqueue, requestbuf);
838 iochan_setflag(p->iochan, EVENT_OUTPUT);
842 void http_send_response(struct http_channel *ch)
844 struct http_response *rs = ch->response;
848 hb = http_serialize_response(ch, rs);
851 yaz_log(YLOG_WARN, "Failed to serialize HTTP response");
852 http_channel_destroy(ch->iochan);
856 http_buf_enqueue(&ch->oqueue, hb);
857 iochan_setflag(ch->iochan, EVENT_OUTPUT);
858 ch->state = Http_Idle;
862 static void http_error(struct http_channel *hc, int no, const char *msg)
864 struct http_response *rs = http_create_response(hc);
867 hc->keep_alive = 0; // not keeping this HTTP session alive
869 sprintf(rs->code, "%d", no);
871 rs->msg = nmem_strdup(hc->nmem, msg);
872 rs->payload = nmem_malloc(hc->nmem, 100);
873 yaz_snprintf(rs->payload, 99, "<error>HTTP Error %d: %s</error>\n",
875 http_send_response(hc);
878 static void http_io(IOCHAN i, int event)
880 struct http_channel *hc = iochan_getdata(i);
883 if (event == EVENT_INPUT)
886 struct http_buf *htbuf;
888 htbuf = http_buf_create(hc->http_server);
889 res = recv(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1, 0);
890 if (res == -1 && errno == EAGAIN)
892 http_buf_destroy(hc->http_server, htbuf);
897 http_buf_destroy(hc->http_server, htbuf);
898 http_channel_destroy(i);
901 htbuf->buf[res] = '\0';
903 http_buf_enqueue(&hc->iqueue, htbuf);
907 if (hc->state == Http_Busy)
909 reqlen = request_check(hc->iqueue);
912 // we have a complete HTTP request
913 nmem_reset(hc->nmem);
914 if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
916 yaz_log(YLOG_WARN, "Failed to parse request");
917 http_error(hc, 400, "Bad Request");
921 yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
923 *hc->request->search ? "?" : "",
924 hc->request->search);
925 if (hc->request->content_buf)
926 yaz_log(YLOG_LOG, "%s", hc->request->content_buf);
927 if (http_weshouldproxy(hc->request))
928 http_proxy(hc->request);
931 // Execute our business logic!
932 hc->state = Http_Busy;
937 else if (event == EVENT_OUTPUT)
942 struct http_buf *wb = hc->oqueue;
944 res = send(iochan_getfd(hc->iochan),
945 wb->buf + wb->offset, wb->len, 0);
948 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
949 http_channel_destroy(i);
954 hc->oqueue = hc->oqueue->next;
955 http_buf_destroy(hc->http_server, wb);
966 http_channel_destroy(i);
971 iochan_clearflag(i, EVENT_OUTPUT);
977 if (!hc->oqueue && hc->proxy && !hc->proxy->iochan)
978 http_channel_destroy(i); // Server closed; we're done
982 yaz_log(YLOG_WARN, "Unexpected event on connection");
983 http_channel_destroy(i);
989 // Handles I/O on a client connection to a backend web server (proxy mode)
990 static void proxy_io(IOCHAN pi, int event)
992 struct http_proxy *pc = iochan_getdata(pi);
993 struct http_channel *hc = pc->channel;
998 struct http_buf *htbuf;
1001 htbuf = http_buf_create(hc->http_server);
1002 res = recv(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1, 0);
1003 if (res == 0 || (res < 0 && !is_inprogress()))
1007 yaz_log(YLOG_WARN, "Proxy read came up short");
1008 // Close channel and alert client HTTP channel that we're gone
1009 http_buf_destroy(hc->http_server, htbuf);
1011 closesocket(iochan_getfd(pi));
1013 close(iochan_getfd(pi));
1020 http_channel_destroy(hc->iochan);
1026 htbuf->buf[res] = '\0';
1029 // Write any remaining payload
1030 if (htbuf->len - htbuf->offset > 0)
1031 http_buf_enqueue(&hc->oqueue, htbuf);
1033 iochan_setflag(hc->iochan, EVENT_OUTPUT);
1036 if (!(htbuf = pc->oqueue))
1038 iochan_clearflag(pi, EVENT_OUTPUT);
1041 res = send(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len, 0);
1044 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
1045 http_channel_destroy(hc->iochan);
1048 if (res == htbuf->len)
1050 struct http_buf *np = htbuf->next;
1051 http_buf_destroy(hc->http_server, htbuf);
1057 htbuf->offset += res;
1061 iochan_setflags(pi, EVENT_INPUT); // Turns off output flag
1065 yaz_log(YLOG_WARN, "Unexpected event on connection");
1066 http_channel_destroy(hc->iochan);
1070 static void http_fire_observers(struct http_channel *c);
1071 static void http_destroy_observers(struct http_channel *c);
1074 static void http_channel_destroy(IOCHAN i)
1076 struct http_channel *s = iochan_getdata(i);
1077 http_server_t http_server;
1081 if (s->proxy->iochan)
1084 closesocket(iochan_getfd(s->proxy->iochan));
1086 close(iochan_getfd(s->proxy->iochan));
1088 iochan_destroy(s->proxy->iochan);
1090 http_buf_destroy_queue(s->http_server, s->proxy->oqueue);
1093 http_buf_destroy_queue(s->http_server, s->iqueue);
1094 http_buf_destroy_queue(s->http_server, s->oqueue);
1095 http_fire_observers(s);
1096 http_destroy_observers(s);
1098 http_server = s->http_server; /* save it for destroy (decref) */
1100 yaz_mutex_enter(s->http_server->mutex);
1101 s->next = s->http_server->http_channel_freelist;
1102 s->http_server->http_channel_freelist = s;
1103 yaz_mutex_leave(s->http_server->mutex);
1105 http_server_destroy(http_server);
1108 closesocket(iochan_getfd(i));
1110 close(iochan_getfd(i));
1115 static struct http_channel *http_channel_create(http_server_t hs,
1117 struct conf_server *server)
1119 struct http_channel *r;
1121 yaz_mutex_enter(hs->mutex);
1122 r = hs->http_channel_freelist;
1124 hs->http_channel_freelist = r->next;
1125 yaz_mutex_leave(hs->mutex);
1129 nmem_reset(r->nmem);
1130 wrbuf_rewind(r->wrbuf);
1134 r = xmalloc(sizeof(struct http_channel));
1135 r->nmem = nmem_create();
1136 r->wrbuf = wrbuf_alloc();
1138 http_server_incref(hs);
1139 r->http_server = hs;
1140 r->http_sessions = hs->http_sessions;
1141 assert(r->http_sessions);
1145 r->iqueue = r->oqueue = 0;
1146 r->state = Http_Idle;
1152 yaz_log(YLOG_WARN, "Invalid HTTP forward address");
1155 strcpy(r->addr, addr);
1161 /* Accept a new command connection */
1162 static void http_accept(IOCHAN i, int event)
1164 struct sockaddr_in addr;
1165 int fd = iochan_getfd(i);
1169 struct http_channel *ch;
1170 struct conf_server *server = iochan_getdata(i);
1173 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
1175 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
1180 yaz_log(YLOG_DEBUG, "New command connection");
1181 c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT, "http_session_socket");
1183 ch = http_channel_create(server->http_server, inet_ntoa(addr.sin_addr),
1186 iochan_setdata(c, ch);
1187 iochan_add(server->iochan_man, c);
1190 /* Create a http-channel listener, syntax [host:]port */
1191 int http_init(const char *addr, struct conf_server *server)
1196 struct sockaddr_in myaddr;
1201 yaz_log(YLOG_LOG, "HTTP listener %s", addr);
1203 memset(&myaddr, 0, sizeof myaddr);
1204 myaddr.sin_family = AF_INET;
1205 pp = strchr(addr, ':');
1208 WRBUF w = wrbuf_alloc();
1211 wrbuf_write(w, addr, pp - addr);
1214 he = gethostbyname(wrbuf_cstr(w));
1218 yaz_log(YLOG_FATAL, "Unable to resolve '%s'", addr);
1221 memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1222 port = atoi(pp + 1);
1227 myaddr.sin_addr.s_addr = INADDR_ANY;
1230 myaddr.sin_port = htons(port);
1232 if (!(p = getprotobyname("tcp"))) {
1235 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
1236 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
1237 if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
1238 &one, sizeof(one)) < 0)
1241 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
1243 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
1246 if (listen(l, SOMAXCONN) < 0)
1248 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
1252 server->http_server = http_server_create();
1254 server->http_server->listener_socket = l;
1256 c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT, "http_server");
1257 iochan_setdata(c, server);
1259 iochan_add(server->iochan_man, c);
1263 void http_close_server(struct conf_server *server)
1265 /* break the event_loop (select) by closing down the HTTP listener sock */
1266 if (server->http_server->listener_socket)
1269 closesocket(server->http_server->listener_socket);
1271 close(server->http_server->listener_socket);
1276 void http_set_proxyaddr(const char *host, struct conf_server *server)
1281 WRBUF w = wrbuf_alloc();
1283 yaz_log(YLOG_LOG, "HTTP backend %s", host);
1285 p = strchr(host, ':');
1289 wrbuf_write(w, host, p - host);
1295 wrbuf_puts(w, host);
1297 if (!(he = gethostbyname(wrbuf_cstr(w))))
1299 fprintf(stderr, "Failed to lookup '%s'\n", wrbuf_cstr(w));
1304 server->http_server->proxy_addr = xmalloc(sizeof(struct sockaddr_in));
1305 server->http_server->proxy_addr->sin_family = he->h_addrtype;
1306 memcpy(&server->http_server->proxy_addr->sin_addr.s_addr,
1307 he->h_addr_list[0], he->h_length);
1308 server->http_server->proxy_addr->sin_port = htons(port);
1311 static void http_fire_observers(struct http_channel *c)
1313 http_channel_observer_t p = c->observers;
1316 p->destroy(p->data, c, p->data2);
1321 static void http_destroy_observers(struct http_channel *c)
1323 while (c->observers)
1325 http_channel_observer_t obs = c->observers;
1326 c->observers = obs->next;
1331 http_channel_observer_t http_add_observer(struct http_channel *c, void *data,
1332 http_channel_destroy_t des)
1334 http_channel_observer_t obs = xmalloc(sizeof(*obs));
1339 obs->next = c->observers;
1344 void http_remove_observer(http_channel_observer_t obs)
1346 struct http_channel *c = obs->chan;
1347 http_channel_observer_t found, *p = &c->observers;
1356 struct http_channel *http_channel_observer_chan(http_channel_observer_t obs)
1361 void http_observer_set_data2(http_channel_observer_t obs, void *data2)
1366 http_server_t http_server_create(void)
1368 http_server_t hs = xmalloc(sizeof(*hs));
1372 hs->http_buf_freelist = 0;
1373 hs->http_channel_freelist = 0;
1374 hs->http_sessions = 0;
1378 void http_server_destroy(http_server_t hs)
1384 yaz_mutex_enter(hs->mutex); /* OK: hs->mutex may be NULL */
1385 r = --(hs->ref_count);
1386 yaz_mutex_leave(hs->mutex);
1390 struct http_buf *b = hs->http_buf_freelist;
1391 struct http_channel *c = hs->http_channel_freelist;
1394 struct http_buf *b_next = b->next;
1400 struct http_channel *c_next = c->next;
1401 nmem_destroy(c->nmem);
1402 wrbuf_destroy(c->wrbuf);
1406 http_sessions_destroy(hs->http_sessions);
1407 xfree(hs->proxy_addr);
1408 yaz_mutex_destroy(&hs->mutex);
1414 void http_server_incref(http_server_t hs)
1417 yaz_mutex_enter(hs->mutex);
1419 yaz_mutex_leave(hs->mutex);
1422 void http_mutex_init(struct conf_server *server)
1426 assert(server->http_server->mutex == 0);
1427 pazpar2_mutex_create(&server->http_server->mutex, "http_server");
1428 server->http_server->http_sessions = http_sessions_create();
1434 * c-file-style: "Stroustrup"
1435 * indent-tabs-mode: nil
1437 * vim: shiftwidth=4 tabstop=8 expandtab