2 * Copyright (c) 2002-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: zgdu.c,v 1.4 2003-02-17 21:23:32 adam Exp $
12 static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers,
13 char **content_buf, int *content_len)
18 while (i < o->size-1 && o->buf[i] == '\r')
22 if (o->buf[i] != '\n')
28 if (o->buf[i] == '\r')
37 else if (o->buf[i] == ':')
40 *headers = (Z_HTTP_Header *) odr_malloc(o, sizeof(**headers));
41 (*headers)->name = (char*) odr_malloc(o, i - po + 1);
42 memcpy ((*headers)->name, o->buf + po, i - po);
43 (*headers)->name[i - po] = '\0';
45 while (i < o->size-1 && o->buf[i] == ' ')
47 for (po = i; i < o->size-1 && o->buf[i] != '\r' ; i++)
50 (*headers)->value = (char*) odr_malloc(o, i - po + 1);
51 memcpy ((*headers)->value, o->buf + po, i - po);
52 (*headers)->value[i - po] = '\0';
54 headers = &(*headers)->next;
58 if (o->buf[i] != '\n')
70 else if (i == o->size)
77 *content_len = o->size - i;
78 *content_buf = (char*) odr_malloc(o, *content_len + 1);
79 memcpy(*content_buf, o->buf + i, *content_len);
80 (*content_buf)[*content_len] = '\0';
85 void z_HTTP_header_add(ODR o, Z_HTTP_Header **hp, const char *n,
90 *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
91 (*hp)->name = odr_strdup(o, n);
92 (*hp)->value = odr_strdup(o, v);
96 const char *z_HTTP_header_lookup(Z_HTTP_Header *hp, const char *n)
98 for (; hp; hp = hp->next)
99 if (!strcmp(hp->name, n))
105 Z_GDU *z_get_HTTP_Request(ODR o)
107 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
108 Z_HTTP_Request *hreq;
110 p->which = Z_GDU_HTTP_Request;
111 p->u.HTTP_Request = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hreq));
112 hreq = p->u.HTTP_Request;
114 hreq->content_len = 0;
115 hreq->content_buf = 0;
116 hreq->version = "1.1";
117 hreq->method = "POST";
119 z_HTTP_header_add(o, &hreq->headers, "User-Agent",
124 Z_GDU *z_get_HTTP_Response(ODR o, int code)
126 Z_GDU *p = (Z_GDU *) odr_malloc(o, sizeof(*p));
127 Z_HTTP_Response *hres;
129 p->which = Z_GDU_HTTP_Response;
130 p->u.HTTP_Response = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hres));
131 hres = p->u.HTTP_Response;
133 hres->content_len = 0;
134 hres->content_buf = 0;
136 hres->version = "1.1";
137 z_HTTP_header_add(o, &hres->headers, "Server",
141 hres->content_buf = (char*) odr_malloc(o, 400);
142 sprintf (hres->content_buf,
143 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
146 " <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
149 " <P><A HREF=\"http://www.indexdata.dk/yaz/\">YAZ</A> "
151 " <P>Error: %d</P>\n"
152 " <P>Description: %.50s</P>\n"
155 code, z_HTTP_errmsg(code));
156 hres->content_len = strlen(hres->content_buf);
157 z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
162 const char *z_HTTP_errmsg(int code)
166 else if (code == 400)
167 return "Bad Request";
168 else if (code == 404)
170 else if (code == 405)
171 return "Method Not Allowed";
172 else if (code == 500)
173 return "Internal Error";
175 return "Unknown Error";
178 int z_GDU (ODR o, Z_GDU **p, int opt, const char *name)
180 if (o->direction == ODR_DECODE) {
181 *p = (Z_GDU *) odr_malloc(o, sizeof(**p));
182 if (o->size > 10 && !memcmp(o->buf, "HTTP/", 5))
186 (*p)->which = Z_GDU_HTTP_Response;
189 fprintf(stderr, "-- HTTP decode:\n%.*s\n", o->size, o->buf);
191 hr = (*p)->u.HTTP_Response = (Z_HTTP_Response *)
192 odr_malloc(o, sizeof(*hr));
194 while (i < o->size-2 && o->buf[i] != ' ' && o->buf[i] != '\r')
196 hr->version = (char *) odr_malloc(o, i - po + 1);
198 memcpy(hr->version, o->buf + po, i - po);
199 hr->version[i-po] = 0;
200 if (o->buf[i] != ' ')
207 while (i < o->size-2 && o->buf[i] >= '0' && o->buf[i] <= '9')
209 hr->code = hr->code*10 + (o->buf[i] - '0');
212 while (i < o->size-1 && o->buf[i] != '\r')
214 return decode_headers_content(o, i, &hr->headers,
215 &hr->content_buf, &hr->content_len);
217 else if (o->size > 5 &&
218 o->buf[0] >= 0x20 && o->buf[0] < 0x7f
219 && o->buf[1] >= 0x20 && o->buf[1] < 0x7f
220 && o->buf[2] >= 0x20 && o->buf[2] < 0x7f
221 && o->buf[3] >= 0x20 && o->buf[3] < 0x7f)
227 fprintf(stderr, "-- HTTP decode:\n%.*s\n", o->size, o->buf);
229 (*p)->which = Z_GDU_HTTP_Request;
230 hr = (*p)->u.HTTP_Request =
231 (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
234 for (i = 0; o->buf[i] != ' '; i++)
235 if (i >= o->size-5 || i > 30)
240 hr->method = (char *) odr_malloc(o, i+1);
241 memcpy (hr->method, o->buf, i);
242 hr->method[i] = '\0';
245 for (i = po; o->buf[i] != ' '; i++)
251 hr->path = (char *) odr_malloc(o, i - po + 1);
252 memcpy (hr->path, o->buf+po, i - po);
253 hr->path[i - po] = '\0';
256 if (i > o->size-5 || memcmp(o->buf+i, "HTTP/", 5))
263 while (o->buf[i] != '\r')
272 hr->version = (char *) odr_malloc(o, i - po + 1);
273 memcpy(hr->version, o->buf + po, i - po);
274 hr->version[i - po] = '\0';
276 return decode_headers_content(o, i, &hr->headers,
277 &hr->content_buf, &hr->content_len);
282 (*p)->which = Z_GDU_Z3950;
283 return z_APDU(o, &(*p)->u.z3950, opt, 0);
286 else if (o->direction == ODR_ENCODE)
292 case Z_GDU_HTTP_Response:
293 sprintf(sbuf, "HTTP/%s %d %s\r\n", (*p)->u.HTTP_Response->version,
294 (*p)->u.HTTP_Response->code,
295 z_HTTP_errmsg((*p)->u.HTTP_Response->code));
296 odr_write(o, (unsigned char *) sbuf, strlen(sbuf));
297 /* apply Content-Length if not already applied */
298 if (!z_HTTP_header_lookup((*p)->u.HTTP_Response->headers,
302 sprintf(lstr, "%d", (*p)->u.HTTP_Response->content_len);
304 &(*p)->u.HTTP_Response->headers,
305 "Content-Length", lstr);
307 for (h = (*p)->u.HTTP_Response->headers; h; h = h->next)
309 odr_write(o, (unsigned char *) h->name, strlen(h->name));
310 odr_write(o, (unsigned char *) ": ", 2);
311 odr_write(o, (unsigned char *) h->value, strlen(h->value));
312 odr_write(o, (unsigned char *) "\r\n", 2);
314 odr_write(o, (unsigned char *) "\r\n", 2);
315 if ((*p)->u.HTTP_Response->content_buf)
316 odr_write(o, (unsigned char *)
317 (*p)->u.HTTP_Response->content_buf,
318 (*p)->u.HTTP_Response->content_len);
320 fprintf(stderr, "-- HTTP response:\n%.*s\n", o->top, o->buf);
323 case Z_GDU_HTTP_Request:
324 odr_write(o, (unsigned char *) (*p)->u.HTTP_Request->method,
325 strlen((*p)->u.HTTP_Request->method));
326 odr_write(o, (unsigned char *) " ", 1);
327 odr_write(o, (unsigned char *) (*p)->u.HTTP_Request->path,
328 strlen((*p)->u.HTTP_Request->path));
329 odr_write(o, (unsigned char *) " HTTP/", 6);
330 odr_write(o, (unsigned char *) (*p)->u.HTTP_Request->version,
331 strlen((*p)->u.HTTP_Request->version));
332 odr_write(o, (unsigned char *) "\r\n", 2);
333 if ((*p)->u.HTTP_Request->content_len &&
334 !z_HTTP_header_lookup((*p)->u.HTTP_Request->headers,
338 sprintf(lstr, "%d", (*p)->u.HTTP_Request->content_len);
340 &(*p)->u.HTTP_Request->headers,
341 "Content-Length", lstr);
343 for (h = (*p)->u.HTTP_Request->headers; h; h = h->next)
345 odr_write(o, (unsigned char *) h->name, strlen(h->name));
346 odr_write(o, (unsigned char *) ": ", 2);
347 odr_write(o, (unsigned char *) h->value, strlen(h->value));
348 odr_write(o, (unsigned char *) "\r\n", 2);
350 odr_write(o, (unsigned char *) "\r\n", 2);
351 if ((*p)->u.HTTP_Request->content_buf)
352 odr_write(o, (unsigned char *)
353 (*p)->u.HTTP_Request->content_buf,
354 (*p)->u.HTTP_Request->content_len);
356 fprintf(stderr, "-- HTTP request:\n%.*s\n", o->top, o->buf);
360 return z_APDU(o, &(*p)->u.z3950, opt, 0);
363 else if (o->direction == ODR_PRINT)
367 case Z_GDU_HTTP_Response:
368 fprintf (stderr, "not implemented");
370 case Z_GDU_HTTP_Request:
371 fprintf (stderr, "not implemented");
374 return z_APDU(o, &(*p)->u.z3950, opt, 0);