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 int http_buf_freelist_count;
99 int http_buf_freelist_max;
101 struct http_channel *http_channel_freelist;
102 int http_channel_freelist_count;
103 int http_channel_freelist_max;
107 http_sessions_t http_sessions;
108 struct sockaddr_in *proxy_addr;
111 struct http_channel_observer_s {
114 http_channel_destroy_t destroy;
115 struct http_channel_observer_s *next;
116 struct http_channel *chan;
120 const char *http_lookup_header(struct http_header *header,
123 for (; header; header = header->next)
124 if (!strcasecmp(name, header->name))
125 return header->value;
129 static struct http_buf *http_buf_create(http_server_t hs)
131 struct http_buf *r = 0;
133 yaz_mutex_enter(hs->mutex);
134 if (hs->http_buf_freelist)
136 r = hs->http_buf_freelist;
137 hs->http_buf_freelist = hs->http_buf_freelist->next;
138 hs->http_buf_freelist_count--;
140 yaz_mutex_leave(hs->mutex);
142 r = xmalloc(sizeof(struct http_buf));
149 static void http_buf_destroy(http_server_t hs, struct http_buf *b)
151 yaz_mutex_enter(hs->mutex);
152 if (hs->http_buf_freelist_max > 0 && hs->http_buf_freelist_count >= hs->http_buf_freelist_max) {
154 while ((b = hs->http_buf_freelist)) {
156 hs->http_buf_freelist = hs->http_buf_freelist->next;
158 hs->http_buf_freelist_count = 0;
161 b->next = hs->http_buf_freelist;
162 hs->http_buf_freelist = b;
163 hs->http_buf_freelist_count++;
165 yaz_log(YLOG_DEBUG, "Free %d http buffers on server.", hs->http_buf_freelist_count);
168 yaz_mutex_leave(hs->mutex);
171 static void http_buf_destroy_queue(http_server_t hs, struct http_buf *b)
177 http_buf_destroy(hs, b);
182 static struct http_buf *http_buf_bybuf(http_server_t hs, char *b, int len)
184 struct http_buf *res = 0;
185 struct http_buf **p = &res;
190 if (tocopy > HTTP_BUF_SIZE)
191 tocopy = HTTP_BUF_SIZE;
192 *p = http_buf_create(hs);
193 memcpy((*p)->buf, b, tocopy);
202 // Add a (chain of) buffers to the end of an existing queue.
203 static void http_buf_enqueue(struct http_buf **queue, struct http_buf *b)
206 queue = &(*queue)->next;
210 static struct http_buf *http_buf_bywrbuf(http_server_t hs, WRBUF wrbuf)
212 // Heavens to Betsy (buf)!
213 return http_buf_bybuf(hs, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
216 // Non-destructively collapse chain of buffers into a string (max *len)
218 static void http_buf_peek(struct http_buf *b, char *buf, int len)
221 while (b && rd < len)
223 int toread = len - rd;
226 memcpy(buf + rd, b->buf + b->offset, toread);
233 static int http_buf_size(struct http_buf *b)
236 for (; b; b = b->next)
241 // Ddestructively munch up to len from head of queue.
242 static int http_buf_read(http_server_t hs,
243 struct http_buf **b, char *buf, int len)
246 while ((*b) && rd < len)
248 int toread = len - rd;
249 if (toread > (*b)->len)
251 memcpy(buf + rd, (*b)->buf + (*b)->offset, toread);
253 if (toread < (*b)->len)
256 (*b)->offset += toread;
261 struct http_buf *n = (*b)->next;
262 http_buf_destroy(hs, *b);
270 // Buffers may overlap.
271 static void urldecode(char *i, char *o)
280 else if (*i == '%' && i[1] && i[2])
284 sscanf(i, "%2x", &v);
294 // Warning: Buffers may not overlap
295 void urlencode(const char *i, char *o)
299 if (strchr(" /:", *i))
301 sprintf(o, "%%%.2X", (int) *i);
311 void http_addheader(struct http_response *r, const char *name, const char *value)
313 struct http_channel *c = r->channel;
314 struct http_header *h = nmem_malloc(c->nmem, sizeof *h);
315 h->name = nmem_strdup(c->nmem, name);
316 h->value = nmem_strdup(c->nmem, value);
317 h->next = r->headers;
321 const char *http_argbyname(struct http_request *r, const char *name)
323 struct http_argument *p;
326 for (p = r->arguments; p; p = p->next)
327 if (!strcmp(p->name, name))
332 const char *http_headerbyname(struct http_header *h, const char *name)
334 for (; h; h = h->next)
335 if (!strcmp(h->name, name))
340 struct http_response *http_create_response(struct http_channel *c)
342 struct http_response *r = nmem_malloc(c->nmem, sizeof(*r));
343 strcpy(r->code, "200");
348 r->content_type = "text/xml";
353 static const char *next_crlf(const char *cp, size_t *skipped)
355 const char *next_cp = strchr(cp, '\n');
358 if (next_cp > cp && next_cp[-1] == '\r')
359 *skipped = next_cp - cp - 1;
361 *skipped = next_cp - cp;
367 // Check if buf contains a package (minus payload)
368 static int package_check(const char *buf, int sz)
376 const char *b = next_crlf(buf, &skipped);
380 // we did not find CRLF.. See if buffer is too large..
381 if (sz >= MAX_HTTP_HEADER-1)
382 return MAX_HTTP_HEADER-1; // yes. Return that (will fail later)
388 // CRLF CRLF , i.e. end of header
389 if (len + content_len <= sz)
390 return len + content_len;
394 // following first skip of \r\n so that we don't consider Method
395 if (!strncasecmp(buf, "Content-Length:", 15))
397 const char *cp = buf+15;
401 while (*cp && isdigit(*(const unsigned char *)cp))
402 content_len = content_len*10 + (*cp++ - '0');
403 if (content_len < 0) /* prevent negative offsets */
407 return 0; // incomplete request
410 // Check if we have a request. Return 0 or length
411 static int request_check(struct http_buf *queue)
413 char tmp[MAX_HTTP_HEADER];
415 // only peek at the header..
416 http_buf_peek(queue, tmp, MAX_HTTP_HEADER-1);
417 // still we only return non-zero if the complete request is received..
418 return package_check(tmp, http_buf_size(queue));
421 struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len)
423 char tmp[MAX_HTTP_HEADER];
424 struct http_response *r = http_create_response(c);
426 struct http_header **hp = &r->headers;
428 if (len >= MAX_HTTP_HEADER)
430 memcpy(tmp, buf, len);
431 for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version
435 for (p2 = p; *p2 && *p2 != ' ' && p2 - p < 3; p2++)
436 r->code[p2 - p] = *p2;
437 if (!(p = strstr(tmp, "\r\n")))
442 if (!(p2 = strstr(p, "\r\n")))
444 if (p == p2) // End of headers
448 struct http_header *h = *hp = nmem_malloc(c->nmem, sizeof(*h));
449 char *value = strchr(p, ':');
453 h->name = nmem_strdup(c->nmem, p);
454 while (isspace(*(const unsigned char *) value))
456 if (value >= p2) // Empty header;
463 h->value = nmem_strdup(c->nmem, value);
472 static int http_parse_arguments(struct http_request *r, NMEM nmem,
475 const char *p2 = args;
479 struct http_argument *a;
480 const char *equal = strchr(p2, '=');
481 const char *eoa = strchr(p2, '&');
484 yaz_log(YLOG_WARN, "Expected '=' in argument");
488 eoa = equal + strlen(equal); // last argument
489 else if (equal > eoa)
491 yaz_log(YLOG_WARN, "Missing '&' in argument");
494 a = nmem_malloc(nmem, sizeof(struct http_argument));
495 a->name = nmem_strdupn(nmem, p2, equal - p2);
496 a->value = nmem_strdupn(nmem, equal+1, eoa - equal - 1);
497 urldecode(a->name, a->name);
498 urldecode(a->value, a->value);
499 a->next = r->arguments;
508 struct http_request *http_parse_request(struct http_channel *c,
509 struct http_buf **queue,
512 struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
514 char *start = nmem_malloc(c->nmem, len+1);
517 if (http_buf_read(c->http_server, queue, buf, len) < len)
519 yaz_log(YLOG_WARN, "http_buf_read < len (%d)", len);
529 for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
533 yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
538 if (!(buf = strchr(buf, ' ')))
540 yaz_log(YLOG_WARN, "Missing Request-URI in HTTP request");
544 if (!(p = strchr(buf, ' ')))
546 yaz_log(YLOG_WARN, "HTTP Request-URI not terminated (too long?)");
550 if ((p2 = strchr(buf, '?'))) // Do we have arguments?
552 r->path = nmem_strdup(c->nmem, buf);
555 r->search = nmem_strdup(c->nmem, p2);
557 http_parse_arguments(r, c->nmem, p2);
561 if (strncmp(buf, "HTTP/", 5))
562 strcpy(r->http_version, "1.0");
566 buf += 5; // strlen("HTTP/")
568 p = (char*) next_crlf(buf, &skipped);
569 if (!p || skipped < 3 || skipped > 5)
572 memcpy(r->http_version, buf, skipped);
573 r->http_version[skipped] = '\0';
576 strcpy(c->version, r->http_version);
583 p = (char *) next_crlf(buf, &skipped);
588 else if (skipped == 0)
596 char *n_v = nmem_malloc(c->nmem, skipped+1);
597 struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
599 memcpy(n_v, buf, skipped);
602 if (!(cp = strchr(n_v, ':')))
604 h->name = nmem_strdupn(c->nmem, n_v, cp - n_v);
608 h->value = nmem_strdup(c->nmem, cp);
609 h->next = r->headers;
615 // determine if we do keep alive
616 if (!strcmp(c->version, "1.0"))
618 const char *v = http_lookup_header(r->headers, "Connection");
619 if (v && !strcmp(v, "Keep-Alive"))
626 const char *v = http_lookup_header(r->headers, "Connection");
627 if (v && !strcmp(v, "close"))
632 if (buf < start + len)
634 const char *content_type = http_lookup_header(r->headers,
636 r->content_len = start + len - buf;
637 r->content_buf = buf;
639 if (!yaz_strcmp_del("application/x-www-form-urlencoded",
642 http_parse_arguments(r, c->nmem, r->content_buf);
648 static struct http_buf *http_serialize_response(struct http_channel *c,
649 struct http_response *r)
651 struct http_header *h;
653 wrbuf_rewind(c->wrbuf);
654 wrbuf_printf(c->wrbuf, "HTTP/%s %s %s\r\n", c->version, r->code, r->msg);
655 for (h = r->headers; h; h = h->next)
656 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
659 wrbuf_printf(c->wrbuf, "Content-Length: %d\r\n", r->payload ?
660 (int) strlen(r->payload) : 0);
661 wrbuf_printf(c->wrbuf, "Content-Type: %s\r\n", r->content_type);
662 if (!strcmp(r->content_type, "text/xml"))
664 xmlDoc *doc = xmlParseMemory(r->payload, strlen(r->payload));
671 yaz_log(YLOG_WARN, "Sending non-wellformed "
672 "response (bug #1162");
673 yaz_log(YLOG_WARN, "payload: %s", r->payload);
677 wrbuf_puts(c->wrbuf, "\r\n");
680 wrbuf_puts(c->wrbuf, r->payload);
682 return http_buf_bywrbuf(c->http_server, c->wrbuf);
685 // Serialize a HTTP request
686 static struct http_buf *http_serialize_request(struct http_request *r)
688 struct http_channel *c = r->channel;
689 struct http_header *h;
691 wrbuf_rewind(c->wrbuf);
692 wrbuf_printf(c->wrbuf, "%s %s%s%s", r->method, r->path,
693 *r->search ? "?" : "", r->search);
695 wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
697 for (h = r->headers; h; h = h->next)
698 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
700 wrbuf_puts(c->wrbuf, "\r\n");
703 wrbuf_write(c->wrbuf, r->content_buf, r->content_len);
706 yaz_log(YLOG_LOG, "WRITING TO PROXY:\n%s\n----",
707 wrbuf_cstr(c->wrbuf));
709 return http_buf_bywrbuf(c->http_server, c->wrbuf);
713 static int http_weshouldproxy(struct http_request *rq)
715 struct http_channel *c = rq->channel;
716 if (c->server->http_server->proxy_addr && !strstr(rq->path, "search.pz2"))
722 struct http_header * http_header_append(struct http_channel *ch,
723 struct http_header * hp,
727 struct http_header *hpnew = 0;
732 while (hp && hp->next)
735 if(name && strlen(name)&& value && strlen(value)){
736 hpnew = nmem_malloc(ch->nmem, sizeof *hpnew);
737 hpnew->name = nmem_strdup(ch->nmem, name);
738 hpnew->value = nmem_strdup(ch->nmem, value);
751 static int is_inprogress(void)
754 if (WSAGetLastError() == WSAEWOULDBLOCK)
757 if (errno == EINPROGRESS)
763 static void enable_nonblock(int sock)
767 flags = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
768 if (ioctlsocket(sock, FIONBIO, &flags) < 0)
769 yaz_log(YLOG_FATAL|YLOG_ERRNO, "ioctlsocket");
771 if ((flags = fcntl(sock, F_GETFL, 0)) < 0)
772 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
773 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
774 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
778 static int http_proxy(struct http_request *rq)
780 struct http_channel *c = rq->channel;
781 struct http_proxy *p = c->proxy;
782 struct http_header *hp;
783 struct http_buf *requestbuf;
784 char server_port[16] = "";
785 struct conf_server *ser = c->server;
787 if (!p) // This is a new connection. Create a proxy channel
793 if (!(pe = getprotobyname("tcp"))) {
796 if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
798 yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
801 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
802 &one, sizeof(one)) < 0)
804 enable_nonblock(sock);
805 if (connect(sock, (struct sockaddr *)
806 c->server->http_server->proxy_addr,
807 sizeof(*c->server->http_server->proxy_addr)) < 0)
809 if (!is_inprogress())
811 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
815 p = xmalloc(sizeof(struct http_proxy));
818 p->first_response = 1;
820 // We will add EVENT_OUTPUT below
821 p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT, "http_proxy");
822 iochan_setdata(p->iochan, p);
824 iochan_add(ser->iochan_man, p->iochan);
827 // Do _not_ modify Host: header, just checking it's existence
829 if (!http_lookup_header(rq->headers, "Host"))
831 yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
835 // Add new header about paraz2 version, host, remote client address, etc.
837 char server_via[128];
840 hp = http_header_append(c, hp,
841 "X-Pazpar2-Version", PACKAGE_VERSION);
842 hp = http_header_append(c, hp,
843 "X-Pazpar2-Server-Host", ser->host);
844 sprintf(server_port, "%d", ser->port);
845 hp = http_header_append(c, hp,
846 "X-Pazpar2-Server-Port", server_port);
847 yaz_snprintf(server_via, sizeof(server_via),
849 ser->host ? ser->host : "@",
850 server_port, PACKAGE_NAME, PACKAGE_VERSION);
851 hp = http_header_append(c, hp, "Via" , server_via);
852 hp = http_header_append(c, hp, "X-Forwarded-For", c->addr);
855 requestbuf = http_serialize_request(rq);
857 http_buf_enqueue(&p->oqueue, requestbuf);
858 iochan_setflag(p->iochan, EVENT_OUTPUT);
862 void http_send_response(struct http_channel *ch)
864 struct http_response *rs = ch->response;
868 hb = http_serialize_response(ch, rs);
871 yaz_log(YLOG_WARN, "Failed to serialize HTTP response");
872 http_channel_destroy(ch->iochan);
876 http_buf_enqueue(&ch->oqueue, hb);
877 iochan_setflag(ch->iochan, EVENT_OUTPUT);
878 ch->state = Http_Idle;
882 static void http_error(struct http_channel *hc, int no, const char *msg)
884 struct http_response *rs = http_create_response(hc);
887 hc->keep_alive = 0; // not keeping this HTTP session alive
889 sprintf(rs->code, "%d", no);
891 rs->msg = nmem_strdup(hc->nmem, msg);
892 rs->payload = nmem_malloc(hc->nmem, 100);
893 yaz_snprintf(rs->payload, 99, "<error>HTTP Error %d: %s</error>\n",
895 http_send_response(hc);
898 static void http_io(IOCHAN i, int event)
900 struct http_channel *hc = iochan_getdata(i);
903 if (event == EVENT_INPUT)
906 struct http_buf *htbuf;
908 htbuf = http_buf_create(hc->http_server);
909 res = recv(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1, 0);
910 if (res == -1 && errno == EAGAIN)
912 http_buf_destroy(hc->http_server, htbuf);
917 http_buf_destroy(hc->http_server, htbuf);
918 http_channel_destroy(i);
921 htbuf->buf[res] = '\0';
923 http_buf_enqueue(&hc->iqueue, htbuf);
927 if (hc->state == Http_Busy)
929 reqlen = request_check(hc->iqueue);
932 // we have a complete HTTP request
933 nmem_reset(hc->nmem);
934 if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
936 yaz_log(YLOG_WARN, "Failed to parse request");
937 http_error(hc, 400, "Bad Request");
941 yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
943 *hc->request->search ? "?" : "",
944 hc->request->search);
945 if (hc->request->content_buf)
946 yaz_log(YLOG_LOG, "%s", hc->request->content_buf);
947 if (http_weshouldproxy(hc->request))
948 http_proxy(hc->request);
951 // Execute our business logic!
952 hc->state = Http_Busy;
957 else if (event == EVENT_OUTPUT)
962 struct http_buf *wb = hc->oqueue;
964 res = send(iochan_getfd(hc->iochan),
965 wb->buf + wb->offset, wb->len, 0);
968 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
969 http_channel_destroy(i);
974 hc->oqueue = hc->oqueue->next;
975 http_buf_destroy(hc->http_server, wb);
986 http_channel_destroy(i);
991 iochan_clearflag(i, EVENT_OUTPUT);
997 if (!hc->oqueue && hc->proxy && !hc->proxy->iochan)
998 http_channel_destroy(i); // Server closed; we're done
1002 yaz_log(YLOG_WARN, "Unexpected event on connection");
1003 http_channel_destroy(i);
1009 // Handles I/O on a client connection to a backend web server (proxy mode)
1010 static void proxy_io(IOCHAN pi, int event)
1012 struct http_proxy *pc = iochan_getdata(pi);
1013 struct http_channel *hc = pc->channel;
1018 struct http_buf *htbuf;
1021 htbuf = http_buf_create(hc->http_server);
1022 res = recv(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1, 0);
1023 if (res == 0 || (res < 0 && !is_inprogress()))
1027 yaz_log(YLOG_WARN, "Proxy read came up short");
1028 // Close channel and alert client HTTP channel that we're gone
1029 http_buf_destroy(hc->http_server, htbuf);
1031 closesocket(iochan_getfd(pi));
1033 close(iochan_getfd(pi));
1040 http_channel_destroy(hc->iochan);
1046 htbuf->buf[res] = '\0';
1049 // Write any remaining payload
1050 if (htbuf->len - htbuf->offset > 0)
1051 http_buf_enqueue(&hc->oqueue, htbuf);
1053 iochan_setflag(hc->iochan, EVENT_OUTPUT);
1056 if (!(htbuf = pc->oqueue))
1058 iochan_clearflag(pi, EVENT_OUTPUT);
1061 res = send(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len, 0);
1064 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
1065 http_channel_destroy(hc->iochan);
1068 if (res == htbuf->len)
1070 struct http_buf *np = htbuf->next;
1071 http_buf_destroy(hc->http_server, htbuf);
1077 htbuf->offset += res;
1081 iochan_setflags(pi, EVENT_INPUT); // Turns off output flag
1085 yaz_log(YLOG_WARN, "Unexpected event on connection");
1086 http_channel_destroy(hc->iochan);
1090 static void http_fire_observers(struct http_channel *c);
1091 static void http_destroy_observers(struct http_channel *c);
1094 static void http_channel_destroy(IOCHAN i)
1096 struct http_channel *s = iochan_getdata(i);
1097 http_server_t http_server;
1101 if (s->proxy->iochan)
1104 closesocket(iochan_getfd(s->proxy->iochan));
1106 close(iochan_getfd(s->proxy->iochan));
1108 iochan_destroy(s->proxy->iochan);
1110 http_buf_destroy_queue(s->http_server, s->proxy->oqueue);
1113 http_buf_destroy_queue(s->http_server, s->iqueue);
1114 http_buf_destroy_queue(s->http_server, s->oqueue);
1115 http_fire_observers(s);
1116 http_destroy_observers(s);
1118 http_server = s->http_server; /* save it for destroy (decref) */
1120 yaz_mutex_enter(s->http_server->mutex);
1121 if (s->http_server->http_channel_freelist_max > 0 && s->http_server->http_channel_freelist_count >= s->http_server->http_channel_freelist_max) {
1122 while ((s->next = s->http_server->http_channel_freelist)) {
1123 nmem_destroy(s->next->nmem);
1124 wrbuf_destroy(s->next->wrbuf);
1126 s->http_server->http_channel_freelist = s->http_server->http_channel_freelist->next;
1128 s->http_server->http_channel_freelist_count = 0;
1131 s->next = s->http_server->http_channel_freelist;
1132 s->http_server->http_channel_freelist = s;
1133 s->http_server->http_channel_freelist_count++;
1134 yaz_log(YLOG_DEBUG, "Free %d channels on server.", s->http_server->http_channel_freelist_count);
1136 yaz_mutex_leave(s->http_server->mutex);
1138 http_server_destroy(http_server);
1141 closesocket(iochan_getfd(i));
1143 close(iochan_getfd(i));
1148 static struct http_channel *http_channel_create(http_server_t hs,
1150 struct conf_server *server)
1152 struct http_channel *r;
1154 yaz_mutex_enter(hs->mutex);
1155 r = hs->http_channel_freelist;
1157 hs->http_channel_freelist = r->next;
1158 hs->http_channel_freelist_count--;
1160 yaz_mutex_leave(hs->mutex);
1164 nmem_reset(r->nmem);
1165 wrbuf_rewind(r->wrbuf);
1169 r = xmalloc(sizeof(struct http_channel));
1170 r->nmem = nmem_create();
1171 r->wrbuf = wrbuf_alloc();
1173 http_server_incref(hs);
1174 r->http_server = hs;
1175 r->http_sessions = hs->http_sessions;
1176 assert(r->http_sessions);
1180 r->iqueue = r->oqueue = 0;
1181 r->state = Http_Idle;
1187 yaz_log(YLOG_WARN, "Invalid HTTP forward address");
1190 strcpy(r->addr, addr);
1196 /* Accept a new command connection */
1197 static void http_accept(IOCHAN i, int event)
1199 struct sockaddr_in addr;
1200 int fd = iochan_getfd(i);
1204 struct http_channel *ch;
1205 struct conf_server *server = iochan_getdata(i);
1208 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
1210 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
1215 yaz_log(YLOG_DEBUG, "New command connection");
1216 c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT, "http_session_socket");
1218 ch = http_channel_create(server->http_server, inet_ntoa(addr.sin_addr),
1221 iochan_setdata(c, ch);
1222 iochan_add(server->iochan_man, c);
1225 /* Create a http-channel listener, syntax [host:]port */
1226 int http_init(const char *addr, struct conf_server *server)
1231 struct sockaddr_in myaddr;
1236 yaz_log(YLOG_LOG, "HTTP listener %s", addr);
1238 memset(&myaddr, 0, sizeof myaddr);
1239 myaddr.sin_family = AF_INET;
1240 pp = strchr(addr, ':');
1243 WRBUF w = wrbuf_alloc();
1246 wrbuf_write(w, addr, pp - addr);
1249 he = gethostbyname(wrbuf_cstr(w));
1253 yaz_log(YLOG_FATAL, "Unable to resolve '%s'", addr);
1256 memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1257 port = atoi(pp + 1);
1262 myaddr.sin_addr.s_addr = INADDR_ANY;
1265 myaddr.sin_port = htons(port);
1267 if (!(p = getprotobyname("tcp"))) {
1270 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
1271 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
1272 if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
1273 &one, sizeof(one)) < 0)
1276 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
1278 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
1281 if (listen(l, SOMAXCONN) < 0)
1283 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
1287 server->http_server = http_server_create();
1289 server->http_server->listener_socket = l;
1291 c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT, "http_server");
1292 iochan_setdata(c, server);
1294 iochan_add(server->iochan_man, c);
1298 void http_close_server(struct conf_server *server)
1300 /* break the event_loop (select) by closing down the HTTP listener sock */
1301 if (server->http_server->listener_socket)
1304 closesocket(server->http_server->listener_socket);
1306 close(server->http_server->listener_socket);
1311 void http_set_proxyaddr(const char *host, struct conf_server *server)
1316 WRBUF w = wrbuf_alloc();
1318 yaz_log(YLOG_LOG, "HTTP backend %s", host);
1320 p = strchr(host, ':');
1324 wrbuf_write(w, host, p - host);
1330 wrbuf_puts(w, host);
1332 if (!(he = gethostbyname(wrbuf_cstr(w))))
1334 fprintf(stderr, "Failed to lookup '%s'\n", wrbuf_cstr(w));
1339 server->http_server->proxy_addr = xmalloc(sizeof(struct sockaddr_in));
1340 server->http_server->proxy_addr->sin_family = he->h_addrtype;
1341 memcpy(&server->http_server->proxy_addr->sin_addr.s_addr,
1342 he->h_addr_list[0], he->h_length);
1343 server->http_server->proxy_addr->sin_port = htons(port);
1346 static void http_fire_observers(struct http_channel *c)
1348 http_channel_observer_t p = c->observers;
1351 p->destroy(p->data, c, p->data2);
1356 static void http_destroy_observers(struct http_channel *c)
1358 while (c->observers)
1360 http_channel_observer_t obs = c->observers;
1361 c->observers = obs->next;
1366 http_channel_observer_t http_add_observer(struct http_channel *c, void *data,
1367 http_channel_destroy_t des)
1369 http_channel_observer_t obs = xmalloc(sizeof(*obs));
1374 obs->next = c->observers;
1379 void http_remove_observer(http_channel_observer_t obs)
1381 struct http_channel *c = obs->chan;
1382 http_channel_observer_t found, *p = &c->observers;
1391 struct http_channel *http_channel_observer_chan(http_channel_observer_t obs)
1396 void http_observer_set_data2(http_channel_observer_t obs, void *data2)
1401 http_server_t http_server_create(void)
1403 http_server_t hs = xmalloc(sizeof(*hs));
1407 hs->http_sessions = 0;
1409 hs->http_channel_freelist = 0;
1410 hs->http_channel_freelist_count = 0;
1411 /* Disable max check */
1412 hs->http_channel_freelist_max = 0;
1414 hs->http_buf_freelist = 0;
1415 hs->http_buf_freelist_count = 0;
1416 /* Disable max check */
1417 hs->http_buf_freelist_max = 0;
1421 void http_server_destroy(http_server_t hs)
1427 yaz_mutex_enter(hs->mutex); /* OK: hs->mutex may be NULL */
1428 r = --(hs->ref_count);
1429 yaz_mutex_leave(hs->mutex);
1433 struct http_buf *b = hs->http_buf_freelist;
1434 struct http_channel *c = hs->http_channel_freelist;
1437 struct http_buf *b_next = b->next;
1443 struct http_channel *c_next = c->next;
1444 nmem_destroy(c->nmem);
1445 wrbuf_destroy(c->wrbuf);
1449 http_sessions_destroy(hs->http_sessions);
1450 xfree(hs->proxy_addr);
1451 yaz_mutex_destroy(&hs->mutex);
1457 void http_server_incref(http_server_t hs)
1460 yaz_mutex_enter(hs->mutex);
1462 yaz_mutex_leave(hs->mutex);
1465 void http_mutex_init(struct conf_server *server)
1469 assert(server->http_server->mutex == 0);
1470 pazpar2_mutex_create(&server->http_server->mutex, "http_server");
1471 server->http_server->http_sessions = http_sessions_create();
1477 * c-file-style: "Stroustrup"
1478 * indent-tabs-mode: nil
1480 * vim: shiftwidth=4 tabstop=8 expandtab