2 * $Id: http.c,v 1.13 2007-03-27 13:41:23 marc Exp $
6 #include <sys/socket.h>
22 #include <netinet/in.h>
24 #include <yaz/yaz-util.h>
25 #include <yaz/comstack.h>
33 #include "http_command.h"
35 static void proxy_io(IOCHAN i, int event);
36 static struct http_channel *http_create(void);
37 static void http_destroy(IOCHAN i);
39 extern IOCHAN channel_list;
41 static struct sockaddr_in *proxy_addr = 0; // If this is set, we proxy normal HTTP requests
42 static char proxy_url[256] = "";
43 static char myurl[256] = "";
44 static struct http_buf *http_buf_freelist = 0;
45 static struct http_channel *http_channel_freelist = 0;
47 static struct http_buf *http_buf_create()
51 if (http_buf_freelist)
53 r = http_buf_freelist;
54 http_buf_freelist = http_buf_freelist->next;
57 r = xmalloc(sizeof(struct http_buf));
64 static void http_buf_destroy(struct http_buf *b)
66 b->next = http_buf_freelist;
67 http_buf_freelist = b;
70 static void http_buf_destroy_queue(struct http_buf *b)
82 // Calculate length of chain
83 static int http_buf_len(struct http_buf *b)
86 for (; b; b = b->next)
92 static struct http_buf *http_buf_bybuf(char *b, int len)
94 struct http_buf *res = 0;
95 struct http_buf **p = &res;
100 if (tocopy > HTTP_BUF_SIZE)
101 tocopy = HTTP_BUF_SIZE;
102 *p = http_buf_create();
103 memcpy((*p)->buf, b, tocopy);
112 // Add a (chain of) buffers to the end of an existing queue.
113 static void http_buf_enqueue(struct http_buf **queue, struct http_buf *b)
116 queue = &(*queue)->next;
120 static struct http_buf *http_buf_bywrbuf(WRBUF wrbuf)
122 // Heavens to Betsy (buf)!
123 return http_buf_bybuf(wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
126 // Non-destructively collapse chain of buffers into a string (max *len)
128 static int http_buf_peek(struct http_buf *b, char *buf, int len)
131 while (b && rd < len)
133 int toread = len - rd;
136 memcpy(buf + rd, b->buf + b->offset, toread);
144 // Ddestructively munch up to len from head of queue.
145 static int http_buf_read(struct http_buf **b, char *buf, int len)
148 while ((*b) && rd < len)
150 int toread = len - rd;
151 if (toread > (*b)->len)
153 memcpy(buf + rd, (*b)->buf + (*b)->offset, toread);
155 if (toread < (*b)->len)
158 (*b)->offset += toread;
163 struct http_buf *n = (*b)->next;
164 http_buf_destroy(*b);
172 // Buffers may overlap.
173 static void urldecode(char *i, char *o)
185 sscanf(i, "%2hhx", o);
195 // Warning: Buffers may not overlap
196 void urlencode(const char *i, char *o)
200 if (strchr(" /:", *i))
202 sprintf(o, "%%%.2X", (int) *i);
212 void http_addheader(struct http_response *r, const char *name, const char *value)
214 struct http_channel *c = r->channel;
215 struct http_header *h = nmem_malloc(c->nmem, sizeof *h);
216 h->name = nmem_strdup(c->nmem, name);
217 h->value = nmem_strdup(c->nmem, value);
218 h->next = r->headers;
222 char *http_argbyname(struct http_request *r, char *name)
224 struct http_argument *p;
227 for (p = r->arguments; p; p = p->next)
228 if (!strcmp(p->name, name))
233 char *http_headerbyname(struct http_header *h, char *name)
235 for (; h; h = h->next)
236 if (!strcmp(h->name, name))
241 struct http_response *http_create_response(struct http_channel *c)
243 struct http_response *r = nmem_malloc(c->nmem, sizeof(*r));
244 strcpy(r->code, "200");
252 // Check if buf contains a package (minus payload)
253 static int package_check(const char *buf)
256 while (*buf) // Check if we have a sequence of lines terminated by an empty line
258 char *b = strstr(buf, "\r\n");
263 len += (b - buf) + 2;
271 // Check if we have a request. Return 0 or length
272 // (including trailing CRNL) FIXME: Does not deal gracefully with requests
273 // carrying payload but this is kind of OK since we will reject anything
274 // other than an empty GET
275 static int request_check(struct http_buf *queue)
279 http_buf_peek(queue, tmp, 4096);
280 return package_check(tmp);
283 struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len)
286 struct http_response *r = http_create_response(c);
288 struct http_header **hp = &r->headers;
292 memcpy(tmp, buf, len);
293 for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version
297 for (p2 = p; *p2 && *p2 != ' ' && p2 - p < 3; p2++)
298 r->code[p2 - p] = *p2;
299 if (!(p = strstr(tmp, "\r\n")))
304 if (!(p2 = strstr(p, "\r\n")))
306 if (p == p2) // End of headers
310 struct http_header *h = *hp = nmem_malloc(c->nmem, sizeof(*h));
311 char *value = strchr(p, ':');
315 h->name = nmem_strdup(c->nmem, p);
316 while (isspace(*value))
318 if (value >= p2) // Empty header;
325 h->value = nmem_strdup(c->nmem, value);
334 struct http_request *http_parse_request(struct http_channel *c, struct http_buf **queue,
337 struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
344 if (http_buf_read(queue, buf, len) < len)
352 for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
356 yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
361 if (!(buf = strchr(buf, ' ')))
363 yaz_log(YLOG_WARN, "Syntax error in request (1)");
367 if (!(p = strchr(buf, ' ')))
369 yaz_log(YLOG_WARN, "Syntax error in request (2)");
373 if ((p2 = strchr(buf, '?'))) // Do we have arguments?
375 r->path = nmem_strdup(c->nmem, buf);
378 r->search = nmem_strdup(c->nmem, p2);
382 struct http_argument *a;
383 char *equal = strchr(p2, '=');
384 char *eoa = strchr(p2, '&');
387 yaz_log(YLOG_WARN, "Expected '=' in argument");
391 eoa = equal + strlen(equal); // last argument
394 a = nmem_malloc(c->nmem, sizeof(struct http_argument));
396 a->name = nmem_strdup(c->nmem, p2);
397 urldecode(equal, equal);
398 a->value = nmem_strdup(c->nmem, equal);
399 a->next = r->arguments;
406 if (strncmp(buf, "HTTP/", 5))
407 strcpy(r->http_version, "1.0");
411 if (!(p = strstr(buf, "\r\n")))
415 strcpy(r->http_version, buf);
418 strcpy(c->version, r->http_version);
423 if (!(p = strstr(buf, "\r\n")))
429 struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
430 if (!(p2 = strchr(buf, ':')))
433 h->name = nmem_strdup(c->nmem, buf);
436 if (p2 >= p) // Empty header?
442 h->value = nmem_strdup(c->nmem, p2);
443 h->next = r->headers;
452 static struct http_buf *http_serialize_response(struct http_channel *c,
453 struct http_response *r)
455 struct http_header *h;
457 wrbuf_rewind(c->wrbuf);
458 wrbuf_printf(c->wrbuf, "HTTP/1.1 %s %s\r\n", r->code, r->msg);
459 for (h = r->headers; h; h = h->next)
460 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
463 wrbuf_printf(c->wrbuf, "Content-length: %d\r\n", r->payload ?
464 (int) strlen(r->payload) : 0);
465 wrbuf_printf(c->wrbuf, "Content-type: text/xml\r\n");
467 wrbuf_puts(c->wrbuf, "\r\n");
470 wrbuf_puts(c->wrbuf, r->payload);
472 return http_buf_bywrbuf(c->wrbuf);
475 // Serialize a HTTP request
476 static struct http_buf *http_serialize_request(struct http_request *r)
478 struct http_channel *c = r->channel;
479 struct http_header *h;
480 struct http_argument *a;
482 wrbuf_rewind(c->wrbuf);
483 wrbuf_printf(c->wrbuf, "%s %s", r->method, r->path);
487 wrbuf_putc(c->wrbuf, '?');
488 for (a = r->arguments; a; a = a->next) {
489 if (a != r->arguments)
490 wrbuf_putc(c->wrbuf, '&');
491 wrbuf_printf(c->wrbuf, "%s=%s", a->name, a->value);
495 wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
497 for (h = r->headers; h; h = h->next)
498 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
500 wrbuf_puts(c->wrbuf, "\r\n");
502 return http_buf_bywrbuf(c->wrbuf);
506 static int http_weshouldproxy(struct http_request *rq)
508 if (proxy_addr && !strstr(rq->path, "search.pz2"))
514 struct http_header * http_header_append(struct http_channel *ch,
515 struct http_header * hp,
519 struct http_header *hpnew = nmem_malloc(ch->nmem, sizeof *hpnew);
521 while (hp && hp->next)
524 hpnew->name = nmem_strdup(ch->nmem, name);
525 hpnew->value = nmem_strdup(ch->nmem, value);
534 static int http_proxy(struct http_request *rq)
536 struct http_channel *c = rq->channel;
537 struct http_proxy *p = c->proxy;
538 struct http_header *hp;
539 struct http_buf *requestbuf;
540 //struct conf_server *ser = global_parameters.server;
543 if (!p) // This is a new connection. Create a proxy channel
550 if (!(pe = getprotobyname("tcp"))) {
553 if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
555 yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
558 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
559 &one, sizeof(one)) < 0)
561 if ((flags = fcntl(sock, F_GETFL, 0)) < 0)
562 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
563 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
564 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
565 if (connect(sock, (struct sockaddr *) proxy_addr,
566 sizeof(*proxy_addr)) < 0)
567 if (errno != EINPROGRESS)
569 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
573 p = xmalloc(sizeof(struct http_proxy));
576 p->first_response = 1;
578 // We will add EVENT_OUTPUT below
579 p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT);
580 iochan_setdata(p->iochan, p);
581 p->iochan->next = channel_list;
582 channel_list = p->iochan;
585 // Modify Host: header
586 for (hp = rq->headers; hp; hp = hp->next)
587 if (!strcmp(hp->name, "Host"))
591 yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
594 hp->value = nmem_strdup(c->nmem, proxy_url);
596 // Add new header about paraz2 version, host, remote client address, etc.
599 hp = http_header_append(c, hp, PACKAGE_NAME "-version", PACKAGE_VERSION);
600 //hp = http_header_append(c, hp, PACKAGE_NAME "-server-host", ser->host);
601 //hp = http_header_append(c, hp, PACKAGE_NAME "-server-port", ser->port);
602 //hp = http_header_append(c, hp, PACKAGE_NAME "-remote-host", "blabla");
603 //hp = http_header_append(c, hp, PACKAGE_NAME "-remote-port", "blabla");
605 requestbuf = http_serialize_request(rq);
606 http_buf_enqueue(&p->oqueue, requestbuf);
607 iochan_setflag(p->iochan, EVENT_OUTPUT);
611 void http_send_response(struct http_channel *ch)
613 struct http_response *rs = ch->response;
617 hb = http_serialize_response(ch, rs);
620 yaz_log(YLOG_WARN, "Failed to serialize HTTP response");
621 http_destroy(ch->iochan);
625 http_buf_enqueue(&ch->oqueue, hb);
626 iochan_setflag(ch->iochan, EVENT_OUTPUT);
627 ch->state = Http_Idle;
631 static void http_io(IOCHAN i, int event)
633 struct http_channel *hc = iochan_getdata(i);
638 struct http_buf *htbuf;
641 htbuf = http_buf_create();
642 res = read(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1);
643 if (res == -1 && errno == EAGAIN)
645 http_buf_destroy(htbuf);
650 http_buf_destroy(htbuf);
656 htbuf->buf[res] = '\0';
658 http_buf_enqueue(&hc->iqueue, htbuf);
661 if (hc->state == Http_Busy)
663 if ((reqlen = request_check(hc->iqueue)) <= 2)
666 nmem_reset(hc->nmem);
667 if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
669 yaz_log(YLOG_WARN, "Failed to parse request");
674 yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
676 *hc->request->search ? "?" : "",
677 hc->request->search);
678 if (http_weshouldproxy(hc->request))
679 http_proxy(hc->request);
682 // Execute our business logic!
683 hc->state = Http_Busy;
688 yaz_log(YLOG_DEBUG, "We think we have more input to read. Forcing event");
689 iochan_setevent(i, EVENT_INPUT);
697 struct http_buf *wb = hc->oqueue;
698 res = write(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len);
701 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
707 hc->oqueue = hc->oqueue->next;
708 http_buf_destroy(wb);
716 if (!strcmp(hc->version, "1.0"))
723 iochan_clearflag(i, EVENT_OUTPUT);
725 iochan_setevent(hc->iochan, EVENT_INPUT);
730 if (!hc->oqueue && hc->proxy && !hc->proxy->iochan)
731 http_destroy(i); // Server closed; we're done
734 yaz_log(YLOG_WARN, "Unexpected event on connection");
739 // If this hostname contains our proxy host as a prefix, replace with myurl
740 static char *sub_hostname(struct http_channel *c, char *buf)
743 if (strlen(buf) > 1023)
745 if (strncmp(buf, "http://", 7))
747 if (!strncmp(buf + 7, proxy_url, strlen(proxy_url)))
750 strcat(tmp, buf + strlen(proxy_url) + 7);
751 return nmem_strdup(c->nmem, tmp);
756 // Handles I/O on a client connection to a backend web server (proxy mode)
757 static void proxy_io(IOCHAN pi, int event)
759 struct http_proxy *pc = iochan_getdata(pi);
760 struct http_channel *hc = pc->channel;
765 struct http_buf *htbuf;
768 htbuf = http_buf_create();
769 res = read(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1);
770 if (res == 0 || (res < 0 && errno != EINPROGRESS))
774 yaz_log(YLOG_WARN, "Proxy read came up short");
775 // Close channel and alert client HTTP channel that we're gone
776 http_buf_destroy(htbuf);
777 close(iochan_getfd(pi));
783 http_destroy(hc->iochan);
789 htbuf->buf[res] = '\0';
792 if (pc->first_response) // Check if this is a redirect
795 if ((len = package_check(htbuf->buf)))
797 struct http_response *res = http_parse_response_buf(hc, htbuf->buf, len);
800 struct http_header *h;
801 for (h = res->headers; h; h = h->next)
802 if (!strcmp(h->name, "Location"))
804 // We found a location header. Rewrite it.
805 struct http_buf *buf;
806 h->value = sub_hostname(hc, h->value);
807 buf = http_serialize_response(hc, res);
808 yaz_log(YLOG_LOG, "Proxy rewrite");
809 http_buf_enqueue(&hc->oqueue, buf);
815 pc->first_response = 0;
817 // Write any remaining payload
818 if (htbuf->len - htbuf->offset > 0)
819 http_buf_enqueue(&hc->oqueue, htbuf);
821 iochan_setflag(hc->iochan, EVENT_OUTPUT);
824 if (!(htbuf = pc->oqueue))
826 iochan_clearflag(pi, EVENT_OUTPUT);
829 res = write(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len);
832 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
833 http_destroy(hc->iochan);
836 if (res == htbuf->len)
838 struct http_buf *np = htbuf->next;
839 http_buf_destroy(htbuf);
845 htbuf->offset += res;
849 iochan_setflags(pi, EVENT_INPUT); // Turns off output flag
853 yaz_log(YLOG_WARN, "Unexpected event on connection");
854 http_destroy(hc->iochan);
859 static void http_destroy(IOCHAN i)
861 struct http_channel *s = iochan_getdata(i);
865 if (s->proxy->iochan)
867 close(iochan_getfd(s->proxy->iochan));
868 iochan_destroy(s->proxy->iochan);
870 http_buf_destroy_queue(s->proxy->oqueue);
873 s->next = http_channel_freelist;
874 http_channel_freelist = s;
875 close(iochan_getfd(i));
879 static struct http_channel *http_create(void)
881 struct http_channel *r = http_channel_freelist;
885 http_channel_freelist = r->next;
887 wrbuf_rewind(r->wrbuf);
891 r = xmalloc(sizeof(struct http_channel));
892 r->nmem = nmem_create();
893 r->wrbuf = wrbuf_alloc();
897 r->iqueue = r->oqueue = 0;
898 r->state = Http_Idle;
905 /* Accept a new command connection */
906 static void http_accept(IOCHAN i, int event)
908 struct sockaddr_in addr;
909 int fd = iochan_getfd(i);
914 struct http_channel *ch;
917 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
919 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
922 if ((flags = fcntl(s, F_GETFL, 0)) < 0)
923 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
924 if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
925 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
927 yaz_log(YLOG_DEBUG, "New command connection");
928 c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT);
932 iochan_setdata(c, ch);
934 c->next = channel_list;
938 /* Create a http-channel listener, syntax [host:]port */
939 void http_init(const char *addr)
944 struct sockaddr_in myaddr;
949 yaz_log(YLOG_LOG, "HTTP listener is %s", addr);
951 memset(&myaddr, 0, sizeof myaddr);
952 myaddr.sin_family = AF_INET;
953 pp = strchr(addr, ':');
960 strncpy(hostname, addr, len);
961 hostname[len] = '\0';
962 if (!(he = gethostbyname(hostname)))
964 yaz_log(YLOG_FATAL, "Unable to resolve '%s'", hostname);
967 memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
973 myaddr.sin_addr.s_addr = INADDR_ANY;
975 myaddr.sin_port = htons(port);
977 if (!(p = getprotobyname("tcp"))) {
980 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
981 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
982 if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
983 &one, sizeof(one)) < 0)
986 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
987 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
988 if (listen(l, SOMAXCONN) < 0)
989 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
991 c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT);
992 c->next = channel_list;
996 void http_set_proxyaddr(char *host, char *base_url)
1002 strcpy(myurl, base_url);
1003 strcpy(proxy_url, host);
1004 p = strchr(host, ':');
1005 yaz_log(YLOG_DEBUG, "Proxying for %s", host);
1012 if (!(he = gethostbyname(host)))
1014 fprintf(stderr, "Failed to lookup '%s'\n", host);
1017 proxy_addr = xmalloc(sizeof(struct sockaddr_in));
1018 proxy_addr->sin_family = he->h_addrtype;
1019 memcpy(&proxy_addr->sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1020 proxy_addr->sin_port = htons(port);
1026 * indent-tabs-mode: nil
1028 * vim: shiftwidth=4 tabstop=8 expandtab