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
31 typedef int socklen_t;
35 #include <sys/socket.h>
38 #include <sys/types.h>
40 #include <yaz/snprintf.h>
58 #include <netinet/in.h>
62 #include <arpa/inet.h>
65 #include <yaz/yaz-util.h>
66 #include <yaz/comstack.h>
68 #include <yaz/mutex.h>
74 #define MAX_HTTP_HEADER 4096
77 #define strncasecmp _strnicmp
78 #define strcasecmp _stricmp
83 #define HTTP_BUF_SIZE 4096
87 struct http_buf *next;
91 static void proxy_io(IOCHAN i, int event);
92 static struct http_channel *http_channel_create(http_server_t http_server,
94 struct conf_server *server);
95 static void http_channel_destroy(IOCHAN i);
96 static http_server_t http_server_create(void);
97 static void http_server_incref(http_server_t hs);
101 struct http_buf *http_buf_freelist;
102 int http_buf_freelist_count;
103 int http_buf_freelist_max;
105 struct http_channel *http_channel_freelist;
106 int http_channel_freelist_count;
107 int http_channel_freelist_max;
111 http_sessions_t http_sessions;
112 struct sockaddr_in *proxy_addr;
116 struct http_channel_observer_s {
119 http_channel_destroy_t destroy;
120 struct http_channel_observer_s *next;
121 struct http_channel *chan;
125 const char *http_lookup_header(struct http_header *header,
128 for (; header; header = header->next)
129 if (!strcasecmp(name, header->name))
130 return header->value;
134 static struct http_buf *http_buf_create(http_server_t hs)
136 struct http_buf *r = 0;
138 yaz_mutex_enter(hs->mutex);
139 if (hs->http_buf_freelist)
141 r = hs->http_buf_freelist;
142 hs->http_buf_freelist = hs->http_buf_freelist->next;
143 hs->http_buf_freelist_count--;
145 yaz_mutex_leave(hs->mutex);
147 r = xmalloc(sizeof(struct http_buf));
154 static void http_buf_destroy(http_server_t hs, struct http_buf *b)
156 yaz_mutex_enter(hs->mutex);
157 if (hs->http_buf_freelist_max > 0 && hs->http_buf_freelist_count >= hs->http_buf_freelist_max) {
159 while ((b = hs->http_buf_freelist)) {
161 hs->http_buf_freelist = hs->http_buf_freelist->next;
163 hs->http_buf_freelist_count = 0;
166 b->next = hs->http_buf_freelist;
167 hs->http_buf_freelist = b;
168 hs->http_buf_freelist_count++;
170 yaz_log(YLOG_DEBUG, "Free %d http buffers on server.", hs->http_buf_freelist_count);
173 yaz_mutex_leave(hs->mutex);
176 static void http_buf_destroy_queue(http_server_t hs, struct http_buf *b)
182 http_buf_destroy(hs, b);
187 static struct http_buf *http_buf_bybuf(http_server_t hs, char *b, int len)
189 struct http_buf *res = 0;
190 struct http_buf **p = &res;
195 if (tocopy > HTTP_BUF_SIZE)
196 tocopy = HTTP_BUF_SIZE;
197 *p = http_buf_create(hs);
198 memcpy((*p)->buf, b, tocopy);
207 // Add a (chain of) buffers to the end of an existing queue.
208 static void http_buf_enqueue(struct http_buf **queue, struct http_buf *b)
211 queue = &(*queue)->next;
215 static struct http_buf *http_buf_bywrbuf(http_server_t hs, WRBUF wrbuf)
217 // Heavens to Betsy (buf)!
218 return http_buf_bybuf(hs, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
221 // Non-destructively collapse chain of buffers into a string (max *len)
223 static void http_buf_peek(struct http_buf *b, char *buf, int len)
226 while (b && rd < len)
228 int toread = len - rd;
231 memcpy(buf + rd, b->buf + b->offset, toread);
238 static int http_buf_size(struct http_buf *b)
241 for (; b; b = b->next)
246 // Ddestructively munch up to len from head of queue.
247 static int http_buf_read(http_server_t hs,
248 struct http_buf **b, char *buf, int len)
251 while ((*b) && rd < len)
253 int toread = len - rd;
254 if (toread > (*b)->len)
256 memcpy(buf + rd, (*b)->buf + (*b)->offset, toread);
258 if (toread < (*b)->len)
261 (*b)->offset += toread;
266 struct http_buf *n = (*b)->next;
267 http_buf_destroy(hs, *b);
275 // Buffers may overlap.
276 static void urldecode(char *i, char *o)
285 else if (*i == '%' && i[1] && i[2])
289 sscanf(i, "%2x", &v);
299 // Warning: Buffers may not overlap
300 void urlencode(const char *i, char *o)
304 if (strchr(" /:", *i))
306 sprintf(o, "%%%.2X", (int) *i);
316 void http_addheader(struct http_response *r, const char *name, const char *value)
318 struct http_channel *c = r->channel;
319 struct http_header *h = nmem_malloc(c->nmem, sizeof *h);
320 h->name = nmem_strdup(c->nmem, name);
321 h->value = nmem_strdup(c->nmem, value);
322 h->next = r->headers;
326 const char *http_argbyname(struct http_request *r, const char *name)
328 struct http_argument *p;
331 for (p = r->arguments; p; p = p->next)
332 if (!strcmp(p->name, name))
337 const char *http_headerbyname(struct http_header *h, const char *name)
339 for (; h; h = h->next)
340 if (!strcmp(h->name, name))
345 struct http_response *http_create_response(struct http_channel *c)
347 struct http_response *r = nmem_malloc(c->nmem, sizeof(*r));
348 strcpy(r->code, "200");
353 r->content_type = "text/xml";
358 static const char *next_crlf(const char *cp, size_t *skipped)
360 const char *next_cp = strchr(cp, '\n');
363 if (next_cp > cp && next_cp[-1] == '\r')
364 *skipped = next_cp - cp - 1;
366 *skipped = next_cp - cp;
372 // Check if buf contains a package (minus payload)
373 static int package_check(const char *buf, int sz)
381 const char *b = next_crlf(buf, &skipped);
385 // we did not find CRLF.. See if buffer is too large..
386 if (sz >= MAX_HTTP_HEADER-1)
387 return MAX_HTTP_HEADER-1; // yes. Return that (will fail later)
393 // CRLF CRLF , i.e. end of header
394 if (len + content_len <= sz)
395 return len + content_len;
399 // following first skip of \r\n so that we don't consider Method
400 if (!strncasecmp(buf, "Content-Length:", 15))
402 const char *cp = buf+15;
406 while (*cp && isdigit(*(const unsigned char *)cp))
407 content_len = content_len*10 + (*cp++ - '0');
408 if (content_len < 0) /* prevent negative offsets */
412 return 0; // incomplete request
415 // Check if we have a request. Return 0 or length
416 static int request_check(struct http_buf *queue)
418 char tmp[MAX_HTTP_HEADER];
420 // only peek at the header..
421 http_buf_peek(queue, tmp, MAX_HTTP_HEADER-1);
422 // still we only return non-zero if the complete request is received..
423 return package_check(tmp, http_buf_size(queue));
426 struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len)
428 char tmp[MAX_HTTP_HEADER];
429 struct http_response *r = http_create_response(c);
431 struct http_header **hp = &r->headers;
433 if (len >= MAX_HTTP_HEADER)
435 memcpy(tmp, buf, len);
436 for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version
440 for (p2 = p; *p2 && *p2 != ' ' && p2 - p < 3; p2++)
441 r->code[p2 - p] = *p2;
442 if (!(p = strstr(tmp, "\r\n")))
447 if (!(p2 = strstr(p, "\r\n")))
449 if (p == p2) // End of headers
453 struct http_header *h = *hp = nmem_malloc(c->nmem, sizeof(*h));
454 char *value = strchr(p, ':');
458 h->name = nmem_strdup(c->nmem, p);
459 while (isspace(*(const unsigned char *) value))
461 if (value >= p2) // Empty header;
468 h->value = nmem_strdup(c->nmem, value);
477 static int http_parse_arguments(struct http_request *r, NMEM nmem,
480 const char *p2 = args;
484 struct http_argument *a;
485 const char *equal = strchr(p2, '=');
486 const char *eoa = strchr(p2, '&');
489 yaz_log(YLOG_WARN, "Expected '=' in argument");
493 eoa = equal + strlen(equal); // last argument
494 else if (equal > eoa)
496 yaz_log(YLOG_WARN, "Missing '&' in argument");
499 a = nmem_malloc(nmem, sizeof(struct http_argument));
500 a->name = nmem_strdupn(nmem, p2, equal - p2);
501 a->value = nmem_strdupn(nmem, equal+1, eoa - equal - 1);
502 urldecode(a->name, a->name);
503 urldecode(a->value, a->value);
504 a->next = r->arguments;
513 struct http_request *http_parse_request(struct http_channel *c,
514 struct http_buf **queue,
517 struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
519 char *start = nmem_malloc(c->nmem, len+1);
522 if (http_buf_read(c->http_server, queue, buf, len) < len)
524 yaz_log(YLOG_WARN, "http_buf_read < len (%d)", len);
534 for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
538 yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
543 if (!(buf = strchr(buf, ' ')))
545 yaz_log(YLOG_WARN, "Missing Request-URI in HTTP request");
549 if (!(p = strchr(buf, ' ')))
551 yaz_log(YLOG_WARN, "HTTP Request-URI not terminated (too long?)");
555 if ((p2 = strchr(buf, '?'))) // Do we have arguments?
557 r->path = nmem_strdup(c->nmem, buf);
560 r->search = nmem_strdup(c->nmem, p2);
562 http_parse_arguments(r, c->nmem, p2);
566 if (strncmp(buf, "HTTP/", 5))
567 strcpy(r->http_version, "1.0");
571 buf += 5; // strlen("HTTP/")
573 p = (char*) next_crlf(buf, &skipped);
574 if (!p || skipped < 3 || skipped > 5)
577 memcpy(r->http_version, buf, skipped);
578 r->http_version[skipped] = '\0';
581 strcpy(c->version, r->http_version);
588 p = (char *) next_crlf(buf, &skipped);
593 else if (skipped == 0)
601 char *n_v = nmem_malloc(c->nmem, skipped+1);
602 struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
604 memcpy(n_v, buf, skipped);
607 if (!(cp = strchr(n_v, ':')))
609 h->name = nmem_strdupn(c->nmem, n_v, cp - n_v);
613 h->value = nmem_strdup(c->nmem, cp);
614 h->next = r->headers;
620 // determine if we do keep alive
621 if (!strcmp(c->version, "1.0"))
623 const char *v = http_lookup_header(r->headers, "Connection");
624 if (v && !strcmp(v, "Keep-Alive"))
631 const char *v = http_lookup_header(r->headers, "Connection");
632 if (v && !strcmp(v, "close"))
637 if (buf < start + len)
639 const char *content_type = http_lookup_header(r->headers,
641 r->content_len = start + len - buf;
642 r->content_buf = buf;
644 if (!yaz_strcmp_del("application/x-www-form-urlencoded",
647 http_parse_arguments(r, c->nmem, r->content_buf);
653 static struct http_buf *http_serialize_response(struct http_channel *c,
654 struct http_response *r)
656 struct http_header *h;
658 wrbuf_rewind(c->wrbuf);
659 wrbuf_printf(c->wrbuf, "HTTP/%s %s %s\r\n", c->version, r->code, r->msg);
660 for (h = r->headers; h; h = h->next)
661 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
664 wrbuf_printf(c->wrbuf, "Content-Length: %d\r\n", r->payload ?
665 (int) strlen(r->payload) : 0);
666 wrbuf_printf(c->wrbuf, "Content-Type: %s\r\n", r->content_type);
667 if (!strcmp(r->content_type, "text/xml"))
669 xmlDoc *doc = xmlParseMemory(r->payload, strlen(r->payload));
676 yaz_log(YLOG_WARN, "Sending non-wellformed "
677 "response (bug #1162");
678 yaz_log(YLOG_WARN, "payload: %s", r->payload);
682 wrbuf_puts(c->wrbuf, "\r\n");
685 wrbuf_puts(c->wrbuf, r->payload);
687 return http_buf_bywrbuf(c->http_server, c->wrbuf);
690 // Serialize a HTTP request
691 static struct http_buf *http_serialize_request(struct http_request *r)
693 struct http_channel *c = r->channel;
694 struct http_header *h;
696 wrbuf_rewind(c->wrbuf);
697 wrbuf_printf(c->wrbuf, "%s %s%s%s", r->method, r->path,
698 *r->search ? "?" : "", r->search);
700 wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
702 for (h = r->headers; h; h = h->next)
703 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
705 wrbuf_puts(c->wrbuf, "\r\n");
708 wrbuf_write(c->wrbuf, r->content_buf, r->content_len);
711 yaz_log(YLOG_LOG, "WRITING TO PROXY:\n%s\n----",
712 wrbuf_cstr(c->wrbuf));
714 return http_buf_bywrbuf(c->http_server, c->wrbuf);
718 static int http_weshouldproxy(struct http_request *rq)
720 struct http_channel *c = rq->channel;
721 if (c->server->http_server->proxy_addr && !strstr(rq->path, "search.pz2"))
727 struct http_header * http_header_append(struct http_channel *ch,
728 struct http_header * hp,
732 struct http_header *hpnew = 0;
737 while (hp && hp->next)
740 if(name && strlen(name)&& value && strlen(value)){
741 hpnew = nmem_malloc(ch->nmem, sizeof *hpnew);
742 hpnew->name = nmem_strdup(ch->nmem, name);
743 hpnew->value = nmem_strdup(ch->nmem, value);
756 static int is_inprogress(void)
759 if (WSAGetLastError() == WSAEWOULDBLOCK)
762 if (errno == EINPROGRESS)
768 static void enable_nonblock(int sock)
772 flags = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
773 if (ioctlsocket(sock, FIONBIO, &flags) < 0)
774 yaz_log(YLOG_FATAL|YLOG_ERRNO, "ioctlsocket");
776 if ((flags = fcntl(sock, F_GETFL, 0)) < 0)
777 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
778 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
779 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
783 static int http_proxy(struct http_request *rq)
785 struct http_channel *c = rq->channel;
786 struct http_proxy *p = c->proxy;
787 struct http_header *hp;
788 struct http_buf *requestbuf;
789 char server_port[16] = "";
790 struct conf_server *ser = c->server;
792 if (!p) // This is a new connection. Create a proxy channel
798 if (!(pe = getprotobyname("tcp"))) {
801 if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
803 yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
806 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
807 &one, sizeof(one)) < 0)
809 enable_nonblock(sock);
810 if (connect(sock, (struct sockaddr *)
811 c->server->http_server->proxy_addr,
812 sizeof(*c->server->http_server->proxy_addr)) < 0)
814 if (!is_inprogress())
816 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
820 p = xmalloc(sizeof(struct http_proxy));
823 p->first_response = 1;
825 // We will add EVENT_OUTPUT below
826 p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT, "http_proxy");
827 iochan_setdata(p->iochan, p);
829 iochan_add(ser->iochan_man, p->iochan);
832 // Do _not_ modify Host: header, just checking it's existence
834 if (!http_lookup_header(rq->headers, "Host"))
836 yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
840 // Add new header about paraz2 version, host, remote client address, etc.
842 char server_via[128];
845 hp = http_header_append(c, hp,
846 "X-Pazpar2-Version", PACKAGE_VERSION);
847 hp = http_header_append(c, hp,
848 "X-Pazpar2-Server-Host", ser->host);
849 sprintf(server_port, "%d", ser->port);
850 hp = http_header_append(c, hp,
851 "X-Pazpar2-Server-Port", server_port);
852 yaz_snprintf(server_via, sizeof(server_via),
854 ser->host ? ser->host : "@",
855 server_port, PACKAGE_NAME, PACKAGE_VERSION);
856 hp = http_header_append(c, hp, "Via" , server_via);
857 hp = http_header_append(c, hp, "X-Forwarded-For", c->addr);
860 requestbuf = http_serialize_request(rq);
862 http_buf_enqueue(&p->oqueue, requestbuf);
863 iochan_setflag(p->iochan, EVENT_OUTPUT);
867 void http_send_response(struct http_channel *ch)
869 struct http_response *rs = ch->response;
873 hb = http_serialize_response(ch, rs);
876 yaz_log(YLOG_WARN, "Failed to serialize HTTP response");
877 http_channel_destroy(ch->iochan);
881 http_buf_enqueue(&ch->oqueue, hb);
882 iochan_setflag(ch->iochan, EVENT_OUTPUT);
883 ch->state = Http_Idle;
887 static void http_error(struct http_channel *hc, int no, const char *msg)
889 struct http_response *rs = http_create_response(hc);
892 hc->keep_alive = 0; // not keeping this HTTP session alive
894 sprintf(rs->code, "%d", no);
896 rs->msg = nmem_strdup(hc->nmem, msg);
897 rs->payload = nmem_malloc(hc->nmem, 100);
898 yaz_snprintf(rs->payload, 99, "<error>HTTP Error %d: %s</error>\n",
900 http_send_response(hc);
903 static void http_io(IOCHAN i, int event)
905 struct http_channel *hc = iochan_getdata(i);
908 if (event == EVENT_INPUT)
911 struct http_buf *htbuf;
913 htbuf = http_buf_create(hc->http_server);
914 res = recv(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1, 0);
915 if (res == -1 && errno == EAGAIN)
917 http_buf_destroy(hc->http_server, htbuf);
923 if (hc->http_server->record_file)
926 gettimeofday(&tv, 0);
927 fprintf(hc->http_server->record_file, "%lld %lld %lld 0\n",
928 (long long) tv.tv_sec, (long long) tv.tv_usec,
929 (long long) iochan_getfd(i));
932 http_buf_destroy(hc->http_server, htbuf);
933 fflush(hc->http_server->record_file);
934 http_channel_destroy(i);
937 htbuf->buf[res] = '\0';
939 http_buf_enqueue(&hc->iqueue, htbuf);
943 if (hc->state == Http_Busy)
945 reqlen = request_check(hc->iqueue);
948 // we have a complete HTTP request
949 nmem_reset(hc->nmem);
951 if (hc->http_server->record_file)
956 for (hb = hc->iqueue; hb; hb = hb->next)
958 gettimeofday(&tv, 0);
959 fprintf(hc->http_server->record_file, "%lld %lld %lld %d\n",
960 (long long) tv.tv_sec, (long long) tv.tv_usec,
961 (long long) iochan_getfd(i), sz);
962 for (hb = hc->iqueue; hb; hb = hb->next)
963 fwrite(hb->buf, 1, hb->len, hc->http_server->record_file);
966 if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
968 yaz_log(YLOG_WARN, "Failed to parse request");
969 http_error(hc, 400, "Bad Request");
973 yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
975 *hc->request->search ? "?" : "",
976 hc->request->search);
977 if (hc->request->content_buf)
978 yaz_log(YLOG_LOG, "%s", hc->request->content_buf);
979 if (http_weshouldproxy(hc->request))
980 http_proxy(hc->request);
983 // Execute our business logic!
984 hc->state = Http_Busy;
989 else if (event == EVENT_OUTPUT)
994 struct http_buf *wb = hc->oqueue;
996 res = send(iochan_getfd(hc->iochan),
997 wb->buf + wb->offset, wb->len, 0);
1000 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
1001 http_channel_destroy(i);
1006 hc->oqueue = hc->oqueue->next;
1007 http_buf_destroy(hc->http_server, wb);
1016 if (!hc->keep_alive)
1018 http_channel_destroy(i);
1023 iochan_clearflag(i, EVENT_OUTPUT);
1025 event = EVENT_INPUT;
1029 if (!hc->oqueue && hc->proxy && !hc->proxy->iochan)
1030 http_channel_destroy(i); // Server closed; we're done
1034 yaz_log(YLOG_WARN, "Unexpected event on connection");
1035 http_channel_destroy(i);
1041 // Handles I/O on a client connection to a backend web server (proxy mode)
1042 static void proxy_io(IOCHAN pi, int event)
1044 struct http_proxy *pc = iochan_getdata(pi);
1045 struct http_channel *hc = pc->channel;
1050 struct http_buf *htbuf;
1053 htbuf = http_buf_create(hc->http_server);
1054 res = recv(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1, 0);
1055 if (res == 0 || (res < 0 && !is_inprogress()))
1059 yaz_log(YLOG_WARN, "Proxy read came up short");
1060 // Close channel and alert client HTTP channel that we're gone
1061 http_buf_destroy(hc->http_server, htbuf);
1063 closesocket(iochan_getfd(pi));
1065 close(iochan_getfd(pi));
1072 http_channel_destroy(hc->iochan);
1078 htbuf->buf[res] = '\0';
1081 // Write any remaining payload
1082 if (htbuf->len - htbuf->offset > 0)
1083 http_buf_enqueue(&hc->oqueue, htbuf);
1085 iochan_setflag(hc->iochan, EVENT_OUTPUT);
1088 if (!(htbuf = pc->oqueue))
1090 iochan_clearflag(pi, EVENT_OUTPUT);
1093 res = send(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len, 0);
1096 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
1097 http_channel_destroy(hc->iochan);
1100 if (res == htbuf->len)
1102 struct http_buf *np = htbuf->next;
1103 http_buf_destroy(hc->http_server, htbuf);
1109 htbuf->offset += res;
1113 iochan_setflags(pi, EVENT_INPUT); // Turns off output flag
1117 yaz_log(YLOG_WARN, "Unexpected event on connection");
1118 http_channel_destroy(hc->iochan);
1122 static void http_fire_observers(struct http_channel *c);
1123 static void http_destroy_observers(struct http_channel *c);
1126 static void http_channel_destroy(IOCHAN i)
1128 struct http_channel *s = iochan_getdata(i);
1129 http_server_t http_server;
1133 if (s->proxy->iochan)
1136 closesocket(iochan_getfd(s->proxy->iochan));
1138 close(iochan_getfd(s->proxy->iochan));
1140 iochan_destroy(s->proxy->iochan);
1142 http_buf_destroy_queue(s->http_server, s->proxy->oqueue);
1145 http_buf_destroy_queue(s->http_server, s->iqueue);
1146 http_buf_destroy_queue(s->http_server, s->oqueue);
1147 http_fire_observers(s);
1148 http_destroy_observers(s);
1150 http_server = s->http_server; /* save it for destroy (decref) */
1152 yaz_mutex_enter(s->http_server->mutex);
1153 if (s->http_server->http_channel_freelist_max > 0 && s->http_server->http_channel_freelist_count >= s->http_server->http_channel_freelist_max) {
1154 while ((s->next = s->http_server->http_channel_freelist)) {
1155 nmem_destroy(s->next->nmem);
1156 wrbuf_destroy(s->next->wrbuf);
1158 s->http_server->http_channel_freelist = s->http_server->http_channel_freelist->next;
1160 s->http_server->http_channel_freelist_count = 0;
1163 s->next = s->http_server->http_channel_freelist;
1164 s->http_server->http_channel_freelist = s;
1165 s->http_server->http_channel_freelist_count++;
1166 yaz_log(YLOG_DEBUG, "Free %d channels on server.", s->http_server->http_channel_freelist_count);
1168 yaz_mutex_leave(s->http_server->mutex);
1170 http_server_destroy(http_server);
1173 closesocket(iochan_getfd(i));
1175 close(iochan_getfd(i));
1180 static struct http_channel *http_channel_create(http_server_t hs,
1182 struct conf_server *server)
1184 struct http_channel *r;
1186 yaz_mutex_enter(hs->mutex);
1187 r = hs->http_channel_freelist;
1189 hs->http_channel_freelist = r->next;
1190 hs->http_channel_freelist_count--;
1192 yaz_mutex_leave(hs->mutex);
1196 nmem_reset(r->nmem);
1197 wrbuf_rewind(r->wrbuf);
1201 r = xmalloc(sizeof(struct http_channel));
1202 r->nmem = nmem_create();
1203 r->wrbuf = wrbuf_alloc();
1205 http_server_incref(hs);
1206 r->http_server = hs;
1207 r->http_sessions = hs->http_sessions;
1208 assert(r->http_sessions);
1212 r->iqueue = r->oqueue = 0;
1213 r->state = Http_Idle;
1219 yaz_log(YLOG_WARN, "Invalid HTTP forward address");
1222 strcpy(r->addr, addr);
1228 /* Accept a new command connection */
1229 static void http_accept(IOCHAN i, int event)
1231 struct sockaddr_in addr;
1232 int fd = iochan_getfd(i);
1236 struct http_channel *ch;
1237 struct conf_server *server = iochan_getdata(i);
1240 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
1242 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
1247 yaz_log(YLOG_DEBUG, "New command connection");
1248 c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT, "http_session_socket");
1250 ch = http_channel_create(server->http_server, inet_ntoa(addr.sin_addr),
1253 iochan_setdata(c, ch);
1254 iochan_add(server->iochan_man, c);
1257 /* Create a http-channel listener, syntax [host:]port */
1258 int http_init(const char *addr, struct conf_server *server,
1259 const char *record_fname)
1264 struct sockaddr_in myaddr;
1268 FILE *record_file = 0;
1270 yaz_log(YLOG_LOG, "HTTP listener %s", addr);
1275 record_file = fopen(record_fname, "wb");
1278 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fopen %s", record_fname);
1283 memset(&myaddr, 0, sizeof myaddr);
1284 myaddr.sin_family = AF_INET;
1285 pp = strchr(addr, ':');
1288 WRBUF w = wrbuf_alloc();
1291 wrbuf_write(w, addr, pp - addr);
1294 he = gethostbyname(wrbuf_cstr(w));
1298 yaz_log(YLOG_FATAL, "Unable to resolve '%s'", addr);
1301 memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1302 port = atoi(pp + 1);
1307 myaddr.sin_addr.s_addr = INADDR_ANY;
1310 myaddr.sin_port = htons(port);
1312 if (!(p = getprotobyname("tcp"))) {
1315 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
1316 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
1317 if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
1318 &one, sizeof(one)) < 0)
1321 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
1323 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
1326 if (listen(l, SOMAXCONN) < 0)
1328 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
1332 server->http_server = http_server_create();
1334 server->http_server->record_file = record_file;
1335 server->http_server->listener_socket = l;
1337 c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT, "http_server");
1338 iochan_setdata(c, server);
1340 iochan_add(server->iochan_man, c);
1344 void http_close_server(struct conf_server *server)
1346 /* break the event_loop (select) by closing down the HTTP listener sock */
1347 if (server->http_server->listener_socket)
1350 closesocket(server->http_server->listener_socket);
1352 close(server->http_server->listener_socket);
1357 void http_set_proxyaddr(const char *host, struct conf_server *server)
1362 WRBUF w = wrbuf_alloc();
1364 yaz_log(YLOG_LOG, "HTTP backend %s", host);
1366 p = strchr(host, ':');
1370 wrbuf_write(w, host, p - host);
1376 wrbuf_puts(w, host);
1378 if (!(he = gethostbyname(wrbuf_cstr(w))))
1380 fprintf(stderr, "Failed to lookup '%s'\n", wrbuf_cstr(w));
1385 server->http_server->proxy_addr = xmalloc(sizeof(struct sockaddr_in));
1386 server->http_server->proxy_addr->sin_family = he->h_addrtype;
1387 memcpy(&server->http_server->proxy_addr->sin_addr.s_addr,
1388 he->h_addr_list[0], he->h_length);
1389 server->http_server->proxy_addr->sin_port = htons(port);
1392 static void http_fire_observers(struct http_channel *c)
1394 http_channel_observer_t p = c->observers;
1397 p->destroy(p->data, c, p->data2);
1402 static void http_destroy_observers(struct http_channel *c)
1404 while (c->observers)
1406 http_channel_observer_t obs = c->observers;
1407 c->observers = obs->next;
1412 http_channel_observer_t http_add_observer(struct http_channel *c, void *data,
1413 http_channel_destroy_t des)
1415 http_channel_observer_t obs = xmalloc(sizeof(*obs));
1420 obs->next = c->observers;
1425 void http_remove_observer(http_channel_observer_t obs)
1427 struct http_channel *c = obs->chan;
1428 http_channel_observer_t found, *p = &c->observers;
1437 struct http_channel *http_channel_observer_chan(http_channel_observer_t obs)
1442 void http_observer_set_data2(http_channel_observer_t obs, void *data2)
1447 http_server_t http_server_create(void)
1449 http_server_t hs = xmalloc(sizeof(*hs));
1453 hs->http_sessions = 0;
1455 hs->http_channel_freelist = 0;
1456 hs->http_channel_freelist_count = 0;
1457 /* Disable max check */
1458 hs->http_channel_freelist_max = 0;
1460 hs->http_buf_freelist = 0;
1461 hs->http_buf_freelist_count = 0;
1462 /* Disable max check */
1463 hs->http_buf_freelist_max = 0;
1464 hs->record_file = 0;
1468 void http_server_destroy(http_server_t hs)
1474 yaz_mutex_enter(hs->mutex); /* OK: hs->mutex may be NULL */
1475 r = --(hs->ref_count);
1476 yaz_mutex_leave(hs->mutex);
1480 struct http_buf *b = hs->http_buf_freelist;
1481 struct http_channel *c = hs->http_channel_freelist;
1484 struct http_buf *b_next = b->next;
1490 struct http_channel *c_next = c->next;
1491 nmem_destroy(c->nmem);
1492 wrbuf_destroy(c->wrbuf);
1496 http_sessions_destroy(hs->http_sessions);
1497 xfree(hs->proxy_addr);
1498 yaz_mutex_destroy(&hs->mutex);
1499 if (hs->record_file)
1500 fclose(hs->record_file);
1506 void http_server_incref(http_server_t hs)
1509 yaz_mutex_enter(hs->mutex);
1511 yaz_mutex_leave(hs->mutex);
1514 void http_mutex_init(struct conf_server *server)
1518 assert(server->http_server->mutex == 0);
1519 pazpar2_mutex_create(&server->http_server->mutex, "http_server");
1520 server->http_server->http_sessions = http_sessions_create();
1526 * c-file-style: "Stroustrup"
1527 * indent-tabs-mode: nil
1529 * vim: shiftwidth=4 tabstop=8 expandtab