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