Towards 2.1.44. Bump copyright year.
[yaz-moved-to-github.git] / src / zgdu.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: zgdu.c,v 1.17 2007-01-03 08:42:15 adam Exp $
6  */
7
8 /**
9  * \file zgdu.c
10  * \brief Implements HTTP and Z39.50 encoding and 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] == '\r')
32     {
33         int po;
34         i++;
35         if (o->buf[i] != '\n')
36         {
37             o->error = OHTTP;
38             return 0;
39         }
40         i++;
41         if (o->buf[i] == '\r')
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 && o->buf[i] != '\r' ; 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     }
73     *headers = 0;
74     i++;
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",
206                       "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 z_GDU (ODR o, Z_GDU **p, int opt, const char *name)
300 {
301     if (o->direction == ODR_DECODE) {
302         *p = (Z_GDU *) odr_malloc(o, sizeof(**p));
303         if (o->size > 10 && !memcmp(o->buf, "HTTP/", 5))
304         {
305             int i, po;
306             Z_HTTP_Response *hr;
307             (*p)->which = Z_GDU_HTTP_Response;
308
309             hr = (*p)->u.HTTP_Response = (Z_HTTP_Response *)
310                 odr_malloc(o, sizeof(*hr));
311             hr->content_buf = 0;
312             hr->content_len = 0;
313
314             po = i = 5;
315             while (i < o->size-2 && o->buf[i] != ' ' && o->buf[i] != '\r')
316                 i++;
317             hr->version = (char *) odr_malloc(o, i - po + 1);
318             if (i - po)
319                 memcpy(hr->version, o->buf + po, i - po);
320             hr->version[i-po] = 0;
321             if (o->buf[i] != ' ')
322             {
323                 o->error = OHTTP;
324                 return 0;
325             }
326             i++;
327             hr->code = 0;
328             while (i < o->size-2 && o->buf[i] >= '0' && o->buf[i] <= '9')
329             {
330                 hr->code = hr->code*10 + (o->buf[i] - '0');
331                 i++;
332             }
333             while (i < o->size-1 && o->buf[i] != '\r')
334                 i++;
335             return decode_headers_content(o, i, &hr->headers,
336                                           &hr->content_buf, &hr->content_len);            
337         }
338         else if (o->size > 5 &&
339             o->buf[0] >= 0x20 && o->buf[0] < 0x7f
340             && o->buf[1] >= 0x20 && o->buf[1] < 0x7f
341             && o->buf[2] >= 0x20 && o->buf[2] < 0x7f
342             && o->buf[3] >= 0x20 && o->buf[3] < 0x7f)
343         {
344             int i, po;
345             Z_HTTP_Request *hr;
346
347             (*p)->which = Z_GDU_HTTP_Request;
348             hr = (*p)->u.HTTP_Request = 
349                 (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
350
351             /* method .. */
352             for (i = 0; o->buf[i] != ' '; i++)
353                 if (i >= o->size-5 || i > 30)
354                 {
355                     o->error = OHTTP;
356                     return 0;
357                 }
358             hr->method = (char *) odr_malloc(o, i+1);
359             memcpy (hr->method, o->buf, i);
360             hr->method[i] = '\0';
361             /* path */
362             po = i+1;
363             for (i = po; o->buf[i] != ' '; i++)
364                 if (i >= o->size-5)
365                 {
366                     o->error = OHTTP;
367                     return 0;
368                 }
369             hr->path = (char *) odr_malloc(o, i - po + 1);
370             memcpy (hr->path, o->buf+po, i - po);
371             hr->path[i - po] = '\0';
372             /* HTTP version */
373             i++;
374             if (i > o->size-5 || memcmp(o->buf+i, "HTTP/", 5))
375             {
376                 o->error = OHTTP;
377                 return 0;
378             }
379             i+= 5;
380             po = i;
381             while (o->buf[i] != '\r')
382             {
383                 if (i >= o->size-1)
384                 {
385                     o->error = OHTTP;
386                     return 0;
387                 }
388                 i++;
389             }
390             hr->version = (char *) odr_malloc(o, i - po + 1);
391             memcpy(hr->version, o->buf + po, i - po);
392             hr->version[i - po] = '\0';
393             /* headers */
394             return decode_headers_content(o, i, &hr->headers,
395                                           &hr->content_buf, &hr->content_len);
396
397         }
398         else
399         {
400             (*p)->which = Z_GDU_Z3950;
401             return z_APDU(o, &(*p)->u.z3950, opt, 0);
402         }
403     }
404     else /* ENCODE or PRINT */
405     {
406         int top0 = o->top;
407         char sbuf[80];
408         Z_HTTP_Header *h;
409         switch((*p)->which)
410         {
411         case Z_GDU_HTTP_Response:
412             sprintf(sbuf, "HTTP/%s %d %s\r\n", (*p)->u.HTTP_Response->version,
413                     (*p)->u.HTTP_Response->code,
414                     z_HTTP_errmsg((*p)->u.HTTP_Response->code));
415             odr_write(o, (unsigned char *) sbuf, strlen(sbuf));
416             /* apply Content-Length if not already applied */
417             if (!z_HTTP_header_lookup((*p)->u.HTTP_Response->headers,
418                                       "Content-Length"))
419             {
420                 char lstr[60];
421                 sprintf(lstr, "Content-Length: %d\r\n",
422                         (*p)->u.HTTP_Response->content_len);
423                 odr_write(o, (unsigned char *) lstr, strlen(lstr));
424             }
425             for (h = (*p)->u.HTTP_Response->headers; h; h = h->next)
426             {
427                 odr_write(o, (unsigned char *) h->name, strlen(h->name));
428                 odr_write(o, (unsigned char *) ": ", 2);
429                 odr_write(o, (unsigned char *) h->value, strlen(h->value));
430                 odr_write(o, (unsigned char *) "\r\n", 2);
431             }
432             odr_write(o, (unsigned char *) "\r\n", 2);
433             if ((*p)->u.HTTP_Response->content_buf)
434                 odr_write(o, (unsigned char *) 
435                           (*p)->u.HTTP_Response->content_buf,
436                           (*p)->u.HTTP_Response->content_len);
437             if (o->direction == ODR_PRINT)
438             {
439                 odr_printf(o, "-- HTTP response:\n%.*s\n", o->top - top0,
440                            o->buf + top0);
441                 odr_printf(o, "-- \n");
442             }
443             break;
444         case Z_GDU_HTTP_Request:
445             odr_write(o, (unsigned char *) (*p)->u.HTTP_Request->method,
446                       strlen((*p)->u.HTTP_Request->method));
447             odr_write(o, (unsigned char *) " ", 1);
448             odr_write(o, (unsigned char *) (*p)->u.HTTP_Request->path,
449                       strlen((*p)->u.HTTP_Request->path));
450             odr_write(o, (unsigned char *) " HTTP/", 6);
451             odr_write(o, (unsigned char *) (*p)->u.HTTP_Request->version,
452                       strlen((*p)->u.HTTP_Request->version));
453             odr_write(o, (unsigned char *) "\r\n", 2);
454             if ((*p)->u.HTTP_Request->content_len &&
455                 !z_HTTP_header_lookup((*p)->u.HTTP_Request->headers,
456                                       "Content-Length"))
457             {
458                 char lstr[60];
459                 sprintf(lstr, "Content-Length: %d\r\n",
460                         (*p)->u.HTTP_Request->content_len);
461                 odr_write(o, (unsigned char *) lstr, strlen(lstr));
462             }
463             for (h = (*p)->u.HTTP_Request->headers; h; h = h->next)
464             {
465                 odr_write(o, (unsigned char *) h->name, strlen(h->name));
466                 odr_write(o, (unsigned char *) ": ", 2);
467                 odr_write(o, (unsigned char *) h->value, strlen(h->value));
468                 odr_write(o, (unsigned char *) "\r\n", 2);
469             }
470             odr_write(o, (unsigned char *) "\r\n", 2);
471             if ((*p)->u.HTTP_Request->content_buf)
472                 odr_write(o, (unsigned char *)
473                           (*p)->u.HTTP_Request->content_buf,
474                           (*p)->u.HTTP_Request->content_len);
475             if (o->direction == ODR_PRINT)
476             {
477                 odr_printf(o, "-- HTTP request:\n%.*s\n", o->top - top0,
478                         o->buf + top0);
479                 odr_printf(o, "-- \n");
480             }
481             break;
482         case Z_GDU_Z3950:
483             return z_APDU(o, &(*p)->u.z3950, opt, 0);
484         }
485     }
486     return 1;
487 }
488
489 /*
490  * Local variables:
491  * c-basic-offset: 4
492  * indent-tabs-mode: nil
493  * End:
494  * vim: shiftwidth=4 tabstop=8 expandtab
495  */
496