1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements HTTP decoding
14 #include <yaz/yaz-version.h>
15 #include <yaz/yaz-iconv.h>
16 #include <yaz/matchstr.h>
18 #include <yaz/base64.h>
21 #define strncasecmp _strnicmp
22 #define strcasecmp _stricmp
25 static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers,
26 char **content_buf, int *content_len)
32 while (i < o->size-1 && o->buf[i] == '\n')
36 if (o->buf[i] == '\r' && i < o->size-1 && o->buf[i+1] == '\n')
41 if (o->buf[i] == '\n')
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 && !strchr("\r\n", o->buf[i]); 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;
72 if (i < o->size-1 && o->buf[i] == '\r')
76 if (o->buf[i] != '\n')
87 /* we know buffer will be smaller than o->size - i*/
88 *content_buf = (char*) odr_malloc(o, o->size - i);
94 for (; i < o->size-2; i++)
95 if (yaz_isdigit(o->buf[i]))
96 chunk_len = chunk_len * 16 +
98 else if (yaz_isupper(o->buf[i]))
99 chunk_len = chunk_len * 16 +
100 (o->buf[i] - ('A'-10));
101 else if (yaz_islower(o->buf[i]))
102 chunk_len = chunk_len * 16 +
103 (o->buf[i] - ('a'-10));
106 /* chunk extension ... */
107 while (o->buf[i] != '\r' && o->buf[i+1] != '\n')
116 i += 2; /* skip CRLF */
119 if (chunk_len < 0 || off + chunk_len > o->size)
125 memcpy (*content_buf + off, o->buf + i, chunk_len);
126 i += chunk_len + 2; /* skip chunk+CRLF */
140 else if (i == o->size)
147 *content_len = o->size - i;
148 *content_buf = (char*) odr_malloc(o, *content_len + 1);
149 memcpy(*content_buf, o->buf + i, *content_len);
150 (*content_buf)[*content_len] = '\0';
156 void z_HTTP_header_add_content_type(ODR o, Z_HTTP_Header **hp,
157 const char *content_type,
160 const char *l = "Content-Type";
163 char *ctype = (char *)
164 odr_malloc(o, strlen(content_type)+strlen(charset) + 15);
165 sprintf(ctype, "%s; charset=%s", content_type, charset);
166 z_HTTP_header_add(o, hp, l, ctype);
169 z_HTTP_header_add(o, hp, l, content_type);
174 * HTTP Basic authentication is described at:
175 * http://tools.ietf.org/html/rfc1945#section-11.1
177 void z_HTTP_header_add_basic_auth(ODR o, Z_HTTP_Header **hp,
178 const char *username, const char *password)
188 len = strlen(username) + strlen(password);
189 tmp = (char *) odr_malloc(o, len+2);
190 sprintf(tmp, "%s:%s", username, password);
191 buf = (char *) odr_malloc(o, (len+1) * 8/6 + 12);
192 strcpy(buf, "Basic ");
193 yaz_base64encode(tmp, &buf[strlen(buf)]);
194 z_HTTP_header_add(o, hp, "Authorization", buf);
198 void z_HTTP_header_add(ODR o, Z_HTTP_Header **hp, const char *n,
203 *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
204 (*hp)->name = odr_strdup(o, n);
205 (*hp)->value = odr_strdup(o, v);
209 void z_HTTP_header_set(ODR o, Z_HTTP_Header **hp, const char *n,
214 if (!strcmp((*hp)->name, n))
216 (*hp)->value = odr_strdup(o, v);
221 *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
222 (*hp)->name = odr_strdup(o, n);
223 (*hp)->value = odr_strdup(o, v);
227 const char *z_HTTP_header_lookup(const Z_HTTP_Header *hp, const char *n)
229 for (; hp; hp = hp->next)
230 if (!yaz_matchstr(hp->name, n))
236 Z_GDU *z_get_HTTP_Request(ODR o)
238 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
239 Z_HTTP_Request *hreq;
241 p->which = Z_GDU_HTTP_Request;
242 p->u.HTTP_Request = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hreq));
243 hreq = p->u.HTTP_Request;
245 hreq->content_len = 0;
246 hreq->content_buf = 0;
247 hreq->version = "1.1";
248 hreq->method = "POST";
250 z_HTTP_header_add(o, &hreq->headers, "User-Agent", "YAZ/" YAZ_VERSION);
255 Z_GDU *z_get_HTTP_Request_host_path(ODR odr,
259 Z_GDU *p = z_get_HTTP_Request(odr);
261 p->u.HTTP_Request->path = odr_strdup(odr, path);
265 const char *cp0 = strstr(host, "://");
272 cp1 = strchr(cp0, '/');
274 cp1 = cp0+strlen(cp0);
278 char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
279 memcpy (h, cp0, cp1 - cp0);
281 z_HTTP_header_add(odr, &p->u.HTTP_Request->headers,
288 Z_GDU *z_get_HTTP_Request_uri(ODR odr, const char *uri, const char *args,
291 Z_GDU *p = z_get_HTTP_Request(odr);
292 const char *cp0 = strstr(uri, "://");
299 cp1 = strchr(cp0, '/');
301 cp1 = cp0+strlen(cp0);
305 char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
306 memcpy (h, cp0, cp1 - cp0);
308 z_HTTP_header_add(odr, &p->u.HTTP_Request->headers,
319 p->u.HTTP_Request->path = odr_malloc(odr, cp1 - uri + strlen(args) + 2);
322 memcpy(p->u.HTTP_Request->path, uri, cp1 - uri);
323 strcpy(p->u.HTTP_Request->path + (cp1 - uri), "/");
326 strcpy(p->u.HTTP_Request->path, "/");
327 strcat(p->u.HTTP_Request->path, args);
331 Z_GDU *z_get_HTTP_Response(ODR o, int code)
333 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
334 Z_HTTP_Response *hres;
336 p->which = Z_GDU_HTTP_Response;
337 p->u.HTTP_Response = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hres));
338 hres = p->u.HTTP_Response;
340 hres->content_len = 0;
341 hres->content_buf = 0;
343 hres->version = "1.1";
344 z_HTTP_header_add(o, &hres->headers, "Server",
348 hres->content_buf = (char*) odr_malloc(o, 400);
349 sprintf(hres->content_buf,
350 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\""
351 " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
354 " <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
357 " <P><A HREF=\"http://www.indexdata.com/yaz/\">YAZ</A> "
359 " <P>Error: %d</P>\n"
360 " <P>Description: %.50s</P>\n"
363 code, z_HTTP_errmsg(code));
364 hres->content_len = strlen(hres->content_buf);
365 z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
370 const char *z_HTTP_errmsg(int code)
377 return "Switching Protocols";
385 return "Non-Authoritative Information";
389 return "Reset Content";
391 return "Partial Content";
393 return "Multiple Choices";
395 return "Moved Permenently";
401 return "Not Modified";
405 return "Temporary Redirect";
407 return "Bad Request";
411 return "Method Not Allowed";
413 return "Not Acceptable";
415 return "Proxy Authentication Required";
417 return "Request Timeout";
423 return "Length Required";
425 return "Precondition Failed";
427 return "Request Entity Too Large";
429 return "Request-URI Too Long";
431 return "Unsupported Media Type";
433 return "Requested Range Not Satisfiable";
435 return "Expectation Failed";
437 return "Internal Error";
439 return "Not Implemented";
441 return "Bad Gateway";
443 return "Service Unavailable";
445 return "Gateway Timeout";
447 return "HTTP Version Not Supported";
449 return "Unknown Error";
453 int yaz_decode_http_response(ODR o, Z_HTTP_Response **hr_p)
456 Z_HTTP_Response *hr = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hr));
463 while (i < o->size-2 && !strchr(" \r\n", o->buf[i]))
465 hr->version = (char *) odr_malloc(o, i - po + 1);
467 memcpy(hr->version, o->buf + po, i - po);
468 hr->version[i-po] = 0;
469 if (o->buf[i] != ' ')
476 while (i < o->size-2 && o->buf[i] >= '0' && o->buf[i] <= '9')
478 hr->code = hr->code*10 + (o->buf[i] - '0');
481 while (i < o->size-1 && o->buf[i] != '\n')
483 return decode_headers_content(o, i, &hr->headers,
484 &hr->content_buf, &hr->content_len);
487 int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p)
490 Z_HTTP_Request *hr = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
495 for (i = 0; o->buf[i] != ' '; i++)
496 if (i >= o->size-5 || i > 30)
501 hr->method = (char *) odr_malloc(o, i+1);
502 memcpy (hr->method, o->buf, i);
503 hr->method[i] = '\0';
506 for (i = po; o->buf[i] != ' '; i++)
512 hr->path = (char *) odr_malloc(o, i - po + 1);
513 memcpy (hr->path, o->buf+po, i - po);
514 hr->path[i - po] = '\0';
517 if (i > o->size-5 || memcmp(o->buf+i, "HTTP/", 5))
524 while (i < o->size && !strchr("\r\n", o->buf[i]))
526 hr->version = (char *) odr_malloc(o, i - po + 1);
527 memcpy(hr->version, o->buf + po, i - po);
528 hr->version[i - po] = '\0';
530 if (i < o->size-1 && o->buf[i] == '\r')
532 if (o->buf[i] != '\n')
537 return decode_headers_content(o, i, &hr->headers,
538 &hr->content_buf, &hr->content_len);
541 static void dump_http_package(ODR o, const char *buf, size_t len)
548 odr_printf(o, "%.*s\n", i, buf);
553 odr_printf(o, "%.*s\n", i, buf);
554 odr_printf(o, "(truncated\n", (long) len);
557 else if (buf[i] == 0)
559 odr_printf(o, "%.*s\n", i, buf);
560 odr_printf(o, "(binary data)\n", (long) len);
566 int yaz_encode_http_response(ODR o, Z_HTTP_Response *hr)
572 sprintf(sbuf, "HTTP/%s %d %s\r\n", hr->version,
574 z_HTTP_errmsg(hr->code));
575 odr_write2(o, sbuf, strlen(sbuf));
576 /* apply Content-Length if not already applied */
577 if (!z_HTTP_header_lookup(hr->headers,
581 sprintf(lstr, "Content-Length: %d\r\n",
583 odr_write2(o, lstr, strlen(lstr));
585 for (h = hr->headers; h; h = h->next)
587 odr_write2(o, h->name, strlen(h->name));
588 odr_write2(o, ": ", 2);
589 odr_write2(o, h->value, strlen(h->value));
590 odr_write2(o, "\r\n", 2);
592 odr_write(o, (unsigned char *) "\r\n", 2);
594 odr_write2(o, hr->content_buf, hr->content_len);
595 if (o->direction == ODR_PRINT)
597 odr_printf(o, "-- HTTP response:\n");
598 dump_http_package(o, (const char *) o->buf + top0, o->top - top0);
599 odr_printf(o, "--\n");
604 int yaz_encode_http_request(ODR o, Z_HTTP_Request *hr)
609 odr_write2(o, hr->method, strlen(hr->method));
610 odr_write2(o, " ", 1);
611 odr_write2(o, hr->path, strlen(hr->path));
612 odr_write2(o, " HTTP/", 6);
613 odr_write2(o, hr->version, strlen(hr->version));
614 odr_write2(o, "\r\n", 2);
615 if (hr->content_len &&
616 !z_HTTP_header_lookup(hr->headers,
620 sprintf(lstr, "Content-Length: %d\r\n",
622 odr_write2(o, lstr, strlen(lstr));
624 for (h = hr->headers; h; h = h->next)
626 odr_write2(o, h->name, strlen(h->name));
627 odr_write2(o, ": ", 2);
628 odr_write2(o, h->value, strlen(h->value));
629 odr_write2(o, "\r\n", 2);
631 odr_write2(o, "\r\n", 2);
633 odr_write2(o, hr->content_buf, hr->content_len);
634 if (o->direction == ODR_PRINT)
636 odr_printf(o, "-- HTTP request:\n");
637 dump_http_package(o, (const char *) o->buf + top0, o->top - top0);
638 odr_printf(o, "--\n");
646 * c-file-style: "Stroustrup"
647 * indent-tabs-mode: nil
649 * vim: shiftwidth=4 tabstop=8 expandtab