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