ZOOM HTTP: refactor and simplify redirect/proxy code
[yaz-moved-to-github.git] / src / http.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file http.c
7  * \brief Implements HTTP decoding
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <yaz/odr.h>
14 #include <yaz/yaz-version.h>
15 #include <yaz/yaz-iconv.h>
16 #include <yaz/matchstr.h>
17 #include <yaz/zgdu.h>
18
19 #ifdef WIN32
20 #define strncasecmp _strnicmp
21 #define strcasecmp _stricmp
22 #endif
23  
24
25 /*
26  * This function's counterpart, yaz_base64decode(), is in srwutil.c.
27  * I feel bad that they're not together, but each function is only
28  * needed in one place, and those places are not together.  Maybe one
29  * day we'll move them into a new httputil.c, and declare them in a
30  * corresponding httputil.h
31  */
32 static void yaz_base64encode(const char *in, char *out)
33 {
34     static char encoding[] =
35         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
36     unsigned char buf[3];
37     long n;
38
39     while (*in != 0) {
40         char *pad = 0;
41         buf[0] = in[0];
42         buf[1] = in[1];
43         if (in[1] == 0) {
44             buf[2] = 0;
45             pad = "==";
46         } else {
47             buf[2] = in[2];
48             if (in[2] == 0)
49                 pad = "=";
50         }
51
52         /* Treat three eight-bit numbers as on 24-bit number */
53         n = (buf[0] << 16) + (buf[1] << 8) + buf[2];
54
55         /* Write the six-bit chunks out as four encoded characters */
56         *out++ = encoding[(n >> 18) & 63];
57         *out++ = encoding[(n >> 12) & 63];
58         if (in[1] != 0)
59             *out++ = encoding[(n >> 6) & 63];
60         if (in[1] != 0 && in[2] != 0)
61             *out++ = encoding[n & 63];
62
63         if (pad != 0) {
64             while (*pad != 0)
65                 *out++ = *pad++;
66             break;
67         }
68         in += 3;
69     }
70
71     *out++ = 0;
72 }
73
74
75 static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers,
76                                   char **content_buf, int *content_len)
77 {
78     int i = off;
79     int chunked = 0;
80
81     *headers = 0;
82     while (i < o->size-1 && o->buf[i] == '\n')
83     {
84         int po;
85         i++;
86         if (o->buf[i] == '\r' && i < o->size-1 && o->buf[i+1] == '\n')
87         {
88             i++;
89             break;
90         }
91         if (o->buf[i] == '\n')
92             break;
93         for (po = i; ; i++)
94         {
95             if (i == o->size)
96             {
97                 o->error = OHTTP;
98                 return 0;
99             }
100             else if (o->buf[i] == ':')
101                 break;
102         }
103         *headers = (Z_HTTP_Header *) odr_malloc(o, sizeof(**headers));
104         (*headers)->name = (char*) odr_malloc(o, i - po + 1);
105         memcpy ((*headers)->name, o->buf + po, i - po);
106         (*headers)->name[i - po] = '\0';
107         i++;
108         while (i < o->size-1 && o->buf[i] == ' ')
109             i++;
110         for (po = i; i < o->size-1 && !strchr("\r\n", o->buf[i]); i++)
111             ;
112         
113         (*headers)->value = (char*) odr_malloc(o, i - po + 1);
114         memcpy ((*headers)->value, o->buf + po, i - po);
115         (*headers)->value[i - po] = '\0';
116         
117         if (!strcasecmp((*headers)->name, "Transfer-Encoding")
118             &&
119             !strcasecmp((*headers)->value, "chunked"))
120             chunked = 1;
121         headers = &(*headers)->next;
122         if (i < o->size-1 && o->buf[i] == '\r')
123             i++;
124     }
125     *headers = 0;
126     if (o->buf[i] != '\n')
127     {
128         o->error = OHTTP;
129         return 0;
130     }
131     i++;
132
133     if (chunked)
134     {
135         int off = 0;
136         
137         /* we know buffer will be smaller than o->size - i*/
138         *content_buf = (char*) odr_malloc(o, o->size - i);  
139         
140         while (1)
141         {
142             /* chunk length .. */
143             int chunk_len = 0;
144             for (; i  < o->size-2; i++)
145                 if (yaz_isdigit(o->buf[i]))
146                     chunk_len = chunk_len * 16 + 
147                         (o->buf[i] - '0');
148                 else if (yaz_isupper(o->buf[i]))
149                     chunk_len = chunk_len * 16 + 
150                         (o->buf[i] - ('A'-10));
151                 else if (yaz_islower(o->buf[i]))
152                     chunk_len = chunk_len * 16 + 
153                         (o->buf[i] - ('a'-10));
154                 else
155                     break;
156             /* chunk extension ... */
157             while (o->buf[i] != '\r' && o->buf[i+1] != '\n')
158             {
159                 if (i >= o->size-2)
160                 {
161                     o->error = OHTTP;
162                     return 0;
163                 }
164                 i++;
165             }
166             i += 2;  /* skip CRLF */
167             if (chunk_len == 0)
168                 break;
169             if (chunk_len < 0 || off + chunk_len > o->size)
170             {
171                 o->error = OHTTP;
172                 return 0;
173             }
174             /* copy chunk .. */
175             memcpy (*content_buf + off, o->buf + i, chunk_len);
176             i += chunk_len + 2; /* skip chunk+CRLF */
177             off += chunk_len;
178         }
179         if (!off)
180             *content_buf = 0;
181         *content_len = off;
182     }
183     else
184     {
185         if (i > o->size)
186         {
187             o->error = OHTTP;
188             return 0;
189         }
190         else if (i == o->size)
191         {
192             *content_buf = 0;
193             *content_len = 0;
194         }
195         else 
196         {
197             *content_len = o->size - i;
198             *content_buf = (char*) odr_malloc(o, *content_len + 1);
199             memcpy(*content_buf, o->buf + i, *content_len);
200             (*content_buf)[*content_len] = '\0';
201         }
202     }
203     return 1;
204 }
205
206 void z_HTTP_header_add_content_type(ODR o, Z_HTTP_Header **hp,
207                                     const char *content_type,
208                                     const char *charset)
209 {
210     const char *l = "Content-Type";
211     if (charset)
212     {
213         char *ctype = (char *)
214             odr_malloc(o, strlen(content_type)+strlen(charset) + 15);
215         sprintf(ctype, "%s; charset=%s", content_type, charset);
216         z_HTTP_header_add(o, hp, l, ctype);
217     }
218     else
219         z_HTTP_header_add(o, hp, l, content_type);
220
221 }
222
223 /*
224  * HTTP Basic authentication is described at:
225  * http://tools.ietf.org/html/rfc1945#section-11.1
226  */
227 void z_HTTP_header_add_basic_auth(ODR o, Z_HTTP_Header **hp,
228                                   const char *username, const char *password)
229 {
230     char *tmp, *buf;
231     int len;
232
233     if (username == 0)
234         return;
235     if (password == 0)
236         password = "";
237
238     len = strlen(username) + strlen(password);
239     tmp = (char *) odr_malloc(o, len+2);
240     sprintf(tmp, "%s:%s", username, password);
241     buf = (char *) odr_malloc(o, (len+1) * 8/6 + 12);
242     strcpy(buf, "Basic ");
243     yaz_base64encode(tmp, &buf[strlen(buf)]);
244     z_HTTP_header_add(o, hp, "Authorization", buf);
245 }
246
247
248 void z_HTTP_header_add(ODR o, Z_HTTP_Header **hp, const char *n,
249                        const char *v)
250 {
251     while (*hp)
252         hp = &(*hp)->next;
253     *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
254     (*hp)->name = odr_strdup(o, n);
255     (*hp)->value = odr_strdup(o, v);
256     (*hp)->next = 0;
257 }
258
259 const char *z_HTTP_header_lookup(const Z_HTTP_Header *hp, const char *n)
260 {
261     for (; hp; hp = hp->next)
262         if (!yaz_matchstr(hp->name, n))
263             return hp->value;
264     return 0;
265 }
266
267
268 Z_GDU *z_get_HTTP_Request(ODR o)
269 {
270     Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
271     Z_HTTP_Request *hreq;
272
273     p->which = Z_GDU_HTTP_Request;
274     p->u.HTTP_Request = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hreq));
275     hreq = p->u.HTTP_Request;
276     hreq->headers = 0;
277     hreq->content_len = 0;
278     hreq->content_buf = 0;
279     hreq->version = "1.1";
280     hreq->method = "POST";
281     hreq->path = "/";
282     z_HTTP_header_add(o, &hreq->headers, "User-Agent", "YAZ/" YAZ_VERSION);
283     return p;
284 }
285
286
287 Z_GDU *z_get_HTTP_Request_host_path(ODR odr,
288                                     const char *host,
289                                     const char *path)
290 {
291     Z_GDU *p = z_get_HTTP_Request(odr);
292
293     p->u.HTTP_Request->path = odr_strdup(odr, path);
294
295     if (host)
296     {
297         const char *cp0 = strstr(host, "://");
298         const char *cp1 = 0;
299         if (cp0)
300             cp0 = cp0+3;
301         else
302             cp0 = host;
303
304         cp1 = strchr(cp0, '/');
305         if (!cp1)
306             cp1 = cp0+strlen(cp0);
307
308         if (cp0 && cp1)
309         {
310             char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
311             memcpy (h, cp0, cp1 - cp0);
312             h[cp1-cp0] = '\0';
313             z_HTTP_header_add(odr, &p->u.HTTP_Request->headers,
314                               "Host", h);
315         }
316     }
317     return p;
318 }
319
320 Z_GDU *z_get_HTTP_Request_uri(ODR odr, const char *uri, const char *args,
321                               int use_full_uri)
322 {
323     Z_GDU *p = z_get_HTTP_Request(odr);
324     const char *cp0 = strstr(uri, "://");
325     const char *cp1 = 0;
326     if (cp0)
327         cp0 = cp0+3;
328     else
329         cp0 = uri;
330     
331     cp1 = strchr(cp0, '/');
332     if (!cp1)
333         cp1 = cp0+strlen(cp0);
334     
335     if (cp0 && cp1)
336     {
337         char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
338         memcpy (h, cp0, cp1 - cp0);
339         h[cp1-cp0] = '\0';
340         z_HTTP_header_add(odr, &p->u.HTTP_Request->headers,
341                           "Host", h);
342     }
343
344     if (!args)
345     {
346         if (*cp1)
347             args = cp1 + 1;
348         else
349             args = "";
350     }
351     p->u.HTTP_Request->path = odr_malloc(odr, cp1 - uri + strlen(args) + 2);
352     if (use_full_uri)
353     {
354         memcpy(p->u.HTTP_Request->path, uri, cp1 - uri);
355         strcpy(p->u.HTTP_Request->path + (cp1 - uri), "/");
356     }
357     else
358         strcpy(p->u.HTTP_Request->path, "/");
359     strcat(p->u.HTTP_Request->path, args);
360     return p;
361 }
362
363 Z_GDU *z_get_HTTP_Response(ODR o, int code)
364 {
365     Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
366     Z_HTTP_Response *hres;
367
368     p->which = Z_GDU_HTTP_Response;
369     p->u.HTTP_Response = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hres));
370     hres = p->u.HTTP_Response;
371     hres->headers = 0;
372     hres->content_len = 0;
373     hres->content_buf = 0;
374     hres->code = code;
375     hres->version = "1.1";
376     z_HTTP_header_add(o, &hres->headers, "Server",
377                       "YAZ/" YAZ_VERSION);
378     if (code != 200)
379     {
380         hres->content_buf = (char*) odr_malloc(o, 400);
381         sprintf(hres->content_buf, 
382                 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\""
383                 " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
384                 "<HTML>\n"
385                 " <HEAD>\n"
386                 "  <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
387                 " </HEAD>\n"
388                 " <BODY>\n"
389                 "  <P><A HREF=\"http://www.indexdata.com/yaz/\">YAZ</A> " 
390                 YAZ_VERSION "</P>\n"
391                 "  <P>Error: %d</P>\n"
392                 "  <P>Description: %.50s</P>\n"
393                 " </BODY>\n"
394                 "</HTML>\n",
395                 code, z_HTTP_errmsg(code));
396         hres->content_len = strlen(hres->content_buf);
397         z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
398     }
399     return p;
400 }
401
402 const char *z_HTTP_errmsg(int code)
403 {
404     if (code == 200)
405         return "OK";
406     else if (code == 400)
407         return "Bad Request";
408     else if (code == 404)
409         return "Not Found";
410     else if (code == 405)
411         return "Method Not Allowed";
412     else if (code == 500)
413         return "Internal Error";
414     else
415         return "Unknown Error";
416 }
417
418 int yaz_decode_http_response(ODR o, Z_HTTP_Response **hr_p)
419 {
420     int i, po;
421     Z_HTTP_Response *hr = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hr));
422     
423     *hr_p = hr;
424     hr->content_buf = 0;
425     hr->content_len = 0;
426     
427     po = i = 5;
428     while (i < o->size-2 && !strchr(" \r\n", o->buf[i]))
429         i++;
430     hr->version = (char *) odr_malloc(o, i - po + 1);
431     if (i - po)
432         memcpy(hr->version, o->buf + po, i - po);
433     hr->version[i-po] = 0;
434     if (o->buf[i] != ' ')
435     {
436         o->error = OHTTP;
437         return 0;
438     }
439     i++;
440     hr->code = 0;
441     while (i < o->size-2 && o->buf[i] >= '0' && o->buf[i] <= '9')
442     {
443         hr->code = hr->code*10 + (o->buf[i] - '0');
444         i++;
445     }
446     while (i < o->size-1 && o->buf[i] != '\n')
447         i++;
448     return decode_headers_content(o, i, &hr->headers,
449                                   &hr->content_buf, &hr->content_len);            
450 }
451
452 int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p)
453 {
454     int i, po;
455     Z_HTTP_Request *hr = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
456     
457     *hr_p = hr;
458     
459     /* method .. */
460     for (i = 0; o->buf[i] != ' '; i++)
461         if (i >= o->size-5 || i > 30)
462         {
463             o->error = OHTTP;
464             return 0;
465         }
466     hr->method = (char *) odr_malloc(o, i+1);
467     memcpy (hr->method, o->buf, i);
468     hr->method[i] = '\0';
469     /* path */
470     po = i+1;
471     for (i = po; o->buf[i] != ' '; i++)
472         if (i >= o->size-5)
473         {
474             o->error = OHTTP;
475             return 0;
476         }
477     hr->path = (char *) odr_malloc(o, i - po + 1);
478     memcpy (hr->path, o->buf+po, i - po);
479     hr->path[i - po] = '\0';
480     /* HTTP version */
481     i++;
482     if (i > o->size-5 || memcmp(o->buf+i, "HTTP/", 5))
483     {
484         o->error = OHTTP;
485         return 0;
486     }
487     i+= 5;
488     po = i;
489     while (i < o->size && !strchr("\r\n", o->buf[i]))
490         i++;
491     hr->version = (char *) odr_malloc(o, i - po + 1);
492     memcpy(hr->version, o->buf + po, i - po);
493     hr->version[i - po] = '\0';
494     /* headers */
495     if (i < o->size-1 && o->buf[i] == '\r')
496         i++;
497     if (o->buf[i] != '\n')
498     {
499         o->error = OHTTP;
500         return 0;
501     }
502     return decode_headers_content(o, i, &hr->headers,
503                                   &hr->content_buf, &hr->content_len);
504 }
505
506 int yaz_encode_http_response(ODR o, Z_HTTP_Response *hr)
507 {
508     char sbuf[80];
509     Z_HTTP_Header *h;
510     int top0 = o->top;
511
512     sprintf(sbuf, "HTTP/%s %d %s\r\n", hr->version,
513             hr->code,
514             z_HTTP_errmsg(hr->code));
515     odr_write(o, (unsigned char *) sbuf, strlen(sbuf));
516     /* apply Content-Length if not already applied */
517     if (!z_HTTP_header_lookup(hr->headers,
518                               "Content-Length"))
519     {
520         char lstr[60];
521         sprintf(lstr, "Content-Length: %d\r\n",
522                 hr->content_len);
523         odr_write(o, (unsigned char *) lstr, strlen(lstr));
524     }
525     for (h = hr->headers; h; h = h->next)
526     {
527         odr_write(o, (unsigned char *) h->name, strlen(h->name));
528         odr_write(o, (unsigned char *) ": ", 2);
529         odr_write(o, (unsigned char *) h->value, strlen(h->value));
530         odr_write(o, (unsigned char *) "\r\n", 2);
531     }
532     odr_write(o, (unsigned char *) "\r\n", 2);
533     if (hr->content_buf)
534         odr_write(o, (unsigned char *) 
535                   hr->content_buf,
536                   hr->content_len);
537     if (o->direction == ODR_PRINT)
538     {
539         odr_printf(o, "-- HTTP response:\n%.*s\n", o->top - top0,
540                    o->buf + top0);
541         odr_printf(o, "-- \n");
542     }
543     return 1;
544 }
545
546 int yaz_encode_http_request(ODR o, Z_HTTP_Request *hr)
547 {
548     Z_HTTP_Header *h;
549     int top0 = o->top;
550
551     odr_write(o, (unsigned char *) hr->method,
552               strlen(hr->method));
553     odr_write(o, (unsigned char *) " ", 1);
554     odr_write(o, (unsigned char *) hr->path,
555               strlen(hr->path));
556     odr_write(o, (unsigned char *) " HTTP/", 6);
557     odr_write(o, (unsigned char *) hr->version,
558               strlen(hr->version));
559     odr_write(o, (unsigned char *) "\r\n", 2);
560     if (hr->content_len &&
561         !z_HTTP_header_lookup(hr->headers,
562                               "Content-Length"))
563     {
564         char lstr[60];
565         sprintf(lstr, "Content-Length: %d\r\n",
566                 hr->content_len);
567         odr_write(o, (unsigned char *) lstr, strlen(lstr));
568     }
569     for (h = hr->headers; h; h = h->next)
570     {
571         odr_write(o, (unsigned char *) h->name, strlen(h->name));
572         odr_write(o, (unsigned char *) ": ", 2);
573         odr_write(o, (unsigned char *) h->value, strlen(h->value));
574         odr_write(o, (unsigned char *) "\r\n", 2);
575     }
576     odr_write(o, (unsigned char *) "\r\n", 2);
577     if (hr->content_buf)
578         odr_write(o, (unsigned char *)
579                   hr->content_buf,
580                   hr->content_len);
581     if (o->direction == ODR_PRINT)
582     {
583         odr_printf(o, "-- HTTP request:\n%.*s\n", o->top - top0,
584                    o->buf + top0);
585         odr_printf(o, "-- \n");
586     }
587     return 1;
588 }
589
590 /*
591  * Local variables:
592  * c-basic-offset: 4
593  * c-file-style: "Stroustrup"
594  * indent-tabs-mode: nil
595  * End:
596  * vim: shiftwidth=4 tabstop=8 expandtab
597  */
598