2 * $Id: http.c,v 1.3 2006-11-26 05:15:43 quinn Exp $
6 #include <sys/socket.h>
17 #include <yaz/yaz-util.h>
18 #include <yaz/comstack.h>
26 #include "http_command.h"
28 static void proxy_io(IOCHAN i, int event);
30 extern IOCHAN channel_list;
32 static struct sockaddr_in *proxy_addr = 0; // If this is set, we proxy normal HTTP requests
33 static char proxy_url[256] = "";
34 static struct http_buf *http_buf_freelist = 0;
36 static struct http_buf *http_buf_create()
40 if (http_buf_freelist)
42 r = http_buf_freelist;
43 http_buf_freelist = http_buf_freelist->next;
46 r = xmalloc(sizeof(struct http_buf));
53 static void http_buf_destroy(struct http_buf *b)
55 b->next = http_buf_freelist;
56 http_buf_freelist = b;
59 static void http_buf_destroy_queue(struct http_buf *b)
71 // Calculate length of chain
72 static int http_buf_len(struct http_buf *b)
75 for (; b; b = b->next)
81 static struct http_buf *http_buf_bybuf(char *b, int len)
83 struct http_buf *res = 0;
84 struct http_buf **p = &res;
88 *p = http_buf_create();
90 if (tocopy > HTTP_BUF_SIZE)
91 tocopy = HTTP_BUF_SIZE;
92 memcpy((*p)->buf, b, tocopy);
101 // Add a (chain of) buffers to the end of an existing queue.
102 static void http_buf_enqueue(struct http_buf **queue, struct http_buf *b)
105 queue = &(*queue)->next;
109 static struct http_buf *http_buf_bywrbuf(WRBUF wrbuf)
111 return http_buf_bybuf(wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
114 // Non-destructively collapse chain of buffers into a string (max *len)
116 static int http_buf_peek(struct http_buf *b, char *buf, int len)
119 while (b && rd < len)
121 int toread = len - rd;
124 memcpy(buf + rd, b->buf + b->offset, toread);
132 // Ddestructively munch up to len from head of queue.
133 static int http_buf_read(struct http_buf **b, char *buf, int len)
136 while ((*b) && rd < len)
138 int toread = len - rd;
139 if (toread > (*b)->len)
141 memcpy(buf + rd, (*b)->buf + (*b)->offset, toread);
143 if (toread < (*b)->len)
146 (*b)->offset += toread;
151 struct http_buf *n = (*b)->next;
152 http_buf_destroy(*b);
160 void http_addheader(struct http_response *r, const char *name, const char *value)
162 struct http_channel *c = r->channel;
163 struct http_header *h = nmem_malloc(c->nmem, sizeof *h);
164 h->name = nmem_strdup(c->nmem, name);
165 h->value = nmem_strdup(c->nmem, value);
166 h->next = r->headers;
170 char *http_argbyname(struct http_request *r, char *name)
172 struct http_argument *p;
175 for (p = r->arguments; p; p = p->next)
176 if (!strcmp(p->name, name))
181 char *http_headerbyname(struct http_request *r, char *name)
183 struct http_header *p;
184 for (p = r->headers; p; p = p->next)
185 if (!strcmp(p->name, name))
190 struct http_response *http_create_response(struct http_channel *c)
192 struct http_response *r = nmem_malloc(c->nmem, sizeof(*r));
193 strcpy(r->code, "200");
201 // Check if we have a complete request. Return 0 or length (including trailing newline)
202 // FIXME: Does not deal gracefully with requests carrying payload
203 // but this is kind of OK since we will reject anything other than an empty GET
204 static int request_check(struct http_buf *queue)
210 http_buf_peek(queue, tmp, 4096);
211 while (*buf) // Check if we have a sequence of lines terminated by an empty line
213 char *b = strstr(buf, "\r\n");
218 len += (b - buf) + 2;
226 struct http_request *http_parse_request(struct http_channel *c, struct http_buf **queue,
229 struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
236 if (http_buf_read(queue, buf, len) < len)
243 for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
247 yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
252 if (!(buf = strchr(buf, ' ')))
254 yaz_log(YLOG_WARN, "Syntax error in request (1)");
258 if (!(p = strchr(buf, ' ')))
260 yaz_log(YLOG_WARN, "Syntax error in request (2)");
264 if ((p2 = strchr(buf, '?'))) // Do we have arguments?
266 r->path = nmem_strdup(c->nmem, buf);
272 struct http_argument *a;
273 char *equal = strchr(p2, '=');
274 char *eoa = strchr(p2, '&');
277 yaz_log(YLOG_WARN, "Expected '=' in argument");
281 eoa = equal + strlen(equal); // last argument
284 a = nmem_malloc(c->nmem, sizeof(struct http_argument));
286 a->name = nmem_strdup(c->nmem, p2);
287 a->value = nmem_strdup(c->nmem, equal);
288 a->next = r->arguments;
295 if (strncmp(buf, "HTTP/", 5))
296 strcpy(r->http_version, "1.0");
300 if (!(p = strstr(buf, "\r\n")))
304 strcpy(r->http_version, buf);
307 strcpy(c->version, r->http_version);
312 if (!(p = strstr(buf, "\r\n")))
318 struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
319 if (!(p2 = strchr(buf, ':')))
322 h->name = nmem_strdup(c->nmem, buf);
325 if (p2 >= p) // Empty header?
331 h->value = nmem_strdup(c->nmem, p2);
332 h->next = r->headers;
342 static struct http_buf *http_serialize_response(struct http_channel *c,
343 struct http_response *r)
345 wrbuf_rewind(c->wrbuf);
346 struct http_header *h;
348 wrbuf_printf(c->wrbuf, "HTTP/1.1 %s %s\r\n", r->code, r->msg);
349 for (h = r->headers; h; h = h->next)
350 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
351 wrbuf_printf(c->wrbuf, "Content-length: %d\r\n", r->payload ? strlen(r->payload) : 0);
352 wrbuf_printf(c->wrbuf, "Content-type: text/xml\r\n");
353 wrbuf_puts(c->wrbuf, "\r\n");
356 wrbuf_puts(c->wrbuf, r->payload);
358 return http_buf_bywrbuf(c->wrbuf);
361 // Serialize a HTTP request
362 static struct http_buf *http_serialize_request(struct http_request *r)
364 struct http_channel *c = r->channel;
365 wrbuf_rewind(c->wrbuf);
366 struct http_header *h;
367 struct http_argument *a;
369 wrbuf_printf(c->wrbuf, "%s %s", r->method, r->path);
373 wrbuf_putc(c->wrbuf, '?');
374 for (a = r->arguments; a; a = a->next) {
375 if (a != r->arguments)
376 wrbuf_putc(c->wrbuf, '&');
377 wrbuf_printf(c->wrbuf, "%s=%s", a->name, a->value);
381 wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
383 for (h = r->headers; h; h = h->next)
384 wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
386 wrbuf_puts(c->wrbuf, "\r\n");
388 return http_buf_bywrbuf(c->wrbuf);
393 static void http_destroy(IOCHAN i)
395 struct http_channel *s = iochan_getdata(i);
399 if (s->proxy->iochan)
401 close(iochan_getfd(s->proxy->iochan));
402 iochan_destroy(s->proxy->iochan);
404 http_buf_destroy_queue(s->proxy->oqueue);
407 http_buf_destroy_queue(s->iqueue);
408 http_buf_destroy_queue(s->oqueue);
409 nmem_destroy(s->nmem);
410 wrbuf_free(s->wrbuf, 1);
412 close(iochan_getfd(i));
416 static int http_weshouldproxy(struct http_request *rq)
418 if (proxy_addr && !strstr(rq->path, "search.pz2"))
423 static int http_proxy(struct http_request *rq)
425 struct http_channel *c = rq->channel;
426 struct http_proxy *p = c->proxy;
427 struct http_header *hp;
428 struct http_buf *requestbuf;
430 if (!p) // This is a new connection. Create a proxy channel
437 if (!(pe = getprotobyname("tcp"))) {
440 if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
442 yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
445 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
446 &one, sizeof(one)) < 0)
448 if ((flags = fcntl(sock, F_GETFL, 0)) < 0)
449 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
450 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
451 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
452 if (connect(sock, (struct sockaddr *) proxy_addr, sizeof(*proxy_addr)) < 0)
453 if (errno != EINPROGRESS)
455 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
459 p = xmalloc(sizeof(struct http_proxy));
463 // We will add EVENT_OUTPUT below
464 p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT);
465 iochan_setdata(p->iochan, p);
466 p->iochan->next = channel_list;
467 channel_list = p->iochan;
470 // Modify Host: header
471 for (hp = rq->headers; hp; hp = hp->next)
472 if (!strcmp(hp->name, "Host"))
476 yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
479 hp->value = nmem_strdup(c->nmem, proxy_url);
480 requestbuf = http_serialize_request(rq);
481 http_buf_enqueue(&p->oqueue, requestbuf);
482 iochan_setflag(p->iochan, EVENT_OUTPUT);
486 static void http_io(IOCHAN i, int event)
488 struct http_channel *hc = iochan_getdata(i);
489 struct http_request *request;
490 struct http_response *response;
495 struct http_buf *htbuf;
498 htbuf = http_buf_create();
499 res = read(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1);
500 if (res <= 0 && errno != EAGAIN)
502 http_buf_destroy(htbuf);
508 htbuf->buf[res] = '\0';
510 http_buf_enqueue(&hc->iqueue, htbuf);
513 if ((reqlen = request_check(hc->iqueue)) <= 2)
516 nmem_reset(hc->nmem);
517 if (!(request = http_parse_request(hc, &hc->iqueue, reqlen)))
519 yaz_log(YLOG_WARN, "Failed to parse request");
523 yaz_log(YLOG_LOG, "Request: %s %s v %s", request->method, request->path,
524 request->http_version);
525 if (http_weshouldproxy(request))
530 // Execute our business logic!
531 response = http_command(request);
537 if (!(hb = http_serialize_response(hc, response)))
542 http_buf_enqueue(&hc->oqueue, hb);
543 iochan_setflags(i, EVENT_OUTPUT); // Turns off input selecting
547 yaz_log(YLOG_DEBUG, "We think we have more input to read. Forcing event");
548 iochan_setevent(i, EVENT_INPUT);
556 struct http_buf *wb = hc->oqueue;
557 res = write(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len);
560 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
566 hc->oqueue = hc->oqueue->next;
567 http_buf_destroy(wb);
575 if (!strcmp(hc->version, "1.0"))
581 iochan_setflags(i, EVENT_INPUT); // Turns off output flag
585 if (!hc->oqueue && hc->proxy && !hc->proxy->iochan)
586 http_destroy(i); // Server closed; we're done
589 yaz_log(YLOG_WARN, "Unexpected event on connection");
594 // Handles I/O on a client connection to a backend web server (proxy mode)
595 static void proxy_io(IOCHAN pi, int event)
597 struct http_proxy *pc = iochan_getdata(pi);
598 struct http_channel *hc = pc->channel;
603 struct http_buf *htbuf;
606 htbuf = http_buf_create();
607 res = read(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1);
608 if (res == 0 || (res < 0 && errno != EINPROGRESS))
612 yaz_log(YLOG_WARN, "Proxy read came up short");
613 // Close channel and alert client HTTP channel that we're gone
614 http_buf_destroy(htbuf);
615 close(iochan_getfd(pi));
621 http_destroy(hc->iochan);
627 htbuf->buf[res] = '\0';
629 http_buf_enqueue(&hc->oqueue, htbuf);
631 iochan_setflag(hc->iochan, EVENT_OUTPUT);
634 if (!(htbuf = pc->oqueue))
636 iochan_clearflag(pi, EVENT_OUTPUT);
639 res = write(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len);
642 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
643 http_destroy(hc->iochan);
646 if (res == htbuf->len)
648 struct http_buf *np = htbuf->next;
649 http_buf_destroy(htbuf);
655 htbuf->offset += res;
659 iochan_setflags(pi, EVENT_INPUT); // Turns off output flag
663 yaz_log(YLOG_WARN, "Unexpected event on connection");
664 http_destroy(hc->iochan);
668 /* Accept a new command connection */
669 static void http_accept(IOCHAN i, int event)
671 struct sockaddr_in addr;
672 int fd = iochan_getfd(i);
677 struct http_channel *ch;
680 if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
682 yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
685 if ((flags = fcntl(s, F_GETFL, 0)) < 0)
686 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
687 if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0)
688 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
690 yaz_log(YLOG_LOG, "New command connection");
691 c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT);
693 ch = xmalloc(sizeof(*ch));
695 ch->nmem = nmem_create();
696 ch->wrbuf = wrbuf_alloc();
698 ch->iqueue = ch->oqueue = 0;
699 iochan_setdata(c, ch);
701 c->next = channel_list;
705 /* Create a http-channel listener */
706 void http_init(int port)
711 struct sockaddr_in myaddr;
714 yaz_log(YLOG_LOG, "HTTP port is %d", port);
715 if (!(p = getprotobyname("tcp"))) {
718 if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
719 yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
720 if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
721 &one, sizeof(one)) < 0)
724 bzero(&myaddr, sizeof myaddr);
725 myaddr.sin_family = AF_INET;
726 myaddr.sin_addr.s_addr = INADDR_ANY;
727 myaddr.sin_port = htons(port);
728 if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0)
729 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
730 if (listen(l, SOMAXCONN) < 0)
731 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
733 c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT);
734 c->next = channel_list;
738 void http_set_proxyaddr(char *host)
744 strcpy(proxy_url, host);
745 p = strchr(host, ':');
746 yaz_log(YLOG_DEBUG, "Proxying for %s", host);
753 if (!(he = gethostbyname(host)))
755 fprintf(stderr, "Failed to lookup '%s'\n", host);
758 proxy_addr = xmalloc(sizeof(struct sockaddr_in));
759 proxy_addr->sin_family = he->h_addrtype;
760 memcpy(&proxy_addr->sin_addr.s_addr, he->h_addr_list[0], he->h_length);
761 proxy_addr->sin_port = htons(port);
767 * indent-tabs-mode: nil
769 * vim: shiftwidth=4 tabstop=8 expandtab