2 * Copyright (c) 2002-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: zgdu.c,v 1.9 2004-02-25 12:59:56 adam Exp $
10 #include <yaz/yaz-version.h>
11 #include <yaz/yaz-iconv.h>
15 #define strncasecmp _strnicmp
16 #define strcasecmp _stricmp
19 static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers,
20 char **content_buf, int *content_len)
26 while (i < o->size-1 && o->buf[i] == '\r')
30 if (o->buf[i] != '\n')
36 if (o->buf[i] == '\r')
45 else if (o->buf[i] == ':')
48 *headers = (Z_HTTP_Header *) odr_malloc(o, sizeof(**headers));
49 (*headers)->name = (char*) odr_malloc(o, i - po + 1);
50 memcpy ((*headers)->name, o->buf + po, i - po);
51 (*headers)->name[i - po] = '\0';
53 while (i < o->size-1 && o->buf[i] == ' ')
55 for (po = i; i < o->size-1 && o->buf[i] != '\r' ; i++)
58 (*headers)->value = (char*) odr_malloc(o, i - po + 1);
59 memcpy ((*headers)->value, o->buf + po, i - po);
60 (*headers)->value[i - po] = '\0';
62 if (!strcasecmp((*headers)->name, "Transfer-Encoding")
64 !strcasecmp((*headers)->value, "chunked"))
66 headers = &(*headers)->next;
70 if (o->buf[i] != '\n')
81 /* we know buffer will be smaller than o->size - i*/
82 *content_buf = (char*) odr_malloc(o, o->size - i);
88 for (; i < o->size-2; i++)
89 if (isdigit(o->buf[i]))
90 chunk_len = chunk_len * 16 +
92 else if (isupper(o->buf[i]))
93 chunk_len = chunk_len * 16 +
94 (o->buf[i] - ('A'-10));
95 else if (islower(o->buf[i]))
96 chunk_len = chunk_len * 16 +
97 (o->buf[i] - ('a'-10));
100 /* chunk extension ... */
101 while (o->buf[i] != '\r' && o->buf[i+1] != '\n')
110 i += 2; /* skip CRLF */
113 if (chunk_len < 0 || off + chunk_len > o->size)
119 memcpy (*content_buf + off, o->buf + i, chunk_len);
120 i += chunk_len + 2; /* skip chunk+CRLF */
134 else if (i == o->size)
141 *content_len = o->size - i;
142 *content_buf = (char*) odr_malloc(o, *content_len + 1);
143 memcpy(*content_buf, o->buf + i, *content_len);
144 (*content_buf)[*content_len] = '\0';
150 void z_HTTP_header_add(ODR o, Z_HTTP_Header **hp, const char *n,
155 *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
156 (*hp)->name = odr_strdup(o, n);
157 (*hp)->value = odr_strdup(o, v);
161 const char *z_HTTP_header_lookup(Z_HTTP_Header *hp, const char *n)
163 for (; hp; hp = hp->next)
164 if (!yaz_matchstr(hp->name, n))
170 Z_GDU *z_get_HTTP_Request(ODR o)
172 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
173 Z_HTTP_Request *hreq;
175 p->which = Z_GDU_HTTP_Request;
176 p->u.HTTP_Request = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hreq));
177 hreq = p->u.HTTP_Request;
179 hreq->content_len = 0;
180 hreq->content_buf = 0;
181 hreq->version = "1.1";
182 hreq->method = "POST";
184 z_HTTP_header_add(o, &hreq->headers, "User-Agent",
189 Z_GDU *z_get_HTTP_Response(ODR o, int code)
191 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
192 Z_HTTP_Response *hres;
194 p->which = Z_GDU_HTTP_Response;
195 p->u.HTTP_Response = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hres));
196 hres = p->u.HTTP_Response;
198 hres->content_len = 0;
199 hres->content_buf = 0;
201 hres->version = "1.1";
202 z_HTTP_header_add(o, &hres->headers, "Server",
206 hres->content_buf = (char*) odr_malloc(o, 400);
207 sprintf (hres->content_buf,
208 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
211 " <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
214 " <P><A HREF=\"http://www.indexdata.dk/yaz/\">YAZ</A> "
216 " <P>Error: %d</P>\n"
217 " <P>Description: %.50s</P>\n"
220 code, z_HTTP_errmsg(code));
221 hres->content_len = strlen(hres->content_buf);
222 z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
227 const char *z_HTTP_errmsg(int code)
231 else if (code == 400)
232 return "Bad Request";
233 else if (code == 404)
235 else if (code == 405)
236 return "Method Not Allowed";
237 else if (code == 500)
238 return "Internal Error";
240 return "Unknown Error";
243 int z_GDU (ODR o, Z_GDU **p, int opt, const char *name)
245 if (o->direction == ODR_DECODE) {
246 *p = (Z_GDU *) odr_malloc(o, sizeof(**p));
247 if (o->size > 10 && !memcmp(o->buf, "HTTP/", 5))
251 (*p)->which = Z_GDU_HTTP_Response;
253 hr = (*p)->u.HTTP_Response = (Z_HTTP_Response *)
254 odr_malloc(o, sizeof(*hr));
259 while (i < o->size-2 && o->buf[i] != ' ' && o->buf[i] != '\r')
261 hr->version = (char *) odr_malloc(o, i - po + 1);
263 memcpy(hr->version, o->buf + po, i - po);
264 hr->version[i-po] = 0;
265 if (o->buf[i] != ' ')
272 while (i < o->size-2 && o->buf[i] >= '0' && o->buf[i] <= '9')
274 hr->code = hr->code*10 + (o->buf[i] - '0');
277 while (i < o->size-1 && o->buf[i] != '\r')
279 return decode_headers_content(o, i, &hr->headers,
280 &hr->content_buf, &hr->content_len);
282 else if (o->size > 5 &&
283 o->buf[0] >= 0x20 && o->buf[0] < 0x7f
284 && o->buf[1] >= 0x20 && o->buf[1] < 0x7f
285 && o->buf[2] >= 0x20 && o->buf[2] < 0x7f
286 && o->buf[3] >= 0x20 && o->buf[3] < 0x7f)
291 (*p)->which = Z_GDU_HTTP_Request;
292 hr = (*p)->u.HTTP_Request =
293 (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
296 for (i = 0; o->buf[i] != ' '; i++)
297 if (i >= o->size-5 || i > 30)
302 hr->method = (char *) odr_malloc(o, i+1);
303 memcpy (hr->method, o->buf, i);
304 hr->method[i] = '\0';
307 for (i = po; o->buf[i] != ' '; i++)
313 hr->path = (char *) odr_malloc(o, i - po + 1);
314 memcpy (hr->path, o->buf+po, i - po);
315 hr->path[i - po] = '\0';
318 if (i > o->size-5 || memcmp(o->buf+i, "HTTP/", 5))
325 while (o->buf[i] != '\r')
334 hr->version = (char *) odr_malloc(o, i - po + 1);
335 memcpy(hr->version, o->buf + po, i - po);
336 hr->version[i - po] = '\0';
338 return decode_headers_content(o, i, &hr->headers,
339 &hr->content_buf, &hr->content_len);
344 (*p)->which = Z_GDU_Z3950;
345 return z_APDU(o, &(*p)->u.z3950, opt, 0);
348 else /* ENCODE or PRINT */
355 case Z_GDU_HTTP_Response:
356 sprintf(sbuf, "HTTP/%s %d %s\r\n", (*p)->u.HTTP_Response->version,
357 (*p)->u.HTTP_Response->code,
358 z_HTTP_errmsg((*p)->u.HTTP_Response->code));
359 odr_write(o, (unsigned char *) sbuf, strlen(sbuf));
360 /* apply Content-Length if not already applied */
361 if (!z_HTTP_header_lookup((*p)->u.HTTP_Response->headers,
365 sprintf(lstr, "Content-Length: %d\r\n",
366 (*p)->u.HTTP_Response->content_len);
367 odr_write(o, (unsigned char *) lstr, strlen(lstr));
369 for (h = (*p)->u.HTTP_Response->headers; h; h = h->next)
371 odr_write(o, (unsigned char *) h->name, strlen(h->name));
372 odr_write(o, (unsigned char *) ": ", 2);
373 odr_write(o, (unsigned char *) h->value, strlen(h->value));
374 odr_write(o, (unsigned char *) "\r\n", 2);
376 odr_write(o, (unsigned char *) "\r\n", 2);
377 if ((*p)->u.HTTP_Response->content_buf)
378 odr_write(o, (unsigned char *)
379 (*p)->u.HTTP_Response->content_buf,
380 (*p)->u.HTTP_Response->content_len);
381 if (o->direction == ODR_PRINT)
383 fprintf(o->print, "-- HTTP response:\n%.*s\n", o->top - top0,
385 fprintf(o->print, "-- \n");
388 case Z_GDU_HTTP_Request:
389 odr_write(o, (unsigned char *) (*p)->u.HTTP_Request->method,
390 strlen((*p)->u.HTTP_Request->method));
391 odr_write(o, (unsigned char *) " ", 1);
392 odr_write(o, (unsigned char *) (*p)->u.HTTP_Request->path,
393 strlen((*p)->u.HTTP_Request->path));
394 odr_write(o, (unsigned char *) " HTTP/", 6);
395 odr_write(o, (unsigned char *) (*p)->u.HTTP_Request->version,
396 strlen((*p)->u.HTTP_Request->version));
397 odr_write(o, (unsigned char *) "\r\n", 2);
398 if ((*p)->u.HTTP_Request->content_len &&
399 !z_HTTP_header_lookup((*p)->u.HTTP_Request->headers,
403 sprintf(lstr, "Content-Length: %d\r\n",
404 (*p)->u.HTTP_Request->content_len);
405 odr_write(o, (unsigned char *) lstr, strlen(lstr));
407 for (h = (*p)->u.HTTP_Request->headers; h; h = h->next)
409 odr_write(o, (unsigned char *) h->name, strlen(h->name));
410 odr_write(o, (unsigned char *) ": ", 2);
411 odr_write(o, (unsigned char *) h->value, strlen(h->value));
412 odr_write(o, (unsigned char *) "\r\n", 2);
414 odr_write(o, (unsigned char *) "\r\n", 2);
415 if ((*p)->u.HTTP_Request->content_buf)
416 odr_write(o, (unsigned char *)
417 (*p)->u.HTTP_Request->content_buf,
418 (*p)->u.HTTP_Request->content_len);
419 if (o->direction == ODR_PRINT)
421 fprintf(o->print, "-- HTTP request:\n%.*s\n", o->top - top0,
423 fprintf(o->print, "-- \n");
427 return z_APDU(o, &(*p)->u.z3950, opt, 0);