File: php_http_filter.c

package info (click to toggle)
php-pecl-http 2.0.4-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,436 kB
  • ctags: 1,839
  • sloc: ansic: 15,322; php: 726; xml: 254; makefile: 10; pascal: 3
file content (453 lines) | stat: -rw-r--r-- 12,371 bytes parent folder | download
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
450
451
452
453
/*
    +--------------------------------------------------------------------+
    | PECL :: http                                                       |
    +--------------------------------------------------------------------+
    | Redistribution and use in source and binary forms, with or without |
    | modification, are permitted provided that the conditions mentioned |
    | in the accompanying LICENSE file are met.                          |
    +--------------------------------------------------------------------+
    | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
    +--------------------------------------------------------------------+
*/

#include "php_http_api.h"

#ifndef DBG_FILTER
#	define DBG_FILTER 0
#endif

PHP_MINIT_FUNCTION(http_filter)
{
	php_stream_filter_register_factory("http.*", &php_http_filter_factory TSRMLS_CC);
	return SUCCESS;
}

#define PHP_HTTP_FILTER_PARAMS \
	php_stream *stream, \
	php_stream_filter *this, \
	php_stream_bucket_brigade *buckets_in, \
	php_stream_bucket_brigade *buckets_out, \
	size_t *bytes_consumed, int flags \
	TSRMLS_DC
#define PHP_HTTP_FILTER_OP(filter) \
	http_filter_op_ ##filter
#define PHP_HTTP_FILTER_OPS(filter) \
	php_stream_filter_ops PHP_HTTP_FILTER_OP(filter)
#define PHP_HTTP_FILTER_DTOR(filter) \
	http_filter_ ##filter## _dtor
#define PHP_HTTP_FILTER_DESTRUCTOR(filter) \
	void PHP_HTTP_FILTER_DTOR(filter)(php_stream_filter *this TSRMLS_DC)
#define PHP_HTTP_FILTER_FUNC(filter) \
	http_filter_ ##filter
#define PHP_HTTP_FILTER_FUNCTION(filter) \
	php_stream_filter_status_t PHP_HTTP_FILTER_FUNC(filter)(PHP_HTTP_FILTER_PARAMS)
#define PHP_HTTP_FILTER_BUFFER(filter) \
	http_filter_ ##filter## _buffer

#define PHP_HTTP_FILTER_IS_CLOSING(stream, flags) \
	(	(flags & PSFS_FLAG_FLUSH_CLOSE) \
	||	php_stream_eof(stream) \
	|| ((stream->ops == &php_stream_temp_ops || stream->ops == &php_stream_memory_ops) && stream->eof) \
	)

#define NEW_BUCKET(data, length) \
	{ \
		char *__data; \
		php_stream_bucket *__buck; \
		\
		__data = pemalloc(length, this->is_persistent); \
		if (!__data) { \
			return PSFS_ERR_FATAL; \
		} \
		memcpy(__data, data, length); \
		\
		__buck = php_stream_bucket_new(stream, __data, length, 1, this->is_persistent TSRMLS_CC); \
		if (!__buck) { \
			pefree(__data, this->is_persistent); \
			return PSFS_ERR_FATAL; \
		} \
		\
		php_stream_bucket_append(buckets_out, __buck TSRMLS_CC); \
	}

typedef struct _http_chunked_decode_filter_buffer_t {
	php_http_buffer_t	buffer;
	ulong	hexlen;
} PHP_HTTP_FILTER_BUFFER(chunked_decode);

typedef php_http_encoding_stream_t PHP_HTTP_FILTER_BUFFER(zlib);

static PHP_HTTP_FILTER_FUNCTION(chunked_decode)
{
	int out_avail = 0;
	php_stream_bucket *ptr, *nxt;
	PHP_HTTP_FILTER_BUFFER(chunked_decode) *buffer = (PHP_HTTP_FILTER_BUFFER(chunked_decode) *) (this->abstract);
	
	if (bytes_consumed) {
		*bytes_consumed = 0;
	}
	
	/* fetch available bucket data */
	for (ptr = buckets_in->head; ptr; ptr = nxt) {
		if (bytes_consumed) {
			*bytes_consumed += ptr->buflen;
		}

		if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_append(PHP_HTTP_BUFFER(buffer), ptr->buf, ptr->buflen)) {
			return PSFS_ERR_FATAL;
		}

		nxt = ptr->next;
		php_stream_bucket_unlink(ptr TSRMLS_CC);
		php_stream_bucket_delref(ptr TSRMLS_CC);
	}
	
	if (!php_http_buffer_fix(PHP_HTTP_BUFFER(buffer))) {
		return PSFS_ERR_FATAL;
	}

	/* we have data in our buffer */
	while (PHP_HTTP_BUFFER(buffer)->used) {
	
		/* we already know the size of the chunk and are waiting for data */
		if (buffer->hexlen) {
		
			/* not enough data buffered */
			if (PHP_HTTP_BUFFER(buffer)->used < buffer->hexlen) {
			
				/* flush anyway? */
				if (flags & PSFS_FLAG_FLUSH_INC) {
				
					/* flush all data (should only be chunk data) */
					out_avail = 1;
					NEW_BUCKET(PHP_HTTP_BUFFER(buffer)->data, PHP_HTTP_BUFFER(buffer)->used);
					
					/* waiting for less data now */
					buffer->hexlen -= PHP_HTTP_BUFFER(buffer)->used;
					/* no more buffered data */
					php_http_buffer_reset(PHP_HTTP_BUFFER(buffer));
					/* break */
				} 
				
				/* we have too less data and don't need to flush */
				else {
					break;
				}
			} 
			
			/* we seem to have all data of the chunk */
			else {
				out_avail = 1;
				NEW_BUCKET(PHP_HTTP_BUFFER(buffer)->data, buffer->hexlen);
				
				/* remove outgoing data from the buffer */
				php_http_buffer_cut(PHP_HTTP_BUFFER(buffer), 0, buffer->hexlen);
				/* reset hexlen */
				buffer->hexlen = 0;
				/* continue */
			}
		} 
		
		/* we don't know the length of the chunk yet */
		else {
			size_t off = 0;
			
			/* ignore preceeding CRLFs (too loose?) */
			while (off < PHP_HTTP_BUFFER(buffer)->used && (
					PHP_HTTP_BUFFER(buffer)->data[off] == '\n' || 
					PHP_HTTP_BUFFER(buffer)->data[off] == '\r')) {
				++off;
			}
			if (off) {
				php_http_buffer_cut(PHP_HTTP_BUFFER(buffer), 0, off);
			}
			
			/* still data there? */
			if (PHP_HTTP_BUFFER(buffer)->used) {
				int eollen;
				const char *eolstr;
				
				/* we need eol, so we can be sure we have all hex digits */
				php_http_buffer_fix(PHP_HTTP_BUFFER(buffer));
				if ((eolstr = php_http_locate_bin_eol(PHP_HTTP_BUFFER(buffer)->data, PHP_HTTP_BUFFER(buffer)->used, &eollen))) {
					char *stop = NULL;
					
					/* read in chunk size */
					buffer->hexlen = strtoul(PHP_HTTP_BUFFER(buffer)->data, &stop, 16);
					
					/*	if strtoul() stops at the beginning of the buffered data
						there's something oddly wrong, i.e. bad input */
					if (stop == PHP_HTTP_BUFFER(buffer)->data) {
						return PSFS_ERR_FATAL;
					}
					
					/* cut out <chunk size hex><chunk extension><eol> */
					php_http_buffer_cut(PHP_HTTP_BUFFER(buffer), 0, eolstr + eollen - PHP_HTTP_BUFFER(buffer)->data);
					/* buffer->hexlen is 0 now or contains the size of the next chunk */
					if (!buffer->hexlen) {
						php_stream_notify_info(stream->context, PHP_STREAM_NOTIFY_COMPLETED, NULL, 0);
						break;
					}
					/* continue */
				} else {
					/* we have not enough data buffered to read in chunk size */
					break;
				}
			}
			/* break */
		}
	}
	
	/* flush before close, but only if we are already waiting for more data */
	if (PHP_HTTP_FILTER_IS_CLOSING(stream, flags) && buffer->hexlen && PHP_HTTP_BUFFER(buffer)->used) {
		out_avail = 1;
		NEW_BUCKET(PHP_HTTP_BUFFER(buffer)->data, PHP_HTTP_BUFFER(buffer)->used);
		php_http_buffer_reset(PHP_HTTP_BUFFER(buffer));
		buffer->hexlen = 0;
	}
	
	return out_avail ? PSFS_PASS_ON : PSFS_FEED_ME;
}

static PHP_HTTP_FILTER_DESTRUCTOR(chunked_decode)
{
	PHP_HTTP_FILTER_BUFFER(chunked_decode) *b = (PHP_HTTP_FILTER_BUFFER(chunked_decode) *) (this->abstract);
	
	php_http_buffer_dtor(PHP_HTTP_BUFFER(b));
	pefree(b, this->is_persistent);
}

static PHP_HTTP_FILTER_FUNCTION(chunked_encode)
{
	php_http_buffer_t buf;
	php_stream_bucket *ptr, *nxt;
	
	if (bytes_consumed) {
		*bytes_consumed = 0;
	}
	
	/* new data available? */
	php_http_buffer_init(&buf);

	/* fetch available bucket data */
	for (ptr = buckets_in->head; ptr; ptr = nxt) {
		if (bytes_consumed) {
			*bytes_consumed += ptr->buflen;
		}
#if DBG_FILTER
		fprintf(stderr, "update: chunked (-> %zu) (w: %zu, r: %zu)\n", ptr->buflen, stream->writepos, stream->readpos);
#endif
		
		nxt = ptr->next;
		php_stream_bucket_unlink(ptr TSRMLS_CC);
		php_http_buffer_appendf(&buf, "%lx" PHP_HTTP_CRLF, (long unsigned int) ptr->buflen);
		php_http_buffer_append(&buf, ptr->buf, ptr->buflen);
		php_http_buffer_appends(&buf, PHP_HTTP_CRLF);

		/* pass through */
		NEW_BUCKET(buf.data, buf.used);
		/* reset */
		php_http_buffer_reset(&buf);
		php_stream_bucket_delref(ptr TSRMLS_CC);
	}

	/* free buffer */
	php_http_buffer_dtor(&buf);
	
	/* terminate with "0" */
	if (PHP_HTTP_FILTER_IS_CLOSING(stream, flags)) {
#if DBG_FILTER
		fprintf(stderr, "finish: chunked\n");
#endif
		
		NEW_BUCKET("0" PHP_HTTP_CRLF PHP_HTTP_CRLF, lenof("0" PHP_HTTP_CRLF PHP_HTTP_CRLF));
	}
	
	return PSFS_PASS_ON;
}

static PHP_HTTP_FILTER_OPS(chunked_decode) = {
	PHP_HTTP_FILTER_FUNC(chunked_decode),
	PHP_HTTP_FILTER_DTOR(chunked_decode),
	"http.chunked_decode"
};

static PHP_HTTP_FILTER_OPS(chunked_encode) = {
	PHP_HTTP_FILTER_FUNC(chunked_encode),
	NULL,
	"http.chunked_encode"
};

static PHP_HTTP_FILTER_FUNCTION(zlib)
{
	php_stream_bucket *ptr, *nxt;
	PHP_HTTP_FILTER_BUFFER(zlib) *buffer = (PHP_HTTP_FILTER_BUFFER(zlib) *) this->abstract;
	
	if (bytes_consumed) {
		*bytes_consumed = 0;
	}
	
	/* fetch available bucket data */
	for (ptr = buckets_in->head; ptr; ptr = nxt) {
		char *encoded = NULL;
		size_t encoded_len = 0;

		if (bytes_consumed) {
			*bytes_consumed += ptr->buflen;
		}

#if DBG_FILTER
		fprintf(stderr, "bucket: b=%p p=%p p=%p\n", ptr->brigade, ptr->prev,  ptr->next);
#endif
		
		nxt = ptr->next;
		php_stream_bucket_unlink(ptr TSRMLS_CC);
		php_http_encoding_stream_update(buffer, ptr->buf, ptr->buflen, &encoded, &encoded_len);
		
#if DBG_FILTER
		fprintf(stderr, "update: deflate (-> %zu) (w: %zu, r: %zu)\n", encoded_len, stream->writepos, stream->readpos);
#endif
		
		if (encoded) {
			if (encoded_len) {
				NEW_BUCKET(encoded, encoded_len);
			}
			efree(encoded);
		}
		php_stream_bucket_delref(ptr TSRMLS_CC);
	}

	/* flush & close */
	if (flags & PSFS_FLAG_FLUSH_INC) {
		char *encoded = NULL;
		size_t encoded_len = 0;
		
		php_http_encoding_stream_flush(buffer, &encoded, &encoded_len);
		
#if DBG_FILTER
		fprintf(stderr, "flush: deflate (-> %zu)\n", encoded_len);
#endif
		
		if (encoded) {
			if (encoded_len) {
				NEW_BUCKET(encoded, encoded_len);
			}
			efree(encoded);
		}
	}
	
	if (PHP_HTTP_FILTER_IS_CLOSING(stream, flags)) {
		char *encoded = NULL;
		size_t encoded_len = 0;
		
		php_http_encoding_stream_finish(buffer, &encoded, &encoded_len);
		
#if DBG_FILTER
		fprintf(stderr, "finish: deflate (-> %zu)\n", encoded_len);
#endif
		
		if (encoded) {
			if (encoded_len) {
				NEW_BUCKET(encoded, encoded_len);
			}
			efree(encoded);
		}
	}
	
	return PSFS_PASS_ON;
}
static PHP_HTTP_FILTER_DESTRUCTOR(zlib)
{
	PHP_HTTP_FILTER_BUFFER(zlib) *buffer = (PHP_HTTP_FILTER_BUFFER(zlib) *) this->abstract;
	php_http_encoding_stream_free(&buffer);
}

static PHP_HTTP_FILTER_OPS(deflate) = {
	PHP_HTTP_FILTER_FUNC(zlib),
	PHP_HTTP_FILTER_DTOR(zlib),
	"http.deflate"
};

static PHP_HTTP_FILTER_OPS(inflate) = {
	PHP_HTTP_FILTER_FUNC(zlib),
	PHP_HTTP_FILTER_DTOR(zlib),
	"http.inflate"
};

static php_stream_filter *http_filter_create(const char *name, zval *params, int p TSRMLS_DC)
{
	zval **tmp = &params;
	php_stream_filter *f = NULL;
	int flags = p ? PHP_HTTP_ENCODING_STREAM_PERSISTENT : 0;
	
	if (params) {
		switch (Z_TYPE_P(params)) {
			case IS_ARRAY:
			case IS_OBJECT:
				if (SUCCESS != zend_hash_find(HASH_OF(params), "flags", sizeof("flags"), (void *) &tmp)) {
					break;
				}
				/* no break */
			default:
			{
				zval *num = php_http_ztyp(IS_LONG, *tmp);
				
				flags |= (Z_LVAL_P(num) & 0x0fffffff);
				zval_ptr_dtor(&num);

			}
			break;
		}
	}

	if (!strcasecmp(name, "http.chunked_decode")) {
		PHP_HTTP_FILTER_BUFFER(chunked_decode) *b = NULL;
		
		if ((b = pecalloc(1, sizeof(PHP_HTTP_FILTER_BUFFER(chunked_decode)), p))) {
			php_http_buffer_init_ex(PHP_HTTP_BUFFER(b), 4096, p ? PHP_HTTP_BUFFER_INIT_PERSISTENT : 0);
			if (!(f = php_stream_filter_alloc(&PHP_HTTP_FILTER_OP(chunked_decode), b, p))) {
				pefree(b, p);
			}
		}
	} else
	
	if (!strcasecmp(name, "http.chunked_encode")) {
		f = php_stream_filter_alloc(&PHP_HTTP_FILTER_OP(chunked_encode), NULL, p);
	} else
	
	if (!strcasecmp(name, "http.inflate")) {
		PHP_HTTP_FILTER_BUFFER(zlib) *b = NULL;
		
		if ((b = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_inflate_ops(), flags TSRMLS_CC))) {
			if (!(f = php_stream_filter_alloc(&PHP_HTTP_FILTER_OP(inflate), b, p))) {
				php_http_encoding_stream_free(&b);
			}
		}
	} else
	
	if (!strcasecmp(name, "http.deflate")) {
		PHP_HTTP_FILTER_BUFFER(zlib) *b = NULL;
		
		if ((b = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_deflate_ops(), flags TSRMLS_CC))) {
			if (!(f = php_stream_filter_alloc(&PHP_HTTP_FILTER_OP(deflate), b, p))) {
				php_http_encoding_stream_free(&b);
			}
		}
	}
	
	return f;
}

php_stream_filter_factory php_http_filter_factory = {
	http_filter_create
};


/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */