2 * Copyright (c) 2002-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: zgdu.c,v 1.7 2004-02-23 10:14:06 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 - 2*/
82 *content_buf = (char*) odr_malloc(o, o->size - i - 2);
87 for (; i < o->size-2; i++)
88 if (isdigit(o->buf[i]))
89 chunk_len = chunk_len * 16 +
91 else if (isupper(o->buf[i]))
92 chunk_len = chunk_len * 16 +
93 (o->buf[i] - ('A'-10));
94 else if (islower(o->buf[i]))
95 chunk_len = chunk_len * 16 +
96 (o->buf[i] - ('a'-10));
101 if (o->buf[i] != '\r' || o->buf[i+1] != '\n')
102 { /* chunk length must be followed by \r\n */
109 if (chunk_len < 0 || off + chunk_len > o->size)
114 memcpy (*content_buf + off, o->buf + i, chunk_len);
129 else if (i == o->size)
136 *content_len = o->size - i;
137 *content_buf = (char*) odr_malloc(o, *content_len + 1);
138 memcpy(*content_buf, o->buf + i, *content_len);
139 (*content_buf)[*content_len] = '\0';
145 void z_HTTP_header_add(ODR o, Z_HTTP_Header **hp, const char *n,
150 *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
151 (*hp)->name = odr_strdup(o, n);
152 (*hp)->value = odr_strdup(o, v);
156 const char *z_HTTP_header_lookup(Z_HTTP_Header *hp, const char *n)
158 for (; hp; hp = hp->next)
159 if (!yaz_matchstr(hp->name, n))
165 Z_GDU *z_get_HTTP_Request(ODR o)
167 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
168 Z_HTTP_Request *hreq;
170 p->which = Z_GDU_HTTP_Request;
171 p->u.HTTP_Request = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hreq));
172 hreq = p->u.HTTP_Request;
174 hreq->content_len = 0;
175 hreq->content_buf = 0;
176 hreq->version = "1.1";
177 hreq->method = "POST";
179 z_HTTP_header_add(o, &hreq->headers, "User-Agent",
184 Z_GDU *z_get_HTTP_Response(ODR o, int code)
186 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
187 Z_HTTP_Response *hres;
189 p->which = Z_GDU_HTTP_Response;
190 p->u.HTTP_Response = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hres));
191 hres = p->u.HTTP_Response;
193 hres->content_len = 0;
194 hres->content_buf = 0;
196 hres->version = "1.1";
197 z_HTTP_header_add(o, &hres->headers, "Server",
201 hres->content_buf = (char*) odr_malloc(o, 400);
202 sprintf (hres->content_buf,
203 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
206 " <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
209 " <P><A HREF=\"http://www.indexdata.dk/yaz/\">YAZ</A> "
211 " <P>Error: %d</P>\n"
212 " <P>Description: %.50s</P>\n"
215 code, z_HTTP_errmsg(code));
216 hres->content_len = strlen(hres->content_buf);
217 z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
222 const char *z_HTTP_errmsg(int code)
226 else if (code == 400)
227 return "Bad Request";
228 else if (code == 404)
230 else if (code == 405)
231 return "Method Not Allowed";
232 else if (code == 500)
233 return "Internal Error";
235 return "Unknown Error";
238 int z_GDU (ODR o, Z_GDU **p, int opt, const char *name)
240 if (o->direction == ODR_DECODE) {
241 *p = (Z_GDU *) odr_malloc(o, sizeof(**p));
242 if (o->size > 10 && !memcmp(o->buf, "HTTP/", 5))
246 (*p)->which = Z_GDU_HTTP_Response;
248 hr = (*p)->u.HTTP_Response = (Z_HTTP_Response *)
249 odr_malloc(o, sizeof(*hr));
251 while (i < o->size-2 && o->buf[i] != ' ' && o->buf[i] != '\r')
253 hr->version = (char *) odr_malloc(o, i - po + 1);
255 memcpy(hr->version, o->buf + po, i - po);
256 hr->version[i-po] = 0;
257 if (o->buf[i] != ' ')
264 while (i < o->size-2 && o->buf[i] >= '0' && o->buf[i] <= '9')
266 hr->code = hr->code*10 + (o->buf[i] - '0');
269 while (i < o->size-1 && o->buf[i] != '\r')
271 return decode_headers_content(o, i, &hr->headers,
272 &hr->content_buf, &hr->content_len);
274 else if (o->size > 5 &&
275 o->buf[0] >= 0x20 && o->buf[0] < 0x7f
276 && o->buf[1] >= 0x20 && o->buf[1] < 0x7f
277 && o->buf[2] >= 0x20 && o->buf[2] < 0x7f
278 && o->buf[3] >= 0x20 && o->buf[3] < 0x7f)
283 (*p)->which = Z_GDU_HTTP_Request;
284 hr = (*p)->u.HTTP_Request =
285 (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
288 for (i = 0; o->buf[i] != ' '; i++)
289 if (i >= o->size-5 || i > 30)
294 hr->method = (char *) odr_malloc(o, i+1);
295 memcpy (hr->method, o->buf, i);
296 hr->method[i] = '\0';
299 for (i = po; o->buf[i] != ' '; i++)
305 hr->path = (char *) odr_malloc(o, i - po + 1);
306 memcpy (hr->path, o->buf+po, i - po);
307 hr->path[i - po] = '\0';
310 if (i > o->size-5 || memcmp(o->buf+i, "HTTP/", 5))
317 while (o->buf[i] != '\r')
326 hr->version = (char *) odr_malloc(o, i - po + 1);
327 memcpy(hr->version, o->buf + po, i - po);
328 hr->version[i - po] = '\0';
330 return decode_headers_content(o, i, &hr->headers,
331 &hr->content_buf, &hr->content_len);
336 (*p)->which = Z_GDU_Z3950;
337 return z_APDU(o, &(*p)->u.z3950, opt, 0);
340 else /* ENCODE or PRINT */
347 case Z_GDU_HTTP_Response:
348 sprintf(sbuf, "HTTP/%s %d %s\r\n", (*p)->u.HTTP_Response->version,
349 (*p)->u.HTTP_Response->code,
350 z_HTTP_errmsg((*p)->u.HTTP_Response->code));
351 odr_write(o, (unsigned char *) sbuf, strlen(sbuf));
352 /* apply Content-Length if not already applied */
353 if (!z_HTTP_header_lookup((*p)->u.HTTP_Response->headers,
357 sprintf(lstr, "Content-Length: %d\r\n",
358 (*p)->u.HTTP_Response->content_len);
359 odr_write(o, (unsigned char *) lstr, strlen(lstr));
361 for (h = (*p)->u.HTTP_Response->headers; h; h = h->next)
363 odr_write(o, (unsigned char *) h->name, strlen(h->name));
364 odr_write(o, (unsigned char *) ": ", 2);
365 odr_write(o, (unsigned char *) h->value, strlen(h->value));
366 odr_write(o, (unsigned char *) "\r\n", 2);
368 odr_write(o, (unsigned char *) "\r\n", 2);
369 if ((*p)->u.HTTP_Response->content_buf)
370 odr_write(o, (unsigned char *)
371 (*p)->u.HTTP_Response->content_buf,
372 (*p)->u.HTTP_Response->content_len);
373 if (o->direction == ODR_PRINT)
375 fprintf(o->print, "-- HTTP response:\n%.*s\n", o->top - top0,
377 fprintf(o->print, "-- \n");
380 case Z_GDU_HTTP_Request:
381 odr_write(o, (unsigned char *) (*p)->u.HTTP_Request->method,
382 strlen((*p)->u.HTTP_Request->method));
383 odr_write(o, (unsigned char *) " ", 1);
384 odr_write(o, (unsigned char *) (*p)->u.HTTP_Request->path,
385 strlen((*p)->u.HTTP_Request->path));
386 odr_write(o, (unsigned char *) " HTTP/", 6);
387 odr_write(o, (unsigned char *) (*p)->u.HTTP_Request->version,
388 strlen((*p)->u.HTTP_Request->version));
389 odr_write(o, (unsigned char *) "\r\n", 2);
390 if ((*p)->u.HTTP_Request->content_len &&
391 !z_HTTP_header_lookup((*p)->u.HTTP_Request->headers,
395 sprintf(lstr, "Content-Length: %d\r\n",
396 (*p)->u.HTTP_Request->content_len);
397 odr_write(o, (unsigned char *) lstr, strlen(lstr));
399 for (h = (*p)->u.HTTP_Request->headers; h; h = h->next)
401 odr_write(o, (unsigned char *) h->name, strlen(h->name));
402 odr_write(o, (unsigned char *) ": ", 2);
403 odr_write(o, (unsigned char *) h->value, strlen(h->value));
404 odr_write(o, (unsigned char *) "\r\n", 2);
406 odr_write(o, (unsigned char *) "\r\n", 2);
407 if ((*p)->u.HTTP_Request->content_buf)
408 odr_write(o, (unsigned char *)
409 (*p)->u.HTTP_Request->content_buf,
410 (*p)->u.HTTP_Request->content_len);
411 if (o->direction == ODR_PRINT)
413 fprintf(o->print, "-- HTTP request:\n%.*s\n", o->top - top0,
415 fprintf(o->print, "-- \n");
419 return z_APDU(o, &(*p)->u.z3950, opt, 0);