2 * $Id: http.c,v 1.21 2007-04-02 09:43:08 marc Exp $
6 #include <sys/socket.h>
23 #include <netinet/in.h>
26 #include <yaz/yaz-util.h>
27 #include <yaz/comstack.h>
35 #include "http_command.h"
37 static void proxy_io(IOCHAN i, int event);
38 static struct http_channel *http_create(void);
39 static void http_destroy(IOCHAN i);
41 extern IOCHAN channel_list;
42 extern struct parameters global_parameters;
45 // If this is set, we proxy normal HTTP requests
46 static struct sockaddr_in *proxy_addr = 0;
47 static char proxy_url[256] = "";
48 static char myurl[256] = "";
49 static struct http_buf *http_buf_freelist = 0;
50 static struct http_channel *http_channel_freelist = 0;
52 static struct http_buf *http_buf_create()
56 if (http_buf_freelist)
58 r = http_buf_freelist;
59 http_buf_freelist = http_buf_freelist->next;
62 r = xmalloc(sizeof(struct http_buf));
69 static void http_buf_destroy(struct http_buf *b)
71 b->next = http_buf_freelist;
72 http_buf_freelist = b;
75 static void http_buf_destroy_queue(struct http_buf *b)
87 // Calculate length of chain
88 static int http_buf_len(struct http_buf *b)
91 for (; b; b = b->next)
97 static struct http_buf *http_buf_bybuf(char *b, int len)
99 struct http_buf *res = 0;
100 struct http_buf **p = &res;
105 if (tocopy > HTTP_BUF_SIZE)
106 tocopy = HTTP_BUF_SIZE;
107 *p = http_buf_create();
108 memcpy((*p)->buf, b, tocopy);
117 // Add a (chain of) buffers to the end of an existing queue.
118 static void http_buf_enqueue(struct http_buf **queue, struct http_buf *b)
121 queue = &(*queue)->next;
125 static struct http_buf *http_buf_bywrbuf(WRBUF wrbuf)
127 // Heavens to Betsy (buf)!
128 return http_buf_bybuf(wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
131 // Non-destructively collapse chain of buffers into a string (max *len)
133 static int http_buf_peek(struct http_buf *b, char *buf, int len)
136 while (b && rd < len)
138 int toread = len - rd;
141 memcpy(buf + rd, b->buf + b->offset, toread);
149 // Ddestructively munch up to len from head of queue.
150 static int http_buf_read(struct http_buf **b, char *buf, int len)
153 while ((*b) && rd < len)
155 int toread = len - rd;
156 if (toread > (*b)->len)
158 memcpy(buf + rd, (*b)->buf + (*b)->offset, toread);
160 if (toread < (*b)->len)
163 (*b)->offset += toread;
168 struct http_buf *n = (*b)->next;
169 http_buf_destroy(*b);
177 // Buffers may overlap.
178 static void urldecode(char *i, char *o)
190 sscanf(i, "%2hhx", o);
200 // Warning: Buffers may not overlap
201 void urlencode(const char *i, char *o)
205 if (strchr(" /:", *i))
207 sprintf(o, "%%%.2X", (int) *i);
217 void http_addheader(struct http_response *r, const char *name, const char *value)
219 struct http_channel *c = r->channel;
220 struct http_header *h = nmem_malloc(c->nmem, sizeof *h);
221 h->name = nmem_strdup(c->nmem, name);
222 h->value = nmem_strdup(c->nmem, value);
223 h->next = r->headers;
227 char *http_argbyname(struct http_request *r, char *name)
229 struct http_argument *p;
232 for (p = r->arguments; p; p = p->next)
233 if (!strcmp(p->name, name))
238 char *http_headerbyname(struct http_header *h, char *name)
240 for (; h; h = h->next)
241 if (!strcmp(h->name, name))
246 struct http_response *http_create_response(struct http_channel *c)
248 struct http_response *r = nmem_malloc(c->nmem, sizeof(*r));
249 strcpy(r->code, "200");
257 // Check if buf contains a package (minus payload)
258 static int package_check(const char *buf)
261 while (*buf) // Check if we have a sequence of lines terminated by an empty line
263 char *b = strstr(buf, "\r\n");
268 len += (b - buf) + 2;
276 // Check if we have a request. Return 0 or length
277 // (including trailing CRNL) FIXME: Does not deal gracefully with requests
278 // carrying payload but this is kind of OK since we will reject anything
279 // other than an empty GET
280 static int request_check(struct http_buf *queue)
284 http_buf_peek(queue, tmp, 4096);
285 return package_check(tmp);
288 struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len)
291 struct http_response *r = http_create_response(c);
293 struct http_header **hp = &r->headers;
297 memcpy(tmp, buf, len);
298 for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version
302 for (p2 = p; *p2 && *p2 != ' ' && p2 - p < 3; p2++)
303 r->code[p2 - p] = *p2;
304 if (!(p = strstr(tmp, "\r\n")))
309 if (!(p2 = strstr(p, "\r\n")))
311 if (p == p2) // End of headers
315 struct http_header *h = *hp = nmem_malloc(c->nmem, sizeof(*h));
316 char *value = strchr(p, ':');
320 h->name = nmem_strdup(c->nmem, p);
321 while (isspace(*value))
323 if (value >= p2) // Empty header;
330 h->value = nmem_strdup(c->nmem, value);
339 struct http_request *http_parse_request(struct http_channel *c, struct http_buf **queue,
342 struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
349 if (http_buf_read(queue, buf, len) < len)
357 for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
361 yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
366 if (!(buf = strchr(buf, ' ')))
368 yaz_log(YLOG_WARN, "Syntax error in request (1)");
372 if (!(p = strchr(buf, ' ')))
374 yaz_log(YLOG_WARN, "Syntax error in request (2)");
378 if ((p2 = strchr(buf, '?'))) // Do we have arguments?
380 r->path = nmem_strdup(c->nmem, buf);
383 r->search = nmem_strdup(c->nmem, p2);
387 struct http_argument *a;
388 char *equal = strchr(p2, '=');
389 char *eoa = strchr(p2, '&');
392 yaz_log(YLOG_WARN, "Expected '=' in argument");
396 eoa = equal + strlen(equal); // last argument
399 a = nmem_malloc(c->nmem, sizeof(struct http_argument));
401 a->name = nmem_strdup(c->nmem, p2);
402 urldecode(equal, equal);
403 a->value = nmem_strdup(c->nmem, equal);
404 a->next = r->arguments;
411 if (strncmp(buf, "HTTP/", 5))
412 strcpy(r->http_version, "1.0");
416 if (!(p = strstr(buf, "\r\n")))
420 strcpy(r->http_version, buf);
423 strcpy(c->version, r->http_version);
428 if (!(p = strstr(buf, "\r\n")))
434 struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
435 if (!(p2 = strchr(buf, ':')))
438 h->name = nmem_strdup(c->nmem, buf);
441 if (p2 >= p) // Empty header?
447 h->value = nmem_strdup(c->nmem, p2);
448 h->next = r->headers;
457 static struct http_buf *http_serialize_response(struct http_channel *c,
458 struct http_response *r)
460 struct http_header *h;
462 wrbuf_rewind(c->wrbuf);
463 wrbuf_printf(c->wrbuf, "HTTP/1.1 %s %s\r\n", r->code, r->msg);
464 for (h = r->headers; h; h = h->next)
465 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
468 wrbuf_printf(c->wrbuf, "Content-length: %d\r\n", r->payload ?
469 (int) strlen(r->payload) : 0);
470 wrbuf_printf(c->wrbuf, "Content-type: text/xml\r\n");
472 wrbuf_puts(c->wrbuf, "\r\n");
475 wrbuf_puts(c->wrbuf, r->payload);
477 return http_buf_bywrbuf(c->wrbuf);
480 // Serialize a HTTP request
481 static struct http_buf *http_serialize_request(struct http_request *r)
483 struct http_channel *c = r->channel;
484 struct http_header *h;
485 struct http_argument *a;
487 wrbuf_rewind(c->wrbuf);
488 wrbuf_printf(c->wrbuf, "%s %s", r->method, r->path);
492 wrbuf_putc(c->wrbuf, '?');
493 for (a = r->arguments; a; a = a->next) {
494 if (a != r->arguments)
495 wrbuf_putc(c->wrbuf, '&');
496 wrbuf_printf(c->wrbuf, "%s=%s", a->name, a->value);
500 wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
502 for (h = r->headers; h; h = h->next)
503 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
505 wrbuf_puts(c->wrbuf, "\r\n");
507 return http_buf_bywrbuf(c->wrbuf);
511 static int http_weshouldproxy(struct http_request *rq)
513 if (proxy_addr && !strstr(rq->path, "search.pz2"))
519 struct http_header * http_header_append(struct http_channel *ch,
520 struct http_header * hp,
524 struct http_header *hpnew = 0;
529 while (hp && hp->next)
532 if(name && strlen(name)&& value && strlen(value)){
533 hpnew = nmem_malloc(ch->nmem, sizeof *hpnew);
534 hpnew->name = nmem_strdup(ch->nmem, name);
535 hpnew->value = nmem_strdup(ch->nmem, value);
549 static int http_proxy(struct http_request *rq)
551 struct http_channel *c = rq->channel;
552 struct http_proxy *p = c->proxy;
553 struct http_header *hp;
554 struct http_buf *requestbuf;
555 char server_via[128] = "";
556 char server_port[16] = "";
557 struct conf_server *ser = global_parameters.server;
559 if (!p) // This is a new connection. Create a proxy channel
566 if (!(pe = getprotobyname("tcp"))) {
569 if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
571 yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
574 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
575 &one, sizeof(one)) < 0)
577 if ((flags = fcntl(sock, F_GETFL, 0)) < 0)
578 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
579 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
580 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
581 if (connect(sock, (struct sockaddr *) proxy_addr,
582 sizeof(*proxy_addr)) < 0)
583 if (errno != EINPROGRESS)
585 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
589 p = xmalloc(sizeof(struct http_proxy));
592 p->first_response = 1;
594 // We will add EVENT_OUTPUT below
595 p->iochan = iochan_create(sock, 0, proxy_io, EVENT_INPUT);
596 iochan_setdata(p->iochan, p);
597 p->iochan->next = channel_list;
598 channel_list = p->iochan;
601 // Do _not_ modify Host: header, just checking it's existence
602 for (hp = rq->headers; hp; hp = hp->next)
603 if (!strcmp(hp->name, "Host"))
607 yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
611 // Add new header about paraz2 version, host, remote client address, etc.
614 hp = http_header_append(c, hp,
615 "X-Pazpar2-Version", PACKAGE_VERSION);
616 hp = http_header_append(c, hp,
617 "X-Pazpar2-Server-Host", ser->host);
618 sprintf(server_port, "%d", ser->port);
619 hp = http_header_append(c, hp,
620 "X-Pazpar2-Server-Port", server_port);
621 sprintf(server_via, "1.1 %s:%s (%s/%s)",
622 ser->host, server_port, PACKAGE_NAME, PACKAGE_VERSION);
623 hp = http_header_append(c, hp, "Via" , server_via);
624 hp = http_header_append(c, hp,"X-Forwarded-For", c->iochan->addr_str);
627 requestbuf = http_serialize_request(rq);
628 http_buf_enqueue(&p->oqueue, requestbuf);
629 iochan_setflag(p->iochan, EVENT_OUTPUT);
633 void http_send_response(struct http_channel *ch)
635 struct http_response *rs = ch->response;
639 hb = http_serialize_response(ch, rs);
642 yaz_log(YLOG_WARN, "Failed to serialize HTTP response");
643 http_destroy(ch->iochan);
647 http_buf_enqueue(&ch->oqueue, hb);
648 iochan_setflag(ch->iochan, EVENT_OUTPUT);
649 ch->state = Http_Idle;
653 static void http_io(IOCHAN i, int event)
655 struct http_channel *hc = iochan_getdata(i);
660 struct http_buf *htbuf;
663 htbuf = http_buf_create();
664 res = read(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1);
665 if (res == -1 && errno == EAGAIN)
667 http_buf_destroy(htbuf);
672 http_buf_destroy(htbuf);
678 htbuf->buf[res] = '\0';
680 http_buf_enqueue(&hc->iqueue, htbuf);
683 if (hc->state == Http_Busy)
685 if ((reqlen = request_check(hc->iqueue)) <= 2)
688 nmem_reset(hc->nmem);
689 if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
691 yaz_log(YLOG_WARN, "Failed to parse request");
696 yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
698 *hc->request->search ? "?" : "",
699 hc->request->search);
700 if (http_weshouldproxy(hc->request))
701 http_proxy(hc->request);
704 // Execute our business logic!
705 hc->state = Http_Busy;
710 yaz_log(YLOG_DEBUG, "We think we have more input to read. Forcing event");
711 iochan_setevent(i, EVENT_INPUT);
719 struct http_buf *wb = hc->oqueue;
720 res = write(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len);
723 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
729 hc->oqueue = hc->oqueue->next;
730 http_buf_destroy(wb);
738 if (!strcmp(hc->version, "1.0"))
745 iochan_clearflag(i, EVENT_OUTPUT);
747 iochan_setevent(hc->iochan, EVENT_INPUT);
752 if (!hc->oqueue && hc->proxy && !hc->proxy->iochan)
753 http_destroy(i); // Server closed; we're done
756 yaz_log(YLOG_WARN, "Unexpected event on connection");
762 // If this hostname contains our proxy host as a prefix, replace with myurl
763 static char *sub_hostname(struct http_channel *c, char *buf)
766 if (strlen(buf) > 1023)
768 if (strncmp(buf, "http://", 7))
770 if (!strncmp(buf + 7, proxy_url, strlen(proxy_url)))
773 strcat(tmp, buf + strlen(proxy_url) + 7);
774 return nmem_strdup(c->nmem, tmp);
780 // Handles I/O on a client connection to a backend web server (proxy mode)
781 static void proxy_io(IOCHAN pi, int event)
783 struct http_proxy *pc = iochan_getdata(pi);
784 struct http_channel *hc = pc->channel;
789 struct http_buf *htbuf;
792 htbuf = http_buf_create();
793 res = read(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1);
794 if (res == 0 || (res < 0 && errno != EINPROGRESS))
798 yaz_log(YLOG_WARN, "Proxy read came up short");
799 // Close channel and alert client HTTP channel that we're gone
800 http_buf_destroy(htbuf);
801 close(iochan_getfd(pi));
807 http_destroy(hc->iochan);
813 htbuf->buf[res] = '\0';
817 if (pc->first_response) // Check if this is a redirect
820 if ((len = package_check(htbuf->buf)))
822 struct http_response *res = http_parse_response_buf(hc, htbuf->buf, len);
825 struct http_header *h;
826 for (h = res->headers; h; h = h->next)
827 if (!strcmp(h->name, "Location"))
829 // We found a location header. Rewrite it.
830 struct http_buf *buf;
831 h->value = sub_hostname(hc, h->value);
832 buf = http_serialize_response(hc, res);
833 yaz_log(YLOG_LOG, "Proxy rewrite");
834 http_buf_enqueue(&hc->oqueue, buf);
840 pc->first_response = 0;
843 // Write any remaining payload
844 if (htbuf->len - htbuf->offset > 0)
845 http_buf_enqueue(&hc->oqueue, htbuf);
847 iochan_setflag(hc->iochan, EVENT_OUTPUT);
850 if (!(htbuf = pc->oqueue))
852 iochan_clearflag(pi, EVENT_OUTPUT);
855 res = write(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len);
858 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
859 http_destroy(hc->iochan);
862 if (res == htbuf->len)
864 struct http_buf *np = htbuf->next;
865 http_buf_destroy(htbuf);
871 htbuf->offset += res;
875 iochan_setflags(pi, EVENT_INPUT); // Turns off output flag
879 yaz_log(YLOG_WARN, "Unexpected event on connection");
880 http_destroy(hc->iochan);
885 static void http_destroy(IOCHAN i)
887 struct http_channel *s = iochan_getdata(i);
891 if (s->proxy->iochan)
893 close(iochan_getfd(s->proxy->iochan));
894 iochan_destroy(s->proxy->iochan);
896 http_buf_destroy_queue(s->proxy->oqueue);
899 s->next = http_channel_freelist;
900 http_channel_freelist = s;
901 close(iochan_getfd(i));
905 static struct http_channel *http_create(void)
907 struct http_channel *r = http_channel_freelist;
911 http_channel_freelist = r->next;
913 wrbuf_rewind(r->wrbuf);
917 r = xmalloc(sizeof(struct http_channel));
918 r->nmem = nmem_create();
919 r->wrbuf = wrbuf_alloc();
923 r->iqueue = r->oqueue = 0;
924 r->state = Http_Idle;
931 /* Accept a new command connection */
932 static void http_accept(IOCHAN i, int event)
934 struct sockaddr_in addr;
935 int fd = iochan_getfd(i);
940 struct http_channel *ch;
943 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
945 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
948 if ((flags = fcntl(s, F_GETFL, 0)) < 0)
949 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
950 if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
951 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
953 yaz_log(YLOG_DEBUG, "New command connection");
954 c = iochan_create(s, &addr, http_io, EVENT_INPUT | EVENT_EXCEPT);
958 iochan_setdata(c, ch);
960 c->next = channel_list;
964 /* Create a http-channel listener, syntax [host:]port */
965 void http_init(const char *addr)
970 struct sockaddr_in myaddr;
975 yaz_log(YLOG_LOG, "HTTP listener %s", addr);
977 memset(&myaddr, 0, sizeof myaddr);
978 myaddr.sin_family = AF_INET;
979 pp = strchr(addr, ':');
986 strncpy(hostname, addr, len);
987 hostname[len] = '\0';
988 if (!(he = gethostbyname(hostname)))
990 yaz_log(YLOG_FATAL, "Unable to resolve '%s'", hostname);
994 memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
997 yaz_log(YLOG_LOG, "HTTP address %s:%d",
998 "" == he->h_addr_list[0] ? he->h_addr_list[0] : "127.0.0.1" ,
1007 myaddr.sin_addr.s_addr = INADDR_ANY;
1010 // get hostname from system - after deciding to bind to any
1011 // IP address this box might have.
1012 if (0 == gethostname(h, len)){
1014 global_parameters.server->host = nmem_strdup(nmem, h);
1016 yaz_log(YLOG_WARN, "Could not get host name");
1021 myaddr.sin_port = htons(port);
1024 if (!(p = getprotobyname("tcp"))) {
1027 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
1028 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
1029 if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
1030 &one, sizeof(one)) < 0)
1033 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
1034 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
1035 if (listen(l, SOMAXCONN) < 0)
1036 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
1038 c = iochan_create(l, &myaddr, http_accept, EVENT_INPUT | EVENT_EXCEPT);
1039 c->next = channel_list;
1043 void http_set_proxyaddr(char *host, char *base_url)
1049 strcpy(myurl, base_url);
1050 strcpy(proxy_url, host);
1051 p = strchr(host, ':');
1052 yaz_log(YLOG_DEBUG, "Proxying for %s", host);
1053 yaz_log(YLOG_LOG, "HTTP backend %s", proxy_url);
1060 if (!(he = gethostbyname(host)))
1062 fprintf(stderr, "Failed to lookup '%s'\n", host);
1065 proxy_addr = xmalloc(sizeof(struct sockaddr_in));
1066 proxy_addr->sin_family = he->h_addrtype;
1067 memcpy(&proxy_addr->sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1068 proxy_addr->sin_port = htons(port);
1074 * indent-tabs-mode: nil
1076 * vim: shiftwidth=4 tabstop=8 expandtab