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_server(ODR o, int code, const char *details,
344 const char *server, const char *server_url)
346 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
347 Z_HTTP_Response *hres;
349 p->which = Z_GDU_HTTP_Response;
350 p->u.HTTP_Response = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hres));
351 hres = p->u.HTTP_Response;
353 hres->content_len = 0;
354 hres->content_buf = 0;
356 hres->version = "1.1";
357 z_HTTP_header_add(o, &hres->headers, "Server", 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>%s</TITLE>\n"
372 " <P><A HREF=\"%s\">%s</A></P>\n"
373 " <P>Error: %d</P>\n"
374 " <P>Description: %s</P>\n", server, server_url, server,
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_details(ODR o, int code, const char *details)
392 return z_get_HTTP_Response_server(o, code, details, "YAZ/" YAZ_VERSION,
393 "http://www.indexdata.com/yaz");
396 Z_GDU *z_get_HTTP_Response(ODR o, int code)
398 return z_get_HTTP_Response_details(o, code, 0);
401 const char *z_HTTP_errmsg(int code)
408 return "Switching Protocols";
416 return "Non-Authoritative Information";
420 return "Reset Content";
422 return "Partial Content";
424 return "Multiple Choices";
426 return "Moved Permenently";
432 return "Not Modified";
436 return "Temporary Redirect";
438 return "Bad Request";
442 return "Method Not Allowed";
444 return "Not Acceptable";
446 return "Proxy Authentication Required";
448 return "Request Timeout";
454 return "Length Required";
456 return "Precondition Failed";
458 return "Request Entity Too Large";
460 return "Request-URI Too Long";
462 return "Unsupported Media Type";
464 return "Requested Range Not Satisfiable";
466 return "Expectation Failed";
468 return "Internal Error";
470 return "Not Implemented";
472 return "Bad Gateway";
474 return "Service Unavailable";
476 return "Gateway Timeout";
478 return "HTTP Version Not Supported";
480 return "Unknown Error";
484 int yaz_decode_http_response(ODR o, Z_HTTP_Response **hr_p)
487 Z_HTTP_Response *hr = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hr));
488 const char *buf = o->op->buf;
489 int size = o->op->size;
496 while (i < size-2 && !strchr(" \r\n", buf[i]))
498 hr->version = (char *) odr_malloc(o, i - po + 1);
500 memcpy(hr->version, buf + po, i - po);
501 hr->version[i-po] = 0;
509 while (i < size-2 && buf[i] >= '0' && buf[i] <= '9')
511 hr->code = hr->code*10 + (buf[i] - '0');
514 while (i < size-1 && buf[i] != '\n')
516 return decode_headers_content(o, i, &hr->headers,
517 &hr->content_buf, &hr->content_len);
520 int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p)
523 Z_HTTP_Request *hr = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
524 const char *buf = o->op->buf;
525 int size = o->op->size;
530 for (i = 0; buf[i] != ' '; i++)
531 if (i >= size-5 || i > 30)
536 hr->method = (char *) odr_malloc(o, i+1);
537 memcpy (hr->method, buf, i);
538 hr->method[i] = '\0';
541 for (i = po; buf[i] != ' '; i++)
547 hr->path = (char *) odr_malloc(o, i - po + 1);
548 memcpy (hr->path, buf+po, i - po);
549 hr->path[i - po] = '\0';
552 if (i > size-5 || memcmp(buf+i, "HTTP/", 5))
559 while (i < size && !strchr("\r\n", buf[i]))
561 hr->version = (char *) odr_malloc(o, i - po + 1);
562 memcpy(hr->version, buf + po, i - po);
563 hr->version[i - po] = '\0';
565 if (i < size-1 && buf[i] == '\r')
572 return decode_headers_content(o, i, &hr->headers,
573 &hr->content_buf, &hr->content_len);
576 static void dump_http_package(ODR o, const char *buf, size_t len)
583 odr_printf(o, "%.*s\n", i, buf);
588 odr_printf(o, "%.*s\n", i, buf);
589 odr_printf(o, "(truncated\n", (long) len);
592 else if (buf[i] == 0)
594 odr_printf(o, "%.*s\n", i, buf);
595 odr_printf(o, "(binary data)\n", (long) len);
601 int yaz_encode_http_response(ODR o, Z_HTTP_Response *hr)
605 int top0 = o->op->top;
607 sprintf(sbuf, "HTTP/%s %d %s\r\n", hr->version,
609 z_HTTP_errmsg(hr->code));
610 odr_write(o, sbuf, strlen(sbuf));
611 /* use content_len for Content-Length */
612 sprintf(sbuf, "Content-Length: %d\r\n", hr->content_len);
613 odr_write(o, sbuf, strlen(sbuf));
614 for (h = hr->headers; h; h = h->next)
616 if (yaz_strcasecmp(h->name, "Content-Length")
617 && yaz_strcasecmp(h->name, "Transfer-Encoding"))
618 { /* skip Content-Length if given. content_len rules */
619 odr_write(o, h->name, strlen(h->name));
620 odr_write(o, ": ", 2);
621 odr_write(o, h->value, strlen(h->value));
622 odr_write(o, "\r\n", 2);
625 odr_write(o, "\r\n", 2);
627 odr_write(o, hr->content_buf, hr->content_len);
628 if (o->direction == ODR_PRINT)
630 odr_printf(o, "-- HTTP response:\n");
631 dump_http_package(o, o->op->buf + top0, o->op->top - top0);
632 odr_printf(o, "--\n");
637 int yaz_encode_http_request(ODR o, Z_HTTP_Request *hr)
640 int top0 = o->op->top;
642 odr_write(o, hr->method, strlen(hr->method));
643 odr_write(o, " ", 1);
644 odr_write(o, hr->path, strlen(hr->path));
645 odr_write(o, " HTTP/", 6);
646 odr_write(o, hr->version, strlen(hr->version));
647 odr_write(o, "\r\n", 2);
648 if (hr->content_len &&
649 !z_HTTP_header_lookup(hr->headers,
653 sprintf(lstr, "Content-Length: %d\r\n",
655 odr_write(o, lstr, strlen(lstr));
657 for (h = hr->headers; h; h = h->next)
659 odr_write(o, h->name, strlen(h->name));
660 odr_write(o, ": ", 2);
661 odr_write(o, h->value, strlen(h->value));
662 odr_write(o, "\r\n", 2);
664 odr_write(o, "\r\n", 2);
666 odr_write(o, hr->content_buf, hr->content_len);
667 if (o->direction == ODR_PRINT)
669 odr_printf(o, "-- HTTP request:\n");
670 dump_http_package(o, o->op->buf + top0, o->op->top - top0);
671 odr_printf(o, "--\n");
679 * c-file-style: "Stroustrup"
680 * indent-tabs-mode: nil
682 * vim: shiftwidth=4 tabstop=8 expandtab