unfold url tests
[m6w6/ext-http] / php_http_env_response.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-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 static void set_option(zval *options, const char *name_str, size_t name_len, int type, void *value_ptr, size_t value_len TSRMLS_DC)
16 {
17 if (Z_TYPE_P(options) == IS_OBJECT) {
18 if (value_ptr) {
19 switch (type) {
20 case IS_DOUBLE:
21 zend_update_property_double(Z_OBJCE_P(options), options, name_str, name_len, *(double *)value_ptr TSRMLS_CC);
22 break;
23 case IS_LONG:
24 zend_update_property_long(Z_OBJCE_P(options), options, name_str, name_len, *(long *)value_ptr TSRMLS_CC);
25 break;
26 case IS_STRING:
27 zend_update_property_stringl(Z_OBJCE_P(options), options, name_str, name_len, value_ptr, value_len TSRMLS_CC);
28 break;
29 case IS_OBJECT:
30 zend_update_property(Z_OBJCE_P(options), options, name_str, name_len, value_ptr TSRMLS_CC);
31 break;
32 }
33 } else {
34 zend_update_property_null(Z_OBJCE_P(options), options, name_str, name_len TSRMLS_CC);
35 }
36 } else {
37 convert_to_array(options);
38 if (value_ptr) {
39 switch (type) {
40 case IS_DOUBLE:
41 add_assoc_double_ex(options, name_str, name_len + 1, *(double *)value_ptr);
42 break;
43 case IS_LONG:
44 add_assoc_long_ex(options, name_str, name_len + 1, *(long *)value_ptr);
45 break;
46 case IS_STRING: {
47 char *value = estrndup(value_ptr, value_len);
48 add_assoc_stringl_ex(options, name_str, name_len + 1, value, value_len, 0);
49 break;
50 case IS_OBJECT:
51 Z_ADDREF_P(value_ptr);
52 add_assoc_zval_ex(options, name_str, name_len + 1, value_ptr);
53 break;
54 }
55 }
56 } else {
57 add_assoc_null_ex(options, name_str, name_len + 1);
58 }
59 }
60 }
61 static zval *get_option(zval *options, const char *name_str, size_t name_len TSRMLS_DC)
62 {
63 zval *val, **valptr;
64
65 if (Z_TYPE_P(options) == IS_OBJECT) {
66 val = zend_read_property(Z_OBJCE_P(options), options, name_str, name_len, 0 TSRMLS_CC);
67 } else {
68 if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(options), name_str, name_len + 1, (void *) &valptr)) {
69 val = *valptr;
70 } else {
71 val = NULL;
72 }
73 }
74 if (val) {
75 Z_ADDREF_P(val);
76 }
77 return val;
78 }
79 static php_http_message_body_t *get_body(zval *options TSRMLS_DC)
80 {
81 zval *zbody;
82 php_http_message_body_t *body = NULL;
83
84 if ((zbody = get_option(options, ZEND_STRL("body") TSRMLS_CC))) {
85 if ((Z_TYPE_P(zbody) == IS_OBJECT) && instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry TSRMLS_CC)) {
86 php_http_message_body_object_t *body_obj = zend_object_store_get_object(zbody TSRMLS_CC);
87
88 body = body_obj->body;
89 }
90 zval_ptr_dtor(&zbody);
91 }
92
93 return body;
94 }
95 static php_http_message_t *get_request(zval *options TSRMLS_DC)
96 {
97 zval *zrequest;
98 php_http_message_t *request = NULL;
99
100 if ((zrequest = get_option(options, ZEND_STRL("request") TSRMLS_CC))) {
101 if (Z_TYPE_P(zrequest) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zrequest), php_http_message_class_entry TSRMLS_CC)) {
102 php_http_message_object_t *request_obj = zend_object_store_get_object(zrequest TSRMLS_CC);
103
104 request = request_obj->message;
105 }
106 zval_ptr_dtor(&zrequest);
107 }
108
109 return request;
110 }
111
112 php_http_cache_status_t php_http_env_is_response_cached_by_etag(zval *options, const char *header_str, size_t header_len, php_http_message_t *request TSRMLS_DC)
113 {
114 php_http_cache_status_t ret = PHP_HTTP_CACHE_NO;
115 int free_etag = 0;
116 char *header = NULL, *etag;
117 php_http_message_body_t *body;
118 zval *zetag;
119
120
121 if (!(body = get_body(options TSRMLS_CC))) {
122 return ret;
123 }
124
125 if ((zetag = get_option(options, ZEND_STRL("etag") TSRMLS_CC))) {
126 zval *zetag_copy = php_http_ztyp(IS_STRING, zetag);
127 zval_ptr_dtor(&zetag);
128 zetag = zetag_copy;
129 }
130
131 if (zetag && Z_STRLEN_P(zetag)) {
132 etag = Z_STRVAL_P(zetag);
133 } else if ((etag = php_http_message_body_etag(body))) {
134 set_option(options, ZEND_STRL("etag"), IS_STRING, etag, strlen(etag) TSRMLS_CC);
135 free_etag = 1;
136 }
137
138 if (zetag) {
139 zval_ptr_dtor(&zetag);
140 }
141
142 if (etag && (header = php_http_env_get_request_header(header_str, header_len, NULL, request TSRMLS_CC))) {
143 ret = php_http_match(header, etag, PHP_HTTP_MATCH_WORD) ? PHP_HTTP_CACHE_HIT : PHP_HTTP_CACHE_MISS;
144 }
145
146 if (free_etag) {
147 efree(etag);
148 }
149
150 STR_FREE(header);
151 return ret;
152 }
153
154 php_http_cache_status_t php_http_env_is_response_cached_by_last_modified(zval *options, const char *header_str, size_t header_len, php_http_message_t *request TSRMLS_DC)
155 {
156 php_http_cache_status_t ret = PHP_HTTP_CACHE_NO;
157 char *header;
158 time_t ums, lm = 0;
159 php_http_message_body_t *body;
160 zval *zlm;
161
162 if (!(body = get_body(options TSRMLS_CC))) {
163 return ret;
164 }
165
166 if ((zlm = get_option(options, ZEND_STRL("lastModified") TSRMLS_CC))) {
167 zval *zlm_copy = php_http_ztyp(IS_LONG, zlm);
168 zval_ptr_dtor(&zlm);
169 zlm = zlm_copy;
170 }
171
172 if (zlm && Z_LVAL_P(zlm) > 0) {
173 lm = Z_LVAL_P(zlm);
174 } else {
175 lm = php_http_message_body_mtime(body);
176 set_option(options, ZEND_STRL("lastModified"), IS_LONG, &lm, 0 TSRMLS_CC);
177 }
178
179 if (zlm) {
180 zval_ptr_dtor(&zlm);
181 }
182
183 if ((header = php_http_env_get_request_header(header_str, header_len, NULL, request TSRMLS_CC))) {
184 ums = php_parse_date(header, NULL);
185
186 if (ums > 0 && ums >= lm) {
187 ret = PHP_HTTP_CACHE_HIT;
188 } else {
189 ret = PHP_HTTP_CACHE_MISS;
190 }
191 }
192
193 STR_FREE(header);
194 return ret;
195 }
196
197 static zend_bool php_http_env_response_is_cacheable(php_http_env_response_t *r, php_http_message_t *request)
198 {
199 TSRMLS_FETCH_FROM_CTX(r->ts);
200
201 if (r->ops->get_status(r) >= 400) {
202 return 0;
203 }
204
205 if (php_http_env_got_request_header(ZEND_STRL("Authorization"), request TSRMLS_CC)) {
206 return 0;
207 }
208
209 if (-1 == php_http_select_str(php_http_env_get_request_method(request TSRMLS_CC), 2, "HEAD", "GET")) {
210 return 0;
211 }
212
213 return 1;
214 }
215
216 static size_t output(void *context, char *buf, size_t len TSRMLS_DC)
217 {
218 php_http_env_response_t *r = context;
219
220 r->ops->write(r, buf, len);
221
222 /* we really only need to flush when throttling is enabled,
223 because we push the data as fast as possible anyway if not */
224 if (r->throttle.delay >= PHP_HTTP_DIFFSEC) {
225 r->ops->flush(r);
226 php_http_sleep(r->throttle.delay);
227 }
228 return len;
229 }
230
231 #define php_http_env_response_send_done(r) php_http_env_response_send_data((r), NULL, 0)
232 static STATUS php_http_env_response_send_data(php_http_env_response_t *r, const char *buf, size_t len)
233 {
234 size_t chunk = r->throttle.chunk ? r->throttle.chunk : PHP_HTTP_SENDBUF_SIZE;
235 TSRMLS_FETCH_FROM_CTX(r->ts);
236
237 if (r->content.encoder) {
238 char *enc_str = NULL;
239 size_t enc_len = 0;
240
241 if (buf) {
242 if (SUCCESS != php_http_encoding_stream_update(r->content.encoder, buf, len, &enc_str, &enc_len)) {
243 return FAILURE;
244 }
245 } else {
246 if (SUCCESS != php_http_encoding_stream_finish(r->content.encoder, &enc_str, &enc_len)) {
247 return FAILURE;
248 }
249 }
250
251 if (enc_str) {
252 php_http_buffer_chunked_output(&r->buffer, enc_str, enc_len, buf ? chunk : 0, output, r TSRMLS_CC);
253 STR_FREE(enc_str);
254 }
255 } else {
256 php_http_buffer_chunked_output(&r->buffer, buf, len, buf ? chunk : 0, output, r TSRMLS_CC);
257 }
258
259 return SUCCESS;
260 }
261
262 php_http_env_response_t *php_http_env_response_init(php_http_env_response_t *r, zval *options, php_http_env_response_ops_t *ops, void *init_arg TSRMLS_DC)
263 {
264 zend_bool free_r;
265
266 if ((free_r = !r)) {
267 r = emalloc(sizeof(*r));
268 }
269 memset(r, 0, sizeof(*r));
270
271 if (ops) {
272 r->ops = ops;
273 } else {
274 r->ops = php_http_env_response_get_sapi_ops();
275 }
276
277 r->buffer = php_http_buffer_init(NULL);
278
279 Z_ADDREF_P(options);
280 r->options = options;
281
282 TSRMLS_SET_CTX(r->ts);
283
284 if (r->ops->init && (SUCCESS != r->ops->init(r, init_arg))) {
285 if (free_r) {
286 php_http_env_response_free(&r);
287 } else {
288 php_http_env_response_dtor(r);
289 r = NULL;
290 }
291 }
292
293 return r;
294 }
295
296 void php_http_env_response_dtor(php_http_env_response_t *r)
297 {
298 if (r->ops->dtor) {
299 r->ops->dtor(r);
300 }
301 php_http_buffer_free(&r->buffer);
302 zval_ptr_dtor(&r->options);
303 STR_FREE(r->content.type);
304 STR_FREE(r->content.encoding);
305 if (r->content.encoder) {
306 php_http_encoding_stream_free(&r->content.encoder);
307 }
308 }
309
310 void php_http_env_response_free(php_http_env_response_t **r)
311 {
312 if (*r) {
313 php_http_env_response_dtor(*r);
314 efree(*r);
315 *r = NULL;
316 }
317 }
318
319 static STATUS php_http_env_response_send_head(php_http_env_response_t *r, php_http_message_t *request)
320 {
321 STATUS ret = SUCCESS;
322 zval *zoption, *options = r->options;
323 TSRMLS_FETCH_FROM_CTX(r->ts);
324
325 if (r->done) {
326 return ret;
327 }
328
329 if ((zoption = get_option(options, ZEND_STRL("responseCode") TSRMLS_CC))) {
330 zval *zoption_copy = php_http_ztyp(IS_LONG, zoption);
331
332 zval_ptr_dtor(&zoption);
333 if (Z_LVAL_P(zoption_copy) > 0) {
334 ret = r->ops->set_status(r, Z_LVAL_P(zoption_copy));
335 }
336 zval_ptr_dtor(&zoption_copy);
337 }
338
339 if (ret != SUCCESS) {
340 return ret;
341 }
342
343 if ((zoption = get_option(options, ZEND_STRL("httpVersion") TSRMLS_CC))) {
344 php_http_version_t v;
345 zval *zoption_copy = php_http_ztyp(IS_STRING, zoption);
346
347 zval_ptr_dtor(&zoption);
348 if (Z_STRLEN_P(zoption_copy) && php_http_version_parse(&v, Z_STRVAL_P(zoption_copy) TSRMLS_CC)) {
349 ret = r->ops->set_protocol_version(r, &v);
350 php_http_version_dtor(&v);
351 }
352 zval_ptr_dtor(&zoption_copy);
353 }
354
355 if (ret != SUCCESS) {
356 return ret;
357 }
358
359 if ((zoption = get_option(options, ZEND_STRL("headers") TSRMLS_CC))) {
360 if (Z_TYPE_P(zoption) == IS_ARRAY) {
361 php_http_header_to_callback(Z_ARRVAL_P(zoption), 0, (php_http_pass_format_callback_t) r->ops->set_header, r TSRMLS_CC);
362 }
363 zval_ptr_dtor(&zoption);
364 }
365
366 if (ret != SUCCESS) {
367 return ret;
368 }
369
370 if ((zoption = get_option(options, ZEND_STRL("contentType") TSRMLS_CC))) {
371 zval *zoption_copy = php_http_ztyp(IS_STRING, zoption);
372
373 zval_ptr_dtor(&zoption);
374 if (Z_STRLEN_P(zoption_copy) && strchr(Z_STRVAL_P(zoption_copy), '/')) {
375 if (SUCCESS == (ret = r->ops->set_header(r, "Content-Type: %.*s", Z_STRLEN_P(zoption_copy), Z_STRVAL_P(zoption_copy)))) {
376 r->content.type = estrndup(Z_STRVAL_P(zoption_copy), Z_STRLEN_P(zoption_copy));
377 }
378 }
379 zval_ptr_dtor(&zoption_copy);
380 }
381
382 if (ret != SUCCESS) {
383 return ret;
384 }
385
386 if (r->range.status == PHP_HTTP_RANGE_OK) {
387 if (zend_hash_num_elements(&r->range.values) == 1) {
388 zval **range, **begin, **end;
389
390 if ( 1 == php_http_array_list(&r->range.values TSRMLS_CC, 1, &range)
391 && 2 == php_http_array_list(Z_ARRVAL_PP(range) TSRMLS_CC, 2, &begin, &end)
392 ) {
393 if (SUCCESS == (ret = r->ops->set_status(r, 206))) {
394 ret = r->ops->set_header(r, "Content-Range: bytes %ld-%ld/%zu", Z_LVAL_PP(begin), Z_LVAL_PP(end), r->content.length);
395 }
396 } else {
397 /* this should never happen */
398 zend_hash_destroy(&r->range.values);
399 ret = FAILURE;
400 }
401 } else {
402 php_http_boundary(r->range.boundary, sizeof(r->range.boundary) TSRMLS_CC);
403 if (SUCCESS == (ret = r->ops->set_status(r, 206))) {
404 ret = r->ops->set_header(r, "Content-Type: multipart/byteranges; boundary=%s", r->range.boundary);
405 }
406 }
407 } else {
408 if ((zoption = get_option(options, ZEND_STRL("cacheControl") TSRMLS_CC))) {
409 zval *zoption_copy = php_http_ztyp(IS_STRING, zoption);
410
411 zval_ptr_dtor(&zoption);
412 if (Z_STRLEN_P(zoption_copy)) {
413 ret = r->ops->set_header(r, "Cache-Control: %.*s", Z_STRLEN_P(zoption_copy), Z_STRVAL_P(zoption_copy));
414 }
415 zval_ptr_dtor(&zoption_copy);
416 }
417
418 if (ret != SUCCESS) {
419 return ret;
420 }
421
422 if ((zoption = get_option(options, ZEND_STRL("contentDisposition") TSRMLS_CC))) {
423 zval *zoption_copy = php_http_ztyp(IS_ARRAY, zoption);
424 php_http_buffer_t buf;
425
426 php_http_buffer_init(&buf);
427 if (php_http_params_to_string(&buf, Z_ARRVAL_P(zoption_copy), ZEND_STRL(","), ZEND_STRL(";"), ZEND_STRL("="), PHP_HTTP_PARAMS_DEFAULT TSRMLS_CC)) {
428 if (buf.used) {
429 ret = r->ops->set_header(r, "Content-Disposition: %.*s", buf.used, buf.data);
430 }
431 }
432
433 php_http_buffer_dtor(&buf);
434 zval_ptr_dtor(&zoption_copy);
435 zval_ptr_dtor(&zoption);
436 }
437
438 if (ret != SUCCESS) {
439 return ret;
440 }
441
442 if ((zoption = get_option(options, ZEND_STRL("contentEncoding") TSRMLS_CC))) {
443 zval *zoption_copy = php_http_ztyp(IS_LONG, zoption);
444 zval zsupported;
445 HashTable *result = NULL;
446
447 zval_ptr_dtor(&zoption);
448 switch (Z_LVAL_P(zoption_copy)) {
449 case PHP_HTTP_CONTENT_ENCODING_GZIP:
450 INIT_PZVAL(&zsupported);
451 array_init(&zsupported);
452 add_next_index_stringl(&zsupported, ZEND_STRL("none"), 1);
453 add_next_index_stringl(&zsupported, ZEND_STRL("gzip"), 1);
454 add_next_index_stringl(&zsupported, ZEND_STRL("deflate"), 1);
455
456 if ((result = php_http_negotiate_encoding(Z_ARRVAL(zsupported), request TSRMLS_CC))) {
457 char *key_str = NULL;
458 uint key_len = 0;
459
460 zend_hash_internal_pointer_reset(result);
461 if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key_str, &key_len, NULL, 0, NULL)) {
462 if (!strcmp(key_str, "gzip")) {
463 if (!(r->content.encoder = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_GZIP TSRMLS_CC))) {
464 ret = FAILURE;
465 } else if (SUCCESS == (ret = r->ops->set_header(r, "Content-Encoding: gzip"))) {
466 r->content.encoding = estrndup(key_str, key_len - 1);
467 }
468 } else if (!strcmp(key_str, "deflate")) {
469 if (!(r->content.encoder = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_ZLIB TSRMLS_CC))) {
470 ret = FAILURE;
471 } else if (SUCCESS == (ret = r->ops->set_header(r, "Content-Encoding: deflate"))) {
472 r->content.encoding = estrndup(key_str, key_len - 1);
473 }
474 } else {
475 ret = r->ops->del_header(r, ZEND_STRL("Content-Encoding"));
476 }
477
478 if (SUCCESS == ret) {
479 ret = r->ops->add_header(r, "Vary: Accept-Encoding");
480 }
481 }
482
483 zend_hash_destroy(result);
484 FREE_HASHTABLE(result);
485 }
486
487 zval_dtor(&zsupported);
488 break;
489
490 case PHP_HTTP_CONTENT_ENCODING_NONE:
491 default:
492 ret = r->ops->del_header(r, ZEND_STRL("Content-Encoding"));
493 break;
494 }
495 zval_ptr_dtor(&zoption_copy);
496 }
497
498 if (SUCCESS != ret) {
499 return ret;
500 }
501
502 if (php_http_env_response_is_cacheable(r, request)) {
503 switch (php_http_env_is_response_cached_by_etag(options, ZEND_STRL("If-None-Match"), request TSRMLS_CC)) {
504 case PHP_HTTP_CACHE_MISS:
505 break;
506
507 case PHP_HTTP_CACHE_NO:
508 if (PHP_HTTP_CACHE_HIT != php_http_env_is_response_cached_by_last_modified(options, ZEND_STRL("If-Modified-Since"), request TSRMLS_CC)) {
509 break;
510 }
511 /* no break */
512
513 case PHP_HTTP_CACHE_HIT:
514 ret = r->ops->set_status(r, 304);
515 r->done = 1;
516 break;
517 }
518
519 if ((zoption = get_option(options, ZEND_STRL("etag") TSRMLS_CC))) {
520 zval *zoption_copy = php_http_ztyp(IS_STRING, zoption);
521
522 zval_ptr_dtor(&zoption);
523 if (*Z_STRVAL_P(zoption_copy) != '"' && strncmp(Z_STRVAL_P(zoption_copy), "W/\"", 3)) {
524 ret = r->ops->set_header(r, "ETag: \"%s\"", Z_STRVAL_P(zoption_copy));
525 } else {
526 ret = r->ops->set_header(r, "ETag: %s", Z_STRVAL_P(zoption_copy));
527 }
528 zval_ptr_dtor(&zoption_copy);
529 }
530 if ((zoption = get_option(options, ZEND_STRL("lastModified") TSRMLS_CC))) {
531 zval *zoption_copy = php_http_ztyp(IS_LONG, zoption);
532
533 zval_ptr_dtor(&zoption);
534 if (Z_LVAL_P(zoption_copy)) {
535 char *date = php_format_date(ZEND_STRL(PHP_HTTP_DATE_FORMAT), Z_LVAL_P(zoption_copy), 0 TSRMLS_CC);
536 if (date) {
537 ret = r->ops->set_header(r, "Last-Modified: %s", date);
538 efree(date);
539 }
540 }
541 zval_ptr_dtor(&zoption_copy);
542 }
543 }
544 }
545
546 return ret;
547 }
548
549 static STATUS php_http_env_response_send_body(php_http_env_response_t *r)
550 {
551 STATUS ret = SUCCESS;
552 zval *zoption;
553 php_http_message_body_t *body;
554 TSRMLS_FETCH_FROM_CTX(r->ts);
555
556 if (r->done) {
557 return ret;
558 }
559
560 if ((body = get_body(r->options TSRMLS_CC))) {
561 if ((zoption = get_option(r->options, ZEND_STRL("throttleDelay") TSRMLS_CC))) {
562 if (Z_TYPE_P(zoption) == IS_DOUBLE) {
563 r->throttle.delay = Z_DVAL_P(zoption);
564 }
565 zval_ptr_dtor(&zoption);
566 }
567 if ((zoption = get_option(r->options, ZEND_STRL("throttleChunk") TSRMLS_CC))) {
568 if (Z_TYPE_P(zoption) == IS_LONG) {
569 r->throttle.chunk = Z_LVAL_P(zoption);
570 }
571 zval_ptr_dtor(&zoption);
572 }
573
574 if (r->range.status == PHP_HTTP_RANGE_OK) {
575 if (zend_hash_num_elements(&r->range.values) == 1) {
576 /* single range */
577 zval **range, **begin, **end;
578
579 if ( 1 == php_http_array_list(&r->range.values TSRMLS_CC, 1, &range)
580 && 2 == php_http_array_list(Z_ARRVAL_PP(range) TSRMLS_CC, 2, &begin, &end)
581 ) {
582 /* send chunk */
583 ret = php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_env_response_send_data, r, Z_LVAL_PP(begin), Z_LVAL_PP(end) - Z_LVAL_PP(begin) + 1);
584 if (ret == SUCCESS) {
585 ret = php_http_env_response_send_done(r);
586 }
587 zend_hash_destroy(&r->range.values);
588 } else {
589 /* this should never happen */
590 zend_hash_destroy(&r->range.values);
591 r->ops->set_status(r, 500);
592 ret = FAILURE;
593 }
594
595 } else {
596 /* send multipart/byte-ranges message */
597 HashPosition pos;
598 zval **chunk;
599
600 FOREACH_HASH_VAL(pos, &r->range.values, chunk) {
601 zval **begin, **end;
602
603 if (2 == php_http_array_list(Z_ARRVAL_PP(chunk) TSRMLS_CC, 2, &begin, &end)) {
604 php_http_buffer_appendf(r->buffer,
605 PHP_HTTP_CRLF
606 "--%s" PHP_HTTP_CRLF
607 "Content-Type: %s" PHP_HTTP_CRLF
608 "Content-Range: bytes %ld-%ld/%zu" PHP_HTTP_CRLF PHP_HTTP_CRLF,
609 /* - */
610 r->range.boundary,
611 r->content.type ? r->content.type : "application/octet-stream",
612 Z_LVAL_PP(begin),
613 Z_LVAL_PP(end),
614 r->content.length
615 );
616 ret = php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_env_response_send_data, r, Z_LVAL_PP(begin), Z_LVAL_PP(end) - Z_LVAL_PP(begin) + 1);
617 }
618 }
619
620 if (ret == SUCCESS) {
621 php_http_buffer_appendf(r->buffer, PHP_HTTP_CRLF "--%s--", r->range.boundary);
622 ret = php_http_env_response_send_done(r);
623 }
624 zend_hash_destroy(&r->range.values);
625 }
626
627 } else {
628 ret = php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_env_response_send_data, r, 0, 0);
629 if (ret == SUCCESS) {
630 ret = php_http_env_response_send_done(r);
631 }
632 }
633 }
634 return ret;
635 }
636
637 STATUS php_http_env_response_send(php_http_env_response_t *r)
638 {
639 php_http_message_t *request;
640 php_http_message_body_t *body;
641 TSRMLS_FETCH_FROM_CTX(r->ts);
642
643 request = get_request(r->options TSRMLS_CC);
644
645 /* check for ranges */
646 if ((body = get_body(r->options TSRMLS_CC))) {
647 r->content.length = php_http_message_body_size(body);
648
649 if (SUCCESS != r->ops->set_header(r, "Accept-Ranges: bytes")) {
650 return FAILURE;
651 } else {
652 zend_hash_init(&r->range.values, 0, NULL, ZVAL_PTR_DTOR, 0);
653 r->range.status = php_http_env_get_request_ranges(&r->range.values, r->content.length, request TSRMLS_CC);
654
655 switch (r->range.status) {
656 case PHP_HTTP_RANGE_NO:
657 zend_hash_destroy(&r->range.values);
658 break;
659
660 case PHP_HTTP_RANGE_ERR:
661 if (php_http_env_got_request_header(ZEND_STRL("If-Range"), request TSRMLS_CC)) {
662 r->range.status = PHP_HTTP_RANGE_NO;
663 zend_hash_destroy(&r->range.values);
664 } else {
665 r->done = 1;
666 zend_hash_destroy(&r->range.values);
667 if (SUCCESS != r->ops->set_status(r, 416)) {
668 return FAILURE;
669 }
670 if (SUCCESS != r->ops->set_header(r, "Content-Range: bytes */%zu", r->content.length)) {
671 return FAILURE;
672 }
673 }
674 break;
675
676 case PHP_HTTP_RANGE_OK:
677 if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(r->options, ZEND_STRL("If-Range"), request TSRMLS_CC)
678 || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("If-Range"), request TSRMLS_CC)
679 ) {
680 r->range.status = PHP_HTTP_RANGE_NO;
681 zend_hash_destroy(&r->range.values);
682 break;
683 }
684 if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(r->options, ZEND_STRL("If-Match"), request TSRMLS_CC)
685 || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("If-Unmodified-Since"), request TSRMLS_CC)
686 || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("Unless-Modified-Since"), request TSRMLS_CC)
687 ) {
688 r->done = 1;
689 zend_hash_destroy(&r->range.values);
690 if (SUCCESS != r->ops->set_status(r, 412)) {
691 return FAILURE;
692 }
693 break;
694 }
695
696 break;
697 }
698 }
699 }
700
701 if (SUCCESS != php_http_env_response_send_head(r, request)) {
702 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to send response headers");
703 return FAILURE;
704 }
705
706 if (SUCCESS != php_http_env_response_send_body(r)) {
707 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to send response body");
708 return FAILURE;
709 }
710
711 if (SUCCESS != r->ops->finish(r)) {
712 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to finish response");
713 return FAILURE;
714 }
715
716 return SUCCESS;
717 }
718
719 static long php_http_env_response_sapi_get_status(php_http_env_response_t *r)
720 {
721 TSRMLS_FETCH_FROM_CTX(r->ts);
722
723 return php_http_env_get_response_code(TSRMLS_C);
724 }
725 static STATUS php_http_env_response_sapi_set_status(php_http_env_response_t *r, long http_code)
726 {
727 TSRMLS_FETCH_FROM_CTX(r->ts);
728
729 return php_http_env_set_response_code(http_code TSRMLS_CC);
730 }
731 static STATUS php_http_env_response_sapi_set_protocol_version(php_http_env_response_t *r, php_http_version_t *v)
732 {
733 TSRMLS_FETCH_FROM_CTX(r->ts);
734
735 return php_http_env_set_response_protocol_version(v TSRMLS_CC);
736 }
737 static STATUS php_http_env_response_sapi_set_header(php_http_env_response_t *r, const char *fmt, ...)
738 {
739 STATUS ret;
740 va_list args;
741 TSRMLS_FETCH_FROM_CTX(r->ts);
742
743 va_start(args, fmt);
744 ret = php_http_env_set_response_header_va(0, 1, fmt, args TSRMLS_CC);
745 va_end(args);
746
747 return ret;
748 }
749 static STATUS php_http_env_response_sapi_add_header(php_http_env_response_t *r, const char *fmt, ...)
750 {
751 STATUS ret;
752 va_list args;
753 TSRMLS_FETCH_FROM_CTX(r->ts);
754
755 va_start(args, fmt);
756 ret = php_http_env_set_response_header_va(0, 0, fmt, args TSRMLS_CC);
757 va_end(args);
758
759 return ret;
760 }
761 static STATUS php_http_env_response_sapi_del_header(php_http_env_response_t *r, const char *header_str, size_t header_len)
762 {
763 TSRMLS_FETCH_FROM_CTX(r->ts);
764
765 return php_http_env_set_response_header_value(0, header_str, header_len, NULL, 1 TSRMLS_CC);
766 }
767 static STATUS php_http_env_response_sapi_write(php_http_env_response_t *r, const char *data_str, size_t data_len)
768 {
769 TSRMLS_FETCH_FROM_CTX(r->ts);
770
771 if (0 < PHPWRITE(data_str, data_len)) {
772 return SUCCESS;
773 }
774 return FAILURE;
775 }
776 static STATUS php_http_env_response_sapi_flush(php_http_env_response_t *r)
777 {
778 TSRMLS_FETCH_FROM_CTX(r->ts);
779
780 #if PHP_VERSION_ID >= 50400
781 if (php_output_get_level(TSRMLS_C)) {
782 php_output_flush_all(TSRMLS_C);
783 }
784 if (!(php_output_get_status(TSRMLS_C) & PHP_OUTPUT_IMPLICITFLUSH)) {
785 sapi_flush(TSRMLS_C);
786 }
787 #else
788 php_end_ob_buffer(1, 1 TSRMLS_CC);
789 sapi_flush(TSRMLS_C);
790 #endif
791
792 return SUCCESS;
793 }
794 static STATUS php_http_env_response_sapi_finish(php_http_env_response_t *r)
795 {
796 return SUCCESS;
797 }
798
799 static php_http_env_response_ops_t php_http_env_response_sapi_ops = {
800 NULL,
801 NULL,
802 php_http_env_response_sapi_get_status,
803 php_http_env_response_sapi_set_status,
804 php_http_env_response_sapi_set_protocol_version,
805 php_http_env_response_sapi_set_header,
806 php_http_env_response_sapi_add_header,
807 php_http_env_response_sapi_del_header,
808 php_http_env_response_sapi_write,
809 php_http_env_response_sapi_flush,
810 php_http_env_response_sapi_finish
811 };
812
813 php_http_env_response_ops_t *php_http_env_response_get_sapi_ops(void)
814 {
815 return &php_http_env_response_sapi_ops;
816 }
817
818 typedef struct php_http_env_response_stream_ctx {
819 HashTable header;
820 php_http_version_t version;
821 long status_code;
822
823 php_stream *stream;
824
825 unsigned started:1;
826 unsigned finished:1;
827 } php_http_env_response_stream_ctx_t;
828
829 static STATUS php_http_env_response_stream_init(php_http_env_response_t *r, void *init_arg)
830 {
831 php_http_env_response_stream_ctx_t *ctx;
832 TSRMLS_FETCH_FROM_CTX(r->ts);
833
834 ctx = ecalloc(1, sizeof(*ctx));
835
836 ctx->stream = init_arg;
837 if (SUCCESS != zend_list_addref(ctx->stream->rsrc_id)) {
838 efree(ctx);
839 return FAILURE;
840 }
841 zend_hash_init(&ctx->header, 0, NULL, ZVAL_PTR_DTOR, 0);
842 php_http_version_init(&ctx->version, 1, 1 TSRMLS_CC);
843 ctx->status_code = 200;
844
845 r->ctx = ctx;
846
847 return SUCCESS;
848 }
849 static void php_http_env_response_stream_dtor(php_http_env_response_t *r)
850 {
851 php_http_env_response_stream_ctx_t *ctx = r->ctx;
852 TSRMLS_FETCH_FROM_CTX(r->ts);
853
854 zend_hash_destroy(&ctx->header);
855 zend_list_delete(ctx->stream->rsrc_id);
856 efree(ctx);
857 r->ctx = NULL;
858 }
859 static void php_http_env_response_stream_header(php_http_env_response_stream_ctx_t *ctx, HashTable *header TSRMLS_DC)
860 {
861 HashPosition pos;
862 zval **val;
863
864 FOREACH_HASH_VAL(pos, &ctx->header, val) {
865 if (Z_TYPE_PP(val) == IS_ARRAY) {
866 php_http_env_response_stream_header(ctx, Z_ARRVAL_PP(val) TSRMLS_CC);
867 } else {
868 php_stream_write(ctx->stream, Z_STRVAL_PP(val), Z_STRLEN_PP(val));
869 php_stream_write_string(ctx->stream, PHP_HTTP_CRLF);
870 }
871 }
872 }
873 static STATUS php_http_env_response_stream_start(php_http_env_response_stream_ctx_t *ctx TSRMLS_DC)
874 {
875 if (ctx->started || ctx->finished) {
876 return FAILURE;
877 }
878
879 php_stream_printf(ctx->stream TSRMLS_CC, "HTTP/%u.%u %ld %s" PHP_HTTP_CRLF, ctx->version.major, ctx->version.minor, ctx->status_code, php_http_env_get_response_status_for_code(ctx->status_code));
880 php_http_env_response_stream_header(ctx, &ctx->header TSRMLS_CC);
881 php_stream_write_string(ctx->stream, PHP_HTTP_CRLF);
882 ctx->started = 1;
883 return SUCCESS;
884 }
885 static long php_http_env_response_stream_get_status(php_http_env_response_t *r)
886 {
887 php_http_env_response_stream_ctx_t *ctx = r->ctx;
888
889 return ctx->status_code;
890 }
891 static STATUS php_http_env_response_stream_set_status(php_http_env_response_t *r, long http_code)
892 {
893 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
894
895 if (stream_ctx->started || stream_ctx->finished) {
896 return FAILURE;
897 }
898
899 stream_ctx->status_code = http_code;
900
901 return SUCCESS;
902 }
903 static STATUS php_http_env_response_stream_set_protocol_version(php_http_env_response_t *r, php_http_version_t *v)
904 {
905 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
906
907 if (stream_ctx->started || stream_ctx->finished) {
908 return FAILURE;
909 }
910
911 memcpy(&stream_ctx->version, v, sizeof(stream_ctx->version));
912
913 return SUCCESS;
914 }
915 static STATUS php_http_env_response_stream_set_header_ex(php_http_env_response_t *r, zend_bool replace, const char *fmt, va_list argv)
916 {
917 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
918 char *header_end, *header_str = NULL;
919 size_t header_len = 0;
920 zval *zheader, **zheader_ptr;
921
922 if (stream_ctx->started || stream_ctx->finished) {
923 return FAILURE;
924 }
925
926 header_len = vspprintf(&header_str, 0, fmt, argv);
927
928 if (!(header_end = strchr(header_str, ':'))) {
929 efree(header_str);
930 return FAILURE;
931 }
932
933 *header_end = '\0';
934
935 if (!replace && (SUCCESS == zend_hash_find(&stream_ctx->header, header_str, header_end - header_str + 1, (void *) &zheader_ptr))) {
936 convert_to_array(*zheader_ptr);
937 *header_end = ':';
938 return add_next_index_stringl(*zheader_ptr, header_str, header_len, 0);
939 } else {
940 MAKE_STD_ZVAL(zheader);
941 ZVAL_STRINGL(zheader, header_str, header_len, 0);
942
943 if (SUCCESS != zend_hash_update(&stream_ctx->header, header_str, header_end - header_str + 1, (void *) &zheader, sizeof(zval *), NULL)) {
944 zval_ptr_dtor(&zheader);
945 return FAILURE;
946 }
947
948 *header_end = ':';
949 return SUCCESS;
950 }
951 }
952 static STATUS php_http_env_response_stream_set_header(php_http_env_response_t *r, const char *fmt, ...)
953 {
954 STATUS ret;
955 va_list argv;
956
957 va_start(argv, fmt);
958 ret = php_http_env_response_stream_set_header_ex(r, 1, fmt, argv);
959 va_end(argv);
960
961 return ret;
962 }
963 static STATUS php_http_env_response_stream_add_header(php_http_env_response_t *r, const char *fmt, ...)
964 {
965 STATUS ret;
966 va_list argv;
967
968 va_start(argv, fmt);
969 ret = php_http_env_response_stream_set_header_ex(r, 0, fmt, argv);
970 va_end(argv);
971
972 return ret;
973 }
974 static STATUS php_http_env_response_stream_del_header(php_http_env_response_t *r, const char *header_str, size_t header_len)
975 {
976 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
977
978 if (stream_ctx->started || stream_ctx->finished) {
979 return FAILURE;
980 }
981
982 zend_hash_del(&stream_ctx->header, header_str, header_len + 1);
983 return SUCCESS;
984 }
985 static STATUS php_http_env_response_stream_write(php_http_env_response_t *r, const char *data_str, size_t data_len)
986 {
987 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
988 TSRMLS_FETCH_FROM_CTX(r->ts);
989
990 if (stream_ctx->finished) {
991 return FAILURE;
992 }
993 if (!stream_ctx->started) {
994 if (SUCCESS != php_http_env_response_stream_start(stream_ctx TSRMLS_CC)) {
995 return FAILURE;
996 }
997 }
998
999 php_stream_write(stream_ctx->stream, data_str, data_len);
1000
1001 return SUCCESS;
1002 }
1003 static STATUS php_http_env_response_stream_flush(php_http_env_response_t *r)
1004 {
1005 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
1006 TSRMLS_FETCH_FROM_CTX(r->ts);
1007
1008 if (stream_ctx->finished) {
1009 return FAILURE;
1010 }
1011 if (!stream_ctx->started) {
1012 if (SUCCESS != php_http_env_response_stream_start(stream_ctx TSRMLS_CC)) {
1013 return FAILURE;
1014 }
1015 }
1016
1017 return php_stream_flush(stream_ctx->stream);
1018 }
1019 static STATUS php_http_env_response_stream_finish(php_http_env_response_t *r)
1020 {
1021 php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
1022 TSRMLS_FETCH_FROM_CTX(r->ts);
1023
1024 if (stream_ctx->finished) {
1025 return FAILURE;
1026 }
1027 if (!stream_ctx->started) {
1028 if (SUCCESS != php_http_env_response_stream_start(stream_ctx TSRMLS_CC)) {
1029 return FAILURE;
1030 }
1031 }
1032
1033 stream_ctx->finished = 1;
1034
1035 return SUCCESS;
1036 }
1037
1038 static php_http_env_response_ops_t php_http_env_response_stream_ops = {
1039 php_http_env_response_stream_init,
1040 php_http_env_response_stream_dtor,
1041 php_http_env_response_stream_get_status,
1042 php_http_env_response_stream_set_status,
1043 php_http_env_response_stream_set_protocol_version,
1044 php_http_env_response_stream_set_header,
1045 php_http_env_response_stream_add_header,
1046 php_http_env_response_stream_del_header,
1047 php_http_env_response_stream_write,
1048 php_http_env_response_stream_flush,
1049 php_http_env_response_stream_finish
1050 };
1051
1052 php_http_env_response_ops_t *php_http_env_response_get_stream_ops(void)
1053 {
1054 return &php_http_env_response_stream_ops;
1055 }
1056
1057 #define PHP_HTTP_ENV_RESPONSE_OBJECT_INIT(obj) \
1058 do { \
1059 if (!obj->message) { \
1060 obj->message = php_http_message_init_env(NULL, PHP_HTTP_RESPONSE TSRMLS_CC); \
1061 } \
1062 } while (0)
1063
1064 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse___construct, 0, 0, 0)
1065 ZEND_END_ARG_INFO();
1066 static PHP_METHOD(HttpEnvResponse, __construct)
1067 {
1068 php_http_message_object_t *obj;
1069
1070 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1071
1072 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1073
1074 php_http_expect(obj->message = php_http_message_init_env(obj->message, PHP_HTTP_RESPONSE TSRMLS_CC), unexpected_val, return);
1075 }
1076
1077 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse___invoke, 0, 0, 1)
1078 ZEND_ARG_INFO(0, ob_string)
1079 ZEND_ARG_INFO(0, ob_flags)
1080 ZEND_END_ARG_INFO();
1081 static PHP_METHOD(HttpEnvResponse, __invoke)
1082 {
1083 char *ob_str;
1084 int ob_len;
1085 long ob_flags = 0;
1086
1087 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &ob_str, &ob_len, &ob_flags)) {
1088 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1089
1090 PHP_HTTP_ENV_RESPONSE_OBJECT_INIT(obj);
1091
1092 php_http_message_object_init_body_object(obj);
1093 php_http_message_body_append(obj->message->body, ob_str, ob_len);
1094 RETURN_TRUE;
1095 }
1096 }
1097
1098 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setEnvRequest, 0, 0, 1)
1099 ZEND_ARG_OBJ_INFO(0, env_request, http\\Message, 1)
1100 ZEND_END_ARG_INFO();
1101 static PHP_METHOD(HttpEnvResponse, setEnvRequest)
1102 {
1103 zval *env_req = NULL;
1104
1105 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O", &env_req, php_http_message_class_entry), invalid_arg, return);
1106
1107 set_option(getThis(), ZEND_STRL("request"), IS_OBJECT, env_req, 0 TSRMLS_CC);
1108 RETVAL_ZVAL(getThis(), 1, 0);
1109 }
1110
1111 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentType, 0, 0, 1)
1112 ZEND_ARG_INFO(0, content_type)
1113 ZEND_END_ARG_INFO();
1114 static PHP_METHOD(HttpEnvResponse, setContentType)
1115 {
1116 char *ct_str = NULL;
1117 int ct_len = 0;
1118
1119 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &ct_str, &ct_len), invalid_arg, return);
1120
1121 set_option(getThis(), ZEND_STRL("contentType"), IS_STRING, ct_str, ct_len TSRMLS_CC);
1122 RETVAL_ZVAL(getThis(), 1, 0);
1123 }
1124
1125 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentDisposition, 0, 0, 1)
1126 ZEND_ARG_ARRAY_INFO(0, disposition_params, 1)
1127 ZEND_END_ARG_INFO();
1128 static PHP_METHOD(HttpEnvResponse, setContentDisposition)
1129 {
1130 zval *zdisposition;
1131
1132 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &zdisposition), invalid_arg, return);
1133
1134 zend_update_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("contentDisposition"), zdisposition TSRMLS_CC);
1135 RETVAL_ZVAL(getThis(), 1, 0);
1136 }
1137
1138 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentEncoding, 0, 0, 1)
1139 ZEND_ARG_INFO(0, content_encoding)
1140 ZEND_END_ARG_INFO();
1141 static PHP_METHOD(HttpEnvResponse, setContentEncoding)
1142 {
1143 long ce;
1144
1145 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ce), invalid_arg, return);
1146
1147 set_option(getThis(), ZEND_STRL("contentEncoding"), IS_LONG, &ce, 0 TSRMLS_CC);
1148 RETVAL_ZVAL(getThis(), 1, 0);
1149 }
1150
1151 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setCacheControl, 0, 0, 1)
1152 ZEND_ARG_INFO(0, cache_control)
1153 ZEND_END_ARG_INFO();
1154 static PHP_METHOD(HttpEnvResponse, setCacheControl)
1155 {
1156 char *cc_str = NULL;
1157 int cc_len = 0;
1158
1159 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &cc_str, &cc_len), invalid_arg, return);
1160
1161 set_option(getThis(), ZEND_STRL("cacheControl"), IS_STRING, cc_str, cc_len TSRMLS_CC);
1162 RETVAL_ZVAL(getThis(), 1, 0);
1163 }
1164
1165 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setLastModified, 0, 0, 1)
1166 ZEND_ARG_INFO(0, last_modified)
1167 ZEND_END_ARG_INFO();
1168 static PHP_METHOD(HttpEnvResponse, setLastModified)
1169 {
1170 long last_modified;
1171
1172 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &last_modified), invalid_arg, return);
1173
1174 set_option(getThis(), ZEND_STRL("lastModified"), IS_LONG, &last_modified, 0 TSRMLS_CC);
1175 RETVAL_ZVAL(getThis(), 1, 0);
1176 }
1177
1178 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_isCachedByLastModified, 0, 0, 0)
1179 ZEND_ARG_INFO(0, header_name)
1180 ZEND_END_ARG_INFO();
1181 static PHP_METHOD(HttpEnvResponse, isCachedByLastModified)
1182 {
1183 char *header_name_str = NULL;
1184 int header_name_len = 0;
1185
1186 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &header_name_str, &header_name_len)) {
1187 if (!header_name_str || !header_name_len) {
1188 header_name_str = "If-Modified-Since";
1189 header_name_len = lenof("If-Modified-Since");
1190 }
1191
1192 RETURN_LONG(php_http_env_is_response_cached_by_last_modified(getThis(), header_name_str, header_name_len, get_request(getThis() TSRMLS_CC) TSRMLS_CC));
1193 }
1194 }
1195
1196 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setEtag, 0, 0, 1)
1197 ZEND_ARG_INFO(0, etag)
1198 ZEND_END_ARG_INFO();
1199 static PHP_METHOD(HttpEnvResponse, setEtag)
1200 {
1201 char *etag_str = NULL;
1202 int etag_len = 0;
1203
1204 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &etag_str, &etag_len), invalid_arg, return);
1205
1206 set_option(getThis(), ZEND_STRL("etag"), IS_STRING, etag_str, etag_len TSRMLS_CC);
1207 RETVAL_ZVAL(getThis(), 1, 0);
1208 }
1209
1210 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_isCachedByEtag, 0, 0, 0)
1211 ZEND_ARG_INFO(0, header_name)
1212 ZEND_END_ARG_INFO();
1213 static PHP_METHOD(HttpEnvResponse, isCachedByEtag)
1214 {
1215 char *header_name_str = NULL;
1216 int header_name_len = 0;
1217
1218 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &header_name_str, &header_name_len)) {
1219 if (!header_name_str || !header_name_len) {
1220 header_name_str = "If-None-Match";
1221 header_name_len = lenof("If-None-Match");
1222 }
1223 RETURN_LONG(php_http_env_is_response_cached_by_etag(getThis(), header_name_str, header_name_len, get_request(getThis() TSRMLS_CC) TSRMLS_CC));
1224 }
1225 }
1226
1227 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setThrottleRate, 0, 0, 1)
1228 ZEND_ARG_INFO(0, chunk_size)
1229 ZEND_ARG_INFO(0, delay)
1230 ZEND_END_ARG_INFO();
1231 static PHP_METHOD(HttpEnvResponse, setThrottleRate)
1232 {
1233 long chunk_size;
1234 double delay = 1;
1235
1236 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|d", &chunk_size, &delay), invalid_arg, return);
1237
1238 set_option(getThis(), ZEND_STRL("throttleDelay"), IS_DOUBLE, &delay, 0 TSRMLS_CC);
1239 set_option(getThis(), ZEND_STRL("throttleChunk"), IS_LONG, &chunk_size, 0 TSRMLS_CC);
1240 RETVAL_ZVAL(getThis(), 1, 0);
1241 }
1242
1243 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_send, 0, 0, 0)
1244 ZEND_ARG_INFO(0, stream)
1245 ZEND_END_ARG_INFO();
1246 static PHP_METHOD(HttpEnvResponse, send)
1247 {
1248 zval *zstream = NULL;
1249 php_stream *s = NULL;
1250
1251 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &zstream)) {
1252 /* first flush the output layer to avoid conflicting headers and output;
1253 * also, ob_start($thisEnvResponse) might have been called */
1254 #if PHP_VERSION_ID >= 50400
1255 php_output_end_all(TSRMLS_C);
1256 #else
1257 php_end_ob_buffers(1 TSRMLS_CC);
1258 #endif
1259 if (zstream) {
1260 php_http_env_response_t *r;
1261
1262 php_stream_from_zval(s, &zstream);
1263 r = php_http_env_response_init(NULL, getThis(), php_http_env_response_get_stream_ops(), s TSRMLS_CC);
1264 if (!r) {
1265 RETURN_FALSE;
1266 }
1267
1268 RETVAL_BOOL(SUCCESS == php_http_env_response_send(r));
1269 php_http_env_response_free(&r);
1270 } else {
1271 php_http_env_response_t r;
1272
1273 if (!php_http_env_response_init(&r, getThis(), NULL, NULL TSRMLS_CC)) {
1274 RETURN_FALSE;
1275 }
1276
1277 RETVAL_BOOL(SUCCESS == php_http_env_response_send(&r));
1278 php_http_env_response_dtor(&r);
1279 }
1280 }
1281 }
1282
1283 static zend_function_entry php_http_env_response_methods[] = {
1284 PHP_ME(HttpEnvResponse, __construct, ai_HttpEnvResponse___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
1285 PHP_ME(HttpEnvResponse, __invoke, ai_HttpEnvResponse___invoke, ZEND_ACC_PUBLIC)
1286 PHP_ME(HttpEnvResponse, setEnvRequest, ai_HttpEnvResponse_setEnvRequest, ZEND_ACC_PUBLIC)
1287 PHP_ME(HttpEnvResponse, setContentType, ai_HttpEnvResponse_setContentType, ZEND_ACC_PUBLIC)
1288 PHP_ME(HttpEnvResponse, setContentDisposition, ai_HttpEnvResponse_setContentDisposition, ZEND_ACC_PUBLIC)
1289 PHP_ME(HttpEnvResponse, setContentEncoding, ai_HttpEnvResponse_setContentEncoding, ZEND_ACC_PUBLIC)
1290 PHP_ME(HttpEnvResponse, setCacheControl, ai_HttpEnvResponse_setCacheControl, ZEND_ACC_PUBLIC)
1291 PHP_ME(HttpEnvResponse, setLastModified, ai_HttpEnvResponse_setLastModified, ZEND_ACC_PUBLIC)
1292 PHP_ME(HttpEnvResponse, isCachedByLastModified, ai_HttpEnvResponse_isCachedByLastModified, ZEND_ACC_PUBLIC)
1293 PHP_ME(HttpEnvResponse, setEtag, ai_HttpEnvResponse_setEtag, ZEND_ACC_PUBLIC)
1294 PHP_ME(HttpEnvResponse, isCachedByEtag, ai_HttpEnvResponse_isCachedByEtag, ZEND_ACC_PUBLIC)
1295 PHP_ME(HttpEnvResponse, setThrottleRate, ai_HttpEnvResponse_setThrottleRate, ZEND_ACC_PUBLIC)
1296 PHP_ME(HttpEnvResponse, send, ai_HttpEnvResponse_send, ZEND_ACC_PUBLIC)
1297 EMPTY_FUNCTION_ENTRY
1298 };
1299
1300 zend_class_entry *php_http_env_response_class_entry;
1301
1302 PHP_MINIT_FUNCTION(http_env_response)
1303 {
1304 zend_class_entry ce = {0};
1305
1306 INIT_NS_CLASS_ENTRY(ce, "http\\Env", "Response", php_http_env_response_methods);
1307 php_http_env_response_class_entry = zend_register_internal_class_ex(&ce, php_http_message_class_entry, NULL TSRMLS_CC);
1308
1309 zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_ENCODING_NONE"), PHP_HTTP_CONTENT_ENCODING_NONE TSRMLS_CC);
1310 zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_ENCODING_GZIP"), PHP_HTTP_CONTENT_ENCODING_GZIP TSRMLS_CC);
1311
1312 zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_NO"), PHP_HTTP_CACHE_NO TSRMLS_CC);
1313 zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_HIT"), PHP_HTTP_CACHE_HIT TSRMLS_CC);
1314 zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_MISS"), PHP_HTTP_CACHE_MISS TSRMLS_CC);
1315
1316 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("request"), ZEND_ACC_PROTECTED TSRMLS_CC);
1317 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentType"), ZEND_ACC_PROTECTED TSRMLS_CC);
1318 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentDisposition"), ZEND_ACC_PROTECTED TSRMLS_CC);
1319 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentEncoding"), ZEND_ACC_PROTECTED TSRMLS_CC);
1320 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("cacheControl"), ZEND_ACC_PROTECTED TSRMLS_CC);
1321 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("etag"), ZEND_ACC_PROTECTED TSRMLS_CC);
1322 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("lastModified"), ZEND_ACC_PROTECTED TSRMLS_CC);
1323 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("throttleDelay"), ZEND_ACC_PROTECTED TSRMLS_CC);
1324 zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("throttleChunk"), ZEND_ACC_PROTECTED TSRMLS_CC);
1325
1326 return SUCCESS;
1327 }
1328
1329
1330 /*
1331 * Local variables:
1332 * tab-width: 4
1333 * c-basic-offset: 4
1334 * End:
1335 * vim600: noet sw=4 ts=4 fdm=marker
1336 * vim<600: noet sw=4 ts=4
1337 */