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++;
164 yaz_log(YLOG_DEBUG, "Free %d http buffers on server.", hs->http_buf_freelist_count);
166 yaz_mutex_leave(hs->mutex);
169 static void http_buf_destroy_queue(http_server_t hs, struct http_buf *b)
175 http_buf_destroy(hs, b);
180 static struct http_buf *http_buf_bybuf(http_server_t hs, char *b, int len)
182 struct http_buf *res = 0;
183 struct http_buf **p = &res;
188 if (tocopy > HTTP_BUF_SIZE)
189 tocopy = HTTP_BUF_SIZE;
190 *p = http_buf_create(hs);
191 memcpy((*p)->buf, b, tocopy);
200 // Add a (chain of) buffers to the end of an existing queue.
201 static void http_buf_enqueue(struct http_buf **queue, struct http_buf *b)
204 queue = &(*queue)->next;
208 static struct http_buf *http_buf_bywrbuf(http_server_t hs, WRBUF wrbuf)
210 // Heavens to Betsy (buf)!
211 return http_buf_bybuf(hs, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
214 // Non-destructively collapse chain of buffers into a string (max *len)
216 static void http_buf_peek(struct http_buf *b, char *buf, int len)
219 while (b && rd < len)
221 int toread = len - rd;
224 memcpy(buf + rd, b->buf + b->offset, toread);
231 static int http_buf_size(struct http_buf *b)
234 for (; b; b = b->next)
239 // Ddestructively munch up to len from head of queue.
240 static int http_buf_read(http_server_t hs,
241 struct http_buf **b, char *buf, int len)
244 while ((*b) && rd < len)
246 int toread = len - rd;
247 if (toread > (*b)->len)
249 memcpy(buf + rd, (*b)->buf + (*b)->offset, toread);
251 if (toread < (*b)->len)
254 (*b)->offset += toread;
259 struct http_buf *n = (*b)->next;
260 http_buf_destroy(hs, *b);
268 // Buffers may overlap.
269 static void urldecode(char *i, char *o)
278 else if (*i == '%' && i[1] && i[2])
282 sscanf(i, "%2x", &v);
292 // Warning: Buffers may not overlap
293 void urlencode(const char *i, char *o)
297 if (strchr(" /:", *i))
299 sprintf(o, "%%%.2X", (int) *i);
309 void http_addheader(struct http_response *r, const char *name, const char *value)
311 struct http_channel *c = r->channel;
312 struct http_header *h = nmem_malloc(c->nmem, sizeof *h);
313 h->name = nmem_strdup(c->nmem, name);
314 h->value = nmem_strdup(c->nmem, value);
315 h->next = r->headers;
319 const char *http_argbyname(struct http_request *r, const char *name)
321 struct http_argument *p;
324 for (p = r->arguments; p; p = p->next)
325 if (!strcmp(p->name, name))
330 const char *http_headerbyname(struct http_header *h, const char *name)
332 for (; h; h = h->next)
333 if (!strcmp(h->name, name))
338 struct http_response *http_create_response(struct http_channel *c)
340 struct http_response *r = nmem_malloc(c->nmem, sizeof(*r));
341 strcpy(r->code, "200");
346 r->content_type = "text/xml";
351 static const char *next_crlf(const char *cp, size_t *skipped)
353 const char *next_cp = strchr(cp, '\n');
356 if (next_cp > cp && next_cp[-1] == '\r')
357 *skipped = next_cp - cp - 1;
359 *skipped = next_cp - cp;
365 // Check if buf contains a package (minus payload)
366 static int package_check(const char *buf, int sz)
374 const char *b = next_crlf(buf, &skipped);
378 // we did not find CRLF.. See if buffer is too large..
379 if (sz >= MAX_HTTP_HEADER-1)
380 return MAX_HTTP_HEADER-1; // yes. Return that (will fail later)
386 // CRLF CRLF , i.e. end of header
387 if (len + content_len <= sz)
388 return len + content_len;
392 // following first skip of \r\n so that we don't consider Method
393 if (!strncasecmp(buf, "Content-Length:", 15))
395 const char *cp = buf+15;
399 while (*cp && isdigit(*(const unsigned char *)cp))
400 content_len = content_len*10 + (*cp++ - '0');
401 if (content_len < 0) /* prevent negative offsets */
405 return 0; // incomplete request
408 // Check if we have a request. Return 0 or length
409 static int request_check(struct http_buf *queue)
411 char tmp[MAX_HTTP_HEADER];
413 // only peek at the header..
414 http_buf_peek(queue, tmp, MAX_HTTP_HEADER-1);
415 // still we only return non-zero if the complete request is received..
416 return package_check(tmp, http_buf_size(queue));
419 struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len)
421 char tmp[MAX_HTTP_HEADER];
422 struct http_response *r = http_create_response(c);
424 struct http_header **hp = &r->headers;
426 if (len >= MAX_HTTP_HEADER)
428 memcpy(tmp, buf, len);
429 for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version
433 for (p2 = p; *p2 && *p2 != ' ' && p2 - p < 3; p2++)
434 r->code[p2 - p] = *p2;
435 if (!(p = strstr(tmp, "\r\n")))
440 if (!(p2 = strstr(p, "\r\n")))
442 if (p == p2) // End of headers
446 struct http_header *h = *hp = nmem_malloc(c->nmem, sizeof(*h));
447 char *value = strchr(p, ':');
451 h->name = nmem_strdup(c->nmem, p);
452 while (isspace(*(const unsigned char *) value))
454 if (value >= p2) // Empty header;
461 h->value = nmem_strdup(c->nmem, value);
470 static int http_parse_arguments(struct http_request *r, NMEM nmem,
473 const char *p2 = args;
477 struct http_argument *a;
478 const char *equal = strchr(p2, '=');
479 const char *eoa = strchr(p2, '&');
482 yaz_log(YLOG_WARN, "Expected '=' in argument");
486 eoa = equal + strlen(equal); // last argument
487 else if (equal > eoa)
489 yaz_log(YLOG_WARN, "Missing '&' in argument");
492 a = nmem_malloc(nmem, sizeof(struct http_argument));
493 a->name = nmem_strdupn(nmem, p2, equal - p2);
494 a->value = nmem_strdupn(nmem, equal+1, eoa - equal - 1);
495 urldecode(a->name, a->name);
496 urldecode(a->value, a->value);
497 a->next = r->arguments;
506 struct http_request *http_parse_request(struct http_channel *c,
507 struct http_buf **queue,
510 struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
512 char *start = nmem_malloc(c->nmem, len+1);
515 if (http_buf_read(c->http_server, queue, buf, len) < len)
517 yaz_log(YLOG_WARN, "http_buf_read < len (%d)", len);
527 for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
531 yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
536 if (!(buf = strchr(buf, ' ')))
538 yaz_log(YLOG_WARN, "Missing Request-URI in HTTP request");
542 if (!(p = strchr(buf, ' ')))
544 yaz_log(YLOG_WARN, "HTTP Request-URI not terminated (too long?)");
548 if ((p2 = strchr(buf, '?'))) // Do we have arguments?
550 r->path = nmem_strdup(c->nmem, buf);
553 r->search = nmem_strdup(c->nmem, p2);
555 http_parse_arguments(r, c->nmem, p2);
559 if (strncmp(buf, "HTTP/", 5))
560 strcpy(r->http_version, "1.0");
564 buf += 5; // strlen("HTTP/")
566 p = (char*) next_crlf(buf, &skipped);
567 if (!p || skipped < 3 || skipped > 5)
570 memcpy(r->http_version, buf, skipped);
571 r->http_version[skipped] = '\0';
574 strcpy(c->version, r->http_version);
581 p = (char *) next_crlf(buf, &skipped);
586 else if (skipped == 0)
594 char *n_v = nmem_malloc(c->nmem, skipped+1);
595 struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
597 memcpy(n_v, buf, skipped);
600 if (!(cp = strchr(n_v, ':')))
602 h->name = nmem_strdupn(c->nmem, n_v, cp - n_v);
606 h->value = nmem_strdup(c->nmem, cp);
607 h->next = r->headers;
613 // determine if we do keep alive
614 if (!strcmp(c->version, "1.0"))
616 const char *v = http_lookup_header(r->headers, "Connection");
617 if (v && !strcmp(v, "Keep-Alive"))
624 const char *v = http_lookup_header(r->headers, "Connection");
625 if (v && !strcmp(v, "close"))
630 if (buf < start + len)
632 const char *content_type = http_lookup_header(r->headers,
634 r->content_len = start + len - buf;
635 r->content_buf = buf;
637 if (!yaz_strcmp_del("application/x-www-form-urlencoded",
640 http_parse_arguments(r, c->nmem, r->content_buf);
646 static struct http_buf *http_serialize_response(struct http_channel *c,
647 struct http_response *r)
649 struct http_header *h;
651 wrbuf_rewind(c->wrbuf);
652 wrbuf_printf(c->wrbuf, "HTTP/%s %s %s\r\n", c->version, r->code, r->msg);
653 for (h = r->headers; h; h = h->next)
654 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
657 wrbuf_printf(c->wrbuf, "Content-Length: %d\r\n", r->payload ?
658 (int) strlen(r->payload) : 0);
659 wrbuf_printf(c->wrbuf, "Content-Type: %s\r\n", r->content_type);
660 if (!strcmp(r->content_type, "text/xml"))
662 xmlDoc *doc = xmlParseMemory(r->payload, strlen(r->payload));
669 yaz_log(YLOG_WARN, "Sending non-wellformed "
670 "response (bug #1162");
671 yaz_log(YLOG_WARN, "payload: %s", r->payload);
675 wrbuf_puts(c->wrbuf, "\r\n");
678 wrbuf_puts(c->wrbuf, r->payload);
680 return http_buf_bywrbuf(c->http_server, c->wrbuf);
683 // Serialize a HTTP request
684 static struct http_buf *http_serialize_request(struct http_request *r)
686 struct http_channel *c = r->channel;
687 struct http_header *h;
689 wrbuf_rewind(c->wrbuf);
690 wrbuf_printf(c->wrbuf, "%s %s%s%s", r->method, r->path,
691 *r->search ? "?" : "", r->search);
693 wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
695 for (h = r->headers; h; h = h->next)
696 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
698 wrbuf_puts(c->wrbuf, "\r\n");
701 wrbuf_write(c->wrbuf, r->content_buf, r->content_len);
704 yaz_log(YLOG_LOG, "WRITING TO PROXY:\n%s\n----",
705 wrbuf_cstr(c->wrbuf));
707 return http_buf_bywrbuf(c->http_server, c->wrbuf);
711 static int http_weshouldproxy(struct http_request *rq)
713 struct http_channel *c = rq->channel;
714 if (c->server->http_server->proxy_addr && !strstr(rq->path, "search.pz2"))
720 struct http_header * http_header_append(struct http_channel *ch,
721 struct http_header * hp,
725 struct http_header *hpnew = 0;
730 while (hp && hp->next)
733 if(name && strlen(name)&& value && strlen(value)){
734 hpnew = nmem_malloc(ch->nmem, sizeof *hpnew);
735 hpnew->name = nmem_strdup(ch->nmem, name);
736 hpnew->value = nmem_strdup(ch->nmem, value);
749 static int is_inprogress(void)
752 if (WSAGetLastError() == WSAEWOULDBLOCK)
755 if (errno == EINPROGRESS)
761 static void enable_nonblock(int sock)
765 flags = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
766 if (ioctlsocket(sock, FIONBIO, &flags) < 0)
767 yaz_log(YLOG_FATAL|YLOG_ERRNO, "ioctlsocket");
769 if ((flags = fcntl(sock, F_GETFL, 0)) < 0)
770 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
771 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
772 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
776 static int http_proxy(struct http_request *rq)
778 struct http_channel *c = rq->channel;
779 struct http_proxy *p = c->proxy;
780 struct http_header *hp;
781 struct http_buf *requestbuf;
782 char server_port[16] = "";
783 struct conf_server *ser = c->server;
785 if (!p) // This is a new connection. Create a proxy channel
791 if (!(pe = getprotobyname("tcp"))) {
794 if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
796 yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
799 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
800 &one, sizeof(one)) < 0)
802 enable_nonblock(sock);
803 if (connect(sock, (struct sockaddr *)
804 c->server->http_server->proxy_addr,
805 sizeof(*c->server->http_server->proxy_addr)) < 0)
807 if (!is_inprogress())
809 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
813 p = xmalloc(sizeof(struct http_proxy));
816 p->first_response = 1;
818 // We will add EVENT_OUTPUT below
819 p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT, "http_proxy");
820 iochan_setdata(p->iochan, p);
822 iochan_add(ser->iochan_man, p->iochan);
825 // Do _not_ modify Host: header, just checking it's existence
827 if (!http_lookup_header(rq->headers, "Host"))
829 yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
833 // Add new header about paraz2 version, host, remote client address, etc.
835 char server_via[128];
838 hp = http_header_append(c, hp,
839 "X-Pazpar2-Version", PACKAGE_VERSION);
840 hp = http_header_append(c, hp,
841 "X-Pazpar2-Server-Host", ser->host);
842 sprintf(server_port, "%d", ser->port);
843 hp = http_header_append(c, hp,
844 "X-Pazpar2-Server-Port", server_port);
845 yaz_snprintf(server_via, sizeof(server_via),
847 ser->host ? ser->host : "@",
848 server_port, PACKAGE_NAME, PACKAGE_VERSION);
849 hp = http_header_append(c, hp, "Via" , server_via);
850 hp = http_header_append(c, hp, "X-Forwarded-For", c->addr);
853 requestbuf = http_serialize_request(rq);
855 http_buf_enqueue(&p->oqueue, requestbuf);
856 iochan_setflag(p->iochan, EVENT_OUTPUT);
860 void http_send_response(struct http_channel *ch)
862 struct http_response *rs = ch->response;
866 hb = http_serialize_response(ch, rs);
869 yaz_log(YLOG_WARN, "Failed to serialize HTTP response");
870 http_channel_destroy(ch->iochan);
874 http_buf_enqueue(&ch->oqueue, hb);
875 iochan_setflag(ch->iochan, EVENT_OUTPUT);
876 ch->state = Http_Idle;
880 static void http_error(struct http_channel *hc, int no, const char *msg)
882 struct http_response *rs = http_create_response(hc);
885 hc->keep_alive = 0; // not keeping this HTTP session alive
887 sprintf(rs->code, "%d", no);
889 rs->msg = nmem_strdup(hc->nmem, msg);
890 rs->payload = nmem_malloc(hc->nmem, 100);
891 yaz_snprintf(rs->payload, 99, "<error>HTTP Error %d: %s</error>\n",
893 http_send_response(hc);
896 static void http_io(IOCHAN i, int event)
898 struct http_channel *hc = iochan_getdata(i);
901 if (event == EVENT_INPUT)
904 struct http_buf *htbuf;
906 htbuf = http_buf_create(hc->http_server);
907 res = recv(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1, 0);
908 if (res == -1 && errno == EAGAIN)
910 http_buf_destroy(hc->http_server, htbuf);
915 http_buf_destroy(hc->http_server, htbuf);
916 http_channel_destroy(i);
919 htbuf->buf[res] = '\0';
921 http_buf_enqueue(&hc->iqueue, htbuf);
925 if (hc->state == Http_Busy)
927 reqlen = request_check(hc->iqueue);
930 // we have a complete HTTP request
931 nmem_reset(hc->nmem);
932 if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
934 yaz_log(YLOG_WARN, "Failed to parse request");
935 http_error(hc, 400, "Bad Request");
939 yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
941 *hc->request->search ? "?" : "",
942 hc->request->search);
943 if (hc->request->content_buf)
944 yaz_log(YLOG_LOG, "%s", hc->request->content_buf);
945 if (http_weshouldproxy(hc->request))
946 http_proxy(hc->request);
949 // Execute our business logic!
950 hc->state = Http_Busy;
955 else if (event == EVENT_OUTPUT)
960 struct http_buf *wb = hc->oqueue;
962 res = send(iochan_getfd(hc->iochan),
963 wb->buf + wb->offset, wb->len, 0);
966 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
967 http_channel_destroy(i);
972 hc->oqueue = hc->oqueue->next;
973 http_buf_destroy(hc->http_server, wb);
984 http_channel_destroy(i);
989 iochan_clearflag(i, EVENT_OUTPUT);
995 if (!hc->oqueue && hc->proxy && !hc->proxy->iochan)
996 http_channel_destroy(i); // Server closed; we're done
1000 yaz_log(YLOG_WARN, "Unexpected event on connection");
1001 http_channel_destroy(i);
1007 // Handles I/O on a client connection to a backend web server (proxy mode)
1008 static void proxy_io(IOCHAN pi, int event)
1010 struct http_proxy *pc = iochan_getdata(pi);
1011 struct http_channel *hc = pc->channel;
1016 struct http_buf *htbuf;
1019 htbuf = http_buf_create(hc->http_server);
1020 res = recv(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1, 0);
1021 if (res == 0 || (res < 0 && !is_inprogress()))
1025 yaz_log(YLOG_WARN, "Proxy read came up short");
1026 // Close channel and alert client HTTP channel that we're gone
1027 http_buf_destroy(hc->http_server, htbuf);
1029 closesocket(iochan_getfd(pi));
1031 close(iochan_getfd(pi));
1038 http_channel_destroy(hc->iochan);
1044 htbuf->buf[res] = '\0';
1047 // Write any remaining payload
1048 if (htbuf->len - htbuf->offset > 0)
1049 http_buf_enqueue(&hc->oqueue, htbuf);
1051 iochan_setflag(hc->iochan, EVENT_OUTPUT);
1054 if (!(htbuf = pc->oqueue))
1056 iochan_clearflag(pi, EVENT_OUTPUT);
1059 res = send(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len, 0);
1062 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
1063 http_channel_destroy(hc->iochan);
1066 if (res == htbuf->len)
1068 struct http_buf *np = htbuf->next;
1069 http_buf_destroy(hc->http_server, htbuf);
1075 htbuf->offset += res;
1079 iochan_setflags(pi, EVENT_INPUT); // Turns off output flag
1083 yaz_log(YLOG_WARN, "Unexpected event on connection");
1084 http_channel_destroy(hc->iochan);
1088 static void http_fire_observers(struct http_channel *c);
1089 static void http_destroy_observers(struct http_channel *c);
1092 static void http_channel_destroy(IOCHAN i)
1094 struct http_channel *s = iochan_getdata(i);
1095 http_server_t http_server;
1099 if (s->proxy->iochan)
1102 closesocket(iochan_getfd(s->proxy->iochan));
1104 close(iochan_getfd(s->proxy->iochan));
1106 iochan_destroy(s->proxy->iochan);
1108 http_buf_destroy_queue(s->http_server, s->proxy->oqueue);
1111 http_buf_destroy_queue(s->http_server, s->iqueue);
1112 http_buf_destroy_queue(s->http_server, s->oqueue);
1113 http_fire_observers(s);
1114 http_destroy_observers(s);
1116 http_server = s->http_server; /* save it for destroy (decref) */
1118 yaz_mutex_enter(s->http_server->mutex);
1119 if (s->http_server->http_channel_freelist_max > 0 && s->http_server->http_channel_freelist_count >= s->http_server->http_channel_freelist_max) {
1120 while ((s->next = s->http_server->http_channel_freelist)) {
1121 nmem_destroy(s->next->nmem);
1122 wrbuf_destroy(s->next->wrbuf);
1124 s->http_server->http_channel_freelist = s->http_server->http_channel_freelist->next;
1126 s->http_server->http_channel_freelist_count = 0;
1129 s->next = s->http_server->http_channel_freelist;
1130 s->http_server->http_channel_freelist = s;
1131 s->http_server->http_channel_freelist_count++;
1132 yaz_log(YLOG_DEBUG, "Free %d channels on server.", s->http_server->http_channel_freelist_count);
1134 yaz_mutex_leave(s->http_server->mutex);
1136 http_server_destroy(http_server);
1139 closesocket(iochan_getfd(i));
1141 close(iochan_getfd(i));
1146 static struct http_channel *http_channel_create(http_server_t hs,
1148 struct conf_server *server)
1150 struct http_channel *r;
1152 yaz_mutex_enter(hs->mutex);
1153 r = hs->http_channel_freelist;
1155 hs->http_channel_freelist = r->next;
1156 hs->http_channel_freelist_count--;
1158 yaz_mutex_leave(hs->mutex);
1162 nmem_reset(r->nmem);
1163 wrbuf_rewind(r->wrbuf);
1167 r = xmalloc(sizeof(struct http_channel));
1168 r->nmem = nmem_create();
1169 r->wrbuf = wrbuf_alloc();
1171 http_server_incref(hs);
1172 r->http_server = hs;
1173 r->http_sessions = hs->http_sessions;
1174 assert(r->http_sessions);
1178 r->iqueue = r->oqueue = 0;
1179 r->state = Http_Idle;
1185 yaz_log(YLOG_WARN, "Invalid HTTP forward address");
1188 strcpy(r->addr, addr);
1194 /* Accept a new command connection */
1195 static void http_accept(IOCHAN i, int event)
1197 struct sockaddr_in addr;
1198 int fd = iochan_getfd(i);
1202 struct http_channel *ch;
1203 struct conf_server *server = iochan_getdata(i);
1206 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
1208 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
1213 yaz_log(YLOG_DEBUG, "New command connection");
1214 c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT, "http_session_socket");
1216 ch = http_channel_create(server->http_server, inet_ntoa(addr.sin_addr),
1219 iochan_setdata(c, ch);
1220 iochan_add(server->iochan_man, c);
1223 /* Create a http-channel listener, syntax [host:]port */
1224 int http_init(const char *addr, struct conf_server *server)
1229 struct sockaddr_in myaddr;
1234 yaz_log(YLOG_LOG, "HTTP listener %s", addr);
1236 memset(&myaddr, 0, sizeof myaddr);
1237 myaddr.sin_family = AF_INET;
1238 pp = strchr(addr, ':');
1241 WRBUF w = wrbuf_alloc();
1244 wrbuf_write(w, addr, pp - addr);
1247 he = gethostbyname(wrbuf_cstr(w));
1251 yaz_log(YLOG_FATAL, "Unable to resolve '%s'", addr);
1254 memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1255 port = atoi(pp + 1);
1260 myaddr.sin_addr.s_addr = INADDR_ANY;
1263 myaddr.sin_port = htons(port);
1265 if (!(p = getprotobyname("tcp"))) {
1268 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
1269 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
1270 if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
1271 &one, sizeof(one)) < 0)
1274 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
1276 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
1279 if (listen(l, SOMAXCONN) < 0)
1281 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
1285 server->http_server = http_server_create();
1287 server->http_server->listener_socket = l;
1289 c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT, "http_server");
1290 iochan_setdata(c, server);
1292 iochan_add(server->iochan_man, c);
1296 void http_close_server(struct conf_server *server)
1298 /* break the event_loop (select) by closing down the HTTP listener sock */
1299 if (server->http_server->listener_socket)
1302 closesocket(server->http_server->listener_socket);
1304 close(server->http_server->listener_socket);
1309 void http_set_proxyaddr(const char *host, struct conf_server *server)
1314 WRBUF w = wrbuf_alloc();
1316 yaz_log(YLOG_LOG, "HTTP backend %s", host);
1318 p = strchr(host, ':');
1322 wrbuf_write(w, host, p - host);
1328 wrbuf_puts(w, host);
1330 if (!(he = gethostbyname(wrbuf_cstr(w))))
1332 fprintf(stderr, "Failed to lookup '%s'\n", wrbuf_cstr(w));
1337 server->http_server->proxy_addr = xmalloc(sizeof(struct sockaddr_in));
1338 server->http_server->proxy_addr->sin_family = he->h_addrtype;
1339 memcpy(&server->http_server->proxy_addr->sin_addr.s_addr,
1340 he->h_addr_list[0], he->h_length);
1341 server->http_server->proxy_addr->sin_port = htons(port);
1344 static void http_fire_observers(struct http_channel *c)
1346 http_channel_observer_t p = c->observers;
1349 p->destroy(p->data, c, p->data2);
1354 static void http_destroy_observers(struct http_channel *c)
1356 while (c->observers)
1358 http_channel_observer_t obs = c->observers;
1359 c->observers = obs->next;
1364 http_channel_observer_t http_add_observer(struct http_channel *c, void *data,
1365 http_channel_destroy_t des)
1367 http_channel_observer_t obs = xmalloc(sizeof(*obs));
1372 obs->next = c->observers;
1377 void http_remove_observer(http_channel_observer_t obs)
1379 struct http_channel *c = obs->chan;
1380 http_channel_observer_t found, *p = &c->observers;
1389 struct http_channel *http_channel_observer_chan(http_channel_observer_t obs)
1394 void http_observer_set_data2(http_channel_observer_t obs, void *data2)
1399 http_server_t http_server_create(void)
1401 http_server_t hs = xmalloc(sizeof(*hs));
1405 hs->http_sessions = 0;
1407 hs->http_channel_freelist = 0;
1408 hs->http_channel_freelist_count = 0;
1409 /* Disable max check */
1410 hs->http_channel_freelist_max = 0;
1412 hs->http_buf_freelist = 0;
1413 hs->http_buf_freelist_count = 0;
1414 /* Disable max check */
1415 hs->http_buf_freelist_max = 0;
1419 void http_server_destroy(http_server_t hs)
1425 yaz_mutex_enter(hs->mutex); /* OK: hs->mutex may be NULL */
1426 r = --(hs->ref_count);
1427 yaz_mutex_leave(hs->mutex);
1431 struct http_buf *b = hs->http_buf_freelist;
1432 struct http_channel *c = hs->http_channel_freelist;
1435 struct http_buf *b_next = b->next;
1441 struct http_channel *c_next = c->next;
1442 nmem_destroy(c->nmem);
1443 wrbuf_destroy(c->wrbuf);
1447 http_sessions_destroy(hs->http_sessions);
1448 xfree(hs->proxy_addr);
1449 yaz_mutex_destroy(&hs->mutex);
1455 void http_server_incref(http_server_t hs)
1458 yaz_mutex_enter(hs->mutex);
1460 yaz_mutex_leave(hs->mutex);
1463 void http_mutex_init(struct conf_server *server)
1467 assert(server->http_server->mutex == 0);
1468 pazpar2_mutex_create(&server->http_server->mutex, "http_server");
1469 server->http_server->http_sessions = http_sessions_create();
1475 * c-file-style: "Stroustrup"
1476 * indent-tabs-mode: nil
1478 * vim: shiftwidth=4 tabstop=8 expandtab