X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fzgdu.c;h=293c2e6ec38f26f0ddf08c2a0da05e0e7344929c;hb=60a702f390f7e2addfdab79f2328db3ba2897c8b;hp=543eda6d387ebd2d3797367d27aaae6f21bbae2a;hpb=da52cf68299952193806f8093c04e46d1bbf8375;p=yaz-moved-to-github.git diff --git a/src/zgdu.c b/src/zgdu.c index 543eda6..293c2e6 100644 --- a/src/zgdu.c +++ b/src/zgdu.c @@ -1,19 +1,31 @@ /* - * Copyright (c) 2002-2003, Index Data. + * Copyright (C) 1995-2005, Index Data ApS * See the file LICENSE for details. * - * $Id: zgdu.c,v 1.5 2003-12-31 00:14:01 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 #include #include #include #include +#ifdef WIN32 +#define strncasecmp _strnicmp +#define strcasecmp _stricmp +#endif + static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers, char **content_buf, int *content_len) { int i = off; + int chunked = 0; *headers = 0; while (i < o->size-1 && o->buf[i] == '\r') @@ -52,6 +64,10 @@ static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers, memcpy ((*headers)->value, o->buf + po, i - po); (*headers)->value[i - po] = '\0'; + if (!strcasecmp((*headers)->name, "Transfer-Encoding") + && + !strcasecmp((*headers)->value, "chunked")) + chunked = 1; headers = &(*headers)->next; } *headers = 0; @@ -63,22 +79,75 @@ static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers, } i++; - if (i > o->size) + if (chunked) { - o->error = OHTTP; - return 0; + int off = 0; + + /* 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])) + chunk_len = chunk_len * 16 + + (o->buf[i] - '0'); + else if (isupper(o->buf[i])) + chunk_len = chunk_len * 16 + + (o->buf[i] - ('A'-10)); + else if (islower(o->buf[i])) + chunk_len = chunk_len * 16 + + (o->buf[i] - ('a'-10)); + else + break; + /* 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; /* skip CRLF */ + if (chunk_len == 0) + break; + if (chunk_len < 0 || off + chunk_len > o->size) + { + o->error = OHTTP; + return 0; + } + /* copy chunk .. */ + memcpy (*content_buf + off, o->buf + i, chunk_len); + i += chunk_len + 2; /* skip chunk+CRLF */ + off += chunk_len; + } + if (!off) + *content_buf = 0; + *content_len = off; } - else if (i == o->size) - { - *content_buf = 0; - *content_len = 0; - } - else + else { - *content_len = o->size - i; - *content_buf = (char*) odr_malloc(o, *content_len + 1); - memcpy(*content_buf, o->buf + i, *content_len); - (*content_buf)[*content_len] = '\0'; + if (i > o->size) + { + o->error = OHTTP; + return 0; + } + else if (i == o->size) + { + *content_buf = 0; + *content_len = 0; + } + else + { + *content_len = o->size - i; + *content_buf = (char*) odr_malloc(o, *content_len + 1); + memcpy(*content_buf, o->buf + i, *content_len); + (*content_buf)[*content_len] = '\0'; + } } return 1; } @@ -188,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++; @@ -313,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: @@ -351,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: