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