- Countable also requires PHP-5.1+
[m6w6/ext-http] / http_send_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18 #include "php.h"
19
20 #include "SAPI.h"
21 #include "php_streams.h"
22 #include "ext/standard/php_lcg.h"
23
24 #include "php_http.h"
25 #include "php_http_std_defs.h"
26 #include "php_http_api.h"
27 #include "php_http_date_api.h"
28 #include "php_http_send_api.h"
29 #include "php_http_headers_api.h"
30 #include "php_http_date_api.h"
31 #include "php_http_cache_api.h"
32 #include "php_http_encoding_api.h"
33
34 ZEND_EXTERN_MODULE_GLOBALS(http);
35
36 #define http_flush(d, l) _http_flush((d), (l) SRMLS_CC)
37 /* {{{ static inline void http_flush() */
38 static inline void _http_flush(const char *data, size_t data_len TSRMLS_DC)
39 {
40 PHPWRITE(data, data_len);
41 php_end_ob_buffer(1, 1 TSRMLS_CC);
42 sapi_flush(TSRMLS_C);
43
44 #define HTTP_MSEC(s) (s * 1000)
45 #define HTTP_USEC(s) (HTTP_MSEC(s) * 1000)
46 #define HTTP_NSEC(s) (HTTP_USEC(s) * 1000)
47 #define HTTP_NANOSEC (1000 * 1000 * 1000)
48 #define HTTP_DIFFSEC (0.001)
49
50 if (HTTP_G(send).throttle_delay >= HTTP_DIFFSEC) {
51 #if defined(PHP_WIN32)
52 Sleep((DWORD) HTTP_MSEC(HTTP_G(send).throttle_delay));
53 #elif defined(HAVE_USLEEP)
54 usleep(HTTP_USEC(HTTP_G(send).throttle_delay));
55 #elif defined(HAVE_NANOSLEEP)
56 struct timespec req, rem;
57
58 req.tv_sec = (time_t) HTTP_G(send).throttle_delay;
59 req.tv_nsec = HTTP_NSEC(HTTP_G(send).throttle_delay) % HTTP_NANOSEC;
60
61 while (nanosleep(&req, &rem) && (errno == EINTR) && (HTTP_NSEC(rem.tv_sec) + rem.tv_nsec) > HTTP_NSEC(HTTP_DIFFSEC))) {
62 req.tv_sec = rem.tv_sec;
63 req.tv_nsec = rem.tv_nsec;
64 }
65 #endif
66 }
67 }
68 /* }}} */
69
70 /* {{{ http_send_response_start */
71 #define http_send_response_start(b, cl) _http_send_response_start((b), (cl) TSRMLS_CC)
72 static inline void _http_send_response_start(void **buffer, size_t content_length TSRMLS_DC)
73 {
74 if (http_encoding_response_start(content_length)) {
75 #ifdef HTTP_HAVE_ZLIB
76 char *encoded;
77 size_t encoded_len;
78 http_encoding_stream *s = emalloc(sizeof(http_encoding_stream));
79
80 http_encoding_stream_init(s, HTTP_G(send).gzip_encoding == HTTP_ENCODING_GZIP, -1, &encoded, &encoded_len);
81 phpstr_chunked_output(&s->storage, encoded, encoded_len, HTTP_G(send).buffer_size, _http_flush TSRMLS_CC);
82 STR_FREE(encoded);
83 *buffer = s;
84 #endif
85 }
86 }
87 /* }}} */
88
89 /* {{{ http_send_response_data_plain */
90 #define http_send_response_data_plain(b, d, dl) _http_send_response_data_plain((b), (d), (dl) TSRMLS_CC)
91 static inline void _http_send_response_data_plain(void **buffer, const char *data, size_t data_len TSRMLS_DC)
92 {
93 if (HTTP_G(send).gzip_encoding) {
94 #ifdef HTTP_HAVE_ZLIB
95 char *encoded;
96 size_t encoded_len;
97 http_encoding_stream *s = *((http_encoding_stream **) buffer);
98
99 http_encoding_stream_update(s, data, data_len, &encoded, &encoded_len);
100 phpstr_chunked_output(&s->storage, encoded, encoded_len, HTTP_G(send).buffer_size, _http_flush TSRMLS_CC);
101 efree(encoded);
102 #else
103 http_error(HE_ERROR, HTTP_E_RESPONSE, "Attempt to send GZIP response despite being able to do so; please report this bug");
104 #endif
105 } else {
106 phpstr_chunked_output((phpstr **) buffer, data, data_len, HTTP_G(send).buffer_size, _http_flush TSRMLS_CC);
107 }
108 }
109 /* }}} */
110
111 #define HTTP_CHUNK_AVAIL(len, cs) ((len -= cs) >= 0)
112 /* {{{ http_send_response_data_fetch */
113 #define http_send_response_data_fetch(b, d, l, m, s, e) _http_send_response_data_fetch((b), (d), (l), (m), (s), (e) TSRMLS_CC)
114 static inline void _http_send_response_data_fetch(void **buffer, const void *data, size_t data_len, http_send_mode mode, size_t begin, size_t end TSRMLS_DC)
115 {
116 long len = end - begin, chunk_size = 40960;
117
118 switch (mode)
119 {
120 case SEND_RSRC:
121 {
122 php_stream *s = (php_stream *) data;
123
124 if (SUCCESS == php_stream_seek(s, begin, SEEK_SET)) {
125 char *buf = emalloc(chunk_size);
126
127 while (HTTP_CHUNK_AVAIL(len, chunk_size)) {
128 http_send_response_data_plain(buffer, buf, php_stream_read(s, buf, chunk_size));
129 }
130 /* read & write left over */
131 if (len) {
132 http_send_response_data_plain(buffer, buf, php_stream_read(s, buf, chunk_size + len));
133 }
134
135 efree(buf);
136 }
137 }
138 break;
139
140 case SEND_DATA:
141 {
142 char *s = (char *) data + begin;
143
144 while (HTTP_CHUNK_AVAIL(len, chunk_size)) {
145 http_send_response_data_plain(buffer, s, chunk_size);
146 s += chunk_size;
147 }
148 /* write left over */
149 if (len) {
150 http_send_response_data_plain(buffer, s, chunk_size + len);
151 }
152 }
153 break;
154
155 EMPTY_SWITCH_DEFAULT_CASE();
156 }
157 }
158 /* }}} */
159
160 /* {{{ http_send_response_finish */
161 #define http_send_response_finish(b) _http_send_response_finish((b) TSRMLS_CC)
162 static inline void _http_send_response_finish(void **buffer TSRMLS_DC)
163 {
164 if (HTTP_G(send).gzip_encoding) {
165 #ifdef HTTP_HAVE_ZLIB
166 char *encoded = NULL;
167 size_t encoded_len = 0;
168 http_encoding_stream *s = *((http_encoding_stream **) buffer);
169
170 http_encoding_stream_finish(s, &encoded, &encoded_len);
171 phpstr_chunked_output(&s->storage, encoded, encoded_len, 0, _http_flush TSRMLS_CC);
172 STR_FREE(encoded);
173 efree(s);
174 #else
175 http_error(HE_ERROR, HTTP_E_RESPONSE, "Attempt to send GZIP response despite being able to do so; please report this bug");
176 #endif
177 } else {
178 phpstr_chunked_output((phpstr **) buffer, NULL, 0, 0, _http_flush TSRMLS_CC);
179 }
180 }
181 /* }}} */
182
183
184 /* {{{ STATUS http_send_header(char *, char *, zend_bool) */
185 PHP_HTTP_API STATUS _http_send_header_ex(const char *name, size_t name_len, const char *value, size_t value_len, zend_bool replace, char **sent_header TSRMLS_DC)
186 {
187 STATUS ret;
188 size_t header_len = sizeof(": ") + name_len + value_len + 1;
189 char *header = emalloc(header_len + 1);
190
191 header[header_len] = '\0';
192 header_len = snprintf(header, header_len, "%s: %s", name, value);
193 ret = http_send_header_string_ex(header, header_len, replace);
194 if (sent_header) {
195 *sent_header = header;
196 } else {
197 efree(header);
198 }
199 return ret;
200 }
201 /* }}} */
202
203 /* {{{ STATUS http_send_status_header(int, char *) */
204 PHP_HTTP_API STATUS _http_send_status_header_ex(int status, const char *header, size_t header_len, zend_bool replace TSRMLS_DC)
205 {
206 STATUS ret;
207 sapi_header_line h = {(char *) header, header_len, status};
208 if (SUCCESS != (ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, &h TSRMLS_CC))) {
209 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Could not send header: %s (%d)", header, status);
210 }
211 return ret;
212 }
213 /* }}} */
214
215 /* {{{ STATUS http_send_last_modified(int) */
216 PHP_HTTP_API STATUS _http_send_last_modified_ex(time_t t, char **sent_header TSRMLS_DC)
217 {
218 STATUS ret;
219 char *date = http_date(t);
220
221 if (!date) {
222 return FAILURE;
223 }
224
225 ret = http_send_header_ex("Last-Modified", lenof("Last-Modified"), date, strlen(date), 1, sent_header);
226 efree(date);
227
228 /* remember */
229 HTTP_G(send).last_modified = t;
230
231 return ret;
232 }
233 /* }}} */
234
235 /* {{{ STATUS http_send_etag(char *, size_t) */
236 PHP_HTTP_API STATUS _http_send_etag_ex(const char *etag, size_t etag_len, char **sent_header TSRMLS_DC)
237 {
238 STATUS status;
239 char *etag_header;
240
241 if (!etag_len){
242 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Attempt to send empty ETag (previous: %s)\n", HTTP_G(send).unquoted_etag);
243 return FAILURE;
244 }
245
246 /* remember */
247 STR_SET(HTTP_G(send).unquoted_etag, estrndup(etag, etag_len));
248
249 etag_len = spprintf(&etag_header, 0, "ETag: \"%s\"", etag);
250 status = http_send_header_string_ex(etag_header, etag_len, 1);
251
252 if (sent_header) {
253 *sent_header = etag_header;
254 } else {
255 efree(etag_header);
256 }
257
258 return status;
259 }
260 /* }}} */
261
262 /* {{{ STATUS http_send_content_type(char *, size_t) */
263 PHP_HTTP_API STATUS _http_send_content_type(const char *content_type, size_t ct_len TSRMLS_DC)
264 {
265 HTTP_CHECK_CONTENT_TYPE(content_type, return FAILURE);
266
267 /* remember for multiple ranges */
268 STR_FREE(HTTP_G(send).content_type);
269 HTTP_G(send).content_type = estrndup(content_type, ct_len);
270
271 return http_send_header_ex("Content-Type", lenof("Content-Type"), content_type, ct_len, 1, NULL);
272 }
273 /* }}} */
274
275 /* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
276 PHP_HTTP_API STATUS _http_send_content_disposition(const char *filename, size_t f_len, zend_bool send_inline TSRMLS_DC)
277 {
278 STATUS status;
279 char *cd_header;
280
281 if (send_inline) {
282 cd_header = ecalloc(1, sizeof("Content-Disposition: inline; filename=\"\"") + f_len);
283 sprintf(cd_header, "Content-Disposition: inline; filename=\"%s\"", filename);
284 } else {
285 cd_header = ecalloc(1, sizeof("Content-Disposition: attachment; filename=\"\"") + f_len);
286 sprintf(cd_header, "Content-Disposition: attachment; filename=\"%s\"", filename);
287 }
288
289 status = http_send_header_string(cd_header);
290 efree(cd_header);
291 return status;
292 }
293 /* }}} */
294
295 /* {{{ STATUS http_send(void *, size_t, http_send_mode) */
296 PHP_HTTP_API STATUS _http_send_ex(const void *data_ptr, size_t data_size, http_send_mode data_mode, zend_bool no_cache TSRMLS_DC)
297 {
298 void *s = NULL;
299 HashTable ranges;
300 http_range_status range_status;
301 int cache_etag = http_interrupt_ob_etaghandler();
302
303 if (!data_ptr) {
304 return FAILURE;
305 }
306 if (!data_size) {
307 return SUCCESS;
308 }
309
310 /* enable partial dl and resume */
311 http_send_header_string("Accept-Ranges: bytes");
312
313 zend_hash_init(&ranges, 0, NULL, ZVAL_PTR_DTOR, 0);
314 range_status = http_get_request_ranges(&ranges, data_size);
315
316 if (range_status == RANGE_ERR) {
317 zend_hash_destroy(&ranges);
318 http_send_status(416);
319 return FAILURE;
320 }
321
322 switch (range_status)
323 {
324 case RANGE_OK:
325 {
326 /* Range Request - only send ranges if entity hasn't changed */
327 if ( http_match_etag_ex("HTTP_IF_MATCH", HTTP_G(send).unquoted_etag, 0) &&
328 http_match_last_modified_ex("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(send).last_modified, 0) &&
329 http_match_last_modified_ex("HTTP_UNLESS_MODIFIED_SINCE", HTTP_G(send).last_modified, 0)) {
330
331 if (zend_hash_num_elements(&ranges) == 1) {
332 /* single range */
333 zval **range, **begin, **end;
334
335 if ( SUCCESS == zend_hash_index_find(&ranges, 0, (void **) &range) &&
336 SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(range), 0, (void **) &begin) &&
337 SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(range), 1, (void **) &end)) {
338 char range_header_str[256];
339 size_t range_header_len;
340
341 range_header_len = snprintf(range_header_str, lenof(range_header_str), "Content-Range: bytes %ld-%ld/%lu", Z_LVAL_PP(begin), Z_LVAL_PP(end), (ulong) data_size);
342 http_send_status_header_ex(206, range_header_str, range_header_len, 1);
343 http_send_response_start(&s, Z_LVAL_PP(end)-Z_LVAL_PP(begin)+1);
344 http_send_response_data_fetch(&s, data_ptr, data_size, data_mode, Z_LVAL_PP(begin), Z_LVAL_PP(end) + 1);
345 http_send_response_finish(&s);
346 zend_hash_destroy(&ranges);
347 return SUCCESS;
348 }
349 } else {
350 /* multi range */
351 zval **range, **begin, **end;
352 const char *content_type = HTTP_G(send).content_type;
353 char boundary_str[32], range_header_str[256];
354 size_t boundary_len, range_header_len;
355
356 boundary_len = snprintf(boundary_str, lenof(boundary_str), "%lu%0.9f", (ulong) time(NULL), (float) php_combined_lcg(TSRMLS_C));
357 range_header_len = snprintf(range_header_str, lenof(range_header_str), "Content-Type: multipart/byteranges; boundary=%s", boundary_str);
358
359 http_send_status_header_ex(206, range_header_str, range_header_len, 1);
360 http_send_response_start(&s, 0);
361
362 if (!content_type) {
363 content_type = "application/x-octetstream";
364 }
365
366 FOREACH_HASH_VAL(&ranges, range) {
367 if ( SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(range), 0, (void **) &begin) &&
368 SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(range), 1, (void **) &end)) {
369 char preface_str[512];
370 size_t preface_len;
371
372 #define HTTP_RANGE_PREFACE \
373 HTTP_CRLF "--%s" \
374 HTTP_CRLF "Content-Type: %s" \
375 HTTP_CRLF "Content-Range: bytes %ld-%ld/%lu" \
376 HTTP_CRLF HTTP_CRLF
377
378 preface_len = snprintf(preface_str, lenof(preface_str), HTTP_RANGE_PREFACE, boundary_str, content_type, Z_LVAL_PP(begin), Z_LVAL_PP(end), data_size);
379 http_send_response_data_plain(&s, preface_str, preface_len);
380 http_send_response_data_fetch(&s, data_ptr, data_size, data_mode, Z_LVAL_PP(begin), Z_LVAL_PP(end) + 1);
381 }
382 }
383
384 http_send_response_data_plain(&s, HTTP_CRLF "--", lenof(HTTP_CRLF "--"));
385 http_send_response_data_plain(&s, boundary_str, boundary_len);
386 http_send_response_data_plain(&s, "--", lenof("--"));
387
388 http_send_response_finish(&s);
389 zend_hash_destroy(&ranges);
390 return SUCCESS;
391 }
392 }
393 }
394 case RANGE_NO:
395 {
396 zend_hash_destroy(&ranges);
397
398 /* send 304 Not Modified if etag matches - DON'T return on ETag generation failure */
399 if (!no_cache && cache_etag) {
400 char *etag = NULL;
401
402 if (etag = http_etag(data_ptr, data_size, data_mode)) {
403 char *sent_header = NULL;
404
405 http_send_etag_ex(etag, strlen(etag), &sent_header);
406 if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) {
407 return http_exit_ex(304, sent_header, NULL, 0);
408 } else {
409 STR_FREE(sent_header);
410 }
411 efree(etag);
412 }
413 }
414
415 /* send 304 Not Modified if last modified matches */
416 if (!no_cache && http_match_last_modified("HTTP_IF_MODIFIED_SINCE", HTTP_G(send).last_modified)) {
417 char *sent_header = NULL;
418 http_send_last_modified_ex(HTTP_G(send).last_modified, &sent_header);
419 return http_exit_ex(304, sent_header, NULL, 0);
420 }
421
422 /* send full response */
423 http_send_response_start(&s, data_size);
424 http_send_response_data_fetch(&s, data_ptr, data_size, data_mode, 0, data_size);
425 http_send_response_finish(&s);
426 return SUCCESS;
427 }
428 }
429 return FAILURE;
430 }
431 /* }}} */
432
433 /* {{{ STATUS http_send_stream(php_stream *) */
434 PHP_HTTP_API STATUS _http_send_stream_ex(php_stream *file, zend_bool close_stream, zend_bool no_cache TSRMLS_DC)
435 {
436 STATUS status;
437 php_stream_statbuf ssb;
438
439 if ((!file) || php_stream_stat(file, &ssb)) {
440 return FAILURE;
441 }
442
443 status = http_send_ex(file, ssb.sb.st_size, SEND_RSRC, no_cache);
444
445 if (close_stream) {
446 php_stream_close(file);
447 }
448
449 return status;
450 }
451 /* }}} */
452
453 /*
454 * Local variables:
455 * tab-width: 4
456 * c-basic-offset: 4
457 * End:
458 * vim600: sw=4 ts=4 fdm=marker
459 * vim<600: sw=4 ts=4
460 */
461