2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: zgdu.c,v 1.14 2006-03-01 23:24:25 adam Exp $
10 * \brief Implements HTTP and Z39.50 encoding and decoding.
15 #include <yaz/yaz-version.h>
16 #include <yaz/yaz-iconv.h>
20 #define strncasecmp _strnicmp
21 #define strcasecmp _stricmp
24 static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers,
25 char **content_buf, int *content_len)
31 while (i < o->size-1 && o->buf[i] == '\r')
35 if (o->buf[i] != '\n')
41 if (o->buf[i] == '\r')
50 else if (o->buf[i] == ':')
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';
58 while (i < o->size-1 && o->buf[i] == ' ')
60 for (po = i; i < o->size-1 && o->buf[i] != '\r' ; i++)
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';
67 if (!strcasecmp((*headers)->name, "Transfer-Encoding")
69 !strcasecmp((*headers)->value, "chunked"))
71 headers = &(*headers)->next;
75 if (o->buf[i] != '\n')
86 /* we know buffer will be smaller than o->size - i*/
87 *content_buf = (char*) odr_malloc(o, o->size - i);
93 for (; i < o->size-2; i++)
94 if (isdigit(o->buf[i]))
95 chunk_len = chunk_len * 16 +
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));
105 /* chunk extension ... */
106 while (o->buf[i] != '\r' && o->buf[i+1] != '\n')
115 i += 2; /* skip CRLF */
118 if (chunk_len < 0 || off + chunk_len > o->size)
124 memcpy (*content_buf + off, o->buf + i, chunk_len);
125 i += chunk_len + 2; /* skip chunk+CRLF */
139 else if (i == o->size)
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';
155 void z_HTTP_header_add_content_type(ODR o, Z_HTTP_Header **hp,
156 const char *content_type,
159 const char *l = "Content-Type";
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);
167 z_HTTP_header_add(o, hp, l, content_type);
171 void z_HTTP_header_add(ODR o, Z_HTTP_Header **hp, const char *n,
176 *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
177 (*hp)->name = odr_strdup(o, n);
178 (*hp)->value = odr_strdup(o, v);
182 const char *z_HTTP_header_lookup(Z_HTTP_Header *hp, const char *n)
184 for (; hp; hp = hp->next)
185 if (!yaz_matchstr(hp->name, n))
191 Z_GDU *z_get_HTTP_Request(ODR o)
193 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
194 Z_HTTP_Request *hreq;
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;
200 hreq->content_len = 0;
201 hreq->content_buf = 0;
202 hreq->version = "1.1";
203 hreq->method = "POST";
205 z_HTTP_header_add(o, &hreq->headers, "User-Agent",
210 Z_GDU *z_get_HTTP_Response(ODR o, int code)
212 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
213 Z_HTTP_Response *hres;
215 p->which = Z_GDU_HTTP_Response;
216 p->u.HTTP_Response = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hres));
217 hres = p->u.HTTP_Response;
219 hres->content_len = 0;
220 hres->content_buf = 0;
222 hres->version = "1.1";
223 z_HTTP_header_add(o, &hres->headers, "Server",
227 hres->content_buf = (char*) odr_malloc(o, 400);
228 sprintf (hres->content_buf,
229 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
232 " <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
235 " <P><A HREF=\"http://www.indexdata.dk/yaz/\">YAZ</A> "
237 " <P>Error: %d</P>\n"
238 " <P>Description: %.50s</P>\n"
241 code, z_HTTP_errmsg(code));
242 hres->content_len = strlen(hres->content_buf);
243 z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
248 const char *z_HTTP_errmsg(int code)
252 else if (code == 400)
253 return "Bad Request";
254 else if (code == 404)
256 else if (code == 405)
257 return "Method Not Allowed";
258 else if (code == 500)
259 return "Internal Error";
261 return "Unknown Error";
264 int z_GDU (ODR o, Z_GDU **p, int opt, const char *name)
266 if (o->direction == ODR_DECODE) {
267 *p = (Z_GDU *) odr_malloc(o, sizeof(**p));
268 if (o->size > 10 && !memcmp(o->buf, "HTTP/", 5))
272 (*p)->which = Z_GDU_HTTP_Response;
274 hr = (*p)->u.HTTP_Response = (Z_HTTP_Response *)
275 odr_malloc(o, sizeof(*hr));
280 while (i < o->size-2 && o->buf[i] != ' ' && o->buf[i] != '\r')
282 hr->version = (char *) odr_malloc(o, i - po + 1);
284 memcpy(hr->version, o->buf + po, i - po);
285 hr->version[i-po] = 0;
286 if (o->buf[i] != ' ')
293 while (i < o->size-2 && o->buf[i] >= '0' && o->buf[i] <= '9')
295 hr->code = hr->code*10 + (o->buf[i] - '0');
298 while (i < o->size-1 && o->buf[i] != '\r')
300 return decode_headers_content(o, i, &hr->headers,
301 &hr->content_buf, &hr->content_len);
303 else if (o->size > 5 &&
304 o->buf[0] >= 0x20 && o->buf[0] < 0x7f
305 && o->buf[1] >= 0x20 && o->buf[1] < 0x7f
306 && o->buf[2] >= 0x20 && o->buf[2] < 0x7f
307 && o->buf[3] >= 0x20 && o->buf[3] < 0x7f)
312 (*p)->which = Z_GDU_HTTP_Request;
313 hr = (*p)->u.HTTP_Request =
314 (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
317 for (i = 0; o->buf[i] != ' '; i++)
318 if (i >= o->size-5 || i > 30)
323 hr->method = (char *) odr_malloc(o, i+1);
324 memcpy (hr->method, o->buf, i);
325 hr->method[i] = '\0';
328 for (i = po; o->buf[i] != ' '; i++)
334 hr->path = (char *) odr_malloc(o, i - po + 1);
335 memcpy (hr->path, o->buf+po, i - po);
336 hr->path[i - po] = '\0';
339 if (i > o->size-5 || memcmp(o->buf+i, "HTTP/", 5))
346 while (o->buf[i] != '\r')
355 hr->version = (char *) odr_malloc(o, i - po + 1);
356 memcpy(hr->version, o->buf + po, i - po);
357 hr->version[i - po] = '\0';
359 return decode_headers_content(o, i, &hr->headers,
360 &hr->content_buf, &hr->content_len);
365 (*p)->which = Z_GDU_Z3950;
366 return z_APDU(o, &(*p)->u.z3950, opt, 0);
369 else /* ENCODE or PRINT */
376 case Z_GDU_HTTP_Response:
377 sprintf(sbuf, "HTTP/%s %d %s\r\n", (*p)->u.HTTP_Response->version,
378 (*p)->u.HTTP_Response->code,
379 z_HTTP_errmsg((*p)->u.HTTP_Response->code));
380 odr_write(o, (unsigned char *) sbuf, strlen(sbuf));
381 /* apply Content-Length if not already applied */
382 if (!z_HTTP_header_lookup((*p)->u.HTTP_Response->headers,
386 sprintf(lstr, "Content-Length: %d\r\n",
387 (*p)->u.HTTP_Response->content_len);
388 odr_write(o, (unsigned char *) lstr, strlen(lstr));
390 for (h = (*p)->u.HTTP_Response->headers; h; h = h->next)
392 odr_write(o, (unsigned char *) h->name, strlen(h->name));
393 odr_write(o, (unsigned char *) ": ", 2);
394 odr_write(o, (unsigned char *) h->value, strlen(h->value));
395 odr_write(o, (unsigned char *) "\r\n", 2);
397 odr_write(o, (unsigned char *) "\r\n", 2);
398 if ((*p)->u.HTTP_Response->content_buf)
399 odr_write(o, (unsigned char *)
400 (*p)->u.HTTP_Response->content_buf,
401 (*p)->u.HTTP_Response->content_len);
402 if (o->direction == ODR_PRINT)
404 odr_printf(o, "-- HTTP response:\n%.*s\n", o->top - top0,
406 odr_printf(o, "-- \n");
409 case Z_GDU_HTTP_Request:
410 odr_write(o, (unsigned char *) (*p)->u.HTTP_Request->method,
411 strlen((*p)->u.HTTP_Request->method));
412 odr_write(o, (unsigned char *) " ", 1);
413 odr_write(o, (unsigned char *) (*p)->u.HTTP_Request->path,
414 strlen((*p)->u.HTTP_Request->path));
415 odr_write(o, (unsigned char *) " HTTP/", 6);
416 odr_write(o, (unsigned char *) (*p)->u.HTTP_Request->version,
417 strlen((*p)->u.HTTP_Request->version));
418 odr_write(o, (unsigned char *) "\r\n", 2);
419 if ((*p)->u.HTTP_Request->content_len &&
420 !z_HTTP_header_lookup((*p)->u.HTTP_Request->headers,
424 sprintf(lstr, "Content-Length: %d\r\n",
425 (*p)->u.HTTP_Request->content_len);
426 odr_write(o, (unsigned char *) lstr, strlen(lstr));
428 for (h = (*p)->u.HTTP_Request->headers; h; h = h->next)
430 odr_write(o, (unsigned char *) h->name, strlen(h->name));
431 odr_write(o, (unsigned char *) ": ", 2);
432 odr_write(o, (unsigned char *) h->value, strlen(h->value));
433 odr_write(o, (unsigned char *) "\r\n", 2);
435 odr_write(o, (unsigned char *) "\r\n", 2);
436 if ((*p)->u.HTTP_Request->content_buf)
437 odr_write(o, (unsigned char *)
438 (*p)->u.HTTP_Request->content_buf,
439 (*p)->u.HTTP_Request->content_len);
440 if (o->direction == ODR_PRINT)
442 odr_printf(o, "-- HTTP request:\n%.*s\n", o->top - top0,
444 odr_printf(o, "-- \n");
448 return z_APDU(o, &(*p)->u.z3950, opt, 0);
457 * indent-tabs-mode: nil
459 * vim: shiftwidth=4 tabstop=8 expandtab