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 = odr_strdupn(o, buf + po, i - po);
53 while (i < size-1 && buf[i] == ' ')
55 for (po = i; i < size-1 && !strchr("\r\n", buf[i]); i++)
58 (*headers)->value = odr_strdupn(o, buf + po, i - po);
59 if (!yaz_strcasecmp((*headers)->name, "Transfer-Encoding")
61 !yaz_strcasecmp((*headers)->value, "chunked"))
63 headers = &(*headers)->next;
64 if (i < size-1 && buf[i] == '\r')
79 /* we know buffer will be smaller than o->size - i*/
80 *content_buf = (char*) odr_malloc(o, size - i);
86 for (; i < size-2; i++)
87 if (yaz_isdigit(buf[i]))
88 chunk_len = chunk_len * 16 +
90 else if (yaz_isupper(buf[i]))
91 chunk_len = chunk_len * 16 +
93 else if (yaz_islower(buf[i]))
94 chunk_len = chunk_len * 16 +
98 /* chunk extension ... */
99 while (buf[i] != '\r' && buf[i+1] != '\n')
108 i += 2; /* skip CRLF */
111 if (chunk_len < 0 || off + chunk_len > size)
117 memcpy (*content_buf + off, buf + i, chunk_len);
118 i += chunk_len + 2; /* skip chunk+CRLF */
139 *content_len = size - i;
140 *content_buf = odr_strdupn(o, buf + i, *content_len);
146 void z_HTTP_header_add_content_type(ODR o, Z_HTTP_Header **hp,
147 const char *content_type,
150 const char *l = "Content-Type";
153 char *ctype = (char *)
154 odr_malloc(o, strlen(content_type)+strlen(charset) + 15);
155 sprintf(ctype, "%s; charset=%s", content_type, charset);
156 z_HTTP_header_add(o, hp, l, ctype);
159 z_HTTP_header_add(o, hp, l, content_type);
164 * HTTP Basic authentication is described at:
165 * http://tools.ietf.org/html/rfc1945#section-11.1
167 void z_HTTP_header_add_basic_auth(ODR o, Z_HTTP_Header **hp,
168 const char *username, const char *password)
178 len = strlen(username) + strlen(password);
179 tmp = (char *) odr_malloc(o, len+2);
180 sprintf(tmp, "%s:%s", username, password);
181 buf = (char *) odr_malloc(o, (len+1) * 8/6 + 12);
182 strcpy(buf, "Basic ");
183 yaz_base64encode(tmp, &buf[strlen(buf)]);
184 z_HTTP_header_set(o, hp, "Authorization", buf);
188 void z_HTTP_header_add(ODR o, Z_HTTP_Header **hp, const char *n,
193 *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
194 (*hp)->name = odr_strdup(o, n);
195 (*hp)->value = odr_strdup(o, v);
199 void z_HTTP_header_set(ODR o, Z_HTTP_Header **hp, const char *n,
204 if (!yaz_strcasecmp((*hp)->name, n))
206 (*hp)->value = odr_strdup(o, v);
211 *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
212 (*hp)->name = odr_strdup(o, n);
213 (*hp)->value = odr_strdup(o, v);
217 const char *z_HTTP_header_remove(Z_HTTP_Header **hp, const char *n)
221 if (!yaz_strcasecmp((*hp)->name, n))
223 const char *v = (*hp)->value;
232 const char *z_HTTP_header_lookup(const Z_HTTP_Header *hp, const char *n)
234 for (; hp; hp = hp->next)
235 if (!yaz_strcasecmp(hp->name, n))
241 Z_GDU *z_get_HTTP_Request(ODR o)
243 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
244 Z_HTTP_Request *hreq;
246 p->which = Z_GDU_HTTP_Request;
247 p->u.HTTP_Request = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hreq));
248 hreq = p->u.HTTP_Request;
250 hreq->content_len = 0;
251 hreq->content_buf = 0;
252 hreq->version = "1.1";
253 hreq->method = "POST";
255 z_HTTP_header_add(o, &hreq->headers, "User-Agent", "YAZ/" YAZ_VERSION);
260 Z_GDU *z_get_HTTP_Request_host_path(ODR odr,
264 Z_GDU *p = z_get_HTTP_Request(odr);
266 p->u.HTTP_Request->path = odr_strdup(odr, path);
270 const char *cp0 = strstr(host, "://");
277 cp1 = strchr(cp0, '/');
279 cp1 = cp0+strlen(cp0);
283 char *h = odr_strdupn(odr, cp0, cp1 - cp0);
284 z_HTTP_header_add(odr, &p->u.HTTP_Request->headers, "Host", h);
290 Z_GDU *z_get_HTTP_Request_uri(ODR odr, const char *uri, const char *args,
293 Z_GDU *p = z_get_HTTP_Request(odr);
294 const char *cp0 = strstr(uri, "://");
301 cp1 = strchr(cp0, '/');
303 cp1 = cp0+strlen(cp0);
307 char *h = odr_strdupn(odr, cp0, cp1 - cp0);
308 z_HTTP_header_add(odr, &p->u.HTTP_Request->headers, "Host", h);
318 p->u.HTTP_Request->path = odr_malloc(odr, cp1 - uri + strlen(args) + 2);
321 memcpy(p->u.HTTP_Request->path, uri, cp1 - uri);
322 strcpy(p->u.HTTP_Request->path + (cp1 - uri), "/");
325 strcpy(p->u.HTTP_Request->path, "/");
326 strcat(p->u.HTTP_Request->path, args);
330 Z_GDU *z_get_HTTP_Response_server(ODR o, int code, const char *details,
331 const char *server, const char *server_url)
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", server);
347 const char *http_err = z_HTTP_errmsg(code);
348 size_t sz = 400 + strlen(http_err) + (details ?
349 strlen(details) : 0);
350 hres->content_buf = (char*) odr_malloc(o, sz);
351 sprintf(hres->content_buf,
352 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\""
353 " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
356 " <TITLE>%s</TITLE>\n"
359 " <P><A HREF=\"%s\">%s</A></P>\n"
360 " <P>Error: %d</P>\n"
361 " <P>Description: %s</P>\n", server, server_url, server,
365 sprintf(hres->content_buf + strlen(hres->content_buf),
366 "<P>Details: %s</P>\n", details);
368 sprintf(hres->content_buf + strlen(hres->content_buf),
371 hres->content_len = strlen(hres->content_buf);
372 z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
377 Z_GDU *z_get_HTTP_Response_details(ODR o, int code, const char *details)
379 return z_get_HTTP_Response_server(o, code, details, "YAZ/" YAZ_VERSION,
380 "http://www.indexdata.com/yaz");
383 Z_GDU *z_get_HTTP_Response(ODR o, int code)
385 return z_get_HTTP_Response_details(o, code, 0);
388 const char *z_HTTP_errmsg(int code)
395 return "Switching Protocols";
403 return "Non-Authoritative Information";
407 return "Reset Content";
409 return "Partial Content";
411 return "Multiple Choices";
413 return "Moved Permenently";
419 return "Not Modified";
423 return "Temporary Redirect";
425 return "Bad Request";
429 return "Method Not Allowed";
431 return "Not Acceptable";
433 return "Proxy Authentication Required";
435 return "Request Timeout";
441 return "Length Required";
443 return "Precondition Failed";
445 return "Request Entity Too Large";
447 return "Request-URI Too Long";
449 return "Unsupported Media Type";
451 return "Requested Range Not Satisfiable";
453 return "Expectation Failed";
455 return "Internal Error";
457 return "Not Implemented";
459 return "Bad Gateway";
461 return "Service Unavailable";
463 return "Gateway Timeout";
465 return "HTTP Version Not Supported";
467 return "Unknown Error";
471 int yaz_decode_http_response(ODR o, Z_HTTP_Response **hr_p)
474 Z_HTTP_Response *hr = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hr));
475 const char *buf = o->op->buf;
476 int size = o->op->size;
483 while (i < size-2 && !strchr(" \r\n", buf[i]))
485 hr->version = odr_strdupn(o, buf + po, i - po);
493 while (i < size-2 && buf[i] >= '0' && buf[i] <= '9')
495 hr->code = hr->code*10 + (buf[i] - '0');
498 while (i < size-1 && buf[i] != '\n')
500 return decode_headers_content(o, i, &hr->headers,
501 &hr->content_buf, &hr->content_len);
504 int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p)
507 Z_HTTP_Request *hr = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
508 const char *buf = o->op->buf;
509 int size = o->op->size;
514 for (i = 0; buf[i] != ' '; i++)
515 if (i >= size-5 || i > 30)
520 hr->method = odr_strdupn(o, buf, i);
523 for (i = po; buf[i] != ' '; i++)
529 hr->path = odr_strdupn(o, buf + po, i - po);
532 if (i > size-5 || memcmp(buf+i, "HTTP/", 5))
539 while (i < size && !strchr("\r\n", buf[i]))
541 hr->version = odr_strdupn(o, buf + po, i - po);
543 if (i < size-1 && buf[i] == '\r')
550 return decode_headers_content(o, i, &hr->headers,
551 &hr->content_buf, &hr->content_len);
554 static void dump_http_package(ODR o, const char *buf, size_t len)
561 odr_printf(o, "%.*s\n", i, buf);
566 odr_printf(o, "%.*s\n", i, buf);
567 odr_printf(o, "(truncated\n", (long) len);
570 else if (buf[i] == 0)
572 odr_printf(o, "%.*s\n", i, buf);
573 odr_printf(o, "(binary data)\n", (long) len);
579 int yaz_encode_http_response(ODR o, Z_HTTP_Response *hr)
583 int top0 = o->op->top;
585 sprintf(sbuf, "HTTP/%s %d %s\r\n", hr->version,
587 z_HTTP_errmsg(hr->code));
588 odr_write(o, sbuf, strlen(sbuf));
589 /* use content_len for Content-Length */
590 sprintf(sbuf, "Content-Length: %d\r\n", hr->content_len);
591 odr_write(o, sbuf, strlen(sbuf));
592 for (h = hr->headers; h; h = h->next)
594 if (yaz_strcasecmp(h->name, "Content-Length")
595 && yaz_strcasecmp(h->name, "Transfer-Encoding"))
596 { /* skip Content-Length if given. content_len rules */
597 odr_write(o, h->name, strlen(h->name));
598 odr_write(o, ": ", 2);
599 odr_write(o, h->value, strlen(h->value));
600 odr_write(o, "\r\n", 2);
603 odr_write(o, "\r\n", 2);
605 odr_write(o, hr->content_buf, hr->content_len);
606 if (o->direction == ODR_PRINT)
608 odr_printf(o, "-- HTTP response:\n");
609 dump_http_package(o, o->op->buf + top0, o->op->top - top0);
610 odr_printf(o, "--\n");
615 int yaz_encode_http_request(ODR o, Z_HTTP_Request *hr)
618 int top0 = o->op->top;
620 odr_write(o, hr->method, strlen(hr->method));
621 odr_write(o, " ", 1);
622 odr_write(o, hr->path, strlen(hr->path));
623 odr_write(o, " HTTP/", 6);
624 odr_write(o, hr->version, strlen(hr->version));
625 odr_write(o, "\r\n", 2);
626 if (hr->content_len &&
627 !z_HTTP_header_lookup(hr->headers,
631 sprintf(lstr, "Content-Length: %d\r\n",
633 odr_write(o, lstr, strlen(lstr));
635 for (h = hr->headers; h; h = h->next)
637 odr_write(o, h->name, strlen(h->name));
638 odr_write(o, ": ", 2);
639 odr_write(o, h->value, strlen(h->value));
640 odr_write(o, "\r\n", 2);
642 odr_write(o, "\r\n", 2);
644 odr_write(o, hr->content_buf, hr->content_len);
645 if (o->direction == ODR_PRINT)
647 odr_printf(o, "-- HTTP request:\n");
648 dump_http_package(o, o->op->buf + top0, o->op->top - top0);
649 odr_printf(o, "--\n");
657 * c-file-style: "Stroustrup"
658 * indent-tabs-mode: nil
660 * vim: shiftwidth=4 tabstop=8 expandtab