X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fzgdu.c;h=293c2e6ec38f26f0ddf08c2a0da05e0e7344929c;hb=60a702f390f7e2addfdab79f2328db3ba2897c8b;hp=e5458e3612fb79a55585fa92648191f0c0045d8d;hpb=0cc88e06f6f9da823d51ea963857659e3685c2de;p=yaz-moved-to-github.git diff --git a/src/zgdu.c b/src/zgdu.c index e5458e3..293c2e6 100644 --- a/src/zgdu.c +++ b/src/zgdu.c @@ -1,8 +1,13 @@ /* - * Copyright (c) 2002-2004, Index Data. + * Copyright (C) 1995-2005, Index Data ApS * See the file LICENSE for details. * - * $Id: zgdu.c,v 1.7 2004-02-23 10:14:06 adam Exp $ + * $Id: zgdu.c,v 1.12 2005-01-15 19:47:14 adam Exp $ + */ + +/** + * \file zgdu.c + * \brief Implements HTTP and Z39.50 encoding and decoding. */ #include @@ -78,11 +83,12 @@ static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers, { int off = 0; - /* we know buffer will be smaller than o->size - i - 2*/ - *content_buf = (char*) odr_malloc(o, o->size - i - 2); + /* we know buffer will be smaller than o->size - i*/ + *content_buf = (char*) odr_malloc(o, o->size - i); while (1) { + /* chunk length .. */ int chunk_len = 0; for (; i < o->size-2; i++) if (isdigit(o->buf[i])) @@ -96,14 +102,17 @@ static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers, (o->buf[i] - ('a'-10)); else break; - if (i > o->size - 2) - break; - if (o->buf[i] != '\r' || o->buf[i+1] != '\n') - { /* chunk length must be followed by \r\n */ - o->error = OHTTP; - return 0; + /* chunk extension ... */ + while (o->buf[i] != '\r' && o->buf[i+1] != '\n') + { + if (i >= o->size-2) + { + o->error = OHTTP; + return 0; + } + i++; } - i += 2; + i += 2; /* skip CRLF */ if (chunk_len == 0) break; if (chunk_len < 0 || off + chunk_len > o->size) @@ -111,8 +120,9 @@ static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers, o->error = OHTTP; return 0; } + /* copy chunk .. */ memcpy (*content_buf + off, o->buf + i, chunk_len); - i += chunk_len; + i += chunk_len + 2; /* skip chunk+CRLF */ off += chunk_len; } if (!off) @@ -247,6 +257,9 @@ int z_GDU (ODR o, Z_GDU **p, int opt, const char *name) hr = (*p)->u.HTTP_Response = (Z_HTTP_Response *) odr_malloc(o, sizeof(*hr)); + hr->content_buf = 0; + hr->content_len = 0; + po = i = 5; while (i < o->size-2 && o->buf[i] != ' ' && o->buf[i] != '\r') i++; @@ -372,9 +385,9 @@ int z_GDU (ODR o, Z_GDU **p, int opt, const char *name) (*p)->u.HTTP_Response->content_len); if (o->direction == ODR_PRINT) { - fprintf(o->print, "-- HTTP response:\n%.*s\n", o->top - top0, - o->buf + top0); - fprintf(o->print, "-- \n"); + odr_printf(o, "-- HTTP response:\n%.*s\n", o->top - top0, + o->buf + top0); + odr_printf(o, "-- \n"); } break; case Z_GDU_HTTP_Request: @@ -410,9 +423,9 @@ int z_GDU (ODR o, Z_GDU **p, int opt, const char *name) (*p)->u.HTTP_Request->content_len); if (o->direction == ODR_PRINT) { - fprintf(o->print, "-- HTTP request:\n%.*s\n", o->top - top0, + odr_printf(o, "-- HTTP request:\n%.*s\n", o->top - top0, o->buf + top0); - fprintf(o->print, "-- \n"); + odr_printf(o, "-- \n"); } break; case Z_GDU_Z3950: