2 * Copyright (c) 2002-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: zgdu.c,v 1.1 2003-02-12 15:06:44 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 = odr_malloc(o, sizeof(**headers));
41 (*headers)->name = 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 = 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 = 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 = 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_Response(ODR o, int code)
107 Z_GDU *p = odr_malloc(o, sizeof(*p));
108 Z_HTTP_Response *hres;
110 p->which = Z_GDU_HTTP_Response;
111 hres = p->u.HTTP_Response = odr_malloc(o, sizeof(*hres));
113 hres->content_len = 0;
114 hres->content_buf = 0;
116 hres->version = "1.1";
117 z_HTTP_header_add(o, &hres->headers, "Server",
121 hres->content_buf = odr_malloc(o, 400);
122 sprintf (hres->content_buf,
123 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
126 " <TITLE>YAZ " YAZ_VERSION "</TITLE>\n"
129 " <P><A HREF=\"http://www.indexdata.dk/yaz/\">YAZ</A> "
131 " <P>Error: %d</P>\n"
132 " <P>Description: %.50s</P>\n"
135 code, z_HTTP_errmsg(code));
136 hres->content_len = strlen(hres->content_buf);
137 z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/html");
142 const char *z_HTTP_errmsg(int code)
146 else if (code == 400)
147 return "Bad Request";
148 else if (code == 404)
150 else if (code == 405)
151 return "Method Not Allowed";
152 else if (code == 500)
153 return "Internal Error";
155 return "Unknown Error";
158 int z_GDU (ODR o, Z_GDU **p, int opt, const char *name)
160 if (o->direction == ODR_DECODE) {
161 *p = odr_malloc(o, sizeof(**p));
162 if (o->size > 10 && !memcmp(o->buf, "HTTP/", 5))
166 (*p)->which = Z_GDU_HTTP_Response;
168 hr = (*p)->u.HTTP_Response = odr_malloc(o, sizeof(*hr));
171 while (i < o->size-2 && o->buf[i] != ' ' && o->buf[i] != '\r')
173 hr->version = odr_malloc(o, i - po + 1);
175 memcpy(hr->version, o->buf + po, i - po);
176 hr->version[i-po] = 0;
177 if (o->buf[i] != ' ')
184 while (i < o->size-2 && o->buf[i] >= '0' && o->buf[i] <= '9')
186 hr->code = hr->code*10 + (o->buf[i] - '0');
189 while (i < o->size-1 && o->buf[i] != '\r')
191 return decode_headers_content(o, i, &hr->headers,
192 &hr->content_buf, &hr->content_len);
194 else if (o->size > 5 &&
195 o->buf[0] >= 0x20 && o->buf[0] < 0x7f
196 && o->buf[1] >= 0x20 && o->buf[1] < 0x7f
197 && o->buf[2] >= 0x20 && o->buf[2] < 0x7f
198 && o->buf[3] >= 0x20 && o->buf[3] < 0x7f)
204 fprintf(stderr, "HTTP decode:\n%.*s\n", o->size, o->buf);
206 (*p)->which = Z_GDU_HTTP_Request;
207 hr = (*p)->u.HTTP_Request = odr_malloc(o, sizeof(*hr));
210 for (i = 0; o->buf[i] != ' '; i++)
211 if (i >= o->size-5 || i > 30)
216 hr->method = odr_malloc(o, i+1);
217 memcpy (hr->method, o->buf, i);
218 hr->method[i] = '\0';
221 for (i = po; o->buf[i] != ' '; i++)
227 hr->path = odr_malloc(o, i - po + 1);
228 memcpy (hr->path, o->buf+po, i - po);
229 hr->path[i - po] = '\0';
232 if (i > o->size-5 || memcmp(o->buf+i, "HTTP/", 5))
239 while (o->buf[i] != '\r')
248 hr->version = odr_malloc(o, i - po + 1);
249 memcpy(hr->version, o->buf + po, i - po);
250 hr->version[i - po] = '\0';
252 return decode_headers_content(o, i, &hr->headers,
253 &hr->content_buf, &hr->content_len);
258 (*p)->which = Z_GDU_Z3950;
259 return z_APDU(o, &(*p)->u.z3950, opt, 0);
262 else if (o->direction == ODR_ENCODE)
268 case Z_GDU_HTTP_Response:
269 sprintf(sbuf, "HTTP/%s %d %s\r\n", (*p)->u.HTTP_Response->version,
270 (*p)->u.HTTP_Response->code,
271 z_HTTP_errmsg((*p)->u.HTTP_Response->code));
272 odr_write(o, sbuf, strlen(sbuf));
273 /* apply Content-Length if not already applied */
274 if (!z_HTTP_header_lookup((*p)->u.HTTP_Response->headers,
278 sprintf(lstr, "%d", (*p)->u.HTTP_Response->content_len);
280 &(*p)->u.HTTP_Response->headers,
281 "Content-Length", lstr);
283 for (h = (*p)->u.HTTP_Response->headers; h; h = h->next)
285 odr_write(o, h->name, strlen(h->name));
286 odr_write(o, ": ", 2);
287 odr_write(o, h->value, strlen(h->value));
288 odr_write(o, "\r\n", 2);
290 odr_write(o, "\r\n", 2);
291 if ((*p)->u.HTTP_Response->content_buf)
292 odr_write(o, (*p)->u.HTTP_Response->content_buf,
293 (*p)->u.HTTP_Response->content_len);
295 fprintf(stderr, "HTTP response:\n%.*s\n", o->top, o->buf);
298 case Z_GDU_HTTP_Request:
299 odr_write(o, (*p)->u.HTTP_Request->method,
300 strlen((*p)->u.HTTP_Request->method));
301 odr_write(o, " ", 1);
302 odr_write(o, (*p)->u.HTTP_Request->path,
303 strlen((*p)->u.HTTP_Request->path));
304 odr_write(o, " HTTP/", 6);
305 odr_write(o, (*p)->u.HTTP_Request->version,
306 strlen((*p)->u.HTTP_Request->version));
307 odr_write(o, "\r\n", 2);
308 for (h = (*p)->u.HTTP_Request->headers; h; h = h->next)
310 odr_write(o, h->name, strlen(h->name));
311 odr_write(o, ": ", 2);
312 odr_write(o, h->value, strlen(h->value));
313 odr_write(o, "\r\n", 2);
315 odr_write(o, "\r\n", 2);
316 if ((*p)->u.HTTP_Request->content_buf)
317 odr_write(o, (*p)->u.HTTP_Request->content_buf,
318 (*p)->u.HTTP_Request->content_len);
320 fprintf(stderr, "HTTP request:\n%.*s\n", o->top, o->buf);
324 return z_APDU(o, &(*p)->u.z3950, opt, 0);
327 else if (o->direction == ODR_PRINT)
331 case Z_GDU_HTTP_Response:
332 fprintf (stderr, "not implemented");
334 case Z_GDU_HTTP_Request:
335 fprintf (stderr, "not implemented");
338 return z_APDU(o, &(*p)->u.z3950, opt, 0);