2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: http.c,v 1.1 2007-01-11 10:55:57 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 = 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(const 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", "YAZ/" YAZ_VERSION);
210 Z_GDU *z_get_HTTP_Request_host_path(ODR odr,
214 Z_GDU *p = z_get_HTTP_Request(odr);
216 p->u.HTTP_Request->path = odr_strdup(odr, path);
220 const char *cp0 = strstr(host, "://");
227 cp1 = strchr(cp0, '/');
229 cp1 = cp0+strlen(cp0);
233 char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
234 memcpy (h, cp0, cp1 - cp0);
236 z_HTTP_header_add(odr, &p->u.HTTP_Request->headers,
244 Z_GDU *z_get_HTTP_Response(ODR o, int code)
246 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
247 Z_HTTP_Response *hres;
249 p->which = Z_GDU_HTTP_Response;
250 p->u.HTTP_Response = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hres));
251 hres = p->u.HTTP_Response;
253 hres->content_len = 0;
254 hres->content_buf = 0;
256 hres->version = "1.1";
257 z_HTTP_header_add(o, &hres->headers, "Server",
261 hres->content_buf = (char*) odr_malloc(o, 400);
262 sprintf (hres->content_buf,
263 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
266 " <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
269 " <P><A HREF=\"http://www.indexdata.dk/yaz/\">YAZ</A> "
271 " <P>Error: %d</P>\n"
272 " <P>Description: %.50s</P>\n"
275 code, z_HTTP_errmsg(code));
276 hres->content_len = strlen(hres->content_buf);
277 z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
282 const char *z_HTTP_errmsg(int code)
286 else if (code == 400)
287 return "Bad Request";
288 else if (code == 404)
290 else if (code == 405)
291 return "Method Not Allowed";
292 else if (code == 500)
293 return "Internal Error";
295 return "Unknown Error";
298 int yaz_decode_http_response(ODR o, Z_HTTP_Response **hr_p)
301 Z_HTTP_Response *hr = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hr));
308 while (i < o->size-2 && !strchr(" \r\n", o->buf[i]))
310 hr->version = (char *) odr_malloc(o, i - po + 1);
312 memcpy(hr->version, o->buf + po, i - po);
313 hr->version[i-po] = 0;
314 if (o->buf[i] != ' ')
321 while (i < o->size-2 && o->buf[i] >= '0' && o->buf[i] <= '9')
323 hr->code = hr->code*10 + (o->buf[i] - '0');
326 while (i < o->size-1 && o->buf[i] != '\n')
328 return decode_headers_content(o, i, &hr->headers,
329 &hr->content_buf, &hr->content_len);
332 int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p)
335 Z_HTTP_Request *hr = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
340 for (i = 0; o->buf[i] != ' '; i++)
341 if (i >= o->size-5 || i > 30)
346 hr->method = (char *) odr_malloc(o, i+1);
347 memcpy (hr->method, o->buf, i);
348 hr->method[i] = '\0';
351 for (i = po; o->buf[i] != ' '; i++)
357 hr->path = (char *) odr_malloc(o, i - po + 1);
358 memcpy (hr->path, o->buf+po, i - po);
359 hr->path[i - po] = '\0';
362 if (i > o->size-5 || memcmp(o->buf+i, "HTTP/", 5))
369 while (i < o->size && !strchr("\r\n", o->buf[i]))
371 hr->version = (char *) odr_malloc(o, i - po + 1);
372 memcpy(hr->version, o->buf + po, i - po);
373 hr->version[i - po] = '\0';
375 if (i < o->size-1 && o->buf[i] == '\r')
377 if (o->buf[i] != '\n')
382 return decode_headers_content(o, i, &hr->headers,
383 &hr->content_buf, &hr->content_len);
386 int yaz_encode_http_response(ODR o, Z_HTTP_Response *hr)
392 sprintf(sbuf, "HTTP/%s %d %s\r\n", hr->version,
394 z_HTTP_errmsg(hr->code));
395 odr_write(o, (unsigned char *) sbuf, strlen(sbuf));
396 /* apply Content-Length if not already applied */
397 if (!z_HTTP_header_lookup(hr->headers,
401 sprintf(lstr, "Content-Length: %d\r\n",
403 odr_write(o, (unsigned char *) lstr, strlen(lstr));
405 for (h = hr->headers; h; h = h->next)
407 odr_write(o, (unsigned char *) h->name, strlen(h->name));
408 odr_write(o, (unsigned char *) ": ", 2);
409 odr_write(o, (unsigned char *) h->value, strlen(h->value));
410 odr_write(o, (unsigned char *) "\r\n", 2);
412 odr_write(o, (unsigned char *) "\r\n", 2);
414 odr_write(o, (unsigned char *)
417 if (o->direction == ODR_PRINT)
419 odr_printf(o, "-- HTTP response:\n%.*s\n", o->top - top0,
421 odr_printf(o, "-- \n");
426 int yaz_encode_http_request(ODR o, Z_HTTP_Request *hr)
431 odr_write(o, (unsigned char *) hr->method,
433 odr_write(o, (unsigned char *) " ", 1);
434 odr_write(o, (unsigned char *) hr->path,
436 odr_write(o, (unsigned char *) " HTTP/", 6);
437 odr_write(o, (unsigned char *) hr->version,
438 strlen(hr->version));
439 odr_write(o, (unsigned char *) "\r\n", 2);
440 if (hr->content_len &&
441 !z_HTTP_header_lookup(hr->headers,
445 sprintf(lstr, "Content-Length: %d\r\n",
447 odr_write(o, (unsigned char *) lstr, strlen(lstr));
449 for (h = hr->headers; h; h = h->next)
451 odr_write(o, (unsigned char *) h->name, strlen(h->name));
452 odr_write(o, (unsigned char *) ": ", 2);
453 odr_write(o, (unsigned char *) h->value, strlen(h->value));
454 odr_write(o, (unsigned char *) "\r\n", 2);
456 odr_write(o, (unsigned char *) "\r\n", 2);
458 odr_write(o, (unsigned char *)
461 if (o->direction == ODR_PRINT)
463 odr_printf(o, "-- HTTP request:\n%.*s\n", o->top - top0,
465 odr_printf(o, "-- \n");
473 * indent-tabs-mode: nil
475 * vim: shiftwidth=4 tabstop=8 expandtab