2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: http.c,v 1.2 2007-05-06 20:12:20 adam Exp $
10 * \brief Implements HTTP 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] == '\n')
35 if (o->buf[i] == '\r' && i < o->size-1 && o->buf[i+1] == '\n')
40 if (o->buf[i] == '\n')
49 else if (o->buf[i] == ':')
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';
57 while (i < o->size-1 && o->buf[i] == ' ')
59 for (po = i; i < o->size-1 && !strchr("\r\n", o->buf[i]); i++)
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';
66 if (!strcasecmp((*headers)->name, "Transfer-Encoding")
68 !strcasecmp((*headers)->value, "chunked"))
70 headers = &(*headers)->next;
71 if (i < o->size-1 && o->buf[i] == '\r')
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 = (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);
168 z_HTTP_header_add(o, hp, l, content_type);
172 void z_HTTP_header_add(ODR o, Z_HTTP_Header **hp, const char *n,
177 *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
178 (*hp)->name = odr_strdup(o, n);
179 (*hp)->value = odr_strdup(o, v);
183 const char *z_HTTP_header_lookup(const Z_HTTP_Header *hp, const char *n)
185 for (; hp; hp = hp->next)
186 if (!yaz_matchstr(hp->name, n))
192 Z_GDU *z_get_HTTP_Request(ODR o)
194 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
195 Z_HTTP_Request *hreq;
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;
201 hreq->content_len = 0;
202 hreq->content_buf = 0;
203 hreq->version = "1.1";
204 hreq->method = "POST";
206 z_HTTP_header_add(o, &hreq->headers, "User-Agent", "YAZ/" YAZ_VERSION);
211 Z_GDU *z_get_HTTP_Request_host_path(ODR odr,
215 Z_GDU *p = z_get_HTTP_Request(odr);
217 p->u.HTTP_Request->path = odr_strdup(odr, path);
221 const char *cp0 = strstr(host, "://");
228 cp1 = strchr(cp0, '/');
230 cp1 = cp0+strlen(cp0);
234 char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
235 memcpy (h, cp0, cp1 - cp0);
237 z_HTTP_header_add(odr, &p->u.HTTP_Request->headers,
245 Z_GDU *z_get_HTTP_Response(ODR o, int code)
247 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
248 Z_HTTP_Response *hres;
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;
254 hres->content_len = 0;
255 hres->content_buf = 0;
257 hres->version = "1.1";
258 z_HTTP_header_add(o, &hres->headers, "Server",
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"
267 " <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
270 " <P><A HREF=\"http://www.indexdata.dk/yaz/\">YAZ</A> "
272 " <P>Error: %d</P>\n"
273 " <P>Description: %.50s</P>\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");
283 const char *z_HTTP_errmsg(int code)
287 else if (code == 400)
288 return "Bad Request";
289 else if (code == 404)
291 else if (code == 405)
292 return "Method Not Allowed";
293 else if (code == 500)
294 return "Internal Error";
296 return "Unknown Error";
299 int yaz_decode_http_response(ODR o, Z_HTTP_Response **hr_p)
302 Z_HTTP_Response *hr = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hr));
309 while (i < o->size-2 && !strchr(" \r\n", o->buf[i]))
311 hr->version = (char *) odr_malloc(o, i - po + 1);
313 memcpy(hr->version, o->buf + po, i - po);
314 hr->version[i-po] = 0;
315 if (o->buf[i] != ' ')
322 while (i < o->size-2 && o->buf[i] >= '0' && o->buf[i] <= '9')
324 hr->code = hr->code*10 + (o->buf[i] - '0');
327 while (i < o->size-1 && o->buf[i] != '\n')
329 return decode_headers_content(o, i, &hr->headers,
330 &hr->content_buf, &hr->content_len);
333 int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p)
336 Z_HTTP_Request *hr = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
341 for (i = 0; o->buf[i] != ' '; i++)
342 if (i >= o->size-5 || i > 30)
347 hr->method = (char *) odr_malloc(o, i+1);
348 memcpy (hr->method, o->buf, i);
349 hr->method[i] = '\0';
352 for (i = po; o->buf[i] != ' '; i++)
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';
363 if (i > o->size-5 || memcmp(o->buf+i, "HTTP/", 5))
370 while (i < o->size && !strchr("\r\n", o->buf[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';
376 if (i < o->size-1 && o->buf[i] == '\r')
378 if (o->buf[i] != '\n')
383 return decode_headers_content(o, i, &hr->headers,
384 &hr->content_buf, &hr->content_len);
387 int yaz_encode_http_response(ODR o, Z_HTTP_Response *hr)
393 sprintf(sbuf, "HTTP/%s %d %s\r\n", hr->version,
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,
402 sprintf(lstr, "Content-Length: %d\r\n",
404 odr_write(o, (unsigned char *) lstr, strlen(lstr));
406 for (h = hr->headers; h; h = h->next)
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);
413 odr_write(o, (unsigned char *) "\r\n", 2);
415 odr_write(o, (unsigned char *)
418 if (o->direction == ODR_PRINT)
420 odr_printf(o, "-- HTTP response:\n%.*s\n", o->top - top0,
422 odr_printf(o, "-- \n");
427 int yaz_encode_http_request(ODR o, Z_HTTP_Request *hr)
432 odr_write(o, (unsigned char *) hr->method,
434 odr_write(o, (unsigned char *) " ", 1);
435 odr_write(o, (unsigned char *) 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,
446 sprintf(lstr, "Content-Length: %d\r\n",
448 odr_write(o, (unsigned char *) lstr, strlen(lstr));
450 for (h = hr->headers; h; h = h->next)
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);
457 odr_write(o, (unsigned char *) "\r\n", 2);
459 odr_write(o, (unsigned char *)
462 if (o->direction == ODR_PRINT)
464 odr_printf(o, "-- HTTP request:\n%.*s\n", o->top - top0,
466 odr_printf(o, "-- \n");
474 * indent-tabs-mode: nil
476 * vim: shiftwidth=4 tabstop=8 expandtab