1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements HTTP decoding
15 #include <yaz/yaz-version.h>
16 #include <yaz/yaz-iconv.h>
17 #include <yaz/matchstr.h>
21 #define strncasecmp _strnicmp
22 #define strcasecmp _stricmp
27 * This function's counterpart, yaz_base64decode(), is in srwutil.c.
28 * I feel bad that they're not together, but each function is only
29 * needed in one place, and those places are not together. Maybe one
30 * day we'll move them into a new httputil.c, and declare them in a
31 * corresponding httputil.h
33 static void yaz_base64encode(const char *in, char *out)
35 static char encoding[] =
36 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
53 /* Treat three eight-bit numbers as on 24-bit number */
54 n = (buf[0] << 16) + (buf[1] << 8) + buf[2];
56 /* Write the six-bit chunks out as four encoded characters */
57 *out++ = encoding[(n >> 18) & 63];
58 *out++ = encoding[(n >> 12) & 63];
60 *out++ = encoding[(n >> 6) & 63];
61 if (in[1] != 0 && in[2] != 0)
62 *out++ = encoding[n & 63];
76 static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers,
77 char **content_buf, int *content_len)
83 while (i < o->size-1 && o->buf[i] == '\n')
87 if (o->buf[i] == '\r' && i < o->size-1 && o->buf[i+1] == '\n')
92 if (o->buf[i] == '\n')
101 else if (o->buf[i] == ':')
104 *headers = (Z_HTTP_Header *) odr_malloc(o, sizeof(**headers));
105 (*headers)->name = (char*) odr_malloc(o, i - po + 1);
106 memcpy ((*headers)->name, o->buf + po, i - po);
107 (*headers)->name[i - po] = '\0';
109 while (i < o->size-1 && o->buf[i] == ' ')
111 for (po = i; i < o->size-1 && !strchr("\r\n", o->buf[i]); i++)
114 (*headers)->value = (char*) odr_malloc(o, i - po + 1);
115 memcpy ((*headers)->value, o->buf + po, i - po);
116 (*headers)->value[i - po] = '\0';
118 if (!strcasecmp((*headers)->name, "Transfer-Encoding")
120 !strcasecmp((*headers)->value, "chunked"))
122 headers = &(*headers)->next;
123 if (i < o->size-1 && o->buf[i] == '\r')
127 if (o->buf[i] != '\n')
138 /* we know buffer will be smaller than o->size - i*/
139 *content_buf = (char*) odr_malloc(o, o->size - i);
143 /* chunk length .. */
145 for (; i < o->size-2; i++)
146 if (isdigit(o->buf[i]))
147 chunk_len = chunk_len * 16 +
149 else if (isupper(o->buf[i]))
150 chunk_len = chunk_len * 16 +
151 (o->buf[i] - ('A'-10));
152 else if (islower(o->buf[i]))
153 chunk_len = chunk_len * 16 +
154 (o->buf[i] - ('a'-10));
157 /* chunk extension ... */
158 while (o->buf[i] != '\r' && o->buf[i+1] != '\n')
167 i += 2; /* skip CRLF */
170 if (chunk_len < 0 || off + chunk_len > o->size)
176 memcpy (*content_buf + off, o->buf + i, chunk_len);
177 i += chunk_len + 2; /* skip chunk+CRLF */
191 else if (i == o->size)
198 *content_len = o->size - i;
199 *content_buf = (char*) odr_malloc(o, *content_len + 1);
200 memcpy(*content_buf, o->buf + i, *content_len);
201 (*content_buf)[*content_len] = '\0';
207 void z_HTTP_header_add_content_type(ODR o, Z_HTTP_Header **hp,
208 const char *content_type,
211 const char *l = "Content-Type";
214 char *ctype = (char *)
215 odr_malloc(o, strlen(content_type)+strlen(charset) + 15);
216 sprintf(ctype, "%s; charset=%s", content_type, charset);
217 z_HTTP_header_add(o, hp, l, ctype);
220 z_HTTP_header_add(o, hp, l, content_type);
225 * HTTP Basic authentication is described at:
226 * http://tools.ietf.org/html/rfc1945#section-11.1
228 void z_HTTP_header_add_basic_auth(ODR o, Z_HTTP_Header **hp,
229 const char *username, const char *password)
237 len = strlen(username) + strlen(password);
238 tmp = (char *) odr_malloc(o, len+2);
239 sprintf(tmp, "%s:%s", username, password);
240 buf = (char *) odr_malloc(o, (len+1) * 8/6 + 12);
241 strcpy(buf, "Basic ");
242 yaz_base64encode(tmp, &buf[strlen(buf)]);
243 z_HTTP_header_add(o, hp, "Authorization", buf);
247 void z_HTTP_header_add(ODR o, Z_HTTP_Header **hp, const char *n,
252 *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
253 (*hp)->name = odr_strdup(o, n);
254 (*hp)->value = odr_strdup(o, v);
258 const char *z_HTTP_header_lookup(const Z_HTTP_Header *hp, const char *n)
260 for (; hp; hp = hp->next)
261 if (!yaz_matchstr(hp->name, n))
267 Z_GDU *z_get_HTTP_Request(ODR o)
269 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
270 Z_HTTP_Request *hreq;
272 p->which = Z_GDU_HTTP_Request;
273 p->u.HTTP_Request = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hreq));
274 hreq = p->u.HTTP_Request;
276 hreq->content_len = 0;
277 hreq->content_buf = 0;
278 hreq->version = "1.1";
279 hreq->method = "POST";
281 z_HTTP_header_add(o, &hreq->headers, "User-Agent", "YAZ/" YAZ_VERSION);
286 Z_GDU *z_get_HTTP_Request_host_path(ODR odr,
290 Z_GDU *p = z_get_HTTP_Request(odr);
292 p->u.HTTP_Request->path = odr_strdup(odr, path);
296 const char *cp0 = strstr(host, "://");
303 cp1 = strchr(cp0, '/');
305 cp1 = cp0+strlen(cp0);
309 char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
310 memcpy (h, cp0, cp1 - cp0);
312 z_HTTP_header_add(odr, &p->u.HTTP_Request->headers,
320 Z_GDU *z_get_HTTP_Response(ODR o, int code)
322 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
323 Z_HTTP_Response *hres;
325 p->which = Z_GDU_HTTP_Response;
326 p->u.HTTP_Response = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hres));
327 hres = p->u.HTTP_Response;
329 hres->content_len = 0;
330 hres->content_buf = 0;
332 hres->version = "1.1";
333 z_HTTP_header_add(o, &hres->headers, "Server",
337 hres->content_buf = (char*) odr_malloc(o, 400);
338 sprintf(hres->content_buf,
339 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\""
340 " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
343 " <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
346 " <P><A HREF=\"http://www.indexdata.com/yaz/\">YAZ</A> "
348 " <P>Error: %d</P>\n"
349 " <P>Description: %.50s</P>\n"
352 code, z_HTTP_errmsg(code));
353 hres->content_len = strlen(hres->content_buf);
354 z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
359 const char *z_HTTP_errmsg(int code)
363 else if (code == 400)
364 return "Bad Request";
365 else if (code == 404)
367 else if (code == 405)
368 return "Method Not Allowed";
369 else if (code == 500)
370 return "Internal Error";
372 return "Unknown Error";
375 int yaz_decode_http_response(ODR o, Z_HTTP_Response **hr_p)
378 Z_HTTP_Response *hr = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hr));
385 while (i < o->size-2 && !strchr(" \r\n", o->buf[i]))
387 hr->version = (char *) odr_malloc(o, i - po + 1);
389 memcpy(hr->version, o->buf + po, i - po);
390 hr->version[i-po] = 0;
391 if (o->buf[i] != ' ')
398 while (i < o->size-2 && o->buf[i] >= '0' && o->buf[i] <= '9')
400 hr->code = hr->code*10 + (o->buf[i] - '0');
403 while (i < o->size-1 && o->buf[i] != '\n')
405 return decode_headers_content(o, i, &hr->headers,
406 &hr->content_buf, &hr->content_len);
409 int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p)
412 Z_HTTP_Request *hr = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
417 for (i = 0; o->buf[i] != ' '; i++)
418 if (i >= o->size-5 || i > 30)
423 hr->method = (char *) odr_malloc(o, i+1);
424 memcpy (hr->method, o->buf, i);
425 hr->method[i] = '\0';
428 for (i = po; o->buf[i] != ' '; i++)
434 hr->path = (char *) odr_malloc(o, i - po + 1);
435 memcpy (hr->path, o->buf+po, i - po);
436 hr->path[i - po] = '\0';
439 if (i > o->size-5 || memcmp(o->buf+i, "HTTP/", 5))
446 while (i < o->size && !strchr("\r\n", o->buf[i]))
448 hr->version = (char *) odr_malloc(o, i - po + 1);
449 memcpy(hr->version, o->buf + po, i - po);
450 hr->version[i - po] = '\0';
452 if (i < o->size-1 && o->buf[i] == '\r')
454 if (o->buf[i] != '\n')
459 return decode_headers_content(o, i, &hr->headers,
460 &hr->content_buf, &hr->content_len);
463 int yaz_encode_http_response(ODR o, Z_HTTP_Response *hr)
469 sprintf(sbuf, "HTTP/%s %d %s\r\n", hr->version,
471 z_HTTP_errmsg(hr->code));
472 odr_write(o, (unsigned char *) sbuf, strlen(sbuf));
473 /* apply Content-Length if not already applied */
474 if (!z_HTTP_header_lookup(hr->headers,
478 sprintf(lstr, "Content-Length: %d\r\n",
480 odr_write(o, (unsigned char *) lstr, strlen(lstr));
482 for (h = hr->headers; h; h = h->next)
484 odr_write(o, (unsigned char *) h->name, strlen(h->name));
485 odr_write(o, (unsigned char *) ": ", 2);
486 odr_write(o, (unsigned char *) h->value, strlen(h->value));
487 odr_write(o, (unsigned char *) "\r\n", 2);
489 odr_write(o, (unsigned char *) "\r\n", 2);
491 odr_write(o, (unsigned char *)
494 if (o->direction == ODR_PRINT)
496 odr_printf(o, "-- HTTP response:\n%.*s\n", o->top - top0,
498 odr_printf(o, "-- \n");
503 int yaz_encode_http_request(ODR o, Z_HTTP_Request *hr)
508 odr_write(o, (unsigned char *) hr->method,
510 odr_write(o, (unsigned char *) " ", 1);
511 odr_write(o, (unsigned char *) hr->path,
513 odr_write(o, (unsigned char *) " HTTP/", 6);
514 odr_write(o, (unsigned char *) hr->version,
515 strlen(hr->version));
516 odr_write(o, (unsigned char *) "\r\n", 2);
517 if (hr->content_len &&
518 !z_HTTP_header_lookup(hr->headers,
522 sprintf(lstr, "Content-Length: %d\r\n",
524 odr_write(o, (unsigned char *) lstr, strlen(lstr));
526 for (h = hr->headers; h; h = h->next)
528 odr_write(o, (unsigned char *) h->name, strlen(h->name));
529 odr_write(o, (unsigned char *) ": ", 2);
530 odr_write(o, (unsigned char *) h->value, strlen(h->value));
531 odr_write(o, (unsigned char *) "\r\n", 2);
533 odr_write(o, (unsigned char *) "\r\n", 2);
535 odr_write(o, (unsigned char *)
538 if (o->direction == ODR_PRINT)
540 odr_printf(o, "-- HTTP request:\n%.*s\n", o->top - top0,
542 odr_printf(o, "-- \n");
550 * c-file-style: "Stroustrup"
551 * indent-tabs-mode: nil
553 * vim: shiftwidth=4 tabstop=8 expandtab