1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
8 * \brief Implements HTTP decoding
13 #include <yaz/yaz-version.h>
14 #include <yaz/yaz-iconv.h>
18 #define strncasecmp _strnicmp
19 #define strcasecmp _stricmp
24 * This function's counterpart, yaz_base64decode(), is in srwutil.c.
25 * I feel bad that they're not together, but each function is only
26 * needed in one place, and those places are not together. Maybe one
27 * day we'll move them into a new httputil.c, and declare them in a
28 * corresponding httputil.h
30 static void yaz_base64encode(const char *in, char *out)
32 static char encoding[] =
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
50 /* Treat three eight-bit numbers as on 24-bit number */
51 n = (buf[0] << 16) + (buf[1] << 8) + buf[2];
53 /* Write the six-bit chunks out as four encoded characters */
54 *out++ = encoding[(n >> 18) & 63];
55 *out++ = encoding[(n >> 12) & 63];
57 *out++ = encoding[(n >> 6) & 63];
58 if (in[1] != 0 && in[2] != 0)
59 *out++ = encoding[n & 63];
73 static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers,
74 char **content_buf, int *content_len)
80 while (i < o->size-1 && o->buf[i] == '\n')
84 if (o->buf[i] == '\r' && i < o->size-1 && o->buf[i+1] == '\n')
89 if (o->buf[i] == '\n')
98 else if (o->buf[i] == ':')
101 *headers = (Z_HTTP_Header *) odr_malloc(o, sizeof(**headers));
102 (*headers)->name = (char*) odr_malloc(o, i - po + 1);
103 memcpy ((*headers)->name, o->buf + po, i - po);
104 (*headers)->name[i - po] = '\0';
106 while (i < o->size-1 && o->buf[i] == ' ')
108 for (po = i; i < o->size-1 && !strchr("\r\n", o->buf[i]); i++)
111 (*headers)->value = (char*) odr_malloc(o, i - po + 1);
112 memcpy ((*headers)->value, o->buf + po, i - po);
113 (*headers)->value[i - po] = '\0';
115 if (!strcasecmp((*headers)->name, "Transfer-Encoding")
117 !strcasecmp((*headers)->value, "chunked"))
119 headers = &(*headers)->next;
120 if (i < o->size-1 && o->buf[i] == '\r')
124 if (o->buf[i] != '\n')
135 /* we know buffer will be smaller than o->size - i*/
136 *content_buf = (char*) odr_malloc(o, o->size - i);
140 /* chunk length .. */
142 for (; i < o->size-2; i++)
143 if (isdigit(o->buf[i]))
144 chunk_len = chunk_len * 16 +
146 else if (isupper(o->buf[i]))
147 chunk_len = chunk_len * 16 +
148 (o->buf[i] - ('A'-10));
149 else if (islower(o->buf[i]))
150 chunk_len = chunk_len * 16 +
151 (o->buf[i] - ('a'-10));
154 /* chunk extension ... */
155 while (o->buf[i] != '\r' && o->buf[i+1] != '\n')
164 i += 2; /* skip CRLF */
167 if (chunk_len < 0 || off + chunk_len > o->size)
173 memcpy (*content_buf + off, o->buf + i, chunk_len);
174 i += chunk_len + 2; /* skip chunk+CRLF */
188 else if (i == o->size)
195 *content_len = o->size - i;
196 *content_buf = (char*) odr_malloc(o, *content_len + 1);
197 memcpy(*content_buf, o->buf + i, *content_len);
198 (*content_buf)[*content_len] = '\0';
204 void z_HTTP_header_add_content_type(ODR o, Z_HTTP_Header **hp,
205 const char *content_type,
208 const char *l = "Content-Type";
211 char *ctype = (char *)
212 odr_malloc(o, strlen(content_type)+strlen(charset) + 15);
213 sprintf(ctype, "%s; charset=%s", content_type, charset);
214 z_HTTP_header_add(o, hp, l, ctype);
217 z_HTTP_header_add(o, hp, l, content_type);
222 * HTTP Basic authentication is described at:
223 * http://tools.ietf.org/html/rfc1945#section-11.1
225 void z_HTTP_header_add_basic_auth(ODR o, Z_HTTP_Header **hp,
226 const char *username, const char *password)
234 len = strlen(username) + strlen(password);
235 tmp = (char *) odr_malloc(o, len+2);
236 sprintf(tmp, "%s:%s", username, password);
237 buf = (char *) odr_malloc(o, (len+1) * 8/6 + 12);
238 strcpy(buf, "Basic ");
239 yaz_base64encode(tmp, &buf[strlen(buf)]);
240 z_HTTP_header_add(o, hp, "Authorization", buf);
244 void z_HTTP_header_add(ODR o, Z_HTTP_Header **hp, const char *n,
249 *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
250 (*hp)->name = odr_strdup(o, n);
251 (*hp)->value = odr_strdup(o, v);
255 const char *z_HTTP_header_lookup(const Z_HTTP_Header *hp, const char *n)
257 for (; hp; hp = hp->next)
258 if (!yaz_matchstr(hp->name, n))
264 Z_GDU *z_get_HTTP_Request(ODR o)
266 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
267 Z_HTTP_Request *hreq;
269 p->which = Z_GDU_HTTP_Request;
270 p->u.HTTP_Request = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hreq));
271 hreq = p->u.HTTP_Request;
273 hreq->content_len = 0;
274 hreq->content_buf = 0;
275 hreq->version = "1.1";
276 hreq->method = "POST";
278 z_HTTP_header_add(o, &hreq->headers, "User-Agent", "YAZ/" YAZ_VERSION);
283 Z_GDU *z_get_HTTP_Request_host_path(ODR odr,
287 Z_GDU *p = z_get_HTTP_Request(odr);
289 p->u.HTTP_Request->path = odr_strdup(odr, path);
293 const char *cp0 = strstr(host, "://");
300 cp1 = strchr(cp0, '/');
302 cp1 = cp0+strlen(cp0);
306 char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
307 memcpy (h, cp0, cp1 - cp0);
309 z_HTTP_header_add(odr, &p->u.HTTP_Request->headers,
317 Z_GDU *z_get_HTTP_Response(ODR o, int code)
319 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
320 Z_HTTP_Response *hres;
322 p->which = Z_GDU_HTTP_Response;
323 p->u.HTTP_Response = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hres));
324 hres = p->u.HTTP_Response;
326 hres->content_len = 0;
327 hres->content_buf = 0;
329 hres->version = "1.1";
330 z_HTTP_header_add(o, &hres->headers, "Server",
334 hres->content_buf = (char*) odr_malloc(o, 400);
335 sprintf (hres->content_buf,
336 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
339 " <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
342 " <P><A HREF=\"http://www.indexdata.dk/yaz/\">YAZ</A> "
344 " <P>Error: %d</P>\n"
345 " <P>Description: %.50s</P>\n"
348 code, z_HTTP_errmsg(code));
349 hres->content_len = strlen(hres->content_buf);
350 z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
355 const char *z_HTTP_errmsg(int code)
359 else if (code == 400)
360 return "Bad Request";
361 else if (code == 404)
363 else if (code == 405)
364 return "Method Not Allowed";
365 else if (code == 500)
366 return "Internal Error";
368 return "Unknown Error";
371 int yaz_decode_http_response(ODR o, Z_HTTP_Response **hr_p)
374 Z_HTTP_Response *hr = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hr));
381 while (i < o->size-2 && !strchr(" \r\n", o->buf[i]))
383 hr->version = (char *) odr_malloc(o, i - po + 1);
385 memcpy(hr->version, o->buf + po, i - po);
386 hr->version[i-po] = 0;
387 if (o->buf[i] != ' ')
394 while (i < o->size-2 && o->buf[i] >= '0' && o->buf[i] <= '9')
396 hr->code = hr->code*10 + (o->buf[i] - '0');
399 while (i < o->size-1 && o->buf[i] != '\n')
401 return decode_headers_content(o, i, &hr->headers,
402 &hr->content_buf, &hr->content_len);
405 int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p)
408 Z_HTTP_Request *hr = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
413 for (i = 0; o->buf[i] != ' '; i++)
414 if (i >= o->size-5 || i > 30)
419 hr->method = (char *) odr_malloc(o, i+1);
420 memcpy (hr->method, o->buf, i);
421 hr->method[i] = '\0';
424 for (i = po; o->buf[i] != ' '; i++)
430 hr->path = (char *) odr_malloc(o, i - po + 1);
431 memcpy (hr->path, o->buf+po, i - po);
432 hr->path[i - po] = '\0';
435 if (i > o->size-5 || memcmp(o->buf+i, "HTTP/", 5))
442 while (i < o->size && !strchr("\r\n", o->buf[i]))
444 hr->version = (char *) odr_malloc(o, i - po + 1);
445 memcpy(hr->version, o->buf + po, i - po);
446 hr->version[i - po] = '\0';
448 if (i < o->size-1 && o->buf[i] == '\r')
450 if (o->buf[i] != '\n')
455 return decode_headers_content(o, i, &hr->headers,
456 &hr->content_buf, &hr->content_len);
459 int yaz_encode_http_response(ODR o, Z_HTTP_Response *hr)
465 sprintf(sbuf, "HTTP/%s %d %s\r\n", hr->version,
467 z_HTTP_errmsg(hr->code));
468 odr_write(o, (unsigned char *) sbuf, strlen(sbuf));
469 /* apply Content-Length if not already applied */
470 if (!z_HTTP_header_lookup(hr->headers,
474 sprintf(lstr, "Content-Length: %d\r\n",
476 odr_write(o, (unsigned char *) lstr, strlen(lstr));
478 for (h = hr->headers; h; h = h->next)
480 odr_write(o, (unsigned char *) h->name, strlen(h->name));
481 odr_write(o, (unsigned char *) ": ", 2);
482 odr_write(o, (unsigned char *) h->value, strlen(h->value));
483 odr_write(o, (unsigned char *) "\r\n", 2);
485 odr_write(o, (unsigned char *) "\r\n", 2);
487 odr_write(o, (unsigned char *)
490 if (o->direction == ODR_PRINT)
492 odr_printf(o, "-- HTTP response:\n%.*s\n", o->top - top0,
494 odr_printf(o, "-- \n");
499 int yaz_encode_http_request(ODR o, Z_HTTP_Request *hr)
504 odr_write(o, (unsigned char *) hr->method,
506 odr_write(o, (unsigned char *) " ", 1);
507 odr_write(o, (unsigned char *) hr->path,
509 odr_write(o, (unsigned char *) " HTTP/", 6);
510 odr_write(o, (unsigned char *) hr->version,
511 strlen(hr->version));
512 odr_write(o, (unsigned char *) "\r\n", 2);
513 if (hr->content_len &&
514 !z_HTTP_header_lookup(hr->headers,
518 sprintf(lstr, "Content-Length: %d\r\n",
520 odr_write(o, (unsigned char *) lstr, strlen(lstr));
522 for (h = hr->headers; h; h = h->next)
524 odr_write(o, (unsigned char *) h->name, strlen(h->name));
525 odr_write(o, (unsigned char *) ": ", 2);
526 odr_write(o, (unsigned char *) h->value, strlen(h->value));
527 odr_write(o, (unsigned char *) "\r\n", 2);
529 odr_write(o, (unsigned char *) "\r\n", 2);
531 odr_write(o, (unsigned char *)
534 if (o->direction == ODR_PRINT)
536 odr_printf(o, "-- HTTP request:\n%.*s\n", o->top - top0,
538 odr_printf(o, "-- \n");
546 * indent-tabs-mode: nil
548 * vim: shiftwidth=4 tabstop=8 expandtab