2 * $Id: http.c,v 1.9 2007-01-10 11:56:10 adam Exp $
6 #include <sys/socket.h>
22 #include <netinet/in.h>
24 #include <yaz/yaz-util.h>
25 #include <yaz/comstack.h>
32 #include "http_command.h"
34 static void proxy_io(IOCHAN i, int event);
35 static struct http_channel *http_create(void);
36 static void http_destroy(IOCHAN i);
38 extern IOCHAN channel_list;
40 static struct sockaddr_in *proxy_addr = 0; // If this is set, we proxy normal HTTP requests
41 static char proxy_url[256] = "";
42 static struct http_buf *http_buf_freelist = 0;
43 static struct http_channel *http_channel_freelist = 0;
45 static struct http_buf *http_buf_create()
49 if (http_buf_freelist)
51 r = http_buf_freelist;
52 http_buf_freelist = http_buf_freelist->next;
55 r = xmalloc(sizeof(struct http_buf));
62 static void http_buf_destroy(struct http_buf *b)
64 b->next = http_buf_freelist;
65 http_buf_freelist = b;
68 static void http_buf_destroy_queue(struct http_buf *b)
80 // Calculate length of chain
81 static int http_buf_len(struct http_buf *b)
84 for (; b; b = b->next)
90 static struct http_buf *http_buf_bybuf(char *b, int len)
92 struct http_buf *res = 0;
93 struct http_buf **p = &res;
98 if (tocopy > HTTP_BUF_SIZE)
99 tocopy = HTTP_BUF_SIZE;
100 *p = http_buf_create();
101 memcpy((*p)->buf, b, tocopy);
110 // Add a (chain of) buffers to the end of an existing queue.
111 static void http_buf_enqueue(struct http_buf **queue, struct http_buf *b)
114 queue = &(*queue)->next;
118 static struct http_buf *http_buf_bywrbuf(WRBUF wrbuf)
120 // Heavens to Betsy (buf)!
121 return http_buf_bybuf(wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
124 // Non-destructively collapse chain of buffers into a string (max *len)
126 static int http_buf_peek(struct http_buf *b, char *buf, int len)
129 while (b && rd < len)
131 int toread = len - rd;
134 memcpy(buf + rd, b->buf + b->offset, toread);
142 // Ddestructively munch up to len from head of queue.
143 static int http_buf_read(struct http_buf **b, char *buf, int len)
146 while ((*b) && rd < len)
148 int toread = len - rd;
149 if (toread > (*b)->len)
151 memcpy(buf + rd, (*b)->buf + (*b)->offset, toread);
153 if (toread < (*b)->len)
156 (*b)->offset += toread;
161 struct http_buf *n = (*b)->next;
162 http_buf_destroy(*b);
170 void static urldecode(char *i, char *o)
182 sscanf(i, "%2hhx", o);
192 void http_addheader(struct http_response *r, const char *name, const char *value)
194 struct http_channel *c = r->channel;
195 struct http_header *h = nmem_malloc(c->nmem, sizeof *h);
196 h->name = nmem_strdup(c->nmem, name);
197 h->value = nmem_strdup(c->nmem, value);
198 h->next = r->headers;
202 char *http_argbyname(struct http_request *r, char *name)
204 struct http_argument *p;
207 for (p = r->arguments; p; p = p->next)
208 if (!strcmp(p->name, name))
213 char *http_headerbyname(struct http_request *r, char *name)
215 struct http_header *p;
216 for (p = r->headers; p; p = p->next)
217 if (!strcmp(p->name, name))
222 struct http_response *http_create_response(struct http_channel *c)
224 struct http_response *r = nmem_malloc(c->nmem, sizeof(*r));
225 strcpy(r->code, "200");
233 // Check if we have a complete request. Return 0 or length (including trailing newline)
234 // FIXME: Does not deal gracefully with requests carrying payload
235 // but this is kind of OK since we will reject anything other than an empty GET
236 static int request_check(struct http_buf *queue)
242 http_buf_peek(queue, tmp, 4096);
243 while (*buf) // Check if we have a sequence of lines terminated by an empty line
245 char *b = strstr(buf, "\r\n");
250 len += (b - buf) + 2;
258 struct http_request *http_parse_request(struct http_channel *c, struct http_buf **queue,
261 struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
268 if (http_buf_read(queue, buf, len) < len)
276 for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
280 yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
285 if (!(buf = strchr(buf, ' ')))
287 yaz_log(YLOG_WARN, "Syntax error in request (1)");
291 if (!(p = strchr(buf, ' ')))
293 yaz_log(YLOG_WARN, "Syntax error in request (2)");
297 if ((p2 = strchr(buf, '?'))) // Do we have arguments?
299 r->path = nmem_strdup(c->nmem, buf);
302 r->search = nmem_strdup(c->nmem, p2);
306 struct http_argument *a;
307 char *equal = strchr(p2, '=');
308 char *eoa = strchr(p2, '&');
311 yaz_log(YLOG_WARN, "Expected '=' in argument");
315 eoa = equal + strlen(equal); // last argument
318 a = nmem_malloc(c->nmem, sizeof(struct http_argument));
320 a->name = nmem_strdup(c->nmem, p2);
321 urldecode(equal, equal);
322 a->value = nmem_strdup(c->nmem, equal);
323 a->next = r->arguments;
330 if (strncmp(buf, "HTTP/", 5))
331 strcpy(r->http_version, "1.0");
335 if (!(p = strstr(buf, "\r\n")))
339 strcpy(r->http_version, buf);
342 strcpy(c->version, r->http_version);
347 if (!(p = strstr(buf, "\r\n")))
353 struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
354 if (!(p2 = strchr(buf, ':')))
357 h->name = nmem_strdup(c->nmem, buf);
360 if (p2 >= p) // Empty header?
366 h->value = nmem_strdup(c->nmem, p2);
367 h->next = r->headers;
377 static struct http_buf *http_serialize_response(struct http_channel *c,
378 struct http_response *r)
380 struct http_header *h;
382 wrbuf_rewind(c->wrbuf);
383 wrbuf_printf(c->wrbuf, "HTTP/1.1 %s %s\r\n", r->code, r->msg);
384 for (h = r->headers; h; h = h->next)
385 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
386 wrbuf_printf(c->wrbuf, "Content-length: %d\r\n", r->payload ? (int) strlen(r->payload) : 0);
387 wrbuf_printf(c->wrbuf, "Content-type: text/xml\r\n");
388 wrbuf_puts(c->wrbuf, "\r\n");
391 wrbuf_puts(c->wrbuf, r->payload);
393 return http_buf_bywrbuf(c->wrbuf);
396 // Serialize a HTTP request
397 static struct http_buf *http_serialize_request(struct http_request *r)
399 struct http_channel *c = r->channel;
400 struct http_header *h;
401 struct http_argument *a;
403 wrbuf_rewind(c->wrbuf);
404 wrbuf_printf(c->wrbuf, "%s %s", r->method, r->path);
408 wrbuf_putc(c->wrbuf, '?');
409 for (a = r->arguments; a; a = a->next) {
410 if (a != r->arguments)
411 wrbuf_putc(c->wrbuf, '&');
412 wrbuf_printf(c->wrbuf, "%s=%s", a->name, a->value);
416 wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
418 for (h = r->headers; h; h = h->next)
419 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
421 wrbuf_puts(c->wrbuf, "\r\n");
423 return http_buf_bywrbuf(c->wrbuf);
427 static int http_weshouldproxy(struct http_request *rq)
429 if (proxy_addr && !strstr(rq->path, "search.pz2"))
434 static int http_proxy(struct http_request *rq)
436 struct http_channel *c = rq->channel;
437 struct http_proxy *p = c->proxy;
438 struct http_header *hp;
439 struct http_buf *requestbuf;
441 if (!p) // This is a new connection. Create a proxy channel
448 if (!(pe = getprotobyname("tcp"))) {
451 if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
453 yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
456 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
457 &one, sizeof(one)) < 0)
459 if ((flags = fcntl(sock, F_GETFL, 0)) < 0)
460 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
461 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
462 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
463 if (connect(sock, (struct sockaddr *) proxy_addr, sizeof(*proxy_addr)) < 0)
464 if (errno != EINPROGRESS)
466 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
470 p = xmalloc(sizeof(struct http_proxy));
474 // We will add EVENT_OUTPUT below
475 p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT);
476 iochan_setdata(p->iochan, p);
477 p->iochan->next = channel_list;
478 channel_list = p->iochan;
481 // Modify Host: header
482 for (hp = rq->headers; hp; hp = hp->next)
483 if (!strcmp(hp->name, "Host"))
487 yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
490 hp->value = nmem_strdup(c->nmem, proxy_url);
491 requestbuf = http_serialize_request(rq);
492 http_buf_enqueue(&p->oqueue, requestbuf);
493 iochan_setflag(p->iochan, EVENT_OUTPUT);
497 void http_send_response(struct http_channel *ch)
499 struct http_response *rs = ch->response;
503 hb = http_serialize_response(ch, rs);
506 yaz_log(YLOG_WARN, "Failed to serialize HTTP response");
507 http_destroy(ch->iochan);
511 http_buf_enqueue(&ch->oqueue, hb);
512 iochan_setflag(ch->iochan, EVENT_OUTPUT);
513 ch->state = Http_Idle;
517 static void http_io(IOCHAN i, int event)
519 struct http_channel *hc = iochan_getdata(i);
524 struct http_buf *htbuf;
527 htbuf = http_buf_create();
528 res = read(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1);
529 if (res == -1 && errno == EAGAIN)
531 http_buf_destroy(htbuf);
536 http_buf_destroy(htbuf);
542 htbuf->buf[res] = '\0';
544 http_buf_enqueue(&hc->iqueue, htbuf);
547 if (hc->state == Http_Busy)
549 if ((reqlen = request_check(hc->iqueue)) <= 2)
552 nmem_reset(hc->nmem);
553 if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
555 yaz_log(YLOG_WARN, "Failed to parse request");
560 yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
562 *hc->request->search ? "?" : "",
563 hc->request->search);
564 if (http_weshouldproxy(hc->request))
565 http_proxy(hc->request);
568 // Execute our business logic!
569 hc->state = Http_Busy;
574 yaz_log(YLOG_DEBUG, "We think we have more input to read. Forcing event");
575 iochan_setevent(i, EVENT_INPUT);
583 struct http_buf *wb = hc->oqueue;
584 res = write(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len);
587 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
593 hc->oqueue = hc->oqueue->next;
594 http_buf_destroy(wb);
602 if (!strcmp(hc->version, "1.0"))
609 iochan_clearflag(i, EVENT_OUTPUT);
611 iochan_setevent(hc->iochan, EVENT_INPUT);
616 if (!hc->oqueue && hc->proxy && !hc->proxy->iochan)
617 http_destroy(i); // Server closed; we're done
620 yaz_log(YLOG_WARN, "Unexpected event on connection");
625 // Handles I/O on a client connection to a backend web server (proxy mode)
626 static void proxy_io(IOCHAN pi, int event)
628 struct http_proxy *pc = iochan_getdata(pi);
629 struct http_channel *hc = pc->channel;
634 struct http_buf *htbuf;
637 htbuf = http_buf_create();
638 res = read(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1);
639 if (res == 0 || (res < 0 && errno != EINPROGRESS))
643 yaz_log(YLOG_WARN, "Proxy read came up short");
644 // Close channel and alert client HTTP channel that we're gone
645 http_buf_destroy(htbuf);
646 close(iochan_getfd(pi));
652 http_destroy(hc->iochan);
658 htbuf->buf[res] = '\0';
660 http_buf_enqueue(&hc->oqueue, htbuf);
662 iochan_setflag(hc->iochan, EVENT_OUTPUT);
665 if (!(htbuf = pc->oqueue))
667 iochan_clearflag(pi, EVENT_OUTPUT);
670 res = write(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len);
673 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
674 http_destroy(hc->iochan);
677 if (res == htbuf->len)
679 struct http_buf *np = htbuf->next;
680 http_buf_destroy(htbuf);
686 htbuf->offset += res;
690 iochan_setflags(pi, EVENT_INPUT); // Turns off output flag
694 yaz_log(YLOG_WARN, "Unexpected event on connection");
695 http_destroy(hc->iochan);
700 static void http_destroy(IOCHAN i)
702 struct http_channel *s = iochan_getdata(i);
706 if (s->proxy->iochan)
708 close(iochan_getfd(s->proxy->iochan));
709 iochan_destroy(s->proxy->iochan);
711 http_buf_destroy_queue(s->proxy->oqueue);
714 s->next = http_channel_freelist;
715 http_channel_freelist = s;
716 close(iochan_getfd(i));
720 static struct http_channel *http_create(void)
722 struct http_channel *r = http_channel_freelist;
726 http_channel_freelist = r->next;
728 wrbuf_rewind(r->wrbuf);
732 r = xmalloc(sizeof(struct http_channel));
733 r->nmem = nmem_create();
734 r->wrbuf = wrbuf_alloc();
738 r->iqueue = r->oqueue = 0;
739 r->state = Http_Idle;
746 /* Accept a new command connection */
747 static void http_accept(IOCHAN i, int event)
749 struct sockaddr_in addr;
750 int fd = iochan_getfd(i);
755 struct http_channel *ch;
758 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
760 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
763 if ((flags = fcntl(s, F_GETFL, 0)) < 0)
764 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
765 if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
766 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
768 yaz_log(YLOG_DEBUG, "New command connection");
769 c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT);
773 iochan_setdata(c, ch);
775 c->next = channel_list;
779 /* Create a http-channel listener, syntax [host:]port */
780 void http_init(const char *addr)
785 struct sockaddr_in myaddr;
790 yaz_log(YLOG_LOG, "HTTP listener is %s", addr);
792 memset(&myaddr, 0, sizeof myaddr);
793 myaddr.sin_family = AF_INET;
794 pp = strchr(addr, ':');
801 strncpy(hostname, addr, len);
802 hostname[len] = '\0';
803 if (!(he = gethostbyname(hostname)))
805 yaz_log(YLOG_FATAL, "Unable to resolve '%s'", hostname);
808 memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
814 myaddr.sin_addr.s_addr = INADDR_ANY;
816 myaddr.sin_port = htons(port);
818 if (!(p = getprotobyname("tcp"))) {
821 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
822 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
823 if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
824 &one, sizeof(one)) < 0)
827 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
828 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
829 if (listen(l, SOMAXCONN) < 0)
830 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
832 c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT);
833 c->next = channel_list;
837 void http_set_proxyaddr(char *host)
843 strcpy(proxy_url, host);
844 p = strchr(host, ':');
845 yaz_log(YLOG_DEBUG, "Proxying for %s", host);
852 if (!(he = gethostbyname(host)))
854 fprintf(stderr, "Failed to lookup '%s'\n", host);
857 proxy_addr = xmalloc(sizeof(struct sockaddr_in));
858 proxy_addr->sin_family = he->h_addrtype;
859 memcpy(&proxy_addr->sin_addr.s_addr, he->h_addr_list[0], he->h_length);
860 proxy_addr->sin_port = htons(port);
866 * indent-tabs-mode: nil
868 * vim: shiftwidth=4 tabstop=8 expandtab