- parameter to set*() and add*() methods is optional
[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
36 ZEND_EXTERN_MODULE_GLOBALS(http);
37
38 #define http_flush() _http_flush(TSRMLS_C)
39 /* {{{ static inline void http_flush() */
40 static inline void _http_flush(TSRMLS_D)
41 {
42 php_end_ob_buffer(1, 1 TSRMLS_CC);
43 sapi_flush(TSRMLS_C);
44 }
45 /* }}} */
46
47 #define http_sleep() _http_sleep(TSRMLS_C)
48 /* {{{ static inline void http_sleep() */
49 static inline void _http_sleep(TSRMLS_D)
50 {
51 #define HTTP_MSEC(s) (s * 1000)
52 #define HTTP_USEC(s) (HTTP_MSEC(s) * 1000)
53 #define HTTP_NSEC(s) (HTTP_USEC(s) * 1000)
54 #define HTTP_NANOSEC (1000 * 1000 * 1000)
55 #define HTTP_DIFFSEC (0.001)
56
57 if (HTTP_G(send).throttle_delay >= HTTP_DIFFSEC) {
58 #if defined(PHP_WIN32)
59 Sleep((DWORD) HTTP_MSEC(HTTP_G(send).throttle_delay));
60 #elif defined(HAVE_USLEEP)
61 usleep(HTTP_USEC(HTTP_G(send).throttle_delay));
62 #elif defined(HAVE_NANOSLEEP)
63 struct timespec req, rem;
64
65 req.tv_sec = (time_t) HTTP_G(send).throttle_delay;
66 req.tv_nsec = HTTP_NSEC(HTTP_G(send).throttle_delay) % HTTP_NANOSEC;
67
68 while (nanosleep(&req, &rem) && (errno == EINTR) && (HTTP_NSEC(rem.tv_sec) + rem.tv_nsec) > HTTP_NSEC(HTTP_DIFFSEC))) {
69 req.tv_sec = rem.tv_sec;
70 req.tv_nsec = rem.tv_nsec;
71 }
72 #endif
73 }
74 }
75 /* }}} */
76
77 #define HTTP_CHUNK_AVAIL(len) ((len -= HTTP_G(send).buffer_size) >= 0)
78 #define HTTP_CHUNK_WRITE(data, l, dofree, dosleep) \
79 { \
80 long size = (long) l; \
81 \
82 if ((1 > size) || (size - PHPWRITE(data, size))) { \
83 if (dofree) { \
84 efree(data); \
85 } \
86 return FAILURE; \
87 } \
88 \
89 http_flush(); \
90 if (dosleep) { \
91 http_sleep(); \
92 } \
93 }
94
95 #define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
96 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t, http_send_mode) */
97 static STATUS _http_send_chunk(const void *data, size_t begin, size_t end, http_send_mode mode TSRMLS_DC)
98 {
99 long len = end - begin;
100
101 switch (mode)
102 {
103 case SEND_RSRC:
104 {
105 char *buf;
106 php_stream *s = (php_stream *) data;
107
108 if (php_stream_seek(s, begin, SEEK_SET)) {
109 return FAILURE;
110 }
111
112 buf = emalloc(HTTP_G(send).buffer_size);
113
114 while (HTTP_CHUNK_AVAIL(len)) {
115 HTTP_CHUNK_WRITE(buf, php_stream_read(s, buf, HTTP_G(send).buffer_size), 1, 1);
116 }
117
118 /* read & write left over */
119 if (len) {
120 HTTP_CHUNK_WRITE(buf, php_stream_read(s, buf, HTTP_G(send).buffer_size + len), 1, 0);
121 }
122
123 efree(buf);
124 return SUCCESS;
125 }
126
127 case SEND_DATA:
128 {
129 char *s = (char *) data + begin;
130
131 while (HTTP_CHUNK_AVAIL(len)) {
132 HTTP_CHUNK_WRITE(s, HTTP_G(send).buffer_size, 0, 1);
133 s += HTTP_G(send).buffer_size;
134 }
135
136 /* write left over */
137 if (len) {
138 HTTP_CHUNK_WRITE(s, HTTP_G(send).buffer_size + len, 0, 0);
139 }
140
141 return SUCCESS;
142 }
143
144 default:
145 return FAILURE;
146 break;
147 }
148 }
149 /* }}} */
150
151 /* {{{ STATUS http_send_header(char *, char *, zend_bool) */
152 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 TSRMLS_DC)
153 {
154 STATUS ret;
155 size_t header_len = 1 + lenof(": ") + name_len + value_len;
156 char *header = emalloc(header_len);
157
158 header[header_len - 1] = '\0';
159 snprintf(header, header_len, "%s: %s", name, value);
160 ret = http_send_header_string_ex(header, replace);
161 efree(header);
162 return ret;
163 }
164 /* }}} */
165
166 /* {{{ STATUS http_send_status_header(int, char *) */
167 PHP_HTTP_API STATUS _http_send_status_header_ex(int status, const char *header, zend_bool replace TSRMLS_DC)
168 {
169 STATUS ret;
170 sapi_header_line h = {(char *) header, header ? strlen(header) : 0, status};
171 if (SUCCESS != (ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, &h TSRMLS_CC))) {
172 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Could not send header: %s (%d)", header, status);
173 }
174 return ret;
175 }
176 /* }}} */
177
178 /* {{{ STATUS http_send_last_modified(int) */
179 PHP_HTTP_API STATUS _http_send_last_modified(time_t t TSRMLS_DC)
180 {
181 STATUS ret;
182 char *date = http_date(t);
183
184 if (!date) {
185 return FAILURE;
186 }
187
188 ret = http_send_header("Last-Modified", date, 1);
189 efree(date);
190
191 /* remember */
192 HTTP_G(send).last_modified = t;
193
194 return ret;
195 }
196 /* }}} */
197
198 /* {{{ STATUS http_send_etag(char *, size_t) */
199 PHP_HTTP_API STATUS _http_send_etag(const char *etag, size_t etag_len TSRMLS_DC)
200 {
201 STATUS status;
202 char *etag_header;
203
204 if (!etag_len){
205 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Attempt to send empty ETag (previous: %s)\n", HTTP_G(send).unquoted_etag);
206 return FAILURE;
207 }
208
209 /* remember */
210 STR_FREE(HTTP_G(send).unquoted_etag);
211 HTTP_G(send).unquoted_etag = estrdup(etag);
212
213 etag_header = ecalloc(1, sizeof("ETag: \"\"") + etag_len);
214 sprintf(etag_header, "ETag: \"%s\"", etag);
215 status = http_send_header_string(etag_header);
216 efree(etag_header);
217 return status;
218 }
219 /* }}} */
220
221 /* {{{ STATUS http_send_content_type(char *, size_t) */
222 PHP_HTTP_API STATUS _http_send_content_type(const char *content_type, size_t ct_len TSRMLS_DC)
223 {
224 if (!strchr(content_type, '/')) {
225 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Content-Type '%s' doesn't seem to consist of a primary and a secondary part", content_type);
226 return FAILURE;
227 }
228
229 /* remember for multiple ranges */
230 STR_FREE(HTTP_G(send).content_type);
231 HTTP_G(send).content_type = estrndup(content_type, ct_len);
232
233 return http_send_header_ex("Content-Type", lenof("Content-Type"), content_type, ct_len, 1);
234 }
235 /* }}} */
236
237 /* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
238 PHP_HTTP_API STATUS _http_send_content_disposition(const char *filename, size_t f_len, zend_bool send_inline TSRMLS_DC)
239 {
240 STATUS status;
241 char *cd_header;
242
243 if (send_inline) {
244 cd_header = ecalloc(1, sizeof("Content-Disposition: inline; filename=\"\"") + f_len);
245 sprintf(cd_header, "Content-Disposition: inline; filename=\"%s\"", filename);
246 } else {
247 cd_header = ecalloc(1, sizeof("Content-Disposition: attachment; filename=\"\"") + f_len);
248 sprintf(cd_header, "Content-Disposition: attachment; filename=\"%s\"", filename);
249 }
250
251 status = http_send_header_string(cd_header);
252 efree(cd_header);
253 return status;
254 }
255 /* }}} */
256
257 /* {{{ STATUS http_send_ranges(HashTable *, void *, size_t, http_send_mode) */
258 PHP_HTTP_API STATUS _http_send_ranges(HashTable *ranges, const void *data, size_t size, http_send_mode mode TSRMLS_DC)
259 {
260 long **begin, **end;
261 zval **zrange;
262
263 /* single range */
264 if (zend_hash_num_elements(ranges) == 1) {
265 char range_header[256] = {0};
266
267 if (SUCCESS != zend_hash_index_find(ranges, 0, (void **) &zrange) ||
268 SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(zrange), 0, (void **) &begin) ||
269 SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(zrange), 1, (void **) &end)) {
270 http_send_status(500);
271 return FAILURE;
272 }
273
274 /* Send HTTP 206 Partial Content */
275 http_send_status(206);
276
277 /* send content range header */
278 snprintf(range_header, 255, "Content-Range: bytes %ld-%ld/%lu", **begin, **end, (ulong) size);
279 http_send_header_string(range_header);
280
281 /* send requested chunk */
282 return http_send_chunk(data, **begin, **end + 1, mode);
283 }
284
285 /* multi range */
286 else {
287 char bound[23] = {0}, preface[1024] = {0},
288 multi_header[68] = "Content-Type: multipart/byteranges; boundary=";
289
290 /* Send HTTP 206 Partial Content */
291 http_send_status(206);
292
293 /* send multipart/byteranges header */
294 snprintf(bound, 22, "--%lu%0.9f", (ulong) time(NULL), php_combined_lcg(TSRMLS_C));
295 strncat(multi_header, bound + 2, 21);
296 http_send_header_string(multi_header);
297
298 /* send each requested chunk */
299 FOREACH_HASH_VAL(ranges, zrange) {
300 if (SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(zrange), 0, (void **) &begin) ||
301 SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(zrange), 1, (void **) &end)) {
302 break;
303 }
304
305 snprintf(preface, 1023,
306 HTTP_CRLF "%s"
307 HTTP_CRLF "Content-Type: %s"
308 HTTP_CRLF "Content-Range: bytes %ld-%ld/%lu"
309 HTTP_CRLF
310 HTTP_CRLF,
311
312 bound,
313 HTTP_G(send).content_type ? HTTP_G(send).content_type : "application/x-octetstream",
314 **begin,
315 **end,
316 (ulong) size
317 );
318
319 PHPWRITE(preface, strlen(preface));
320 http_send_chunk(data, **begin, **end + 1, mode);
321 }
322
323 /* write boundary once more */
324 PHPWRITE(HTTP_CRLF, lenof(HTTP_CRLF));
325 PHPWRITE(bound, strlen(bound));
326 PHPWRITE("--", lenof("--"));
327
328 return SUCCESS;
329 }
330 }
331 /* }}} */
332
333 /* {{{ STATUS http_send(void *, size_t, http_send_mode) */
334 PHP_HTTP_API STATUS _http_send(const void *data_ptr, size_t data_size, http_send_mode data_mode TSRMLS_DC)
335 {
336 HashTable ranges;
337 http_range_status range_status;
338 int cache_etag = 0;
339
340 if (!data_ptr) {
341 return FAILURE;
342 }
343 if (!data_size) {
344 return SUCCESS;
345 }
346
347 /* stop on-the-fly etag generation */
348 if (cache_etag = HTTP_G(etag).started) {
349 /* interrupt ob_etaghandler */
350 HTTP_G(etag).started = 0;
351 }
352
353 /* enable partial dl and resume */
354 http_send_header_string("Accept-Ranges: bytes");
355
356 zend_hash_init(&ranges, 0, NULL, ZVAL_PTR_DTOR, 0);
357 range_status = http_get_request_ranges(&ranges, data_size);
358
359 if (range_status == RANGE_ERR) {
360 zend_hash_destroy(&ranges);
361 http_send_status(416);
362 return FAILURE;
363 }
364
365 /* Range Request - only send ranges if entity hasn't changed */
366 if ( range_status == RANGE_OK &&
367 http_match_etag_ex("HTTP_IF_MATCH", HTTP_G(send).unquoted_etag, 0) &&
368 http_match_last_modified_ex("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(send).last_modified, 0) &&
369 http_match_last_modified_ex("HTTP_UNLESS_MODIFIED_SINCE", HTTP_G(send).last_modified, 0)) {
370 STATUS result = http_send_ranges(&ranges, data_ptr, data_size, data_mode);
371 zend_hash_destroy(&ranges);
372 return result;
373 }
374
375 zend_hash_destroy(&ranges);
376
377 /* send 304 Not Modified if etag matches - DON'T return on ETag generation failure */
378 if (cache_etag) {
379 char *etag = NULL;
380
381 if (!(etag = http_etag(data_ptr, data_size, data_mode))) {
382 http_error(HE_NOTICE, HTTP_E_RUNTIME, "Failed to generate ETag for data source");
383 } else {
384 http_send_etag(etag, 32);
385 if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) {
386 return http_cache_exit_ex(etag, 1, 1);
387 }
388 efree(etag);
389 }
390 }
391
392 /* send 304 Not Modified if last modified matches */
393 if (http_match_last_modified("HTTP_IF_MODIFIED_SINCE", HTTP_G(send).last_modified)) {
394 return http_cache_exit_ex(http_date(HTTP_G(send).last_modified), 0, 1);
395 }
396
397 /* send full entity */
398 return http_send_chunk(data_ptr, 0, data_size, data_mode);
399 }
400 /* }}} */
401
402 /* {{{ STATUS http_send_stream(php_stream *) */
403 PHP_HTTP_API STATUS _http_send_stream_ex(php_stream *file, zend_bool close_stream TSRMLS_DC)
404 {
405 STATUS status;
406 php_stream_statbuf ssb;
407
408 if ((!file) || php_stream_stat(file, &ssb)) {
409 return FAILURE;
410 }
411
412 status = http_send(file, ssb.sb.st_size, SEND_RSRC);
413
414 if (close_stream) {
415 php_stream_close(file);
416 }
417
418 return status;
419 }
420 /* }}} */
421
422 /*
423 * Local variables:
424 * tab-width: 4
425 * c-basic-offset: 4
426 * End:
427 * vim600: sw=4 ts=4 fdm=marker
428 * vim<600: sw=4 ts=4
429 */
430