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