Added skeleton for query charset conversion. Bug #977.
[yaz-moved-to-github.git] / src / http.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: http.c,v 1.1 2007-01-11 10:55:57 adam Exp $
6  */
7
8 /**
9  * \file http.c
10  * \brief Implements HTTP decoding
11  */
12
13 #include <ctype.h>
14 #include <yaz/odr.h>
15 #include <yaz/yaz-version.h>
16 #include <yaz/yaz-iconv.h>
17 #include <yaz/zgdu.h>
18
19 #ifdef WIN32
20 #define strncasecmp _strnicmp
21 #define strcasecmp _stricmp
22 #endif
23  
24 static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers,
25                                   char **content_buf, int *content_len)
26 {
27     int i = off;
28     int chunked = 0;
29
30     *headers = 0;
31     while (i < o->size-1 && o->buf[i] == '\n')
32     {
33         int po;
34         i++;
35         if (o->buf[i] == '\r' && i < o->size-1 && o->buf[i+1] == '\n')
36         {
37             i++;
38             break;
39         }
40         if (o->buf[i] == '\n')
41             break;
42         for (po = i; ; i++)
43         {
44             if (i == o->size)
45             {
46                 o->error = OHTTP;
47                 return 0;
48             }
49             else if (o->buf[i] == ':')
50                 break;
51         }
52         *headers = (Z_HTTP_Header *) odr_malloc(o, sizeof(**headers));
53         (*headers)->name = (char*) odr_malloc(o, i - po + 1);
54         memcpy ((*headers)->name, o->buf + po, i - po);
55         (*headers)->name[i - po] = '\0';
56         i++;
57         while (i < o->size-1 && o->buf[i] == ' ')
58             i++;
59         for (po = i; i < o->size-1 && !strchr("\r\n", o->buf[i]); i++)
60             ;
61         
62         (*headers)->value = (char*) odr_malloc(o, i - po + 1);
63         memcpy ((*headers)->value, o->buf + po, i - po);
64         (*headers)->value[i - po] = '\0';
65         
66         if (!strcasecmp((*headers)->name, "Transfer-Encoding")
67             &&
68             !strcasecmp((*headers)->value, "chunked"))
69             chunked = 1;
70         headers = &(*headers)->next;
71         if (i < o->size-1 && o->buf[i] == '\r')
72             i++;
73     }
74     *headers = 0;
75     if (o->buf[i] != '\n')
76     {
77         o->error = OHTTP;
78         return 0;
79     }
80     i++;
81
82     if (chunked)
83     {
84         int off = 0;
85         
86         /* we know buffer will be smaller than o->size - i*/
87         *content_buf = (char*) odr_malloc(o, o->size - i);  
88         
89         while (1)
90         {
91             /* chunk length .. */
92             int chunk_len = 0;
93             for (; i  < o->size-2; i++)
94                 if (isdigit(o->buf[i]))
95                     chunk_len = chunk_len * 16 + 
96                         (o->buf[i] - '0');
97                 else if (isupper(o->buf[i]))
98                     chunk_len = chunk_len * 16 + 
99                         (o->buf[i] - ('A'-10));
100                 else if (islower(o->buf[i]))
101                     chunk_len = chunk_len * 16 + 
102                         (o->buf[i] - ('a'-10));
103                 else
104                     break;
105             /* chunk extension ... */
106             while (o->buf[i] != '\r' && o->buf[i+1] != '\n')
107             {
108                 if (i >= o->size-2)
109                 {
110                     o->error = OHTTP;
111                     return 0;
112                 }
113                 i++;
114             }
115             i += 2;  /* skip CRLF */
116             if (chunk_len == 0)
117                 break;
118             if (chunk_len < 0 || off + chunk_len > o->size)
119             {
120                 o->error = OHTTP;
121                 return 0;
122             }
123             /* copy chunk .. */
124             memcpy (*content_buf + off, o->buf + i, chunk_len);
125             i += chunk_len + 2; /* skip chunk+CRLF */
126             off += chunk_len;
127         }
128         if (!off)
129             *content_buf = 0;
130         *content_len = off;
131     }
132     else
133     {
134         if (i > o->size)
135         {
136             o->error = OHTTP;
137             return 0;
138         }
139         else if (i == o->size)
140         {
141             *content_buf = 0;
142             *content_len = 0;
143         }
144         else 
145         {
146             *content_len = o->size - i;
147             *content_buf = (char*) odr_malloc(o, *content_len + 1);
148             memcpy(*content_buf, o->buf + i, *content_len);
149             (*content_buf)[*content_len] = '\0';
150         }
151     }
152     return 1;
153 }
154
155 void z_HTTP_header_add_content_type(ODR o, Z_HTTP_Header **hp,
156                                     const char *content_type,
157                                     const char *charset)
158 {
159     const char *l = "Content-Type";
160     if (charset)
161     {
162         char *ctype = odr_malloc(o, strlen(content_type)+strlen(charset) + 15);
163         sprintf(ctype, "%s; charset=%s", content_type, charset);
164         z_HTTP_header_add(o, hp, l, ctype);
165     }
166     else
167         z_HTTP_header_add(o, hp, l, content_type);
168
169 }
170
171 void z_HTTP_header_add(ODR o, Z_HTTP_Header **hp, const char *n,
172                        const char *v)
173 {
174     while (*hp)
175         hp = &(*hp)->next;
176     *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
177     (*hp)->name = odr_strdup(o, n);
178     (*hp)->value = odr_strdup(o, v);
179     (*hp)->next = 0;
180 }
181
182 const char *z_HTTP_header_lookup(const Z_HTTP_Header *hp, const char *n)
183 {
184     for (; hp; hp = hp->next)
185         if (!yaz_matchstr(hp->name, n))
186             return hp->value;
187     return 0;
188 }
189
190
191 Z_GDU *z_get_HTTP_Request(ODR o)
192 {
193     Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
194     Z_HTTP_Request *hreq;
195
196     p->which = Z_GDU_HTTP_Request;
197     p->u.HTTP_Request = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hreq));
198     hreq = p->u.HTTP_Request;
199     hreq->headers = 0;
200     hreq->content_len = 0;
201     hreq->content_buf = 0;
202     hreq->version = "1.1";
203     hreq->method = "POST";
204     hreq->path = "/";
205     z_HTTP_header_add(o, &hreq->headers, "User-Agent", "YAZ/" YAZ_VERSION);
206     return p;
207 }
208
209
210 Z_GDU *z_get_HTTP_Request_host_path(ODR odr,
211                                     const char *host,
212                                     const char *path)
213 {
214     Z_GDU *p = z_get_HTTP_Request(odr);
215
216     p->u.HTTP_Request->path = odr_strdup(odr, path);
217
218     if (host)
219     {
220         const char *cp0 = strstr(host, "://");
221         const char *cp1 = 0;
222         if (cp0)
223             cp0 = cp0+3;
224         else
225             cp0 = host;
226
227         cp1 = strchr(cp0, '/');
228         if (!cp1)
229             cp1 = cp0+strlen(cp0);
230
231         if (cp0 && cp1)
232         {
233             char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
234             memcpy (h, cp0, cp1 - cp0);
235             h[cp1-cp0] = '\0';
236             z_HTTP_header_add(odr, &p->u.HTTP_Request->headers,
237                               "Host", h);
238         }
239     }
240     return p;
241 }
242
243
244 Z_GDU *z_get_HTTP_Response(ODR o, int code)
245 {
246     Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
247     Z_HTTP_Response *hres;
248
249     p->which = Z_GDU_HTTP_Response;
250     p->u.HTTP_Response = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hres));
251     hres = p->u.HTTP_Response;
252     hres->headers = 0;
253     hres->content_len = 0;
254     hres->content_buf = 0;
255     hres->code = code;
256     hres->version = "1.1";
257     z_HTTP_header_add(o, &hres->headers, "Server",
258                       "YAZ/" YAZ_VERSION);
259     if (code != 200)
260     {
261         hres->content_buf = (char*) odr_malloc(o, 400);
262         sprintf (hres->content_buf, 
263                  "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
264                  "<HTML>\n"
265                  " <HEAD>\n"
266                  "  <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
267                  " </HEAD>\n"
268                  " <BODY>\n"
269                  "  <P><A HREF=\"http://www.indexdata.dk/yaz/\">YAZ</A> " 
270                  YAZ_VERSION "</P>\n"
271                  "  <P>Error: %d</P>\n"
272                  "  <P>Description: %.50s</P>\n"
273                  " </BODY>\n"
274                  "</HTML>\n",
275                  code, z_HTTP_errmsg(code));
276         hres->content_len = strlen(hres->content_buf);
277         z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
278     }
279     return p;
280 }
281
282 const char *z_HTTP_errmsg(int code)
283 {
284     if (code == 200)
285         return "OK";
286     else if (code == 400)
287         return "Bad Request";
288     else if (code == 404)
289         return "Not Found";
290     else if (code == 405)
291         return "Method Not Allowed";
292     else if (code == 500)
293         return "Internal Error";
294     else
295         return "Unknown Error";
296 }
297
298 int yaz_decode_http_response(ODR o, Z_HTTP_Response **hr_p)
299 {
300     int i, po;
301     Z_HTTP_Response *hr = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hr));
302     
303     *hr_p = hr;
304     hr->content_buf = 0;
305     hr->content_len = 0;
306     
307     po = i = 5;
308     while (i < o->size-2 && !strchr(" \r\n", o->buf[i]))
309         i++;
310     hr->version = (char *) odr_malloc(o, i - po + 1);
311     if (i - po)
312         memcpy(hr->version, o->buf + po, i - po);
313     hr->version[i-po] = 0;
314     if (o->buf[i] != ' ')
315     {
316         o->error = OHTTP;
317         return 0;
318     }
319     i++;
320     hr->code = 0;
321     while (i < o->size-2 && o->buf[i] >= '0' && o->buf[i] <= '9')
322     {
323         hr->code = hr->code*10 + (o->buf[i] - '0');
324         i++;
325     }
326     while (i < o->size-1 && o->buf[i] != '\n')
327         i++;
328     return decode_headers_content(o, i, &hr->headers,
329                                   &hr->content_buf, &hr->content_len);            
330 }
331
332 int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p)
333 {
334     int i, po;
335     Z_HTTP_Request *hr = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
336     
337     *hr_p = hr;
338     
339     /* method .. */
340     for (i = 0; o->buf[i] != ' '; i++)
341         if (i >= o->size-5 || i > 30)
342         {
343             o->error = OHTTP;
344             return 0;
345         }
346     hr->method = (char *) odr_malloc(o, i+1);
347     memcpy (hr->method, o->buf, i);
348     hr->method[i] = '\0';
349     /* path */
350     po = i+1;
351     for (i = po; o->buf[i] != ' '; i++)
352         if (i >= o->size-5)
353         {
354             o->error = OHTTP;
355             return 0;
356         }
357     hr->path = (char *) odr_malloc(o, i - po + 1);
358     memcpy (hr->path, o->buf+po, i - po);
359     hr->path[i - po] = '\0';
360     /* HTTP version */
361     i++;
362     if (i > o->size-5 || memcmp(o->buf+i, "HTTP/", 5))
363     {
364         o->error = OHTTP;
365         return 0;
366     }
367     i+= 5;
368     po = i;
369     while (i < o->size && !strchr("\r\n", o->buf[i]))
370         i++;
371     hr->version = (char *) odr_malloc(o, i - po + 1);
372     memcpy(hr->version, o->buf + po, i - po);
373     hr->version[i - po] = '\0';
374     /* headers */
375     if (i < o->size-1 && o->buf[i] == '\r')
376         i++;
377     if (o->buf[i] != '\n')
378     {
379         o->error = OHTTP;
380         return 0;
381     }
382     return decode_headers_content(o, i, &hr->headers,
383                                   &hr->content_buf, &hr->content_len);
384 }
385
386 int yaz_encode_http_response(ODR o, Z_HTTP_Response *hr)
387 {
388     char sbuf[80];
389     Z_HTTP_Header *h;
390     int top0 = o->top;
391
392     sprintf(sbuf, "HTTP/%s %d %s\r\n", hr->version,
393             hr->code,
394             z_HTTP_errmsg(hr->code));
395     odr_write(o, (unsigned char *) sbuf, strlen(sbuf));
396     /* apply Content-Length if not already applied */
397     if (!z_HTTP_header_lookup(hr->headers,
398                               "Content-Length"))
399     {
400         char lstr[60];
401         sprintf(lstr, "Content-Length: %d\r\n",
402                 hr->content_len);
403         odr_write(o, (unsigned char *) lstr, strlen(lstr));
404     }
405     for (h = hr->headers; h; h = h->next)
406     {
407         odr_write(o, (unsigned char *) h->name, strlen(h->name));
408         odr_write(o, (unsigned char *) ": ", 2);
409         odr_write(o, (unsigned char *) h->value, strlen(h->value));
410         odr_write(o, (unsigned char *) "\r\n", 2);
411     }
412     odr_write(o, (unsigned char *) "\r\n", 2);
413     if (hr->content_buf)
414         odr_write(o, (unsigned char *) 
415                   hr->content_buf,
416                   hr->content_len);
417     if (o->direction == ODR_PRINT)
418     {
419         odr_printf(o, "-- HTTP response:\n%.*s\n", o->top - top0,
420                    o->buf + top0);
421         odr_printf(o, "-- \n");
422     }
423     return 1;
424 }
425
426 int yaz_encode_http_request(ODR o, Z_HTTP_Request *hr)
427 {
428     Z_HTTP_Header *h;
429     int top0 = o->top;
430
431     odr_write(o, (unsigned char *) hr->method,
432               strlen(hr->method));
433     odr_write(o, (unsigned char *) " ", 1);
434     odr_write(o, (unsigned char *) hr->path,
435               strlen(hr->path));
436     odr_write(o, (unsigned char *) " HTTP/", 6);
437     odr_write(o, (unsigned char *) hr->version,
438               strlen(hr->version));
439     odr_write(o, (unsigned char *) "\r\n", 2);
440     if (hr->content_len &&
441         !z_HTTP_header_lookup(hr->headers,
442                               "Content-Length"))
443     {
444         char lstr[60];
445         sprintf(lstr, "Content-Length: %d\r\n",
446                 hr->content_len);
447         odr_write(o, (unsigned char *) lstr, strlen(lstr));
448     }
449     for (h = hr->headers; h; h = h->next)
450     {
451         odr_write(o, (unsigned char *) h->name, strlen(h->name));
452         odr_write(o, (unsigned char *) ": ", 2);
453         odr_write(o, (unsigned char *) h->value, strlen(h->value));
454         odr_write(o, (unsigned char *) "\r\n", 2);
455     }
456     odr_write(o, (unsigned char *) "\r\n", 2);
457     if (hr->content_buf)
458         odr_write(o, (unsigned char *)
459                   hr->content_buf,
460                   hr->content_len);
461     if (o->direction == ODR_PRINT)
462     {
463         odr_printf(o, "-- HTTP request:\n%.*s\n", o->top - top0,
464                    o->buf + top0);
465         odr_printf(o, "-- \n");
466     }
467     return 1;
468 }
469
470 /*
471  * Local variables:
472  * c-basic-offset: 4
473  * indent-tabs-mode: nil
474  * End:
475  * vim: shiftwidth=4 tabstop=8 expandtab
476  */
477