d7d76df9189aa762dd3b2e30090fa1d93d0d82ac
[yaz-moved-to-github.git] / src / zgdu.c
1 /*
2  * Copyright (c) 2002-2004, Index Data.
3  * See the file LICENSE for details.
4  *
5  * $Id: zgdu.c,v 1.11 2004-10-15 00:19:01 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(ODR o, Z_HTTP_Header **hp, const char *n,
156                        const char *v)
157 {
158     while (*hp)
159         hp = &(*hp)->next;
160     *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
161     (*hp)->name = odr_strdup(o, n);
162     (*hp)->value = odr_strdup(o, v);
163     (*hp)->next = 0;
164 }
165
166 const char *z_HTTP_header_lookup(Z_HTTP_Header *hp, const char *n)
167 {
168     for (; hp; hp = hp->next)
169         if (!yaz_matchstr(hp->name, n))
170             return hp->value;
171     return 0;
172 }
173
174
175 Z_GDU *z_get_HTTP_Request(ODR o)
176 {
177     Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
178     Z_HTTP_Request *hreq;
179
180     p->which = Z_GDU_HTTP_Request;
181     p->u.HTTP_Request = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hreq));
182     hreq = p->u.HTTP_Request;
183     hreq->headers = 0;
184     hreq->content_len = 0;
185     hreq->content_buf = 0;
186     hreq->version = "1.1";
187     hreq->method = "POST";
188     hreq->path = "/";
189     z_HTTP_header_add(o, &hreq->headers, "User-Agent",
190                       "YAZ/" YAZ_VERSION);
191     return p;
192 }
193
194 Z_GDU *z_get_HTTP_Response(ODR o, int code)
195 {
196     Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
197     Z_HTTP_Response *hres;
198
199     p->which = Z_GDU_HTTP_Response;
200     p->u.HTTP_Response = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hres));
201     hres = p->u.HTTP_Response;
202     hres->headers = 0;
203     hres->content_len = 0;
204     hres->content_buf = 0;
205     hres->code = code;
206     hres->version = "1.1";
207     z_HTTP_header_add(o, &hres->headers, "Server",
208                       "YAZ/" YAZ_VERSION);
209     if (code != 200)
210     {
211         hres->content_buf = (char*) odr_malloc(o, 400);
212         sprintf (hres->content_buf, 
213                  "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
214                  "<HTML>\n"
215                  " <HEAD>\n"
216                  "  <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
217                  " </HEAD>\n"
218                  " <BODY>\n"
219                  "  <P><A HREF=\"http://www.indexdata.dk/yaz/\">YAZ</A> " 
220                  YAZ_VERSION "</P>\n"
221                  "  <P>Error: %d</P>\n"
222                  "  <P>Description: %.50s</P>\n"
223                  " </BODY>\n"
224                  "</HTML>\n",
225                  code, z_HTTP_errmsg(code));
226         hres->content_len = strlen(hres->content_buf);
227         z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
228     }
229     return p;
230 }
231
232 const char *z_HTTP_errmsg(int code)
233 {
234     if (code == 200)
235         return "OK";
236     else if (code == 400)
237         return "Bad Request";
238     else if (code == 404)
239         return "Not Found";
240     else if (code == 405)
241         return "Method Not Allowed";
242     else if (code == 500)
243         return "Internal Error";
244     else
245         return "Unknown Error";
246 }
247
248 int z_GDU (ODR o, Z_GDU **p, int opt, const char *name)
249 {
250     if (o->direction == ODR_DECODE) {
251         *p = (Z_GDU *) odr_malloc(o, sizeof(**p));
252         if (o->size > 10 && !memcmp(o->buf, "HTTP/", 5))
253         {
254             int i, po;
255             Z_HTTP_Response *hr;
256             (*p)->which = Z_GDU_HTTP_Response;
257
258             hr = (*p)->u.HTTP_Response = (Z_HTTP_Response *)
259                 odr_malloc(o, sizeof(*hr));
260             hr->content_buf = 0;
261             hr->content_len = 0;
262
263             po = i = 5;
264             while (i < o->size-2 && o->buf[i] != ' ' && o->buf[i] != '\r')
265                 i++;
266             hr->version = (char *) odr_malloc(o, i - po + 1);
267             if (i - po)
268                 memcpy(hr->version, o->buf + po, i - po);
269             hr->version[i-po] = 0;
270             if (o->buf[i] != ' ')
271             {
272                 o->error = OHTTP;
273                 return 0;
274             }
275             i++;
276             hr->code = 0;
277             while (i < o->size-2 && o->buf[i] >= '0' && o->buf[i] <= '9')
278             {
279                 hr->code = hr->code*10 + (o->buf[i] - '0');
280                 i++;
281             }
282             while (i < o->size-1 && o->buf[i] != '\r')
283                 i++;
284             return decode_headers_content(o, i, &hr->headers,
285                                           &hr->content_buf, &hr->content_len);            
286         }
287         else if (o->size > 5 &&
288             o->buf[0] >= 0x20 && o->buf[0] < 0x7f
289             && o->buf[1] >= 0x20 && o->buf[1] < 0x7f
290             && o->buf[2] >= 0x20 && o->buf[2] < 0x7f
291             && o->buf[3] >= 0x20 && o->buf[3] < 0x7f)
292         {
293             int i, po;
294             Z_HTTP_Request *hr;
295
296             (*p)->which = Z_GDU_HTTP_Request;
297             hr = (*p)->u.HTTP_Request = 
298                 (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
299
300             /* method .. */
301             for (i = 0; o->buf[i] != ' '; i++)
302                 if (i >= o->size-5 || i > 30)
303                 {
304                     o->error = OHTTP;
305                     return 0;
306                 }
307             hr->method = (char *) odr_malloc(o, i+1);
308             memcpy (hr->method, o->buf, i);
309             hr->method[i] = '\0';
310             /* path */
311             po = i+1;
312             for (i = po; o->buf[i] != ' '; i++)
313                 if (i >= o->size-5)
314                 {
315                     o->error = OHTTP;
316                     return 0;
317                 }
318             hr->path = (char *) odr_malloc(o, i - po + 1);
319             memcpy (hr->path, o->buf+po, i - po);
320             hr->path[i - po] = '\0';
321             /* HTTP version */
322             i++;
323             if (i > o->size-5 || memcmp(o->buf+i, "HTTP/", 5))
324             {
325                 o->error = OHTTP;
326                 return 0;
327             }
328             i+= 5;
329             po = i;
330             while (o->buf[i] != '\r')
331             {
332                 if (i >= o->size-1)
333                 {
334                     o->error = OHTTP;
335                     return 0;
336                 }
337                 i++;
338             }
339             hr->version = (char *) odr_malloc(o, i - po + 1);
340             memcpy(hr->version, o->buf + po, i - po);
341             hr->version[i - po] = '\0';
342             /* headers */
343             return decode_headers_content(o, i, &hr->headers,
344                                           &hr->content_buf, &hr->content_len);
345
346         }
347         else
348         {
349             (*p)->which = Z_GDU_Z3950;
350             return z_APDU(o, &(*p)->u.z3950, opt, 0);
351         }
352     }
353     else /* ENCODE or PRINT */
354     {
355         int top0 = o->top;
356         char sbuf[80];
357         Z_HTTP_Header *h;
358         switch((*p)->which)
359         {
360         case Z_GDU_HTTP_Response:
361             sprintf(sbuf, "HTTP/%s %d %s\r\n", (*p)->u.HTTP_Response->version,
362                     (*p)->u.HTTP_Response->code,
363                     z_HTTP_errmsg((*p)->u.HTTP_Response->code));
364             odr_write(o, (unsigned char *) sbuf, strlen(sbuf));
365             /* apply Content-Length if not already applied */
366             if (!z_HTTP_header_lookup((*p)->u.HTTP_Response->headers,
367                                       "Content-Length"))
368             {
369                 char lstr[60];
370                 sprintf(lstr, "Content-Length: %d\r\n",
371                         (*p)->u.HTTP_Response->content_len);
372                 odr_write(o, (unsigned char *) lstr, strlen(lstr));
373             }
374             for (h = (*p)->u.HTTP_Response->headers; h; h = h->next)
375             {
376                 odr_write(o, (unsigned char *) h->name, strlen(h->name));
377                 odr_write(o, (unsigned char *) ": ", 2);
378                 odr_write(o, (unsigned char *) h->value, strlen(h->value));
379                 odr_write(o, (unsigned char *) "\r\n", 2);
380             }
381             odr_write(o, (unsigned char *) "\r\n", 2);
382             if ((*p)->u.HTTP_Response->content_buf)
383                 odr_write(o, (unsigned char *) 
384                           (*p)->u.HTTP_Response->content_buf,
385                           (*p)->u.HTTP_Response->content_len);
386             if (o->direction == ODR_PRINT)
387             {
388                 odr_printf(o, "-- HTTP response:\n%.*s\n", o->top - top0,
389                            o->buf + top0);
390                 odr_printf(o, "-- \n");
391             }
392             break;
393         case Z_GDU_HTTP_Request:
394             odr_write(o, (unsigned char *) (*p)->u.HTTP_Request->method,
395                       strlen((*p)->u.HTTP_Request->method));
396             odr_write(o, (unsigned char *) " ", 1);
397             odr_write(o, (unsigned char *) (*p)->u.HTTP_Request->path,
398                       strlen((*p)->u.HTTP_Request->path));
399             odr_write(o, (unsigned char *) " HTTP/", 6);
400             odr_write(o, (unsigned char *) (*p)->u.HTTP_Request->version,
401                       strlen((*p)->u.HTTP_Request->version));
402             odr_write(o, (unsigned char *) "\r\n", 2);
403             if ((*p)->u.HTTP_Request->content_len &&
404                 !z_HTTP_header_lookup((*p)->u.HTTP_Request->headers,
405                                       "Content-Length"))
406             {
407                 char lstr[60];
408                 sprintf(lstr, "Content-Length: %d\r\n",
409                         (*p)->u.HTTP_Request->content_len);
410                 odr_write(o, (unsigned char *) lstr, strlen(lstr));
411             }
412             for (h = (*p)->u.HTTP_Request->headers; h; h = h->next)
413             {
414                 odr_write(o, (unsigned char *) h->name, strlen(h->name));
415                 odr_write(o, (unsigned char *) ": ", 2);
416                 odr_write(o, (unsigned char *) h->value, strlen(h->value));
417                 odr_write(o, (unsigned char *) "\r\n", 2);
418             }
419             odr_write(o, (unsigned char *) "\r\n", 2);
420             if ((*p)->u.HTTP_Request->content_buf)
421                 odr_write(o, (unsigned char *)
422                           (*p)->u.HTTP_Request->content_buf,
423                           (*p)->u.HTTP_Request->content_len);
424             if (o->direction == ODR_PRINT)
425             {
426                 odr_printf(o, "-- HTTP request:\n%.*s\n", o->top - top0,
427                         o->buf + top0);
428                 odr_printf(o, "-- \n");
429             }
430             break;
431         case Z_GDU_Z3950:
432             return z_APDU(o, &(*p)->u.z3950, opt, 0);
433         }
434     }
435     return 1;
436 }
437