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)
25 const char *buf = o->op->buf;
26 int size = o->op->size;
29 while (i < size-1 && buf[i] == '\n')
33 if (buf[i] == '\r' && i < size-1 && buf[i+1] == '\n')
47 else if (buf[i] == ':')
50 *headers = (Z_HTTP_Header *) odr_malloc(o, sizeof(**headers));
51 (*headers)->name = (char*) odr_malloc(o, i - po + 1);
52 memcpy ((*headers)->name, buf + po, i - po);
53 (*headers)->name[i - po] = '\0';
55 while (i < size-1 && buf[i] == ' ')
57 for (po = i; i < size-1 && !strchr("\r\n", buf[i]); i++)
60 (*headers)->value = (char*) odr_malloc(o, i - po + 1);
61 memcpy ((*headers)->value, buf + po, i - po);
62 (*headers)->value[i - po] = '\0';
64 if (!yaz_strcasecmp((*headers)->name, "Transfer-Encoding")
66 !yaz_strcasecmp((*headers)->value, "chunked"))
68 headers = &(*headers)->next;
69 if (i < size-1 && buf[i] == '\r')
84 /* we know buffer will be smaller than o->size - i*/
85 *content_buf = (char*) odr_malloc(o, size - i);
91 for (; i < size-2; i++)
92 if (yaz_isdigit(buf[i]))
93 chunk_len = chunk_len * 16 +
95 else if (yaz_isupper(buf[i]))
96 chunk_len = chunk_len * 16 +
98 else if (yaz_islower(buf[i]))
99 chunk_len = chunk_len * 16 +
103 /* chunk extension ... */
104 while (buf[i] != '\r' && buf[i+1] != '\n')
113 i += 2; /* skip CRLF */
116 if (chunk_len < 0 || off + chunk_len > size)
122 memcpy (*content_buf + off, buf + i, chunk_len);
123 i += chunk_len + 2; /* skip chunk+CRLF */
144 *content_len = size - i;
145 *content_buf = (char*) odr_malloc(o, *content_len + 1);
146 memcpy(*content_buf, buf + i, *content_len);
147 (*content_buf)[*content_len] = '\0';
153 void z_HTTP_header_add_content_type(ODR o, Z_HTTP_Header **hp,
154 const char *content_type,
157 const char *l = "Content-Type";
160 char *ctype = (char *)
161 odr_malloc(o, strlen(content_type)+strlen(charset) + 15);
162 sprintf(ctype, "%s; charset=%s", content_type, charset);
163 z_HTTP_header_add(o, hp, l, ctype);
166 z_HTTP_header_add(o, hp, l, content_type);
171 * HTTP Basic authentication is described at:
172 * http://tools.ietf.org/html/rfc1945#section-11.1
174 void z_HTTP_header_add_basic_auth(ODR o, Z_HTTP_Header **hp,
175 const char *username, const char *password)
185 len = strlen(username) + strlen(password);
186 tmp = (char *) odr_malloc(o, len+2);
187 sprintf(tmp, "%s:%s", username, password);
188 buf = (char *) odr_malloc(o, (len+1) * 8/6 + 12);
189 strcpy(buf, "Basic ");
190 yaz_base64encode(tmp, &buf[strlen(buf)]);
191 z_HTTP_header_set(o, hp, "Authorization", buf);
195 void z_HTTP_header_add(ODR o, Z_HTTP_Header **hp, const char *n,
200 *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
201 (*hp)->name = odr_strdup(o, n);
202 (*hp)->value = odr_strdup(o, v);
206 void z_HTTP_header_set(ODR o, Z_HTTP_Header **hp, const char *n,
211 if (!yaz_strcasecmp((*hp)->name, n))
213 (*hp)->value = odr_strdup(o, v);
218 *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
219 (*hp)->name = odr_strdup(o, n);
220 (*hp)->value = odr_strdup(o, v);
224 const char *z_HTTP_header_remove(Z_HTTP_Header **hp, const char *n)
228 if (!yaz_strcasecmp((*hp)->name, n))
230 const char *v = (*hp)->value;
239 const char *z_HTTP_header_lookup(const Z_HTTP_Header *hp, const char *n)
241 for (; hp; hp = hp->next)
242 if (!yaz_strcasecmp(hp->name, n))
248 Z_GDU *z_get_HTTP_Request(ODR o)
250 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
251 Z_HTTP_Request *hreq;
253 p->which = Z_GDU_HTTP_Request;
254 p->u.HTTP_Request = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hreq));
255 hreq = p->u.HTTP_Request;
257 hreq->content_len = 0;
258 hreq->content_buf = 0;
259 hreq->version = "1.1";
260 hreq->method = "POST";
262 z_HTTP_header_add(o, &hreq->headers, "User-Agent", "YAZ/" YAZ_VERSION);
267 Z_GDU *z_get_HTTP_Request_host_path(ODR odr,
271 Z_GDU *p = z_get_HTTP_Request(odr);
273 p->u.HTTP_Request->path = odr_strdup(odr, path);
277 const char *cp0 = strstr(host, "://");
284 cp1 = strchr(cp0, '/');
286 cp1 = cp0+strlen(cp0);
290 char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
291 memcpy (h, cp0, cp1 - cp0);
293 z_HTTP_header_add(odr, &p->u.HTTP_Request->headers,
300 Z_GDU *z_get_HTTP_Request_uri(ODR odr, const char *uri, const char *args,
303 Z_GDU *p = z_get_HTTP_Request(odr);
304 const char *cp0 = strstr(uri, "://");
311 cp1 = strchr(cp0, '/');
313 cp1 = cp0+strlen(cp0);
317 char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
318 memcpy (h, cp0, cp1 - cp0);
320 z_HTTP_header_add(odr, &p->u.HTTP_Request->headers,
331 p->u.HTTP_Request->path = odr_malloc(odr, cp1 - uri + strlen(args) + 2);
334 memcpy(p->u.HTTP_Request->path, uri, cp1 - uri);
335 strcpy(p->u.HTTP_Request->path + (cp1 - uri), "/");
338 strcpy(p->u.HTTP_Request->path, "/");
339 strcat(p->u.HTTP_Request->path, args);
343 Z_GDU *z_get_HTTP_Response_details(ODR o, int code, const char *details)
345 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
346 Z_HTTP_Response *hres;
348 p->which = Z_GDU_HTTP_Response;
349 p->u.HTTP_Response = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hres));
350 hres = p->u.HTTP_Response;
352 hres->content_len = 0;
353 hres->content_buf = 0;
355 hres->version = "1.1";
356 z_HTTP_header_add(o, &hres->headers, "Server",
360 const char *http_err = z_HTTP_errmsg(code);
361 size_t sz = 400 + strlen(http_err) + (details ?
362 strlen(details) : 0);
363 hres->content_buf = (char*) odr_malloc(o, sz);
364 sprintf(hres->content_buf,
365 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\""
366 " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
369 " <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
372 " <P><A HREF=\"http://www.indexdata.com/yaz/\">YAZ</A> "
374 " <P>Error: %d</P>\n"
375 " <P>Description: %s</P>\n", code, http_err);
378 sprintf(hres->content_buf + strlen(hres->content_buf),
379 "<P>Details: %s</P>\n", details);
381 sprintf(hres->content_buf + strlen(hres->content_buf),
384 hres->content_len = strlen(hres->content_buf);
385 z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
390 Z_GDU *z_get_HTTP_Response(ODR o, int code)
392 return z_get_HTTP_Response_details(o, code, 0);
395 const char *z_HTTP_errmsg(int code)
402 return "Switching Protocols";
410 return "Non-Authoritative Information";
414 return "Reset Content";
416 return "Partial Content";
418 return "Multiple Choices";
420 return "Moved Permenently";
426 return "Not Modified";
430 return "Temporary Redirect";
432 return "Bad Request";
436 return "Method Not Allowed";
438 return "Not Acceptable";
440 return "Proxy Authentication Required";
442 return "Request Timeout";
448 return "Length Required";
450 return "Precondition Failed";
452 return "Request Entity Too Large";
454 return "Request-URI Too Long";
456 return "Unsupported Media Type";
458 return "Requested Range Not Satisfiable";
460 return "Expectation Failed";
462 return "Internal Error";
464 return "Not Implemented";
466 return "Bad Gateway";
468 return "Service Unavailable";
470 return "Gateway Timeout";
472 return "HTTP Version Not Supported";
474 return "Unknown Error";
478 int yaz_decode_http_response(ODR o, Z_HTTP_Response **hr_p)
481 Z_HTTP_Response *hr = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hr));
482 const char *buf = o->op->buf;
483 int size = o->op->size;
490 while (i < size-2 && !strchr(" \r\n", buf[i]))
492 hr->version = (char *) odr_malloc(o, i - po + 1);
494 memcpy(hr->version, buf + po, i - po);
495 hr->version[i-po] = 0;
503 while (i < size-2 && buf[i] >= '0' && buf[i] <= '9')
505 hr->code = hr->code*10 + (buf[i] - '0');
508 while (i < size-1 && buf[i] != '\n')
510 return decode_headers_content(o, i, &hr->headers,
511 &hr->content_buf, &hr->content_len);
514 int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p)
517 Z_HTTP_Request *hr = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
518 const char *buf = o->op->buf;
519 int size = o->op->size;
524 for (i = 0; buf[i] != ' '; i++)
525 if (i >= size-5 || i > 30)
530 hr->method = (char *) odr_malloc(o, i+1);
531 memcpy (hr->method, buf, i);
532 hr->method[i] = '\0';
535 for (i = po; buf[i] != ' '; i++)
541 hr->path = (char *) odr_malloc(o, i - po + 1);
542 memcpy (hr->path, buf+po, i - po);
543 hr->path[i - po] = '\0';
546 if (i > size-5 || memcmp(buf+i, "HTTP/", 5))
553 while (i < size && !strchr("\r\n", buf[i]))
555 hr->version = (char *) odr_malloc(o, i - po + 1);
556 memcpy(hr->version, buf + po, i - po);
557 hr->version[i - po] = '\0';
559 if (i < size-1 && buf[i] == '\r')
566 return decode_headers_content(o, i, &hr->headers,
567 &hr->content_buf, &hr->content_len);
570 static void dump_http_package(ODR o, const char *buf, size_t len)
577 odr_printf(o, "%.*s\n", i, buf);
582 odr_printf(o, "%.*s\n", i, buf);
583 odr_printf(o, "(truncated\n", (long) len);
586 else if (buf[i] == 0)
588 odr_printf(o, "%.*s\n", i, buf);
589 odr_printf(o, "(binary data)\n", (long) len);
595 int yaz_encode_http_response(ODR o, Z_HTTP_Response *hr)
599 int top0 = o->op->top;
601 sprintf(sbuf, "HTTP/%s %d %s\r\n", hr->version,
603 z_HTTP_errmsg(hr->code));
604 odr_write(o, sbuf, strlen(sbuf));
605 /* use content_len for Content-Length */
606 sprintf(sbuf, "Content-Length: %d\r\n", hr->content_len);
607 odr_write(o, sbuf, strlen(sbuf));
608 for (h = hr->headers; h; h = h->next)
610 if (yaz_strcasecmp(h->name, "Content-Length")
611 && yaz_strcasecmp(h->name, "Transfer-Encoding"))
612 { /* skip Content-Length if given. content_len rules */
613 odr_write(o, h->name, strlen(h->name));
614 odr_write(o, ": ", 2);
615 odr_write(o, h->value, strlen(h->value));
616 odr_write(o, "\r\n", 2);
619 odr_write(o, "\r\n", 2);
621 odr_write(o, hr->content_buf, hr->content_len);
622 if (o->direction == ODR_PRINT)
624 odr_printf(o, "-- HTTP response:\n");
625 dump_http_package(o, o->op->buf + top0, o->op->top - top0);
626 odr_printf(o, "--\n");
631 int yaz_encode_http_request(ODR o, Z_HTTP_Request *hr)
634 int top0 = o->op->top;
636 odr_write(o, hr->method, strlen(hr->method));
637 odr_write(o, " ", 1);
638 odr_write(o, hr->path, strlen(hr->path));
639 odr_write(o, " HTTP/", 6);
640 odr_write(o, hr->version, strlen(hr->version));
641 odr_write(o, "\r\n", 2);
642 if (hr->content_len &&
643 !z_HTTP_header_lookup(hr->headers,
647 sprintf(lstr, "Content-Length: %d\r\n",
649 odr_write(o, lstr, strlen(lstr));
651 for (h = hr->headers; h; h = h->next)
653 odr_write(o, h->name, strlen(h->name));
654 odr_write(o, ": ", 2);
655 odr_write(o, h->value, strlen(h->value));
656 odr_write(o, "\r\n", 2);
658 odr_write(o, "\r\n", 2);
660 odr_write(o, hr->content_buf, hr->content_len);
661 if (o->direction == ODR_PRINT)
663 odr_printf(o, "-- HTTP request:\n");
664 dump_http_package(o, o->op->buf + top0, o->op->top - top0);
665 odr_printf(o, "--\n");
673 * c-file-style: "Stroustrup"
674 * indent-tabs-mode: nil
676 * vim: shiftwidth=4 tabstop=8 expandtab