Codebase list cyrus-imapd / upstream/2.5.11 imap / http_client.c
upstream/2.5.11

Tree @upstream/2.5.11 (Download .tar.gz)

http_client.c @upstream/2.5.11raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/* http_client.c - HTTP client-side support functions
 *
 * Copyright (c) 1994-2014 Carnegie Mellon University.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The name "Carnegie Mellon University" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For permission or any legal
 *    details, please contact
 *      Carnegie Mellon University
 *      Center for Technology Transfer and Enterprise Creation
 *      4615 Forbes Avenue
 *      Suite 302
 *      Pittsburgh, PA  15213
 *      (412) 268-7393, fax: (412) 268-7395
 *      innovation@andrew.cmu.edu
 *
 * 4. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by Computing Services
 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
 *
 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include <config.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <string.h>
#include <syslog.h>

#include "http_err.h"
#include "http_client.h"
#include "prot.h"
#include "tok.h"


/* Compare Content-Types */
int is_mediatype(const char *pat, const char *type)
{
    const char *psep = strchr(pat, '/');
    const char *tsep = strchr(type, '/');
    size_t plen;
    size_t tlen;
    int alltypes;

    /* Check type */
    if (!psep || !tsep) return 0;
    plen = psep - pat;
    tlen = tsep - type;

    alltypes = !strncmp(pat, "*", plen);

    if (!alltypes && ((tlen != plen) || strncasecmp(pat, type, tlen))) return 0;

    /* Check subtype */
    pat = ++psep;
    plen = strcspn(pat, "; \r\n\0");
    type = ++tsep;
    tlen = strcspn(type, "; \r\n\0");

    return (!strncmp(pat, "*", plen) ||
	    (!alltypes && (tlen == plen) && !strncasecmp(pat, type, tlen)));
}


/*
 * Parse the framing of a request or response message.
 * Handles chunked, gzip, deflate TE only.
 * Handles close-delimited response bodies (no Content-Length specified) 
 */
int http_parse_framing(hdrcache_t hdrs, struct body_t *body,
		       const char **errstr)
{
    static unsigned max_msgsize = 0;
    const char **hdr;

    if (!max_msgsize) {
	max_msgsize = config_getint(IMAPOPT_MAXMESSAGESIZE);

	/* If max_msgsize is 0, allow any size */
	if (!max_msgsize) max_msgsize = INT_MAX;
    }

    body->framing = FRAMING_LENGTH;
    body->te = TE_NONE;
    body->len = 0;
    body->max = max_msgsize;

    /* Check for Transfer-Encoding */
    if ((hdr = spool_getheader(hdrs, "Transfer-Encoding"))) {
	for (; *hdr; hdr++) {
	    tok_t tok = TOK_INITIALIZER(*hdr, ",", TOK_TRIMLEFT|TOK_TRIMRIGHT);
	    char *token;

	    while ((token = tok_next(&tok))) {
		if (body->te & TE_CHUNKED) {
		    /* "chunked" MUST only appear once and MUST be last */
		    break;
		}
		else if (!strcasecmp(token, "chunked")) {
		    body->te |= TE_CHUNKED;
		    body->framing = FRAMING_CHUNKED;
		}
		else if (body->te & ~TE_CHUNKED) {
		    /* can't combine compression codings */
		    break;
		}
#ifdef HAVE_ZLIB
		else if (!strcasecmp(token, "deflate"))
		    body->te = TE_DEFLATE;
		else if (!strcasecmp(token, "gzip") ||
			 !strcasecmp(token, "x-gzip"))
		    body->te = TE_GZIP;
#endif
		else if (!(body->flags & BODY_DISCARD)) {
		    /* unknown/unsupported TE */
		    break;
		}
	    }
	    tok_fini(&tok);
	    if (token) break;  /* error */
	}

	if (*hdr) {
	    body->te = TE_UNKNOWN;
	    *errstr = "Specified Transfer-Encoding not implemented";
	    return HTTP_BAD_MEDIATYPE;
	}

	/* Check if this is a non-chunked response */
	else if (!(body->te & TE_CHUNKED)) {
	    if ((body->flags & BODY_RESPONSE) && (body->flags & BODY_CLOSE)) {
		body->framing = FRAMING_CLOSE;
	    }
	    else {
		body->te = TE_UNKNOWN;
		*errstr = "Final Transfer-Encoding MUST be \"chunked\"";
		return HTTP_BAD_MEDIATYPE;
	    }
	}
    }

    /* Check for Content-Length */
    else if ((hdr = spool_getheader(hdrs, "Content-Length"))) {
	if (hdr[1]) {
	    *errstr = "Multiple Content-Length header fields";
	    return HTTP_BAD_REQUEST;
	}

	body->len = strtoul(hdr[0], NULL, 10);
	if (body->len > max_msgsize) return HTTP_TOO_LARGE;

	body->framing = FRAMING_LENGTH;
    }
	
    /* Check if this is a close-delimited response */
    else if (body->flags & BODY_RESPONSE) {
	if (body->flags & BODY_CLOSE) body->framing = FRAMING_CLOSE;
	else return HTTP_LENGTH_REQUIRED;
    }

    return 0;
}


/*
 * Read the body of a request or response.
 * Handles chunked, gzip, deflate TE only.
 * Handles close-delimited response bodies (no Content-Length specified) 
 * Handles gzip and deflate CE only.
 */
int http_read_body(struct protstream *pin, struct protstream *pout,
		   hdrcache_t hdrs, struct body_t *body, const char **errstr)
{
    char buf[PROT_BUFSIZE];
    unsigned n;
    int r = 0;

    syslog(LOG_DEBUG, "read_body(%#x)", body->flags);

    if (body->flags & BODY_DONE) return 0;
    body->flags |= BODY_DONE;

    if (!(body->flags & BODY_DISCARD)) buf_reset(&body->payload);
    else if (body->flags & BODY_CONTINUE) {
	/* Don't care about the body and client hasn't sent it, we're done */
	return 0;
    }

    if (body->framing == FRAMING_UNKNOWN) {
	/* Get message framing */
	r = http_parse_framing(hdrs, body, errstr);
	if (r) return r;
    }

    if (body->flags & BODY_CONTINUE) {
	/* Tell client to send the body */
	prot_printf(pout, "%s %s\r\n\r\n",
		    HTTP_VERSION, error_message(HTTP_CONTINUE));
	prot_flush(pout);
    }

    /* Read and buffer the body */
    switch (body->framing) {
    case FRAMING_LENGTH:
	/* Read 'len' octets */
	for (; body->len; body->len -= n) {
	    if (body->flags & BODY_DISCARD)
		n = prot_read(pin, buf, MIN(body->len, PROT_BUFSIZE));
	    else
		n = prot_readbuf(pin, &body->payload, body->len);

	    if (!n) {
		syslog(LOG_ERR, "prot_read() error");
		*errstr = "Unable to read body data";
		goto read_failure;
	    }
	}

	break;

    case FRAMING_CHUNKED:
    {
	unsigned last = 0;

	/* Read chunks until last-chunk (zero chunk-size) */
	do {
	    unsigned chunk;

	    /* Read chunk-size */
	    if (!prot_fgets(buf, PROT_BUFSIZE, pin) ||
		sscanf(buf, "%x", &chunk) != 1) {
		*errstr = "Unable to read chunk size";
		goto read_failure;

		/* XXX  Do we need to parse chunk-ext? */
	    }
	    else if (chunk > body->max - body->len) return HTTP_TOO_LARGE;

	    if (!chunk) {
		/* last-chunk */
		last = 1;

		/* Read/parse any trailing headers */
		spool_fill_hdrcache(pin, NULL, hdrs, NULL);
	    }
	    
	    /* Read 'chunk' octets */ 
	    for (; chunk; chunk -= n) {
		if (body->flags & BODY_DISCARD)
		    n = prot_read(pin, buf, MIN(chunk, PROT_BUFSIZE));
		else
		    n = prot_readbuf(pin, &body->payload, chunk);
		
		if (!n) {
		    syslog(LOG_ERR, "prot_read() error");
		    *errstr = "Unable to read chunk data";
		    goto read_failure;
		}
		body->len += n;
	    }

	    /* Read CRLF terminating the chunk/trailer */
	    if (!prot_fgets(buf, sizeof(buf), pin)) {
		*errstr = "Missing CRLF following chunk/trailer";
		goto read_failure;
	    }

	} while (!last);

	body->te &= ~TE_CHUNKED;

	break;
    }

    case FRAMING_CLOSE:
	/* Read until EOF */
	do {
	    if (body->flags & BODY_DISCARD)
		n = prot_read(pin, buf, PROT_BUFSIZE);
	    else
		n = prot_readbuf(pin, &body->payload, PROT_BUFSIZE);

	    if (n > body->max - body->len) return HTTP_TOO_LARGE;
	    body->len += n;

	} while (n);

	if (!pin->eof) goto read_failure;

	break;

    default:
	/* XXX  Should never get here */
	*errstr = "Unknown length of read body data";
	goto read_failure;
    }


    if (!(body->flags & BODY_DISCARD) && buf_len(&body->payload)) {
#ifdef HAVE_ZLIB
	/* Decode the payload, if necessary */
	if (body->te == TE_DEFLATE)
	    r = buf_inflate(&body->payload, DEFLATE_ZLIB);
	else if (body->te == TE_GZIP)
	    r = buf_inflate(&body->payload, DEFLATE_GZIP);

	if (r) {
	    *errstr = "Error decoding payload";
	    return HTTP_BAD_REQUEST;
	}
#endif

	/* Decode the representation, if necessary */
	if (body->flags & BODY_DECODE) {
	    const char **hdr;

	    if (!(hdr = spool_getheader(hdrs, "Content-Encoding"))) {
		/* nothing to see here */
	    }

#ifdef HAVE_ZLIB
	    else if (!strcasecmp(hdr[0], "deflate")) {
		const char **ua = spool_getheader(hdrs, "User-Agent");

		/* Try to detect Microsoft's broken deflate */
		if (ua && strstr(ua[0], "; MSIE "))
		    r = buf_inflate(&body->payload, DEFLATE_RAW);
		else
		    r = buf_inflate(&body->payload, DEFLATE_ZLIB);
	    }
	    else if (!strcasecmp(hdr[0], "gzip") ||
		     !strcasecmp(hdr[0], "x-gzip"))
		r = buf_inflate(&body->payload, DEFLATE_GZIP);
#endif
	    else {
		*errstr = "Specified Content-Encoding not accepted";
		return HTTP_BAD_MEDIATYPE;
	    }

	    if (r) {
		*errstr = "Error decoding content";
		return HTTP_BAD_REQUEST;
	    }
	}
    }

    return 0;

  read_failure:
    if (strcmpsafe(prot_error(pin), PROT_EOF_STRING)) {
	/* client timed out */
	*errstr = prot_error(pin);
	syslog(LOG_WARNING, "%s, closing connection", *errstr);
	return HTTP_TIMEOUT;
    }
    else return HTTP_BAD_REQUEST;
}


/* Read a response from backend */
int http_read_response(struct backend *be, unsigned meth, unsigned *code,
		       const char **statline, hdrcache_t *hdrs,
		       struct body_t *body, const char **errstr)
{
    static char statbuf[2048];
    const char **conn;
    int c = EOF;

    if (statline) *statline = statbuf;
    *errstr = NULL;
    *code = HTTP_BAD_GATEWAY;

    if (*hdrs) spool_free_hdrcache(*hdrs);
    if (!(*hdrs = spool_new_hdrcache())) {
	*errstr = "Unable to create header cache for backend response";
	return HTTP_SERVER_ERROR;
    }
    if (!prot_fgets(statbuf, sizeof(statbuf), be->in) ||
	(sscanf(statbuf, HTTP_VERSION " %u ", code) != 1) ||
	spool_fill_hdrcache(be->in, NULL, *hdrs, NULL)) {
	*errstr = "Unable to read status-line/headers from backend";
	return HTTP_BAD_GATEWAY;
    }
    eatline(be->in, c); /* CRLF separating headers & body */

    /* 1xx (provisional) response - nothing else to do */
    if (*code < 200) return 0;

    /* Final response */
    if (!body) return 0;  /* body will be piped */
    if (!(body->flags & BODY_DISCARD)) buf_reset(&body->payload);

    /* Check connection persistence */
    if (!strncmp(statbuf, "HTTP/1.0 ", 9)) body->flags |= BODY_CLOSE;
    for (conn = spool_getheader(*hdrs, "Connection"); conn && *conn; conn++) {
	tok_t tok =
	    TOK_INITIALIZER(*conn, ",", TOK_TRIMLEFT|TOK_TRIMRIGHT);
	char *token;

	while ((token = tok_next(&tok))) {
	    if (!strcasecmp(token, "keep-alive")) body->flags &= ~BODY_CLOSE;
	    else if (!strcasecmp(token, "close")) body->flags |= BODY_CLOSE;
	}
	tok_fini(&tok);
    }

    /* Not expecting a body for 204/304 response or any HEAD response */
    switch (*code){
    case 204: /* No Content */
    case 304: /* Not Modified */
	break;

    default:
	if (meth == METH_HEAD) break;

	else {
	    body->flags |= BODY_RESPONSE;
	    body->framing = FRAMING_UNKNOWN;

	    if (http_read_body(be->in, be->out, *hdrs, body, errstr)) {
		return HTTP_BAD_GATEWAY;
	    }
	}
    }

    return 0;
}