2 * $Id: http.c,v 1.18 2007-03-31 19:55:25 marc Exp $
6 #include <sys/socket.h>
23 #include <netinet/in.h>
25 #include <yaz/yaz-util.h>
26 #include <yaz/comstack.h>
34 #include "http_command.h"
36 static void proxy_io(IOCHAN i, int event);
37 static struct http_channel *http_create(void);
38 static void http_destroy(IOCHAN i);
40 extern IOCHAN channel_list;
41 extern struct parameters global_parameters;
43 // If this is set, we proxy normal HTTP requests
44 static struct sockaddr_in *proxy_addr = 0;
45 static char proxy_url[256] = "";
46 static char myurl[256] = "";
47 static struct http_buf *http_buf_freelist = 0;
48 static struct http_channel *http_channel_freelist = 0;
50 static struct http_buf *http_buf_create()
54 if (http_buf_freelist)
56 r = http_buf_freelist;
57 http_buf_freelist = http_buf_freelist->next;
60 r = xmalloc(sizeof(struct http_buf));
67 static void http_buf_destroy(struct http_buf *b)
69 b->next = http_buf_freelist;
70 http_buf_freelist = b;
73 static void http_buf_destroy_queue(struct http_buf *b)
85 // Calculate length of chain
86 static int http_buf_len(struct http_buf *b)
89 for (; b; b = b->next)
95 static struct http_buf *http_buf_bybuf(char *b, int len)
97 struct http_buf *res = 0;
98 struct http_buf **p = &res;
103 if (tocopy > HTTP_BUF_SIZE)
104 tocopy = HTTP_BUF_SIZE;
105 *p = http_buf_create();
106 memcpy((*p)->buf, b, tocopy);
115 // Add a (chain of) buffers to the end of an existing queue.
116 static void http_buf_enqueue(struct http_buf **queue, struct http_buf *b)
119 queue = &(*queue)->next;
123 static struct http_buf *http_buf_bywrbuf(WRBUF wrbuf)
125 // Heavens to Betsy (buf)!
126 return http_buf_bybuf(wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
129 // Non-destructively collapse chain of buffers into a string (max *len)
131 static int http_buf_peek(struct http_buf *b, char *buf, int len)
134 while (b && rd < len)
136 int toread = len - rd;
139 memcpy(buf + rd, b->buf + b->offset, toread);
147 // Ddestructively munch up to len from head of queue.
148 static int http_buf_read(struct http_buf **b, char *buf, int len)
151 while ((*b) && rd < len)
153 int toread = len - rd;
154 if (toread > (*b)->len)
156 memcpy(buf + rd, (*b)->buf + (*b)->offset, toread);
158 if (toread < (*b)->len)
161 (*b)->offset += toread;
166 struct http_buf *n = (*b)->next;
167 http_buf_destroy(*b);
175 // Buffers may overlap.
176 static void urldecode(char *i, char *o)
188 sscanf(i, "%2hhx", o);
198 // Warning: Buffers may not overlap
199 void urlencode(const char *i, char *o)
203 if (strchr(" /:", *i))
205 sprintf(o, "%%%.2X", (int) *i);
215 void http_addheader(struct http_response *r, const char *name, const char *value)
217 struct http_channel *c = r->channel;
218 struct http_header *h = nmem_malloc(c->nmem, sizeof *h);
219 h->name = nmem_strdup(c->nmem, name);
220 h->value = nmem_strdup(c->nmem, value);
221 h->next = r->headers;
225 char *http_argbyname(struct http_request *r, char *name)
227 struct http_argument *p;
230 for (p = r->arguments; p; p = p->next)
231 if (!strcmp(p->name, name))
236 char *http_headerbyname(struct http_header *h, char *name)
238 for (; h; h = h->next)
239 if (!strcmp(h->name, name))
244 struct http_response *http_create_response(struct http_channel *c)
246 struct http_response *r = nmem_malloc(c->nmem, sizeof(*r));
247 strcpy(r->code, "200");
255 // Check if buf contains a package (minus payload)
256 static int package_check(const char *buf)
259 while (*buf) // Check if we have a sequence of lines terminated by an empty line
261 char *b = strstr(buf, "\r\n");
266 len += (b - buf) + 2;
274 // Check if we have a request. Return 0 or length
275 // (including trailing CRNL) FIXME: Does not deal gracefully with requests
276 // carrying payload but this is kind of OK since we will reject anything
277 // other than an empty GET
278 static int request_check(struct http_buf *queue)
282 http_buf_peek(queue, tmp, 4096);
283 return package_check(tmp);
286 struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len)
289 struct http_response *r = http_create_response(c);
291 struct http_header **hp = &r->headers;
295 memcpy(tmp, buf, len);
296 for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version
300 for (p2 = p; *p2 && *p2 != ' ' && p2 - p < 3; p2++)
301 r->code[p2 - p] = *p2;
302 if (!(p = strstr(tmp, "\r\n")))
307 if (!(p2 = strstr(p, "\r\n")))
309 if (p == p2) // End of headers
313 struct http_header *h = *hp = nmem_malloc(c->nmem, sizeof(*h));
314 char *value = strchr(p, ':');
318 h->name = nmem_strdup(c->nmem, p);
319 while (isspace(*value))
321 if (value >= p2) // Empty header;
328 h->value = nmem_strdup(c->nmem, value);
337 struct http_request *http_parse_request(struct http_channel *c, struct http_buf **queue,
340 struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
347 if (http_buf_read(queue, buf, len) < len)
355 for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
359 yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
364 if (!(buf = strchr(buf, ' ')))
366 yaz_log(YLOG_WARN, "Syntax error in request (1)");
370 if (!(p = strchr(buf, ' ')))
372 yaz_log(YLOG_WARN, "Syntax error in request (2)");
376 if ((p2 = strchr(buf, '?'))) // Do we have arguments?
378 r->path = nmem_strdup(c->nmem, buf);
381 r->search = nmem_strdup(c->nmem, p2);
385 struct http_argument *a;
386 char *equal = strchr(p2, '=');
387 char *eoa = strchr(p2, '&');
390 yaz_log(YLOG_WARN, "Expected '=' in argument");
394 eoa = equal + strlen(equal); // last argument
397 a = nmem_malloc(c->nmem, sizeof(struct http_argument));
399 a->name = nmem_strdup(c->nmem, p2);
400 urldecode(equal, equal);
401 a->value = nmem_strdup(c->nmem, equal);
402 a->next = r->arguments;
409 if (strncmp(buf, "HTTP/", 5))
410 strcpy(r->http_version, "1.0");
414 if (!(p = strstr(buf, "\r\n")))
418 strcpy(r->http_version, buf);
421 strcpy(c->version, r->http_version);
426 if (!(p = strstr(buf, "\r\n")))
432 struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
433 if (!(p2 = strchr(buf, ':')))
436 h->name = nmem_strdup(c->nmem, buf);
439 if (p2 >= p) // Empty header?
445 h->value = nmem_strdup(c->nmem, p2);
446 h->next = r->headers;
455 static struct http_buf *http_serialize_response(struct http_channel *c,
456 struct http_response *r)
458 struct http_header *h;
460 wrbuf_rewind(c->wrbuf);
461 wrbuf_printf(c->wrbuf, "HTTP/1.1 %s %s\r\n", r->code, r->msg);
462 for (h = r->headers; h; h = h->next)
463 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
466 wrbuf_printf(c->wrbuf, "Content-length: %d\r\n", r->payload ?
467 (int) strlen(r->payload) : 0);
468 wrbuf_printf(c->wrbuf, "Content-type: text/xml\r\n");
470 wrbuf_puts(c->wrbuf, "\r\n");
473 wrbuf_puts(c->wrbuf, r->payload);
475 return http_buf_bywrbuf(c->wrbuf);
478 // Serialize a HTTP request
479 static struct http_buf *http_serialize_request(struct http_request *r)
481 struct http_channel *c = r->channel;
482 struct http_header *h;
483 struct http_argument *a;
485 wrbuf_rewind(c->wrbuf);
486 wrbuf_printf(c->wrbuf, "%s %s", r->method, r->path);
490 wrbuf_putc(c->wrbuf, '?');
491 for (a = r->arguments; a; a = a->next) {
492 if (a != r->arguments)
493 wrbuf_putc(c->wrbuf, '&');
494 wrbuf_printf(c->wrbuf, "%s=%s", a->name, a->value);
498 wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
500 for (h = r->headers; h; h = h->next)
501 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
503 wrbuf_puts(c->wrbuf, "\r\n");
505 return http_buf_bywrbuf(c->wrbuf);
509 static int http_weshouldproxy(struct http_request *rq)
511 if (proxy_addr && !strstr(rq->path, "search.pz2"))
517 struct http_header * http_header_append(struct http_channel *ch,
518 struct http_header * hp,
522 struct http_header *hpnew = 0;
527 while (hp && hp->next)
530 if(name && strlen(name)&& value && strlen(value)){
531 hpnew = nmem_malloc(ch->nmem, sizeof *hpnew);
532 hpnew->name = nmem_strdup(ch->nmem, name);
533 hpnew->value = nmem_strdup(ch->nmem, value);
547 static int http_proxy(struct http_request *rq)
549 struct http_channel *c = rq->channel;
550 struct http_proxy *p = c->proxy;
551 struct http_header *hp;
552 struct http_buf *requestbuf;
553 char server_via[128] = "";
554 char server_port[16] = "";
555 struct conf_server *ser = global_parameters.server;
557 if (!p) // This is a new connection. Create a proxy channel
564 if (!(pe = getprotobyname("tcp"))) {
567 if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
569 yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
572 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
573 &one, sizeof(one)) < 0)
575 if ((flags = fcntl(sock, F_GETFL, 0)) < 0)
576 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
577 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
578 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
579 if (connect(sock, (struct sockaddr *) proxy_addr,
580 sizeof(*proxy_addr)) < 0)
581 if (errno != EINPROGRESS)
583 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
587 p = xmalloc(sizeof(struct http_proxy));
590 p->first_response = 1;
592 // We will add EVENT_OUTPUT below
593 p->iochan = iochan_create(sock, 0, proxy_io, EVENT_INPUT);
594 iochan_setdata(p->iochan, p);
595 p->iochan->next = channel_list;
596 channel_list = p->iochan;
599 // Do _not_ modify Host: header, just checking it's existence
600 for (hp = rq->headers; hp; hp = hp->next)
601 if (!strcmp(hp->name, "Host"))
605 yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
609 // Add new header about paraz2 version, host, remote client address, etc.
612 hp = http_header_append(c, hp,
613 PACKAGE_NAME "-version", PACKAGE_VERSION);
614 hp = http_header_append(c, hp,
615 PACKAGE_NAME "-server-host", ser->myurl);
616 sprintf(server_port, "%d", ser->port);
617 hp = http_header_append(c, hp,
618 PACKAGE_NAME "-server-port", server_port);
619 sprintf(server_via, "1.1 %s:%s (%s/%s)",
620 ser->myurl, server_port, PACKAGE_NAME, PACKAGE_VERSION);
621 hp = http_header_append(c, hp,
623 //hp = http_header_append(c, hp,"Client-ip",
624 // c->iochan->addr_str);
625 hp = http_header_append(c, hp,"X-Forwarded-For",
626 c->iochan->addr_str);
629 requestbuf = http_serialize_request(rq);
630 http_buf_enqueue(&p->oqueue, requestbuf);
631 iochan_setflag(p->iochan, EVENT_OUTPUT);
635 void http_send_response(struct http_channel *ch)
637 struct http_response *rs = ch->response;
641 hb = http_serialize_response(ch, rs);
644 yaz_log(YLOG_WARN, "Failed to serialize HTTP response");
645 http_destroy(ch->iochan);
649 http_buf_enqueue(&ch->oqueue, hb);
650 iochan_setflag(ch->iochan, EVENT_OUTPUT);
651 ch->state = Http_Idle;
655 static void http_io(IOCHAN i, int event)
657 struct http_channel *hc = iochan_getdata(i);
662 struct http_buf *htbuf;
665 htbuf = http_buf_create();
666 res = read(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1);
667 if (res == -1 && errno == EAGAIN)
669 http_buf_destroy(htbuf);
674 http_buf_destroy(htbuf);
680 htbuf->buf[res] = '\0';
682 http_buf_enqueue(&hc->iqueue, htbuf);
685 if (hc->state == Http_Busy)
687 if ((reqlen = request_check(hc->iqueue)) <= 2)
690 nmem_reset(hc->nmem);
691 if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
693 yaz_log(YLOG_WARN, "Failed to parse request");
698 yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
700 *hc->request->search ? "?" : "",
701 hc->request->search);
702 if (http_weshouldproxy(hc->request))
703 http_proxy(hc->request);
706 // Execute our business logic!
707 hc->state = Http_Busy;
712 yaz_log(YLOG_DEBUG, "We think we have more input to read. Forcing event");
713 iochan_setevent(i, EVENT_INPUT);
721 struct http_buf *wb = hc->oqueue;
722 res = write(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len);
725 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
731 hc->oqueue = hc->oqueue->next;
732 http_buf_destroy(wb);
740 if (!strcmp(hc->version, "1.0"))
747 iochan_clearflag(i, EVENT_OUTPUT);
749 iochan_setevent(hc->iochan, EVENT_INPUT);
754 if (!hc->oqueue && hc->proxy && !hc->proxy->iochan)
755 http_destroy(i); // Server closed; we're done
758 yaz_log(YLOG_WARN, "Unexpected event on connection");
764 // If this hostname contains our proxy host as a prefix, replace with myurl
765 static char *sub_hostname(struct http_channel *c, char *buf)
768 if (strlen(buf) > 1023)
770 if (strncmp(buf, "http://", 7))
772 if (!strncmp(buf + 7, proxy_url, strlen(proxy_url)))
775 strcat(tmp, buf + strlen(proxy_url) + 7);
776 return nmem_strdup(c->nmem, tmp);
782 // Handles I/O on a client connection to a backend web server (proxy mode)
783 static void proxy_io(IOCHAN pi, int event)
785 struct http_proxy *pc = iochan_getdata(pi);
786 struct http_channel *hc = pc->channel;
791 struct http_buf *htbuf;
794 htbuf = http_buf_create();
795 res = read(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1);
796 if (res == 0 || (res < 0 && errno != EINPROGRESS))
800 yaz_log(YLOG_WARN, "Proxy read came up short");
801 // Close channel and alert client HTTP channel that we're gone
802 http_buf_destroy(htbuf);
803 close(iochan_getfd(pi));
809 http_destroy(hc->iochan);
815 htbuf->buf[res] = '\0';
819 if (pc->first_response) // Check if this is a redirect
822 if ((len = package_check(htbuf->buf)))
824 struct http_response *res = http_parse_response_buf(hc, htbuf->buf, len);
827 struct http_header *h;
828 for (h = res->headers; h; h = h->next)
829 if (!strcmp(h->name, "Location"))
831 // We found a location header. Rewrite it.
832 struct http_buf *buf;
833 h->value = sub_hostname(hc, h->value);
834 buf = http_serialize_response(hc, res);
835 yaz_log(YLOG_LOG, "Proxy rewrite");
836 http_buf_enqueue(&hc->oqueue, buf);
842 pc->first_response = 0;
845 // Write any remaining payload
846 if (htbuf->len - htbuf->offset > 0)
847 http_buf_enqueue(&hc->oqueue, htbuf);
849 iochan_setflag(hc->iochan, EVENT_OUTPUT);
852 if (!(htbuf = pc->oqueue))
854 iochan_clearflag(pi, EVENT_OUTPUT);
857 res = write(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len);
860 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
861 http_destroy(hc->iochan);
864 if (res == htbuf->len)
866 struct http_buf *np = htbuf->next;
867 http_buf_destroy(htbuf);
873 htbuf->offset += res;
877 iochan_setflags(pi, EVENT_INPUT); // Turns off output flag
881 yaz_log(YLOG_WARN, "Unexpected event on connection");
882 http_destroy(hc->iochan);
887 static void http_destroy(IOCHAN i)
889 struct http_channel *s = iochan_getdata(i);
893 if (s->proxy->iochan)
895 close(iochan_getfd(s->proxy->iochan));
896 iochan_destroy(s->proxy->iochan);
898 http_buf_destroy_queue(s->proxy->oqueue);
901 s->next = http_channel_freelist;
902 http_channel_freelist = s;
903 close(iochan_getfd(i));
907 static struct http_channel *http_create(void)
909 struct http_channel *r = http_channel_freelist;
913 http_channel_freelist = r->next;
915 wrbuf_rewind(r->wrbuf);
919 r = xmalloc(sizeof(struct http_channel));
920 r->nmem = nmem_create();
921 r->wrbuf = wrbuf_alloc();
925 r->iqueue = r->oqueue = 0;
926 r->state = Http_Idle;
933 /* Accept a new command connection */
934 static void http_accept(IOCHAN i, int event)
936 struct sockaddr_in addr;
937 int fd = iochan_getfd(i);
942 struct http_channel *ch;
945 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
947 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
950 if ((flags = fcntl(s, F_GETFL, 0)) < 0)
951 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
952 if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
953 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
955 yaz_log(YLOG_DEBUG, "New command connection");
956 c = iochan_create(s, &addr, http_io, EVENT_INPUT | EVENT_EXCEPT);
960 iochan_setdata(c, ch);
962 c->next = channel_list;
966 /* Create a http-channel listener, syntax [host:]port */
967 void http_init(const char *addr)
972 struct sockaddr_in myaddr;
977 yaz_log(YLOG_LOG, "HTTP listener is %s", addr);
979 memset(&myaddr, 0, sizeof myaddr);
980 myaddr.sin_family = AF_INET;
981 pp = strchr(addr, ':');
988 strncpy(hostname, addr, len);
989 hostname[len] = '\0';
990 if (!(he = gethostbyname(hostname)))
992 yaz_log(YLOG_FATAL, "Unable to resolve '%s'", hostname);
995 memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1001 myaddr.sin_addr.s_addr = INADDR_ANY;
1003 myaddr.sin_port = htons(port);
1005 if (!(p = getprotobyname("tcp"))) {
1008 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
1009 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
1010 if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
1011 &one, sizeof(one)) < 0)
1014 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
1015 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
1016 if (listen(l, SOMAXCONN) < 0)
1017 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
1019 c = iochan_create(l, &myaddr, http_accept, EVENT_INPUT | EVENT_EXCEPT);
1020 c->next = channel_list;
1024 void http_set_proxyaddr(char *host, char *base_url)
1030 strcpy(myurl, base_url);
1031 strcpy(proxy_url, host);
1032 p = strchr(host, ':');
1033 yaz_log(YLOG_DEBUG, "Proxying for %s", host);
1034 yaz_log(YLOG_LOG, "HTTP backend %s", proxy_url);
1041 if (!(he = gethostbyname(host)))
1043 fprintf(stderr, "Failed to lookup '%s'\n", host);
1046 proxy_addr = xmalloc(sizeof(struct sockaddr_in));
1047 proxy_addr->sin_family = he->h_addrtype;
1048 memcpy(&proxy_addr->sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1049 proxy_addr->sin_port = htons(port);
1055 * indent-tabs-mode: nil
1057 * vim: shiftwidth=4 tabstop=8 expandtab