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>
20 static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers,
21 char **content_buf, int *content_len)
27 while (i < o->size-1 && o->buf[i] == '\n')
31 if (o->buf[i] == '\r' && i < o->size-1 && o->buf[i+1] == '\n')
36 if (o->buf[i] == '\n')
45 else if (o->buf[i] == ':')
48 *headers = (Z_HTTP_Header *) odr_malloc(o, sizeof(**headers));
49 (*headers)->name = (char*) odr_malloc(o, i - po + 1);
50 memcpy ((*headers)->name, o->buf + po, i - po);
51 (*headers)->name[i - po] = '\0';
53 while (i < o->size-1 && o->buf[i] == ' ')
55 for (po = i; i < o->size-1 && !strchr("\r\n", o->buf[i]); i++)
58 (*headers)->value = (char*) odr_malloc(o, i - po + 1);
59 memcpy ((*headers)->value, o->buf + po, i - po);
60 (*headers)->value[i - po] = '\0';
62 if (!yaz_strcasecmp((*headers)->name, "Transfer-Encoding")
64 !yaz_strcasecmp((*headers)->value, "chunked"))
66 headers = &(*headers)->next;
67 if (i < o->size-1 && o->buf[i] == '\r')
71 if (o->buf[i] != '\n')
82 /* we know buffer will be smaller than o->size - i*/
83 *content_buf = (char*) odr_malloc(o, o->size - i);
89 for (; i < o->size-2; i++)
90 if (yaz_isdigit(o->buf[i]))
91 chunk_len = chunk_len * 16 +
93 else if (yaz_isupper(o->buf[i]))
94 chunk_len = chunk_len * 16 +
95 (o->buf[i] - ('A'-10));
96 else if (yaz_islower(o->buf[i]))
97 chunk_len = chunk_len * 16 +
98 (o->buf[i] - ('a'-10));
101 /* chunk extension ... */
102 while (o->buf[i] != '\r' && o->buf[i+1] != '\n')
111 i += 2; /* skip CRLF */
114 if (chunk_len < 0 || off + chunk_len > o->size)
120 memcpy (*content_buf + off, o->buf + i, chunk_len);
121 i += chunk_len + 2; /* skip chunk+CRLF */
135 else if (i == o->size)
142 *content_len = o->size - i;
143 *content_buf = (char*) odr_malloc(o, *content_len + 1);
144 memcpy(*content_buf, o->buf + i, *content_len);
145 (*content_buf)[*content_len] = '\0';
151 void z_HTTP_header_add_content_type(ODR o, Z_HTTP_Header **hp,
152 const char *content_type,
155 const char *l = "Content-Type";
158 char *ctype = (char *)
159 odr_malloc(o, strlen(content_type)+strlen(charset) + 15);
160 sprintf(ctype, "%s; charset=%s", content_type, charset);
161 z_HTTP_header_add(o, hp, l, ctype);
164 z_HTTP_header_add(o, hp, l, content_type);
169 * HTTP Basic authentication is described at:
170 * http://tools.ietf.org/html/rfc1945#section-11.1
172 void z_HTTP_header_add_basic_auth(ODR o, Z_HTTP_Header **hp,
173 const char *username, const char *password)
183 len = strlen(username) + strlen(password);
184 tmp = (char *) odr_malloc(o, len+2);
185 sprintf(tmp, "%s:%s", username, password);
186 buf = (char *) odr_malloc(o, (len+1) * 8/6 + 12);
187 strcpy(buf, "Basic ");
188 yaz_base64encode(tmp, &buf[strlen(buf)]);
189 z_HTTP_header_set(o, hp, "Authorization", buf);
193 void z_HTTP_header_add(ODR o, Z_HTTP_Header **hp, const char *n,
198 *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
199 (*hp)->name = odr_strdup(o, n);
200 (*hp)->value = odr_strdup(o, v);
204 void z_HTTP_header_set(ODR o, Z_HTTP_Header **hp, const char *n,
209 if (!yaz_strcasecmp((*hp)->name, n))
211 (*hp)->value = odr_strdup(o, v);
216 *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
217 (*hp)->name = odr_strdup(o, n);
218 (*hp)->value = odr_strdup(o, v);
222 const char *z_HTTP_header_remove(Z_HTTP_Header **hp, const char *n)
226 if (!yaz_strcasecmp((*hp)->name, n))
228 const char *v = (*hp)->value;
237 const char *z_HTTP_header_lookup(const Z_HTTP_Header *hp, const char *n)
239 for (; hp; hp = hp->next)
240 if (!yaz_strcasecmp(hp->name, n))
246 Z_GDU *z_get_HTTP_Request(ODR o)
248 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
249 Z_HTTP_Request *hreq;
251 p->which = Z_GDU_HTTP_Request;
252 p->u.HTTP_Request = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hreq));
253 hreq = p->u.HTTP_Request;
255 hreq->content_len = 0;
256 hreq->content_buf = 0;
257 hreq->version = "1.1";
258 hreq->method = "POST";
260 z_HTTP_header_add(o, &hreq->headers, "User-Agent", "YAZ/" YAZ_VERSION);
265 Z_GDU *z_get_HTTP_Request_host_path(ODR odr,
269 Z_GDU *p = z_get_HTTP_Request(odr);
271 p->u.HTTP_Request->path = odr_strdup(odr, path);
275 const char *cp0 = strstr(host, "://");
282 cp1 = strchr(cp0, '/');
284 cp1 = cp0+strlen(cp0);
288 char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
289 memcpy (h, cp0, cp1 - cp0);
291 z_HTTP_header_add(odr, &p->u.HTTP_Request->headers,
298 Z_GDU *z_get_HTTP_Request_uri(ODR odr, const char *uri, const char *args,
301 Z_GDU *p = z_get_HTTP_Request(odr);
302 const char *cp0 = strstr(uri, "://");
309 cp1 = strchr(cp0, '/');
311 cp1 = cp0+strlen(cp0);
315 char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
316 memcpy (h, cp0, cp1 - cp0);
318 z_HTTP_header_add(odr, &p->u.HTTP_Request->headers,
329 p->u.HTTP_Request->path = odr_malloc(odr, cp1 - uri + strlen(args) + 2);
332 memcpy(p->u.HTTP_Request->path, uri, cp1 - uri);
333 strcpy(p->u.HTTP_Request->path + (cp1 - uri), "/");
336 strcpy(p->u.HTTP_Request->path, "/");
337 strcat(p->u.HTTP_Request->path, args);
341 Z_GDU *z_get_HTTP_Response_details(ODR o, int code, const char *details)
343 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
344 Z_HTTP_Response *hres;
346 p->which = Z_GDU_HTTP_Response;
347 p->u.HTTP_Response = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hres));
348 hres = p->u.HTTP_Response;
350 hres->content_len = 0;
351 hres->content_buf = 0;
353 hres->version = "1.1";
354 z_HTTP_header_add(o, &hres->headers, "Server",
358 const char *http_err = z_HTTP_errmsg(code);
359 size_t sz = 400 + strlen(http_err) + (details ?
360 strlen(details) : 0);
361 hres->content_buf = (char*) odr_malloc(o, sz);
362 sprintf(hres->content_buf,
363 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\""
364 " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
367 " <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
370 " <P><A HREF=\"http://www.indexdata.com/yaz/\">YAZ</A> "
372 " <P>Error: %d</P>\n"
373 " <P>Description: %s</P>\n", code, http_err);
376 sprintf(hres->content_buf + strlen(hres->content_buf),
377 "<P>Details: %s</P>\n", details);
379 sprintf(hres->content_buf + strlen(hres->content_buf),
382 hres->content_len = strlen(hres->content_buf);
383 z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
388 Z_GDU *z_get_HTTP_Response(ODR o, int code)
390 return z_get_HTTP_Response_details(o, code, 0);
393 const char *z_HTTP_errmsg(int code)
400 return "Switching Protocols";
408 return "Non-Authoritative Information";
412 return "Reset Content";
414 return "Partial Content";
416 return "Multiple Choices";
418 return "Moved Permenently";
424 return "Not Modified";
428 return "Temporary Redirect";
430 return "Bad Request";
434 return "Method Not Allowed";
436 return "Not Acceptable";
438 return "Proxy Authentication Required";
440 return "Request Timeout";
446 return "Length Required";
448 return "Precondition Failed";
450 return "Request Entity Too Large";
452 return "Request-URI Too Long";
454 return "Unsupported Media Type";
456 return "Requested Range Not Satisfiable";
458 return "Expectation Failed";
460 return "Internal Error";
462 return "Not Implemented";
464 return "Bad Gateway";
466 return "Service Unavailable";
468 return "Gateway Timeout";
470 return "HTTP Version Not Supported";
472 return "Unknown Error";
476 int yaz_decode_http_response(ODR o, Z_HTTP_Response **hr_p)
479 Z_HTTP_Response *hr = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hr));
486 while (i < o->size-2 && !strchr(" \r\n", o->buf[i]))
488 hr->version = (char *) odr_malloc(o, i - po + 1);
490 memcpy(hr->version, o->buf + po, i - po);
491 hr->version[i-po] = 0;
492 if (o->buf[i] != ' ')
499 while (i < o->size-2 && o->buf[i] >= '0' && o->buf[i] <= '9')
501 hr->code = hr->code*10 + (o->buf[i] - '0');
504 while (i < o->size-1 && o->buf[i] != '\n')
506 return decode_headers_content(o, i, &hr->headers,
507 &hr->content_buf, &hr->content_len);
510 int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p)
513 Z_HTTP_Request *hr = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
518 for (i = 0; o->buf[i] != ' '; i++)
519 if (i >= o->size-5 || i > 30)
524 hr->method = (char *) odr_malloc(o, i+1);
525 memcpy (hr->method, o->buf, i);
526 hr->method[i] = '\0';
529 for (i = po; o->buf[i] != ' '; i++)
535 hr->path = (char *) odr_malloc(o, i - po + 1);
536 memcpy (hr->path, o->buf+po, i - po);
537 hr->path[i - po] = '\0';
540 if (i > o->size-5 || memcmp(o->buf+i, "HTTP/", 5))
547 while (i < o->size && !strchr("\r\n", o->buf[i]))
549 hr->version = (char *) odr_malloc(o, i - po + 1);
550 memcpy(hr->version, o->buf + po, i - po);
551 hr->version[i - po] = '\0';
553 if (i < o->size-1 && o->buf[i] == '\r')
555 if (o->buf[i] != '\n')
560 return decode_headers_content(o, i, &hr->headers,
561 &hr->content_buf, &hr->content_len);
564 static void dump_http_package(ODR o, const char *buf, size_t len)
571 odr_printf(o, "%.*s\n", i, buf);
576 odr_printf(o, "%.*s\n", i, buf);
577 odr_printf(o, "(truncated\n", (long) len);
580 else if (buf[i] == 0)
582 odr_printf(o, "%.*s\n", i, buf);
583 odr_printf(o, "(binary data)\n", (long) len);
589 int yaz_encode_http_response(ODR o, Z_HTTP_Response *hr)
595 sprintf(sbuf, "HTTP/%s %d %s\r\n", hr->version,
597 z_HTTP_errmsg(hr->code));
598 odr_write(o, sbuf, strlen(sbuf));
599 /* use content_len for Content-Length */
600 sprintf(sbuf, "Content-Length: %d\r\n", hr->content_len);
601 odr_write(o, sbuf, strlen(sbuf));
602 for (h = hr->headers; h; h = h->next)
604 if (yaz_strcasecmp(h->name, "Content-Length")
605 && yaz_strcasecmp(h->name, "Transfer-Encoding"))
606 { /* skip Content-Length if given. content_len rules */
607 odr_write(o, h->name, strlen(h->name));
608 odr_write(o, ": ", 2);
609 odr_write(o, h->value, strlen(h->value));
610 odr_write(o, "\r\n", 2);
613 odr_write(o, "\r\n", 2);
615 odr_write(o, hr->content_buf, hr->content_len);
616 if (o->direction == ODR_PRINT)
618 odr_printf(o, "-- HTTP response:\n");
619 dump_http_package(o, (const char *) o->buf + top0, o->top - top0);
620 odr_printf(o, "--\n");
625 int yaz_encode_http_request(ODR o, Z_HTTP_Request *hr)
630 odr_write(o, hr->method, strlen(hr->method));
631 odr_write(o, " ", 1);
632 odr_write(o, hr->path, strlen(hr->path));
633 odr_write(o, " HTTP/", 6);
634 odr_write(o, hr->version, strlen(hr->version));
635 odr_write(o, "\r\n", 2);
636 if (hr->content_len &&
637 !z_HTTP_header_lookup(hr->headers,
641 sprintf(lstr, "Content-Length: %d\r\n",
643 odr_write(o, lstr, strlen(lstr));
645 for (h = hr->headers; h; h = h->next)
647 odr_write(o, h->name, strlen(h->name));
648 odr_write(o, ": ", 2);
649 odr_write(o, h->value, strlen(h->value));
650 odr_write(o, "\r\n", 2);
652 odr_write(o, "\r\n", 2);
654 odr_write(o, hr->content_buf, hr->content_len);
655 if (o->direction == ODR_PRINT)
657 odr_printf(o, "-- HTTP request:\n");
658 dump_http_package(o, (const char *) o->buf + top0, o->top - top0);
659 odr_printf(o, "--\n");
667 * c-file-style: "Stroustrup"
668 * indent-tabs-mode: nil
670 * vim: shiftwidth=4 tabstop=8 expandtab