-
-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')
- {
- int po;
- i++;
- if (o->buf[i] != '\n')
- {
- o->error = OHTTP;
- return 0;
- }
- i++;
- if (o->buf[i] == '\r')
- break;
- for (po = i; ; i++)
- {
- if (i == o->size)
- {
- o->error = OHTTP;
- return 0;
- }
- else if (o->buf[i] == ':')
- break;
- }
- *headers = (Z_HTTP_Header *) odr_malloc(o, sizeof(**headers));
- (*headers)->name = (char*) odr_malloc(o, i - po + 1);
- memcpy ((*headers)->name, o->buf + po, i - po);
- (*headers)->name[i - po] = '\0';
- i++;
- while (i < o->size-1 && o->buf[i] == ' ')
- i++;
- for (po = i; i < o->size-1 && o->buf[i] != '\r' ; i++)
- ;
-
- (*headers)->value = (char*) odr_malloc(o, i - po + 1);
- 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;
- i++;
- if (o->buf[i] != '\n')
- {
- o->error = OHTTP;
- return 0;
- }
- i++;
-
- if (chunked)
- {
- 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)
- {
- 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;
-}
-
-void z_HTTP_header_add(ODR o, Z_HTTP_Header **hp, const char *n,
- const char *v)
-{
- while (*hp)
- hp = &(*hp)->next;
- *hp = (Z_HTTP_Header *) odr_malloc(o, sizeof(**hp));
- (*hp)->name = odr_strdup(o, n);
- (*hp)->value = odr_strdup(o, v);
- (*hp)->next = 0;
-}
-
-const char *z_HTTP_header_lookup(Z_HTTP_Header *hp, const char *n)
-{
- for (; hp; hp = hp->next)
- if (!yaz_matchstr(hp->name, n))
- return hp->value;
- return 0;
-}
-