Code updates which makes things compile as C++. Mostly type casts were
[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.2 2007-05-06 20:12:20 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 = (char *)
163             odr_malloc(o, strlen(content_type)+strlen(charset) + 15);
164         sprintf(ctype, "%s; charset=%s", content_type, charset);
165         z_HTTP_header_add(o, hp, l, ctype);
166     }
167     else
168         z_HTTP_header_add(o, hp, l, content_type);
169
170 }
171
172 void z_HTTP_header_add(ODR o, Z_HTTP_Header **hp, const char *n,
173                        const char *v)
174 {
175     while (*hp)
176         hp = &(*hp)->next;
177     *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
178     (*hp)->name = odr_strdup(o, n);
179     (*hp)->value = odr_strdup(o, v);
180     (*hp)->next = 0;
181 }
182
183 const char *z_HTTP_header_lookup(const Z_HTTP_Header *hp, const char *n)
184 {
185     for (; hp; hp = hp->next)
186         if (!yaz_matchstr(hp->name, n))
187             return hp->value;
188     return 0;
189 }
190
191
192 Z_GDU *z_get_HTTP_Request(ODR o)
193 {
194     Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
195     Z_HTTP_Request *hreq;
196
197     p->which = Z_GDU_HTTP_Request;
198     p->u.HTTP_Request = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hreq));
199     hreq = p->u.HTTP_Request;
200     hreq->headers = 0;
201     hreq->content_len = 0;
202     hreq->content_buf = 0;
203     hreq->version = "1.1";
204     hreq->method = "POST";
205     hreq->path = "/";
206     z_HTTP_header_add(o, &hreq->headers, "User-Agent", "YAZ/" YAZ_VERSION);
207     return p;
208 }
209
210
211 Z_GDU *z_get_HTTP_Request_host_path(ODR odr,
212                                     const char *host,
213                                     const char *path)
214 {
215     Z_GDU *p = z_get_HTTP_Request(odr);
216
217     p->u.HTTP_Request->path = odr_strdup(odr, path);
218
219     if (host)
220     {
221         const char *cp0 = strstr(host, "://");
222         const char *cp1 = 0;
223         if (cp0)
224             cp0 = cp0+3;
225         else
226             cp0 = host;
227
228         cp1 = strchr(cp0, '/');
229         if (!cp1)
230             cp1 = cp0+strlen(cp0);
231
232         if (cp0 && cp1)
233         {
234             char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
235             memcpy (h, cp0, cp1 - cp0);
236             h[cp1-cp0] = '\0';
237             z_HTTP_header_add(odr, &p->u.HTTP_Request->headers,
238                               "Host", h);
239         }
240     }
241     return p;
242 }
243
244
245 Z_GDU *z_get_HTTP_Response(ODR o, int code)
246 {
247     Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
248     Z_HTTP_Response *hres;
249
250     p->which = Z_GDU_HTTP_Response;
251     p->u.HTTP_Response = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hres));
252     hres = p->u.HTTP_Response;
253     hres->headers = 0;
254     hres->content_len = 0;
255     hres->content_buf = 0;
256     hres->code = code;
257     hres->version = "1.1";
258     z_HTTP_header_add(o, &hres->headers, "Server",
259                       "YAZ/" YAZ_VERSION);
260     if (code != 200)
261     {
262         hres->content_buf = (char*) odr_malloc(o, 400);
263         sprintf (hres->content_buf, 
264                  "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
265                  "<HTML>\n"
266                  " <HEAD>\n"
267                  "  <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
268                  " </HEAD>\n"
269                  " <BODY>\n"
270                  "  <P><A HREF=\"http://www.indexdata.dk/yaz/\">YAZ</A> " 
271                  YAZ_VERSION "</P>\n"
272                  "  <P>Error: %d</P>\n"
273                  "  <P>Description: %.50s</P>\n"
274                  " </BODY>\n"
275                  "</HTML>\n",
276                  code, z_HTTP_errmsg(code));
277         hres->content_len = strlen(hres->content_buf);
278         z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
279     }
280     return p;
281 }
282
283 const char *z_HTTP_errmsg(int code)
284 {
285     if (code == 200)
286         return "OK";
287     else if (code == 400)
288         return "Bad Request";
289     else if (code == 404)
290         return "Not Found";
291     else if (code == 405)
292         return "Method Not Allowed";
293     else if (code == 500)
294         return "Internal Error";
295     else
296         return "Unknown Error";
297 }
298
299 int yaz_decode_http_response(ODR o, Z_HTTP_Response **hr_p)
300 {
301     int i, po;
302     Z_HTTP_Response *hr = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hr));
303     
304     *hr_p = hr;
305     hr->content_buf = 0;
306     hr->content_len = 0;
307     
308     po = i = 5;
309     while (i < o->size-2 && !strchr(" \r\n", o->buf[i]))
310         i++;
311     hr->version = (char *) odr_malloc(o, i - po + 1);
312     if (i - po)
313         memcpy(hr->version, o->buf + po, i - po);
314     hr->version[i-po] = 0;
315     if (o->buf[i] != ' ')
316     {
317         o->error = OHTTP;
318         return 0;
319     }
320     i++;
321     hr->code = 0;
322     while (i < o->size-2 && o->buf[i] >= '0' && o->buf[i] <= '9')
323     {
324         hr->code = hr->code*10 + (o->buf[i] - '0');
325         i++;
326     }
327     while (i < o->size-1 && o->buf[i] != '\n')
328         i++;
329     return decode_headers_content(o, i, &hr->headers,
330                                   &hr->content_buf, &hr->content_len);            
331 }
332
333 int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p)
334 {
335     int i, po;
336     Z_HTTP_Request *hr = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
337     
338     *hr_p = hr;
339     
340     /* method .. */
341     for (i = 0; o->buf[i] != ' '; i++)
342         if (i >= o->size-5 || i > 30)
343         {
344             o->error = OHTTP;
345             return 0;
346         }
347     hr->method = (char *) odr_malloc(o, i+1);
348     memcpy (hr->method, o->buf, i);
349     hr->method[i] = '\0';
350     /* path */
351     po = i+1;
352     for (i = po; o->buf[i] != ' '; i++)
353         if (i >= o->size-5)
354         {
355             o->error = OHTTP;
356             return 0;
357         }
358     hr->path = (char *) odr_malloc(o, i - po + 1);
359     memcpy (hr->path, o->buf+po, i - po);
360     hr->path[i - po] = '\0';
361     /* HTTP version */
362     i++;
363     if (i > o->size-5 || memcmp(o->buf+i, "HTTP/", 5))
364     {
365         o->error = OHTTP;
366         return 0;
367     }
368     i+= 5;
369     po = i;
370     while (i < o->size && !strchr("\r\n", o->buf[i]))
371         i++;
372     hr->version = (char *) odr_malloc(o, i - po + 1);
373     memcpy(hr->version, o->buf + po, i - po);
374     hr->version[i - po] = '\0';
375     /* headers */
376     if (i < o->size-1 && o->buf[i] == '\r')
377         i++;
378     if (o->buf[i] != '\n')
379     {
380         o->error = OHTTP;
381         return 0;
382     }
383     return decode_headers_content(o, i, &hr->headers,
384                                   &hr->content_buf, &hr->content_len);
385 }
386
387 int yaz_encode_http_response(ODR o, Z_HTTP_Response *hr)
388 {
389     char sbuf[80];
390     Z_HTTP_Header *h;
391     int top0 = o->top;
392
393     sprintf(sbuf, "HTTP/%s %d %s\r\n", hr->version,
394             hr->code,
395             z_HTTP_errmsg(hr->code));
396     odr_write(o, (unsigned char *) sbuf, strlen(sbuf));
397     /* apply Content-Length if not already applied */
398     if (!z_HTTP_header_lookup(hr->headers,
399                               "Content-Length"))
400     {
401         char lstr[60];
402         sprintf(lstr, "Content-Length: %d\r\n",
403                 hr->content_len);
404         odr_write(o, (unsigned char *) lstr, strlen(lstr));
405     }
406     for (h = hr->headers; h; h = h->next)
407     {
408         odr_write(o, (unsigned char *) h->name, strlen(h->name));
409         odr_write(o, (unsigned char *) ": ", 2);
410         odr_write(o, (unsigned char *) h->value, strlen(h->value));
411         odr_write(o, (unsigned char *) "\r\n", 2);
412     }
413     odr_write(o, (unsigned char *) "\r\n", 2);
414     if (hr->content_buf)
415         odr_write(o, (unsigned char *) 
416                   hr->content_buf,
417                   hr->content_len);
418     if (o->direction == ODR_PRINT)
419     {
420         odr_printf(o, "-- HTTP response:\n%.*s\n", o->top - top0,
421                    o->buf + top0);
422         odr_printf(o, "-- \n");
423     }
424     return 1;
425 }
426
427 int yaz_encode_http_request(ODR o, Z_HTTP_Request *hr)
428 {
429     Z_HTTP_Header *h;
430     int top0 = o->top;
431
432     odr_write(o, (unsigned char *) hr->method,
433               strlen(hr->method));
434     odr_write(o, (unsigned char *) " ", 1);
435     odr_write(o, (unsigned char *) hr->path,
436               strlen(hr->path));
437     odr_write(o, (unsigned char *) " HTTP/", 6);
438     odr_write(o, (unsigned char *) hr->version,
439               strlen(hr->version));
440     odr_write(o, (unsigned char *) "\r\n", 2);
441     if (hr->content_len &&
442         !z_HTTP_header_lookup(hr->headers,
443                               "Content-Length"))
444     {
445         char lstr[60];
446         sprintf(lstr, "Content-Length: %d\r\n",
447                 hr->content_len);
448         odr_write(o, (unsigned char *) lstr, strlen(lstr));
449     }
450     for (h = hr->headers; h; h = h->next)
451     {
452         odr_write(o, (unsigned char *) h->name, strlen(h->name));
453         odr_write(o, (unsigned char *) ": ", 2);
454         odr_write(o, (unsigned char *) h->value, strlen(h->value));
455         odr_write(o, (unsigned char *) "\r\n", 2);
456     }
457     odr_write(o, (unsigned char *) "\r\n", 2);
458     if (hr->content_buf)
459         odr_write(o, (unsigned char *)
460                   hr->content_buf,
461                   hr->content_len);
462     if (o->direction == ODR_PRINT)
463     {
464         odr_printf(o, "-- HTTP request:\n%.*s\n", o->top - top0,
465                    o->buf + top0);
466         odr_printf(o, "-- \n");
467     }
468     return 1;
469 }
470
471 /*
472  * Local variables:
473  * c-basic-offset: 4
474  * indent-tabs-mode: nil
475  * End:
476  * vim: shiftwidth=4 tabstop=8 expandtab
477  */
478