Handling growing free http buffers and channels
[pazpar2-moved-to-github.git] / src / http.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2011 Index Data
3
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #ifdef WIN32
26 #include <winsock.h>
27 typedef int socklen_t;
28 #endif
29
30 #if HAVE_SYS_SOCKET_H
31 #include <sys/socket.h>
32 #endif
33
34 #include <sys/types.h>
35
36 #include <yaz/snprintf.h>
37 #if HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <fcntl.h>
45 #if HAVE_NETDB_H
46 #include <netdb.h>
47 #endif
48
49 #include <errno.h>
50 #include <assert.h>
51 #include <string.h>
52
53 #if HAVE_NETINET_IN_H
54 #include <netinet/in.h>
55 #endif
56
57 #if HAVE_ARPA_INET_H
58 #include <arpa/inet.h>
59 #endif
60
61 #include <yaz/yaz-util.h>
62 #include <yaz/comstack.h>
63 #include <yaz/nmem.h>
64 #include <yaz/mutex.h>
65
66 #include "ppmutex.h"
67 #include "session.h"
68 #include "http.h"
69
70 #define MAX_HTTP_HEADER 4096
71
72 #ifdef WIN32
73 #define strncasecmp _strnicmp
74 #define strcasecmp _stricmp
75 #endif
76
77 struct http_buf
78 {
79 #define HTTP_BUF_SIZE 4096
80     char buf[4096];
81     int offset;
82     int len;
83     struct http_buf *next;
84 };
85
86
87 static void proxy_io(IOCHAN i, int event);
88 static struct http_channel *http_channel_create(http_server_t http_server,
89                                                 const char *addr,
90                                                 struct conf_server *server);
91 static void http_channel_destroy(IOCHAN i);
92 static http_server_t http_server_create(void);
93 static void http_server_incref(http_server_t hs);
94
95 struct http_server
96 {
97     struct http_buf *http_buf_freelist;
98     int http_buf_freelist_count;
99     int http_buf_freelist_max;
100
101     struct http_channel *http_channel_freelist;
102     int http_channel_freelist_count;
103     int http_channel_freelist_max;
104     YAZ_MUTEX mutex;
105     int listener_socket;
106     int ref_count;
107     http_sessions_t http_sessions;
108     struct sockaddr_in *proxy_addr;
109 };
110
111 struct http_channel_observer_s {
112     void *data;
113     void *data2;
114     http_channel_destroy_t destroy;
115     struct http_channel_observer_s *next;
116     struct http_channel *chan;
117 };
118
119
120 const char *http_lookup_header(struct http_header *header,
121                                const char *name)
122 {
123     for (; header; header = header->next)
124         if (!strcasecmp(name, header->name))
125             return header->value;
126     return 0;
127 }
128
129 static struct http_buf *http_buf_create(http_server_t hs)
130 {
131     struct http_buf *r = 0;
132
133     yaz_mutex_enter(hs->mutex);
134     if (hs->http_buf_freelist)
135     {
136         r = hs->http_buf_freelist;
137         hs->http_buf_freelist = hs->http_buf_freelist->next;
138         hs->http_buf_freelist_count--;
139     }
140     yaz_mutex_leave(hs->mutex);
141     if (!r)
142         r = xmalloc(sizeof(struct http_buf));
143     r->offset = 0;
144     r->len = 0;
145     r->next = 0;
146     return r;
147 }
148
149 static void http_buf_destroy(http_server_t hs, struct http_buf *b)
150 {
151     yaz_mutex_enter(hs->mutex);
152     if (hs->http_buf_freelist_max > 0 && hs->http_buf_freelist_count > hs->http_buf_freelist_max) {
153         xfree(b);
154         while (b = hs->http_buf_freelist) {
155             xfree(b);
156             hs->http_buf_freelist = hs->http_buf_freelist->next;
157         }
158     }
159     else {
160         b->next = hs->http_buf_freelist;
161         hs->http_buf_freelist = b;
162         hs->http_buf_freelist_count++;
163     }
164     yaz_mutex_leave(hs->mutex);
165 }
166
167 static void http_buf_destroy_queue(http_server_t hs, struct http_buf *b)
168 {
169     struct http_buf *p;
170     while (b)
171     {
172         p = b->next;
173         http_buf_destroy(hs, b);
174         b = p;
175     }
176 }
177
178 static struct http_buf *http_buf_bybuf(http_server_t hs, char *b, int len)
179 {
180     struct http_buf *res = 0;
181     struct http_buf **p = &res;
182
183     while (len)
184     {
185         int tocopy = len;
186         if (tocopy > HTTP_BUF_SIZE)
187             tocopy = HTTP_BUF_SIZE;
188         *p = http_buf_create(hs);
189         memcpy((*p)->buf, b, tocopy);
190         (*p)->len = tocopy;
191         len -= tocopy;
192         b += tocopy;
193         p = &(*p)->next;
194     }
195     return res;
196 }
197
198 // Add a (chain of) buffers to the end of an existing queue.
199 static void http_buf_enqueue(struct http_buf **queue, struct http_buf *b)
200 {
201     while (*queue)
202         queue = &(*queue)->next;
203     *queue = b;
204 }
205
206 static struct http_buf *http_buf_bywrbuf(http_server_t hs, WRBUF wrbuf)
207 {
208     // Heavens to Betsy (buf)!
209     return http_buf_bybuf(hs, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
210 }
211
212 // Non-destructively collapse chain of buffers into a string (max *len)
213 // Return
214 static void http_buf_peek(struct http_buf *b, char *buf, int len)
215 {
216     int rd = 0;
217     while (b && rd < len)
218     {
219         int toread = len - rd;
220         if (toread > b->len)
221             toread = b->len;
222         memcpy(buf + rd, b->buf + b->offset, toread);
223         rd += toread;
224         b = b->next;
225     }
226     buf[rd] = '\0';
227 }
228
229 static int http_buf_size(struct http_buf *b)
230 {
231     int sz = 0;
232     for (; b; b = b->next)
233         sz += b->len;
234     return sz;
235 }
236
237 // Ddestructively munch up to len  from head of queue.
238 static int http_buf_read(http_server_t hs,
239                          struct http_buf **b, char *buf, int len)
240 {
241     int rd = 0;
242     while ((*b) && rd < len)
243     {
244         int toread = len - rd;
245         if (toread > (*b)->len)
246             toread = (*b)->len;
247         memcpy(buf + rd, (*b)->buf + (*b)->offset, toread);
248         rd += toread;
249         if (toread < (*b)->len)
250         {
251             (*b)->len -= toread;
252             (*b)->offset += toread;
253             break;
254         }
255         else
256         {
257             struct http_buf *n = (*b)->next;
258             http_buf_destroy(hs, *b);
259             *b = n;
260         }
261     }
262     buf[rd] = '\0';
263     return rd;
264 }
265
266 // Buffers may overlap.
267 static void urldecode(char *i, char *o)
268 {
269     while (*i)
270     {
271         if (*i == '+')
272         {
273             *(o++) = ' ';
274             i++;
275         }
276         else if (*i == '%' && i[1] && i[2])
277         {
278             int v;
279             i++;
280             sscanf(i, "%2x", &v);
281             *o++ = v;
282             i += 2;
283         }
284         else
285             *(o++) = *(i++);
286     }
287     *o = '\0';
288 }
289
290 // Warning: Buffers may not overlap
291 void urlencode(const char *i, char *o)
292 {
293     while (*i)
294     {
295         if (strchr(" /:", *i))
296         {
297             sprintf(o, "%%%.2X", (int) *i);
298             o += 3;
299         }
300         else
301             *(o++) = *i;
302         i++;
303     }
304     *o = '\0';
305 }
306
307 void http_addheader(struct http_response *r, const char *name, const char *value)
308 {
309     struct http_channel *c = r->channel;
310     struct http_header *h = nmem_malloc(c->nmem, sizeof *h);
311     h->name = nmem_strdup(c->nmem, name);
312     h->value = nmem_strdup(c->nmem, value);
313     h->next = r->headers;
314     r->headers = h;
315 }
316
317 const char *http_argbyname(struct http_request *r, const char *name)
318 {
319     struct http_argument *p;
320     if (!name)
321         return 0;
322     for (p = r->arguments; p; p = p->next)
323         if (!strcmp(p->name, name))
324             return p->value;
325     return 0;
326 }
327
328 const char *http_headerbyname(struct http_header *h, const char *name)
329 {
330     for (; h; h = h->next)
331         if (!strcmp(h->name, name))
332             return h->value;
333     return 0;
334 }
335
336 struct http_response *http_create_response(struct http_channel *c)
337 {
338     struct http_response *r = nmem_malloc(c->nmem, sizeof(*r));
339     strcpy(r->code, "200");
340     r->msg = "OK";
341     r->channel = c;
342     r->headers = 0;
343     r->payload = 0;
344     r->content_type = "text/xml";
345     return r;
346 }
347
348
349 static const char *next_crlf(const char *cp, size_t *skipped)
350 {
351     const char *next_cp = strchr(cp, '\n');
352     if (next_cp)
353     {
354         if (next_cp > cp && next_cp[-1] == '\r')
355             *skipped = next_cp - cp - 1;
356         else
357             *skipped = next_cp - cp;
358         next_cp++;
359     }
360     return next_cp;
361 }
362
363 // Check if buf contains a package (minus payload)
364 static int package_check(const char *buf, int sz)
365 {
366     int content_len = 0;
367     int len = 0;
368
369     while (*buf)
370     {
371         size_t skipped = 0;
372         const char *b = next_crlf(buf, &skipped);
373
374         if (!b)
375         {
376             // we did not find CRLF.. See if buffer is too large..
377             if (sz >= MAX_HTTP_HEADER-1)
378                 return MAX_HTTP_HEADER-1; // yes. Return that (will fail later)
379             break;
380         }
381         len += (b - buf);
382         if (skipped == 0)
383         {
384             // CRLF CRLF , i.e. end of header
385             if (len + content_len <= sz)
386                 return len + content_len;
387             break;
388         }
389         buf = b;
390         // following first skip of \r\n so that we don't consider Method
391         if (!strncasecmp(buf, "Content-Length:", 15))
392         {
393             const char *cp = buf+15;
394             while (*cp == ' ')
395                 cp++;
396             content_len = 0;
397             while (*cp && isdigit(*(const unsigned char *)cp))
398                 content_len = content_len*10 + (*cp++ - '0');
399             if (content_len < 0) /* prevent negative offsets */
400                 content_len = 0;
401         }
402     }
403     return 0;     // incomplete request
404 }
405
406 // Check if we have a request. Return 0 or length
407 static int request_check(struct http_buf *queue)
408 {
409     char tmp[MAX_HTTP_HEADER];
410
411     // only peek at the header..
412     http_buf_peek(queue, tmp, MAX_HTTP_HEADER-1);
413     // still we only return non-zero if the complete request is received..
414     return package_check(tmp, http_buf_size(queue));
415 }
416
417 struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len)
418 {
419     char tmp[MAX_HTTP_HEADER];
420     struct http_response *r = http_create_response(c);
421     char *p, *p2;
422     struct http_header **hp = &r->headers;
423
424     if (len >= MAX_HTTP_HEADER)
425         return 0;
426     memcpy(tmp, buf, len);
427     for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version
428         ;
429     p++;
430     // Response code
431     for (p2 = p; *p2 && *p2 != ' ' && p2 - p < 3; p2++)
432         r->code[p2 - p] = *p2;
433     if (!(p = strstr(tmp, "\r\n")))
434         return 0;
435     p += 2;
436     while (*p)
437     {
438         if (!(p2 = strstr(p, "\r\n")))
439             return 0;
440         if (p == p2) // End of headers
441             break;
442         else
443         {
444             struct http_header *h = *hp = nmem_malloc(c->nmem, sizeof(*h));
445             char *value = strchr(p, ':');
446             if (!value)
447                 return 0;
448             *(value++) = '\0';
449             h->name = nmem_strdup(c->nmem, p);
450             while (isspace(*(const unsigned char *) value))
451                 value++;
452             if (value >= p2)  // Empty header;
453             {
454                 h->value = "";
455                 p = p2 + 2;
456                 continue;
457             }
458             *p2 = '\0';
459             h->value = nmem_strdup(c->nmem, value);
460             h->next = 0;
461             hp = &h->next;
462             p = p2 + 2;
463         }
464     }
465     return r;
466 }
467
468 static int http_parse_arguments(struct http_request *r, NMEM nmem,
469                                 const char *args)
470 {
471     const char *p2 = args;
472
473     while (*p2)
474     {
475         struct http_argument *a;
476         const char *equal = strchr(p2, '=');
477         const char *eoa = strchr(p2, '&');
478         if (!equal)
479         {
480             yaz_log(YLOG_WARN, "Expected '=' in argument");
481             return -1;
482         }
483         if (!eoa)
484             eoa = equal + strlen(equal); // last argument
485         else if (equal > eoa)
486         {
487             yaz_log(YLOG_WARN, "Missing '&' in argument");
488             return -1;
489         }
490         a = nmem_malloc(nmem, sizeof(struct http_argument));
491         a->name = nmem_strdupn(nmem, p2, equal - p2);
492         a->value = nmem_strdupn(nmem, equal+1, eoa - equal - 1);
493         urldecode(a->name, a->name);
494         urldecode(a->value, a->value);
495         a->next = r->arguments;
496         r->arguments = a;
497         p2 = eoa;
498         while (*p2 == '&')
499             p2++;
500     }
501     return 0;
502 }
503
504 struct http_request *http_parse_request(struct http_channel *c,
505                                         struct http_buf **queue,
506                                         int len)
507 {
508     struct http_request *r = nmem_malloc(c->nmem, sizeof(*r));
509     char *p, *p2;
510     char *start = nmem_malloc(c->nmem, len+1);
511     char *buf = start;
512
513     if (http_buf_read(c->http_server, queue, buf, len) < len)
514     {
515         yaz_log(YLOG_WARN, "http_buf_read < len (%d)", len);
516         return 0;
517     }
518     r->search = "";
519     r->channel = c;
520     r->arguments = 0;
521     r->headers = 0;
522     r->content_buf = 0;
523     r->content_len = 0;
524     // Parse first line
525     for (p = buf, p2 = r->method; *p && *p != ' ' && p - buf < 19; p++)
526         *(p2++) = *p;
527     if (*p != ' ')
528     {
529         yaz_log(YLOG_WARN, "Unexpected HTTP method in request");
530         return 0;
531     }
532     *p2 = '\0';
533
534     if (!(buf = strchr(buf, ' ')))
535     {
536         yaz_log(YLOG_WARN, "Missing Request-URI in HTTP request");
537         return 0;
538     }
539     buf++;
540     if (!(p = strchr(buf, ' ')))
541     {
542         yaz_log(YLOG_WARN, "HTTP Request-URI not terminated (too long?)");
543         return 0;
544     }
545     *(p++) = '\0';
546     if ((p2 = strchr(buf, '?'))) // Do we have arguments?
547         *(p2++) = '\0';
548     r->path = nmem_strdup(c->nmem, buf);
549     if (p2)
550     {
551         r->search = nmem_strdup(c->nmem, p2);
552         // Parse Arguments
553         http_parse_arguments(r, c->nmem, p2);
554     }
555     buf = p;
556
557     if (strncmp(buf, "HTTP/", 5))
558         strcpy(r->http_version, "1.0");
559     else
560     {
561         size_t skipped;
562         buf += 5; // strlen("HTTP/")
563
564         p = (char*) next_crlf(buf, &skipped);
565         if (!p || skipped < 3 || skipped > 5)
566             return 0;
567
568         memcpy(r->http_version, buf, skipped);
569         r->http_version[skipped] = '\0';
570         buf = p;
571     }
572     strcpy(c->version, r->http_version);
573
574     r->headers = 0;
575     while (*buf)
576     {
577         size_t skipped;
578
579         p = (char *) next_crlf(buf, &skipped);
580         if (!p)
581         {
582             return 0;
583         }
584         else if (skipped == 0)
585         {
586             buf = p;
587             break;
588         }
589         else
590         {
591             char *cp;
592             char *n_v = nmem_malloc(c->nmem, skipped+1);
593             struct http_header *h = nmem_malloc(c->nmem, sizeof(*h));
594
595             memcpy(n_v, buf, skipped);
596             n_v[skipped] = '\0';
597
598             if (!(cp = strchr(n_v, ':')))
599                 return 0;
600             h->name = nmem_strdupn(c->nmem, n_v, cp - n_v);
601             cp++;
602             while (isspace(*cp))
603                 cp++;
604             h->value = nmem_strdup(c->nmem, cp);
605             h->next = r->headers;
606             r->headers = h;
607             buf = p;
608         }
609     }
610
611     // determine if we do keep alive
612     if (!strcmp(c->version, "1.0"))
613     {
614         const char *v = http_lookup_header(r->headers, "Connection");
615         if (v && !strcmp(v, "Keep-Alive"))
616             c->keep_alive = 1;
617         else
618             c->keep_alive = 0;
619     }
620     else
621     {
622         const char *v = http_lookup_header(r->headers, "Connection");
623         if (v && !strcmp(v, "close"))
624             c->keep_alive = 0;
625         else
626             c->keep_alive = 1;
627     }
628     if (buf < start + len)
629     {
630         const char *content_type = http_lookup_header(r->headers,
631                                                       "Content-Type");
632         r->content_len = start + len - buf;
633         r->content_buf = buf;
634
635         if (!yaz_strcmp_del("application/x-www-form-urlencoded",
636                             content_type, "; "))
637         {
638             http_parse_arguments(r, c->nmem, r->content_buf);
639         }
640     }
641     return r;
642 }
643
644 static struct http_buf *http_serialize_response(struct http_channel *c,
645         struct http_response *r)
646 {
647     struct http_header *h;
648
649     wrbuf_rewind(c->wrbuf);
650     wrbuf_printf(c->wrbuf, "HTTP/%s %s %s\r\n", c->version, r->code, r->msg);
651     for (h = r->headers; h; h = h->next)
652         wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
653     if (r->payload)
654     {
655         wrbuf_printf(c->wrbuf, "Content-Length: %d\r\n", r->payload ?
656                 (int) strlen(r->payload) : 0);
657         wrbuf_printf(c->wrbuf, "Content-Type: %s\r\n", r->content_type);
658         if (!strcmp(r->content_type, "text/xml"))
659         {
660             xmlDoc *doc = xmlParseMemory(r->payload, strlen(r->payload));
661             if (doc)
662             {
663                 xmlFreeDoc(doc);
664             }
665             else
666             {
667                 yaz_log(YLOG_WARN, "Sending non-wellformed "
668                         "response (bug #1162");
669                 yaz_log(YLOG_WARN, "payload: %s", r->payload);
670             }
671         }
672     }
673     wrbuf_puts(c->wrbuf, "\r\n");
674
675     if (r->payload)
676         wrbuf_puts(c->wrbuf, r->payload);
677
678     return http_buf_bywrbuf(c->http_server, c->wrbuf);
679 }
680
681 // Serialize a HTTP request
682 static struct http_buf *http_serialize_request(struct http_request *r)
683 {
684     struct http_channel *c = r->channel;
685     struct http_header *h;
686
687     wrbuf_rewind(c->wrbuf);
688     wrbuf_printf(c->wrbuf, "%s %s%s%s", r->method, r->path,
689                  *r->search ? "?" : "", r->search);
690
691     wrbuf_printf(c->wrbuf, " HTTP/%s\r\n", r->http_version);
692
693     for (h = r->headers; h; h = h->next)
694         wrbuf_printf(c->wrbuf, "%s: %s\r\n", h->name, h->value);
695
696     wrbuf_puts(c->wrbuf, "\r\n");
697
698     if (r->content_buf)
699         wrbuf_write(c->wrbuf, r->content_buf, r->content_len);
700
701 #if 0
702     yaz_log(YLOG_LOG, "WRITING TO PROXY:\n%s\n----",
703             wrbuf_cstr(c->wrbuf));
704 #endif
705     return http_buf_bywrbuf(c->http_server, c->wrbuf);
706 }
707
708
709 static int http_weshouldproxy(struct http_request *rq)
710 {
711     struct http_channel *c = rq->channel;
712     if (c->server->http_server->proxy_addr && !strstr(rq->path, "search.pz2"))
713         return 1;
714     return 0;
715 }
716
717
718 struct http_header * http_header_append(struct http_channel *ch, 
719                                         struct http_header * hp, 
720                                         const char *name, 
721                                         const char *value)
722 {
723     struct http_header *hpnew = 0; 
724
725     if (!hp | !ch)
726         return 0;
727
728     while (hp && hp->next)
729         hp = hp->next;
730
731     if(name && strlen(name)&& value && strlen(value)){
732         hpnew = nmem_malloc(ch->nmem, sizeof *hpnew);
733         hpnew->name = nmem_strdup(ch->nmem, name);
734         hpnew->value = nmem_strdup(ch->nmem, value);
735         
736         hpnew->next = 0;
737         hp->next = hpnew;
738         hp = hp->next;
739         
740         return hpnew;
741     }
742
743     return hp;
744 }
745
746    
747 static int is_inprogress(void)
748 {
749 #ifdef WIN32
750     if (WSAGetLastError() == WSAEWOULDBLOCK)
751         return 1;
752 #else
753     if (errno == EINPROGRESS)
754         return 1;
755 #endif
756     return 0;
757
758
759 static void enable_nonblock(int sock)
760 {
761     int flags;
762 #ifdef WIN32
763     flags = (flags & CS_FLAGS_BLOCKING) ? 0 : 1;
764     if (ioctlsocket(sock, FIONBIO, &flags) < 0)
765         yaz_log(YLOG_FATAL|YLOG_ERRNO, "ioctlsocket");
766 #else
767     if ((flags = fcntl(sock, F_GETFL, 0)) < 0) 
768         yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl");
769     if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
770         yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2");
771 #endif
772 }
773
774 static int http_proxy(struct http_request *rq)
775 {
776     struct http_channel *c = rq->channel;
777     struct http_proxy *p = c->proxy;
778     struct http_header *hp;
779     struct http_buf *requestbuf;
780     char server_port[16] = "";
781     struct conf_server *ser = c->server;
782
783     if (!p) // This is a new connection. Create a proxy channel
784     {
785         int sock;
786         struct protoent *pe;
787         int one = 1;
788
789         if (!(pe = getprotobyname("tcp"))) {
790             abort();
791         }
792         if ((sock = socket(PF_INET, SOCK_STREAM, pe->p_proto)) < 0)
793         {
794             yaz_log(YLOG_WARN|YLOG_ERRNO, "socket");
795             return -1;
796         }
797         if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)
798                         &one, sizeof(one)) < 0)
799             abort();
800         enable_nonblock(sock);
801         if (connect(sock, (struct sockaddr *)
802                     c->server->http_server->proxy_addr, 
803                     sizeof(*c->server->http_server->proxy_addr)) < 0)
804         {
805             if (!is_inprogress()) 
806             {
807                 yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect");
808                 return -1;
809             }
810         }
811         p = xmalloc(sizeof(struct http_proxy));
812         p->oqueue = 0;
813         p->channel = c;
814         p->first_response = 1;
815         c->proxy = p;
816         // We will add EVENT_OUTPUT below
817         p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT, "http_proxy");
818         iochan_setdata(p->iochan, p);
819
820         iochan_add(ser->iochan_man, p->iochan);
821     }
822
823     // Do _not_ modify Host: header, just checking it's existence
824
825     if (!http_lookup_header(rq->headers, "Host"))
826     {
827         yaz_log(YLOG_WARN, "Failed to find Host header in proxy");
828         return -1;
829     }
830     
831     // Add new header about paraz2 version, host, remote client address, etc.
832     {
833         char server_via[128];
834
835         hp = rq->headers;
836         hp = http_header_append(c, hp, 
837                                 "X-Pazpar2-Version", PACKAGE_VERSION);
838         hp = http_header_append(c, hp, 
839                                 "X-Pazpar2-Server-Host", ser->host);
840         sprintf(server_port, "%d",  ser->port);
841         hp = http_header_append(c, hp, 
842                                 "X-Pazpar2-Server-Port", server_port);
843         yaz_snprintf(server_via, sizeof(server_via), 
844                      "1.1 %s:%s (%s/%s)",  
845                      ser->host ? ser->host : "@",
846                      server_port, PACKAGE_NAME, PACKAGE_VERSION);
847         hp = http_header_append(c, hp, "Via" , server_via);
848         hp = http_header_append(c, hp, "X-Forwarded-For", c->addr);
849     }
850     
851     requestbuf = http_serialize_request(rq);
852
853     http_buf_enqueue(&p->oqueue, requestbuf);
854     iochan_setflag(p->iochan, EVENT_OUTPUT);
855     return 0;
856 }
857
858 void http_send_response(struct http_channel *ch)
859 {
860     struct http_response *rs = ch->response;
861     struct http_buf *hb;
862
863     assert(rs);
864     hb = http_serialize_response(ch, rs);
865     if (!hb)
866     {
867         yaz_log(YLOG_WARN, "Failed to serialize HTTP response");
868         http_channel_destroy(ch->iochan);
869     }
870     else
871     {
872         http_buf_enqueue(&ch->oqueue, hb);
873         iochan_setflag(ch->iochan, EVENT_OUTPUT);
874         ch->state = Http_Idle;
875     }
876 }
877
878 static void http_error(struct http_channel *hc, int no, const char *msg)
879 {
880     struct http_response *rs = http_create_response(hc);
881
882     hc->response = rs;
883     hc->keep_alive = 0;  // not keeping this HTTP session alive
884
885     sprintf(rs->code, "%d", no);
886
887     rs->msg = nmem_strdup(hc->nmem, msg);
888     rs->payload = nmem_malloc(hc->nmem, 100);
889     yaz_snprintf(rs->payload, 99, "<error>HTTP Error %d: %s</error>\n",
890                  no, msg);
891     http_send_response(hc);
892 }
893
894 static void http_io(IOCHAN i, int event)
895 {
896     struct http_channel *hc = iochan_getdata(i);
897     while (event)
898     {
899         if (event == EVENT_INPUT)
900         {
901             int res, reqlen;
902             struct http_buf *htbuf;
903             
904             htbuf = http_buf_create(hc->http_server);
905             res = recv(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1, 0);
906             if (res == -1 && errno == EAGAIN)
907             {
908                 http_buf_destroy(hc->http_server, htbuf);
909                 return;
910             }
911             if (res <= 0)
912             {
913                 http_buf_destroy(hc->http_server, htbuf);
914                 http_channel_destroy(i);
915                 return;
916             }
917             htbuf->buf[res] = '\0';
918             htbuf->len = res;
919             http_buf_enqueue(&hc->iqueue, htbuf);
920
921             while (1)
922             {
923                 if (hc->state == Http_Busy)
924                     return;
925                 reqlen = request_check(hc->iqueue);
926                 if (reqlen <= 2)
927                     return;
928                 // we have a complete HTTP request
929                 nmem_reset(hc->nmem);
930                 if (!(hc->request = http_parse_request(hc, &hc->iqueue, reqlen)))
931                 {
932                     yaz_log(YLOG_WARN, "Failed to parse request");
933                     http_error(hc, 400, "Bad Request");
934                     return;
935                 }
936                 hc->response = 0;
937                 yaz_log(YLOG_LOG, "Request: %s %s%s%s", hc->request->method,
938                         hc->request->path,
939                         *hc->request->search ? "?" : "",
940                         hc->request->search);
941                 if (hc->request->content_buf)
942                     yaz_log(YLOG_LOG, "%s", hc->request->content_buf);
943                 if (http_weshouldproxy(hc->request))
944                     http_proxy(hc->request);
945                 else
946                 {
947                     // Execute our business logic!
948                     hc->state = Http_Busy;
949                     http_command(hc);
950                 }
951             }
952         }
953         else if (event == EVENT_OUTPUT)
954         {
955             event = 0;
956             if (hc->oqueue)
957             {
958                 struct http_buf *wb = hc->oqueue;
959                 int res;
960                 res = send(iochan_getfd(hc->iochan),
961                            wb->buf + wb->offset, wb->len, 0);
962                 if (res <= 0)
963                 {
964                     yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
965                     http_channel_destroy(i);
966                     return;
967                 }
968                 if (res == wb->len)
969                 {
970                     hc->oqueue = hc->oqueue->next;
971                     http_buf_destroy(hc->http_server, wb);
972                 }
973                 else
974                 {
975                     wb->len -= res;
976                     wb->offset += res;
977                 }
978                 if (!hc->oqueue)
979                 {
980                     if (!hc->keep_alive)
981                     {
982                         http_channel_destroy(i);
983                         return;
984                     }
985                     else
986                     {
987                         iochan_clearflag(i, EVENT_OUTPUT);
988                         if (hc->iqueue)
989                             event = EVENT_INPUT;
990                     }
991                 }
992             }
993             if (!hc->oqueue && hc->proxy && !hc->proxy->iochan) 
994                 http_channel_destroy(i); // Server closed; we're done
995         }
996         else
997         {
998             yaz_log(YLOG_WARN, "Unexpected event on connection");
999             http_channel_destroy(i);
1000             event = 0;
1001         }
1002     }
1003 }
1004
1005 // Handles I/O on a client connection to a backend web server (proxy mode)
1006 static void proxy_io(IOCHAN pi, int event)
1007 {
1008     struct http_proxy *pc = iochan_getdata(pi);
1009     struct http_channel *hc = pc->channel;
1010
1011     switch (event)
1012     {
1013         int res;
1014         struct http_buf *htbuf;
1015
1016         case EVENT_INPUT:
1017             htbuf = http_buf_create(hc->http_server);
1018             res = recv(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1, 0);
1019             if (res == 0 || (res < 0 && !is_inprogress()))
1020             {
1021                 if (hc->oqueue)
1022                 {
1023                     yaz_log(YLOG_WARN, "Proxy read came up short");
1024                     // Close channel and alert client HTTP channel that we're gone
1025                     http_buf_destroy(hc->http_server, htbuf);
1026 #ifdef WIN32
1027                     closesocket(iochan_getfd(pi));
1028 #else
1029                     close(iochan_getfd(pi));
1030 #endif
1031                     iochan_destroy(pi);
1032                     pc->iochan = 0;
1033                 }
1034                 else
1035                 {
1036                     http_channel_destroy(hc->iochan);
1037                     return;
1038                 }
1039             }
1040             else
1041             {
1042                 htbuf->buf[res] = '\0';
1043                 htbuf->offset = 0;
1044                 htbuf->len = res;
1045                 // Write any remaining payload
1046                 if (htbuf->len - htbuf->offset > 0)
1047                     http_buf_enqueue(&hc->oqueue, htbuf);
1048             }
1049             iochan_setflag(hc->iochan, EVENT_OUTPUT);
1050             break;
1051         case EVENT_OUTPUT:
1052             if (!(htbuf = pc->oqueue))
1053             {
1054                 iochan_clearflag(pi, EVENT_OUTPUT);
1055                 return;
1056             }
1057             res = send(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len, 0);
1058             if (res <= 0)
1059             {
1060                 yaz_log(YLOG_WARN|YLOG_ERRNO, "write");
1061                 http_channel_destroy(hc->iochan);
1062                 return;
1063             }
1064             if (res == htbuf->len)
1065             { 
1066                 struct http_buf *np = htbuf->next;
1067                 http_buf_destroy(hc->http_server, htbuf);
1068                 pc->oqueue = np;
1069             }
1070             else
1071             {
1072                 htbuf->len -= res;
1073                 htbuf->offset += res;
1074             }
1075
1076             if (!pc->oqueue) {
1077                 iochan_setflags(pi, EVENT_INPUT); // Turns off output flag
1078             }
1079             break;
1080         default:
1081             yaz_log(YLOG_WARN, "Unexpected event on connection");
1082             http_channel_destroy(hc->iochan);
1083     }
1084 }
1085
1086 static void http_fire_observers(struct http_channel *c);
1087 static void http_destroy_observers(struct http_channel *c);
1088
1089 // Cleanup channel
1090 static void http_channel_destroy(IOCHAN i)
1091 {
1092     struct http_channel *s = iochan_getdata(i);
1093     http_server_t http_server;
1094
1095     if (s->proxy)
1096     {
1097         if (s->proxy->iochan)
1098         {
1099 #ifdef WIN32
1100             closesocket(iochan_getfd(s->proxy->iochan));
1101 #else
1102             close(iochan_getfd(s->proxy->iochan));
1103 #endif
1104             iochan_destroy(s->proxy->iochan);
1105         }
1106         http_buf_destroy_queue(s->http_server, s->proxy->oqueue);
1107         xfree(s->proxy);
1108     }
1109     http_buf_destroy_queue(s->http_server, s->iqueue);
1110     http_buf_destroy_queue(s->http_server, s->oqueue);
1111     http_fire_observers(s);
1112     http_destroy_observers(s);
1113
1114     http_server = s->http_server; /* save it for destroy (decref) */
1115
1116     yaz_mutex_enter(s->http_server->mutex);
1117     if (s->http_server->http_channel_freelist_max > 0 && s->http_server->http_channel_freelist_count > s->http_server->http_channel_freelist_max) {
1118         while (s->next = s->http_server->http_channel_freelist) {
1119             xfree(s->next);
1120             s->http_server->http_channel_freelist = s->http_server->http_channel_freelist->next;
1121         }
1122     }
1123     else {
1124         s->next = s->http_server->http_channel_freelist;
1125         s->http_server->http_channel_freelist = s;
1126     }
1127     yaz_mutex_leave(s->http_server->mutex);
1128
1129     http_server_destroy(http_server);
1130
1131 #ifdef WIN32
1132     closesocket(iochan_getfd(i));
1133 #else
1134     close(iochan_getfd(i));
1135 #endif
1136     iochan_destroy(i);
1137 }
1138
1139 static struct http_channel *http_channel_create(http_server_t hs,
1140                                                 const char *addr,
1141                                                 struct conf_server *server)
1142 {
1143     struct http_channel *r;
1144
1145     yaz_mutex_enter(hs->mutex);
1146     r = hs->http_channel_freelist;
1147     if (r) {
1148         hs->http_channel_freelist = r->next;
1149         hs->http_channel_freelist_count++;
1150     }
1151     yaz_mutex_leave(hs->mutex);
1152
1153     if (r)
1154     {
1155         nmem_reset(r->nmem);
1156         wrbuf_rewind(r->wrbuf);
1157     }
1158     else
1159     {
1160         r = xmalloc(sizeof(struct http_channel));
1161         r->nmem = nmem_create();
1162         r->wrbuf = wrbuf_alloc();
1163     }
1164     http_server_incref(hs);
1165     r->http_server = hs;
1166     r->http_sessions = hs->http_sessions;
1167     assert(r->http_sessions);
1168     r->server = server;
1169     r->proxy = 0;
1170     r->iochan = 0;
1171     r->iqueue = r->oqueue = 0;
1172     r->state = Http_Idle;
1173     r->keep_alive = 0;
1174     r->request = 0;
1175     r->response = 0;
1176     if (!addr)
1177     {
1178         yaz_log(YLOG_WARN, "Invalid HTTP forward address");
1179         exit(1);
1180     }
1181     strcpy(r->addr, addr);
1182     r->observers = 0;
1183     return r;
1184 }
1185
1186
1187 /* Accept a new command connection */
1188 static void http_accept(IOCHAN i, int event)
1189 {
1190     struct sockaddr_in addr;
1191     int fd = iochan_getfd(i);
1192     socklen_t len;
1193     int s;
1194     IOCHAN c;
1195     struct http_channel *ch;
1196     struct conf_server *server = iochan_getdata(i);
1197
1198     len = sizeof addr;
1199     if ((s = accept(fd, (struct sockaddr *) &addr, &len)) < 0)
1200     {
1201         yaz_log(YLOG_WARN|YLOG_ERRNO, "accept");
1202         return;
1203     }
1204     enable_nonblock(s);
1205
1206     yaz_log(YLOG_DEBUG, "New command connection");
1207     c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT, "http_session_socket");
1208     
1209     ch = http_channel_create(server->http_server, inet_ntoa(addr.sin_addr),
1210                              server);
1211     ch->iochan = c;
1212     iochan_setdata(c, ch);
1213     iochan_add(server->iochan_man, c);
1214 }
1215
1216 /* Create a http-channel listener, syntax [host:]port */
1217 int http_init(const char *addr, struct conf_server *server)
1218 {
1219     IOCHAN c;
1220     int l;
1221     struct protoent *p;
1222     struct sockaddr_in myaddr;
1223     int one = 1;
1224     const char *pp;
1225     short port;
1226
1227     yaz_log(YLOG_LOG, "HTTP listener %s", addr);
1228
1229     memset(&myaddr, 0, sizeof myaddr);
1230     myaddr.sin_family = AF_INET;
1231     pp = strchr(addr, ':');
1232     if (pp)
1233     {
1234         WRBUF w = wrbuf_alloc();
1235         struct hostent *he;
1236
1237         wrbuf_write(w, addr, pp - addr);
1238         wrbuf_puts(w, "");
1239
1240         he = gethostbyname(wrbuf_cstr(w));
1241         wrbuf_destroy(w);
1242         if (!he)
1243         {
1244             yaz_log(YLOG_FATAL, "Unable to resolve '%s'", addr);
1245             return 1;
1246         }
1247         memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
1248         port = atoi(pp + 1);
1249     }
1250     else
1251     {
1252         port = atoi(addr);
1253         myaddr.sin_addr.s_addr = INADDR_ANY;
1254     }
1255
1256     myaddr.sin_port = htons(port);
1257
1258     if (!(p = getprotobyname("tcp"))) {
1259         return 1;
1260     }
1261     if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
1262         yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
1263     if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
1264                     &one, sizeof(one)) < 0)
1265         return 1;
1266
1267     if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0) 
1268     {
1269         yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
1270         return 1;
1271     }
1272     if (listen(l, SOMAXCONN) < 0) 
1273     {
1274         yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
1275         return 1;
1276     }
1277
1278     server->http_server = http_server_create();
1279
1280     server->http_server->listener_socket = l;
1281
1282     c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT, "http_server");
1283     iochan_setdata(c, server);
1284
1285     iochan_add(server->iochan_man, c);
1286     return 0;
1287 }
1288
1289 void http_close_server(struct conf_server *server)
1290 {
1291     /* break the event_loop (select) by closing down the HTTP listener sock */
1292     if (server->http_server->listener_socket)
1293     {
1294 #ifdef WIN32
1295         closesocket(server->http_server->listener_socket);
1296 #else
1297         close(server->http_server->listener_socket);
1298 #endif
1299     }
1300 }
1301
1302 void http_set_proxyaddr(const char *host, struct conf_server *server)
1303 {
1304     const char *p;
1305     short port;
1306     struct hostent *he;
1307     WRBUF w = wrbuf_alloc();
1308
1309     yaz_log(YLOG_LOG, "HTTP backend  %s", host);
1310
1311     p = strchr(host, ':');
1312     if (p)
1313     {
1314         port = atoi(p + 1);
1315         wrbuf_write(w, host, p - host);
1316         wrbuf_puts(w, "");
1317     }
1318     else
1319     {
1320         port = 80;
1321         wrbuf_puts(w, host);
1322     }
1323     if (!(he = gethostbyname(wrbuf_cstr(w))))
1324     {
1325         fprintf(stderr, "Failed to lookup '%s'\n", wrbuf_cstr(w));
1326         exit(1);
1327     }
1328     wrbuf_destroy(w);
1329
1330     server->http_server->proxy_addr = xmalloc(sizeof(struct sockaddr_in));
1331     server->http_server->proxy_addr->sin_family = he->h_addrtype;
1332     memcpy(&server->http_server->proxy_addr->sin_addr.s_addr,
1333            he->h_addr_list[0], he->h_length);
1334     server->http_server->proxy_addr->sin_port = htons(port);
1335 }
1336
1337 static void http_fire_observers(struct http_channel *c)
1338 {
1339     http_channel_observer_t p = c->observers;
1340     while (p)
1341     {
1342         p->destroy(p->data, c, p->data2);
1343         p = p->next;
1344     }
1345 }
1346
1347 static void http_destroy_observers(struct http_channel *c)
1348 {
1349     while (c->observers)
1350     {
1351         http_channel_observer_t obs = c->observers;
1352         c->observers = obs->next;
1353         xfree(obs);
1354     }
1355 }
1356
1357 http_channel_observer_t http_add_observer(struct http_channel *c, void *data,
1358                                           http_channel_destroy_t des)
1359 {
1360     http_channel_observer_t obs = xmalloc(sizeof(*obs));
1361     obs->chan = c;
1362     obs->data = data;
1363     obs->data2 = 0;
1364     obs->destroy= des;
1365     obs->next = c->observers;
1366     c->observers = obs;
1367     return obs;
1368 }
1369
1370 void http_remove_observer(http_channel_observer_t obs)
1371 {
1372     struct http_channel *c = obs->chan;
1373     http_channel_observer_t found, *p = &c->observers;
1374     while (*p != obs)
1375         p = &(*p)->next;
1376     found = *p;
1377     assert(found);
1378     *p = (*p)->next;
1379     xfree(found);
1380 }
1381
1382 struct http_channel *http_channel_observer_chan(http_channel_observer_t obs)
1383 {
1384     return obs->chan;
1385 }
1386
1387 void http_observer_set_data2(http_channel_observer_t obs, void *data2)
1388 {
1389     obs->data2 = data2;
1390 }
1391
1392 http_server_t http_server_create(void)
1393 {
1394     http_server_t hs = xmalloc(sizeof(*hs));
1395     hs->mutex = 0;
1396     hs->proxy_addr = 0;
1397     hs->ref_count = 1;
1398     hs->http_sessions = 0;
1399
1400     hs->http_channel_freelist = 0;
1401     hs->http_channel_freelist_count = 0;
1402     hs->http_channel_freelist_max   = 10;
1403
1404     hs->http_buf_freelist = 0;
1405     hs->http_buf_freelist_count = 0;
1406     hs->http_buf_freelist_max = 10;
1407     return hs;
1408 }
1409
1410 void http_server_destroy(http_server_t hs)
1411 {
1412     if (hs)
1413     {
1414         int r;
1415
1416         yaz_mutex_enter(hs->mutex); /* OK: hs->mutex may be NULL */
1417         r = --(hs->ref_count);
1418         yaz_mutex_leave(hs->mutex);
1419
1420         if (r == 0)
1421         {
1422             struct http_buf *b = hs->http_buf_freelist;
1423             struct http_channel *c = hs->http_channel_freelist;
1424             while (b)
1425             {
1426                 struct http_buf *b_next = b->next;
1427                 xfree(b);
1428                 b = b_next;
1429             }
1430             while (c)
1431             {
1432                 struct http_channel *c_next = c->next;
1433                 nmem_destroy(c->nmem);
1434                 wrbuf_destroy(c->wrbuf);
1435                 xfree(c);
1436                 c = c_next;
1437             }
1438             http_sessions_destroy(hs->http_sessions);
1439             xfree(hs->proxy_addr);
1440             yaz_mutex_destroy(&hs->mutex);
1441             xfree(hs);
1442         }
1443     }
1444 }
1445
1446 void http_server_incref(http_server_t hs)
1447 {
1448     assert(hs);
1449     yaz_mutex_enter(hs->mutex);
1450     (hs->ref_count)++;
1451     yaz_mutex_leave(hs->mutex);
1452 }
1453
1454 void http_mutex_init(struct conf_server *server)
1455 {
1456     assert(server);
1457
1458     assert(server->http_server->mutex == 0);
1459     pazpar2_mutex_create(&server->http_server->mutex, "http_server");
1460     server->http_server->http_sessions = http_sessions_create();
1461 }
1462
1463 /*
1464  * Local variables:
1465  * c-basic-offset: 4
1466  * c-file-style: "Stroustrup"
1467  * indent-tabs-mode: nil
1468  * End:
1469  * vim: shiftwidth=4 tabstop=8 expandtab
1470  */
1471