1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
8 * \brief Implements HTTP decoding
13 #include <yaz/yaz-version.h>
14 #include <yaz/yaz-iconv.h>
15 #include <yaz/matchstr.h>
19 #define strncasecmp _strnicmp
20 #define strcasecmp _stricmp
25 * This function's counterpart, yaz_base64decode(), is in srwutil.c.
26 * I feel bad that they're not together, but each function is only
27 * needed in one place, and those places are not together. Maybe one
28 * day we'll move them into a new httputil.c, and declare them in a
29 * corresponding httputil.h
31 static void yaz_base64encode(const char *in, char *out)
33 static char encoding[] =
34 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
51 /* Treat three eight-bit numbers as on 24-bit number */
52 n = (buf[0] << 16) + (buf[1] << 8) + buf[2];
54 /* Write the six-bit chunks out as four encoded characters */
55 *out++ = encoding[(n >> 18) & 63];
56 *out++ = encoding[(n >> 12) & 63];
58 *out++ = encoding[(n >> 6) & 63];
59 if (in[1] != 0 && in[2] != 0)
60 *out++ = encoding[n & 63];
74 static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers,
75 char **content_buf, int *content_len)
81 while (i < o->size-1 && o->buf[i] == '\n')
85 if (o->buf[i] == '\r' && i < o->size-1 && o->buf[i+1] == '\n')
90 if (o->buf[i] == '\n')
99 else if (o->buf[i] == ':')
102 *headers = (Z_HTTP_Header *) odr_malloc(o, sizeof(**headers));
103 (*headers)->name = (char*) odr_malloc(o, i - po + 1);
104 memcpy ((*headers)->name, o->buf + po, i - po);
105 (*headers)->name[i - po] = '\0';
107 while (i < o->size-1 && o->buf[i] == ' ')
109 for (po = i; i < o->size-1 && !strchr("\r\n", o->buf[i]); i++)
112 (*headers)->value = (char*) odr_malloc(o, i - po + 1);
113 memcpy ((*headers)->value, o->buf + po, i - po);
114 (*headers)->value[i - po] = '\0';
116 if (!strcasecmp((*headers)->name, "Transfer-Encoding")
118 !strcasecmp((*headers)->value, "chunked"))
120 headers = &(*headers)->next;
121 if (i < o->size-1 && o->buf[i] == '\r')
125 if (o->buf[i] != '\n')
136 /* we know buffer will be smaller than o->size - i*/
137 *content_buf = (char*) odr_malloc(o, o->size - i);
141 /* chunk length .. */
143 for (; i < o->size-2; i++)
144 if (isdigit(o->buf[i]))
145 chunk_len = chunk_len * 16 +
147 else if (isupper(o->buf[i]))
148 chunk_len = chunk_len * 16 +
149 (o->buf[i] - ('A'-10));
150 else if (islower(o->buf[i]))
151 chunk_len = chunk_len * 16 +
152 (o->buf[i] - ('a'-10));
155 /* chunk extension ... */
156 while (o->buf[i] != '\r' && o->buf[i+1] != '\n')
165 i += 2; /* skip CRLF */
168 if (chunk_len < 0 || off + chunk_len > o->size)
174 memcpy (*content_buf + off, o->buf + i, chunk_len);
175 i += chunk_len + 2; /* skip chunk+CRLF */
189 else if (i == o->size)
196 *content_len = o->size - i;
197 *content_buf = (char*) odr_malloc(o, *content_len + 1);
198 memcpy(*content_buf, o->buf + i, *content_len);
199 (*content_buf)[*content_len] = '\0';
205 void z_HTTP_header_add_content_type(ODR o, Z_HTTP_Header **hp,
206 const char *content_type,
209 const char *l = "Content-Type";
212 char *ctype = (char *)
213 odr_malloc(o, strlen(content_type)+strlen(charset) + 15);
214 sprintf(ctype, "%s; charset=%s", content_type, charset);
215 z_HTTP_header_add(o, hp, l, ctype);
218 z_HTTP_header_add(o, hp, l, content_type);
223 * HTTP Basic authentication is described at:
224 * http://tools.ietf.org/html/rfc1945#section-11.1
226 void z_HTTP_header_add_basic_auth(ODR o, Z_HTTP_Header **hp,
227 const char *username, const char *password)
235 len = strlen(username) + strlen(password);
236 tmp = (char *) odr_malloc(o, len+2);
237 sprintf(tmp, "%s:%s", username, password);
238 buf = (char *) odr_malloc(o, (len+1) * 8/6 + 12);
239 strcpy(buf, "Basic ");
240 yaz_base64encode(tmp, &buf[strlen(buf)]);
241 z_HTTP_header_add(o, hp, "Authorization", buf);
245 void z_HTTP_header_add(ODR o, Z_HTTP_Header **hp, const char *n,
250 *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
251 (*hp)->name = odr_strdup(o, n);
252 (*hp)->value = odr_strdup(o, v);
256 const char *z_HTTP_header_lookup(const Z_HTTP_Header *hp, const char *n)
258 for (; hp; hp = hp->next)
259 if (!yaz_matchstr(hp->name, n))
265 Z_GDU *z_get_HTTP_Request(ODR o)
267 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
268 Z_HTTP_Request *hreq;
270 p->which = Z_GDU_HTTP_Request;
271 p->u.HTTP_Request = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hreq));
272 hreq = p->u.HTTP_Request;
274 hreq->content_len = 0;
275 hreq->content_buf = 0;
276 hreq->version = "1.1";
277 hreq->method = "POST";
279 z_HTTP_header_add(o, &hreq->headers, "User-Agent", "YAZ/" YAZ_VERSION);
284 Z_GDU *z_get_HTTP_Request_host_path(ODR odr,
288 Z_GDU *p = z_get_HTTP_Request(odr);
290 p->u.HTTP_Request->path = odr_strdup(odr, path);
294 const char *cp0 = strstr(host, "://");
301 cp1 = strchr(cp0, '/');
303 cp1 = cp0+strlen(cp0);
307 char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
308 memcpy (h, cp0, cp1 - cp0);
310 z_HTTP_header_add(odr, &p->u.HTTP_Request->headers,
318 Z_GDU *z_get_HTTP_Response(ODR o, int code)
320 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
321 Z_HTTP_Response *hres;
323 p->which = Z_GDU_HTTP_Response;
324 p->u.HTTP_Response = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hres));
325 hres = p->u.HTTP_Response;
327 hres->content_len = 0;
328 hres->content_buf = 0;
330 hres->version = "1.1";
331 z_HTTP_header_add(o, &hres->headers, "Server",
335 hres->content_buf = (char*) odr_malloc(o, 400);
336 sprintf(hres->content_buf,
337 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\""
338 " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
341 " <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
344 " <P><A HREF=\"http://www.indexdata.com/yaz/\">YAZ</A> "
346 " <P>Error: %d</P>\n"
347 " <P>Description: %.50s</P>\n"
350 code, z_HTTP_errmsg(code));
351 hres->content_len = strlen(hres->content_buf);
352 z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
357 const char *z_HTTP_errmsg(int code)
361 else if (code == 400)
362 return "Bad Request";
363 else if (code == 404)
365 else if (code == 405)
366 return "Method Not Allowed";
367 else if (code == 500)
368 return "Internal Error";
370 return "Unknown Error";
373 int yaz_decode_http_response(ODR o, Z_HTTP_Response **hr_p)
376 Z_HTTP_Response *hr = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hr));
383 while (i < o->size-2 && !strchr(" \r\n", o->buf[i]))
385 hr->version = (char *) odr_malloc(o, i - po + 1);
387 memcpy(hr->version, o->buf + po, i - po);
388 hr->version[i-po] = 0;
389 if (o->buf[i] != ' ')
396 while (i < o->size-2 && o->buf[i] >= '0' && o->buf[i] <= '9')
398 hr->code = hr->code*10 + (o->buf[i] - '0');
401 while (i < o->size-1 && o->buf[i] != '\n')
403 return decode_headers_content(o, i, &hr->headers,
404 &hr->content_buf, &hr->content_len);
407 int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p)
410 Z_HTTP_Request *hr = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
415 for (i = 0; o->buf[i] != ' '; i++)
416 if (i >= o->size-5 || i > 30)
421 hr->method = (char *) odr_malloc(o, i+1);
422 memcpy (hr->method, o->buf, i);
423 hr->method[i] = '\0';
426 for (i = po; o->buf[i] != ' '; i++)
432 hr->path = (char *) odr_malloc(o, i - po + 1);
433 memcpy (hr->path, o->buf+po, i - po);
434 hr->path[i - po] = '\0';
437 if (i > o->size-5 || memcmp(o->buf+i, "HTTP/", 5))
444 while (i < o->size && !strchr("\r\n", o->buf[i]))
446 hr->version = (char *) odr_malloc(o, i - po + 1);
447 memcpy(hr->version, o->buf + po, i - po);
448 hr->version[i - po] = '\0';
450 if (i < o->size-1 && o->buf[i] == '\r')
452 if (o->buf[i] != '\n')
457 return decode_headers_content(o, i, &hr->headers,
458 &hr->content_buf, &hr->content_len);
461 int yaz_encode_http_response(ODR o, Z_HTTP_Response *hr)
467 sprintf(sbuf, "HTTP/%s %d %s\r\n", hr->version,
469 z_HTTP_errmsg(hr->code));
470 odr_write(o, (unsigned char *) sbuf, strlen(sbuf));
471 /* apply Content-Length if not already applied */
472 if (!z_HTTP_header_lookup(hr->headers,
476 sprintf(lstr, "Content-Length: %d\r\n",
478 odr_write(o, (unsigned char *) lstr, strlen(lstr));
480 for (h = hr->headers; h; h = h->next)
482 odr_write(o, (unsigned char *) h->name, strlen(h->name));
483 odr_write(o, (unsigned char *) ": ", 2);
484 odr_write(o, (unsigned char *) h->value, strlen(h->value));
485 odr_write(o, (unsigned char *) "\r\n", 2);
487 odr_write(o, (unsigned char *) "\r\n", 2);
489 odr_write(o, (unsigned char *)
492 if (o->direction == ODR_PRINT)
494 odr_printf(o, "-- HTTP response:\n%.*s\n", o->top - top0,
496 odr_printf(o, "-- \n");
501 int yaz_encode_http_request(ODR o, Z_HTTP_Request *hr)
506 odr_write(o, (unsigned char *) hr->method,
508 odr_write(o, (unsigned char *) " ", 1);
509 odr_write(o, (unsigned char *) hr->path,
511 odr_write(o, (unsigned char *) " HTTP/", 6);
512 odr_write(o, (unsigned char *) hr->version,
513 strlen(hr->version));
514 odr_write(o, (unsigned char *) "\r\n", 2);
515 if (hr->content_len &&
516 !z_HTTP_header_lookup(hr->headers,
520 sprintf(lstr, "Content-Length: %d\r\n",
522 odr_write(o, (unsigned char *) lstr, strlen(lstr));
524 for (h = hr->headers; h; h = h->next)
526 odr_write(o, (unsigned char *) h->name, strlen(h->name));
527 odr_write(o, (unsigned char *) ": ", 2);
528 odr_write(o, (unsigned char *) h->value, strlen(h->value));
529 odr_write(o, (unsigned char *) "\r\n", 2);
531 odr_write(o, (unsigned char *) "\r\n", 2);
533 odr_write(o, (unsigned char *)
536 if (o->direction == ODR_PRINT)
538 odr_printf(o, "-- HTTP request:\n%.*s\n", o->top - top0,
540 odr_printf(o, "-- \n");
548 * c-file-style: "Stroustrup"
549 * indent-tabs-mode: nil
551 * vim: shiftwidth=4 tabstop=8 expandtab