1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 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>
19 #include <yaz/comstack.h>
21 static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers,
22 char **content_buf, int *content_len)
26 const char *buf = o->op->buf;
27 int size = o->op->size;
30 while (i < size-1 && buf[i] == '\n')
34 if (buf[i] == '\r' && i < size-1 && buf[i+1] == '\n')
48 else if (buf[i] == ':')
51 *headers = (Z_HTTP_Header *) odr_malloc(o, sizeof(**headers));
52 (*headers)->name = odr_strdupn(o, buf + po, i - po);
54 while (i < size-1 && buf[i] == ' ')
56 for (po = i; i < size-1 && !strchr("\r\n", buf[i]); i++)
59 (*headers)->value = odr_strdupn(o, buf + po, i - po);
60 if (!yaz_strcasecmp((*headers)->name, "Transfer-Encoding")
62 !yaz_strcasecmp((*headers)->value, "chunked"))
64 headers = &(*headers)->next;
65 if (i < size-1 && buf[i] == '\r')
80 /* we know buffer will be smaller than o->size - i*/
81 *content_buf = (char*) odr_malloc(o, size - i);
87 for (; i < size-2; i++)
88 if (yaz_isdigit(buf[i]))
89 chunk_len = chunk_len * 16 +
91 else if (yaz_isupper(buf[i]))
92 chunk_len = chunk_len * 16 +
94 else if (yaz_islower(buf[i]))
95 chunk_len = chunk_len * 16 +
99 /* chunk extension ... */
100 while (buf[i] != '\r' && buf[i+1] != '\n')
109 i += 2; /* skip CRLF */
112 if (chunk_len < 0 || off + chunk_len > size)
118 memcpy (*content_buf + off, buf + i, chunk_len);
119 i += chunk_len + 2; /* skip chunk+CRLF */
140 *content_len = size - i;
141 *content_buf = odr_strdupn(o, buf + i, *content_len);
147 void z_HTTP_header_add_content_type(ODR o, Z_HTTP_Header **hp,
148 const char *content_type,
151 const char *l = "Content-Type";
154 char *ctype = (char *)
155 odr_malloc(o, strlen(content_type)+strlen(charset) + 15);
156 sprintf(ctype, "%s; charset=%s", content_type, charset);
157 z_HTTP_header_add(o, hp, l, ctype);
160 z_HTTP_header_add(o, hp, l, content_type);
165 * HTTP Basic authentication is described at:
166 * http://tools.ietf.org/html/rfc1945#section-11.1
168 void z_HTTP_header_add_basic_auth(ODR o, Z_HTTP_Header **hp,
169 const char *username, const char *password)
179 len = strlen(username) + strlen(password);
180 tmp = (char *) odr_malloc(o, len+2);
181 sprintf(tmp, "%s:%s", username, password);
182 buf = (char *) odr_malloc(o, (len+1) * 8/6 + 12);
183 strcpy(buf, "Basic ");
184 yaz_base64encode(tmp, &buf[strlen(buf)]);
185 z_HTTP_header_set(o, hp, "Authorization", buf);
189 void z_HTTP_header_add(ODR o, Z_HTTP_Header **hp, const char *n,
194 *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
195 (*hp)->name = odr_strdup(o, n);
196 (*hp)->value = odr_strdup(o, v);
200 void z_HTTP_header_set(ODR o, Z_HTTP_Header **hp, const char *n,
205 if (!yaz_strcasecmp((*hp)->name, n))
207 (*hp)->value = odr_strdup(o, v);
212 *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
213 (*hp)->name = odr_strdup(o, n);
214 (*hp)->value = odr_strdup(o, v);
218 const char *z_HTTP_header_remove(Z_HTTP_Header **hp, const char *n)
222 if (!yaz_strcasecmp((*hp)->name, n))
224 const char *v = (*hp)->value;
233 const char *z_HTTP_header_lookup(const Z_HTTP_Header *hp, const char *n)
235 for (; hp; hp = hp->next)
236 if (!yaz_strcasecmp(hp->name, n))
242 Z_GDU *z_get_HTTP_Request(ODR o)
244 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
245 Z_HTTP_Request *hreq;
247 p->which = Z_GDU_HTTP_Request;
248 p->u.HTTP_Request = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hreq));
249 hreq = p->u.HTTP_Request;
251 hreq->content_len = 0;
252 hreq->content_buf = 0;
253 hreq->version = "1.1";
254 hreq->method = "POST";
256 z_HTTP_header_add(o, &hreq->headers, "User-Agent", "YAZ/" YAZ_VERSION);
261 Z_GDU *z_get_HTTP_Request_host_path(ODR odr,
265 Z_GDU *p = z_get_HTTP_Request(odr);
267 p->u.HTTP_Request->path = odr_strdup(odr, path);
271 const char *cp0 = strstr(host, "://");
278 cp1 = strchr(cp0, '/');
280 cp1 = cp0+strlen(cp0);
284 char *h = odr_strdupn(odr, cp0, cp1 - cp0);
285 z_HTTP_header_add(odr, &p->u.HTTP_Request->headers, "Host", h);
291 Z_GDU *z_get_HTTP_Request_uri(ODR odr, const char *uri, const char *args,
294 Z_GDU *p = z_get_HTTP_Request(odr);
295 const char *cp0 = strstr(uri, "://");
302 cp1 = strchr(cp0, '/');
304 cp1 = cp0+strlen(cp0);
308 char *h = odr_strdupn(odr, cp0, cp1 - cp0);
309 z_HTTP_header_add(odr, &p->u.HTTP_Request->headers, "Host", h);
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_server(ODR o, int code, const char *details,
332 const char *server, const char *server_url)
334 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
335 Z_HTTP_Response *hres;
337 p->which = Z_GDU_HTTP_Response;
338 p->u.HTTP_Response = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hres));
339 hres = p->u.HTTP_Response;
341 hres->content_len = 0;
342 hres->content_buf = 0;
344 hres->version = "1.1";
345 z_HTTP_header_add(o, &hres->headers, "Server", server);
348 const char *http_err = z_HTTP_errmsg(code);
349 size_t sz = 400 + strlen(http_err) + (details ?
350 strlen(details) : 0);
351 hres->content_buf = (char*) odr_malloc(o, sz);
352 sprintf(hres->content_buf,
353 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\""
354 " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
357 " <TITLE>%s</TITLE>\n"
360 " <P><A HREF=\"%s\">%s</A></P>\n"
361 " <P>Error: %d</P>\n"
362 " <P>Description: %s</P>\n", server, server_url, server,
366 sprintf(hres->content_buf + strlen(hres->content_buf),
367 "<P>Details: %s</P>\n", details);
369 sprintf(hres->content_buf + strlen(hres->content_buf),
372 hres->content_len = strlen(hres->content_buf);
373 z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
378 Z_GDU *z_get_HTTP_Response_details(ODR o, int code, const char *details)
380 return z_get_HTTP_Response_server(o, code, details, "YAZ/" YAZ_VERSION,
381 "http://www.indexdata.com/yaz");
384 Z_GDU *z_get_HTTP_Response(ODR o, int code)
386 return z_get_HTTP_Response_details(o, code, 0);
389 const char *z_HTTP_errmsg(int code)
396 return "Switching Protocols";
404 return "Non-Authoritative Information";
408 return "Reset Content";
410 return "Partial Content";
412 return "Multiple Choices";
414 return "Moved Permenently";
420 return "Not Modified";
424 return "Temporary Redirect";
426 return "Bad Request";
430 return "Method Not Allowed";
432 return "Not Acceptable";
434 return "Proxy Authentication Required";
436 return "Request Timeout";
442 return "Length Required";
444 return "Precondition Failed";
446 return "Request Entity Too Large";
448 return "Request-URI Too Long";
450 return "Unsupported Media Type";
452 return "Requested Range Not Satisfiable";
454 return "Expectation Failed";
456 return "Internal Error";
458 return "Not Implemented";
460 return "Bad Gateway";
462 return "Service Unavailable";
464 return "Gateway Timeout";
466 return "HTTP Version Not Supported";
468 return "Unknown Error";
472 int yaz_decode_http_response(ODR o, Z_HTTP_Response **hr_p)
475 Z_HTTP_Response *hr = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hr));
476 const char *buf = o->op->buf;
477 int size = o->op->size;
484 while (i < size-2 && !strchr(" \r\n", buf[i]))
486 hr->version = odr_strdupn(o, buf + po, i - po);
494 while (i < size-2 && buf[i] >= '0' && buf[i] <= '9')
496 hr->code = hr->code*10 + (buf[i] - '0');
499 while (i < size-1 && buf[i] != '\n')
501 return decode_headers_content(o, i, &hr->headers,
502 &hr->content_buf, &hr->content_len);
505 int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p)
508 Z_HTTP_Request *hr = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
509 const char *buf = o->op->buf;
510 int size = o->op->size;
521 for (i = 0; buf[i] != ' '; i++)
522 if (i >= size-5 || i > 30)
527 hr->method = odr_strdupn(o, buf, i);
529 while (i < size && !strchr("\r\n", buf[i]))
535 if (!lspace || i >= size || lspace >= size - 5 ||
536 memcmp(buf + lspace + 1, "HTTP/", 5))
541 hr->path = odr_strdupn(o, buf + po, lspace - po);
542 hr->version = odr_strdupn(o, buf + lspace + 6, i - (lspace + 6));
543 if (i < size-1 && buf[i] == '\r')
551 return decode_headers_content(o, i, &hr->headers,
552 &hr->content_buf, &hr->content_len);
555 static void dump_http_package(ODR o, const char *buf, size_t len)
562 o->op->stream_write(o, o->op->print, ODR_VISIBLESTRING, buf, i);
567 o->op->stream_write(o, o->op->print, ODR_VISIBLESTRING, buf, i);
568 odr_printf(o, "(truncated from %ld to %d\n", (long) len, i);
571 else if (buf[i] == 0)
573 o->op->stream_write(o, o->op->print, ODR_VISIBLESTRING, buf, i);
574 odr_printf(o, "(binary data)\n", (long) len);
580 int yaz_encode_http_response(ODR o, Z_HTTP_Response *hr)
584 int top0 = o->op->top;
586 sprintf(sbuf, "HTTP/%s %d %s\r\n", hr->version,
588 z_HTTP_errmsg(hr->code));
589 odr_write(o, sbuf, strlen(sbuf));
590 /* use content_len for Content-Length */
591 sprintf(sbuf, "Content-Length: %d\r\n", hr->content_len);
592 odr_write(o, sbuf, strlen(sbuf));
593 for (h = hr->headers; h; h = h->next)
595 if (yaz_strcasecmp(h->name, "Content-Length")
596 && yaz_strcasecmp(h->name, "Transfer-Encoding"))
597 { /* skip Content-Length if given. content_len rules */
598 odr_write(o, h->name, strlen(h->name));
599 odr_write(o, ": ", 2);
600 odr_write(o, h->value, strlen(h->value));
601 odr_write(o, "\r\n", 2);
604 odr_write(o, "\r\n", 2);
606 odr_write(o, hr->content_buf, hr->content_len);
607 if (o->direction == ODR_PRINT)
609 odr_printf(o, "-- HTTP response:\n");
610 dump_http_package(o, o->op->buf + top0, o->op->top - top0);
611 odr_printf(o, "--\n");
616 int yaz_encode_http_request(ODR o, Z_HTTP_Request *hr)
620 int top0 = o->op->top;
622 if (!hr->method || !hr->path)
624 odr_write(o, hr->method, strlen(hr->method));
625 odr_write(o, " ", 1);
626 cp = strchr(hr->path, '#');
627 odr_write(o, hr->path, cp ? (cp - hr->path) : strlen(hr->path));
628 odr_write(o, " HTTP/", 6);
629 odr_write(o, hr->version, strlen(hr->version));
630 odr_write(o, "\r\n", 2);
631 if (hr->content_len &&
632 !z_HTTP_header_lookup(hr->headers,
636 sprintf(lstr, "Content-Length: %d\r\n",
638 odr_write(o, lstr, strlen(lstr));
640 for (h = hr->headers; h; h = h->next)
642 odr_write(o, h->name, strlen(h->name));
643 odr_write(o, ": ", 2);
644 odr_write(o, h->value, strlen(h->value));
645 odr_write(o, "\r\n", 2);
647 odr_write(o, "\r\n", 2);
649 odr_write(o, hr->content_buf, hr->content_len);
650 if (o->direction == ODR_PRINT)
652 odr_printf(o, "-- HTTP request:\n");
653 dump_http_package(o, o->op->buf + top0, o->op->top - top0);
654 odr_printf(o, "--\n");
659 const char *yaz_check_location(ODR odr, const char *uri, const char *location,
662 if (*location == '/')
663 { /* relative location */
665 char *nlocation = (char *) odr_malloc(odr, strlen(location)
667 strcpy(nlocation, uri);
668 cs_get_host_args(nlocation, (const char **) &args);
670 args = nlocation + strlen(nlocation);
673 strcpy(args, location);
679 /* we don't check if host is the same as before - yet */
688 * c-file-style: "Stroustrup"
689 * indent-tabs-mode: nil
691 * vim: shiftwidth=4 tabstop=8 expandtab