facilitate zend_exception_set_previous() of PHP 5.3
[m6w6/ext-http] / http_api.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-2007, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_SAPI
16 #include "php_http.h"
17
18 #include "php_output.h"
19 #include "ext/standard/url.h"
20 #include "ext/standard/php_lcg.h"
21
22 #include "php_http_api.h"
23 #include "php_http_send_api.h"
24
25 #ifdef ZEND_ENGINE_2
26 # include "php_http_exception_object.h"
27 #endif
28
29 PHP_MINIT_FUNCTION(http_support)
30 {
31 HTTP_LONG_CONSTANT("HTTP_SUPPORT", HTTP_SUPPORT);
32 HTTP_LONG_CONSTANT("HTTP_SUPPORT_REQUESTS", HTTP_SUPPORT_REQUESTS);
33 HTTP_LONG_CONSTANT("HTTP_SUPPORT_MAGICMIME", HTTP_SUPPORT_MAGICMIME);
34 HTTP_LONG_CONSTANT("HTTP_SUPPORT_ENCODINGS", HTTP_SUPPORT_ENCODINGS);
35 HTTP_LONG_CONSTANT("HTTP_SUPPORT_SSLREQUESTS", HTTP_SUPPORT_SSLREQUESTS);
36 HTTP_LONG_CONSTANT("HTTP_SUPPORT_EVENTS", HTTP_SUPPORT_EVENTS);
37
38 HTTP_LONG_CONSTANT("HTTP_PARAMS_ALLOW_COMMA", HTTP_PARAMS_ALLOW_COMMA);
39 HTTP_LONG_CONSTANT("HTTP_PARAMS_ALLOW_FAILURE", HTTP_PARAMS_ALLOW_FAILURE);
40 HTTP_LONG_CONSTANT("HTTP_PARAMS_RAISE_ERROR", HTTP_PARAMS_RAISE_ERROR);
41 HTTP_LONG_CONSTANT("HTTP_PARAMS_DEFAULT", HTTP_PARAMS_DEFAULT);
42
43 return SUCCESS;
44 }
45
46 PHP_HTTP_API long _http_support(long feature)
47 {
48 long support = HTTP_SUPPORT;
49
50 #ifdef HTTP_HAVE_CURL
51 support |= HTTP_SUPPORT_REQUESTS;
52 # ifdef HTTP_HAVE_SSL
53 support |= HTTP_SUPPORT_SSLREQUESTS;
54 # endif
55 # ifdef HTTP_HAVE_EVENT
56 support |= HTTP_SUPPORT_EVENTS;
57 # endif
58 #endif
59 #ifdef HTTP_HAVE_MAGIC
60 support |= HTTP_SUPPORT_MAGICMIME;
61 #endif
62 #ifdef HTTP_HAVE_ZLIB
63 support |= HTTP_SUPPORT_ENCODINGS;
64 #endif
65
66 if (feature) {
67 return (feature == (support & feature));
68 }
69 return support;
70 }
71
72 /* char *pretty_key(char *, size_t, zend_bool, zend_bool) */
73 char *_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen)
74 {
75 size_t i;
76 int wasalpha;
77
78 if (key && key_len) {
79 if ((wasalpha = HTTP_IS_CTYPE(alpha, key[0]))) {
80 key[0] = (char) (uctitle ? HTTP_TO_CTYPE(upper, key[0]) : HTTP_TO_CTYPE(lower, key[0]));
81 }
82 for (i = 1; i < key_len; i++) {
83 if (HTTP_IS_CTYPE(alpha, key[i])) {
84 key[i] = (char) (((!wasalpha) && uctitle) ? HTTP_TO_CTYPE(upper, key[i]) : HTTP_TO_CTYPE(lower, key[i]));
85 wasalpha = 1;
86 } else {
87 if (xhyphen && (key[i] == '_')) {
88 key[i] = '-';
89 }
90 wasalpha = 0;
91 }
92 }
93 }
94 return key;
95 }
96 /* }}} */
97
98 /* {{{ http_boundary(char *, size_t) */
99 size_t _http_boundary(char *buf, size_t buf_len TSRMLS_DC)
100 {
101 return snprintf(buf, buf_len, "%lu%0.9f", (ulong) HTTP_G->request.time, (float) php_combined_lcg(TSRMLS_C));
102 }
103 /* }}} */
104
105 /* {{{ void http_error(long, long, char*) */
106 void _http_error_ex(long type TSRMLS_DC, long code, const char *format, ...)
107 {
108 va_list args;
109
110 va_start(args, format);
111 #ifdef ZEND_ENGINE_2
112 if ((type == E_THROW) || (GLOBAL_ERROR_HANDLING == EH_THROW)) {
113 char *message;
114 zend_class_entry *ce = http_exception_get_for_code(code);
115
116 http_try {
117 vspprintf(&message, 0, format, args);
118 zend_throw_exception(ce, message, code TSRMLS_CC);
119 efree(message);
120 } http_catch(GLOBAL_EXCEPTION_CLASS ? GLOBAL_EXCEPTION_CLASS : HTTP_EX_DEF_CE);
121 } else
122 #endif
123 php_verror(NULL, "", type, format, args TSRMLS_CC);
124 va_end(args);
125 }
126 /* }}} */
127
128 #ifdef ZEND_ENGINE_2
129 static inline void copy_bt_args(zval *from, zval *to TSRMLS_DC)
130 {
131 zval **args, **trace_0, *old_trace_0, *trace = NULL;
132
133 if ((trace = zend_read_property(ZEND_EXCEPTION_GET_DEFAULT(), from, "trace", lenof("trace"), 0 TSRMLS_CC))) {
134 if (Z_TYPE_P(trace) == IS_ARRAY && SUCCESS == zend_hash_index_find(Z_ARRVAL_P(trace), 0, (void *) &trace_0)) {
135 old_trace_0 = *trace_0;
136 if (Z_TYPE_PP(trace_0) == IS_ARRAY && SUCCESS == zend_hash_find(Z_ARRVAL_PP(trace_0), "args", sizeof("args"), (void *) &args)) {
137 if ((trace = zend_read_property(ZEND_EXCEPTION_GET_DEFAULT(), to, "trace", lenof("trace"), 0 TSRMLS_CC))) {
138 if (Z_TYPE_P(trace) == IS_ARRAY && SUCCESS == zend_hash_index_find(Z_ARRVAL_P(trace), 0, (void *) &trace_0)) {
139 ZVAL_ADDREF(*args);
140 add_assoc_zval(*trace_0, "args", *args);
141 }
142 }
143 }
144 }
145 }
146 }
147
148 /* {{{ zval *http_exception_wrap(zval *, zval *, zend_class_entry *) */
149 zval *_http_exception_wrap(zval *old_exception, zval *new_exception, zend_class_entry *ce TSRMLS_DC)
150 {
151 int inner = 1;
152 char *message;
153 zval *sub_exception, *tmp_exception;
154
155 if (!new_exception) {
156 MAKE_STD_ZVAL(new_exception);
157 object_init_ex(new_exception, ce);
158
159 zend_update_property(ce, new_exception, "innerException", lenof("innerException"), old_exception TSRMLS_CC);
160 copy_bt_args(old_exception, new_exception TSRMLS_CC);
161
162 sub_exception = old_exception;
163
164 while ((sub_exception = zend_read_property(Z_OBJCE_P(sub_exception), sub_exception, "innerException", lenof("innerException"), 0 TSRMLS_CC)) && Z_TYPE_P(sub_exception) == IS_OBJECT) {
165 ++inner;
166 }
167
168 spprintf(&message, 0, "Exception caused by %d inner exception(s)", inner);
169 zend_update_property_string(ZEND_EXCEPTION_GET_DEFAULT(), new_exception, "message", lenof("message"), message TSRMLS_CC);
170 efree(message);
171 } else {
172 sub_exception = new_exception;
173 tmp_exception = new_exception;
174
175 while ((tmp_exception = zend_read_property(Z_OBJCE_P(tmp_exception), tmp_exception, "innerException", lenof("innerException"), 0 TSRMLS_CC)) && Z_TYPE_P(tmp_exception) == IS_OBJECT) {
176 sub_exception = tmp_exception;
177 }
178
179 zend_update_property(Z_OBJCE_P(sub_exception), sub_exception, "innerException", lenof("innerException"), old_exception TSRMLS_CC);
180 copy_bt_args(old_exception, new_exception TSRMLS_CC);
181 copy_bt_args(old_exception, sub_exception TSRMLS_CC);
182 }
183 #if PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 3
184 Z_ADDREF_P(old_exception);
185 zend_exception_set_previous(new_exception, old_exception);
186 #endif
187 zval_ptr_dtor(&old_exception);
188 return new_exception;
189 }
190 /* }}} */
191
192 /* {{{ STATUS http_object_new(zend_object_value *, const char *, uint, http_object_new_t, zend_class_entry *, void *, void **) */
193 STATUS _http_object_new(zend_object_value *ov, const char *cname_str, uint cname_len, http_object_new_t create, zend_class_entry *parent_ce, void *intern_ptr, void **obj_ptr TSRMLS_DC)
194 {
195 zend_class_entry *ce = parent_ce;
196
197 if (cname_str && cname_len) {
198 if (!(ce = zend_fetch_class(HTTP_ZAPI_CONST_CAST(char *) cname_str, cname_len, ZEND_FETCH_CLASS_DEFAULT TSRMLS_CC))) {
199 return FAILURE;
200 }
201 if (!instanceof_function(ce, parent_ce TSRMLS_CC)) {
202 http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Class %s does not extend %s", cname_str, parent_ce->name);
203 return FAILURE;
204 }
205 }
206
207 *ov = create(ce, intern_ptr, obj_ptr TSRMLS_CC);
208 return SUCCESS;
209 }
210 /* }}} */
211 #endif /* ZEND_ENGINE_2 */
212
213 /* {{{ void http_log(char *, char *, char *) */
214 void _http_log_ex(char *file, const char *ident, const char *message TSRMLS_DC)
215 {
216 time_t now;
217 struct tm nowtm;
218 char datetime[20] = {0};
219
220 now = HTTP_G->request.time;
221 strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", php_localtime_r(&now, &nowtm));
222
223 #define HTTP_LOG_WRITE(file, type, msg) \
224 if (file && *file) { \
225 php_stream *log = php_stream_open_wrapper_ex(file, "ab", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT); \
226 \
227 if (log) { \
228 php_stream_printf(log TSRMLS_CC, "%s\t[%s]\t%s\t<%s>%s", datetime, type, msg, SG(request_info).request_uri, PHP_EOL); \
229 php_stream_close(log); \
230 } \
231 \
232 }
233
234 HTTP_LOG_WRITE(file, ident, message);
235 HTTP_LOG_WRITE(HTTP_G->log.composite, ident, message);
236 }
237 /* }}} */
238
239 static void http_ob_blackhole(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
240 {
241 *handled_output = ecalloc(1,1);
242 *handled_output_len = 0;
243 }
244
245 /* {{{ STATUS http_exit(int, char*, char*) */
246 STATUS _http_exit_ex(int status, char *header, char *body, zend_bool send_header TSRMLS_DC)
247 {
248 if ( (send_header && (SUCCESS != http_send_status_header(status, header))) ||
249 (status && (SUCCESS != http_send_status(status)))) {
250 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Failed to exit with status/header: %d - %s", status, STR_PTR(header));
251 STR_FREE(header);
252 STR_FREE(body);
253 return FAILURE;
254 }
255
256 if (!OG(ob_lock)) {
257 php_end_ob_buffers(0 TSRMLS_CC);
258 }
259 if ((SUCCESS == sapi_send_headers(TSRMLS_C)) && body) {
260 PHPWRITE(body, strlen(body));
261 }
262
263 switch (status) {
264 case 301: http_log(HTTP_G->log.redirect, "301-REDIRECT", header); break;
265 case 302: http_log(HTTP_G->log.redirect, "302-REDIRECT", header); break;
266 case 303: http_log(HTTP_G->log.redirect, "303-REDIRECT", header); break;
267 case 305: http_log(HTTP_G->log.redirect, "305-REDIRECT", header); break;
268 case 307: http_log(HTTP_G->log.redirect, "307-REDIRECT", header); break;
269 case 304: http_log(HTTP_G->log.cache, "304-CACHE", header); break;
270 case 404: http_log(HTTP_G->log.not_found, "404-NOTFOUND", NULL); break;
271 case 405: http_log(HTTP_G->log.allowed_methods, "405-ALLOWED", header); break;
272 default: http_log(NULL, header, body); break;
273 }
274
275 STR_FREE(header);
276 STR_FREE(body);
277
278 if (HTTP_G->force_exit) {
279 zend_bailout();
280 } else {
281 php_ob_set_internal_handler(http_ob_blackhole, 4096, "blackhole", 0 TSRMLS_CC);
282 }
283
284 return SUCCESS;
285 }
286 /* }}} */
287
288 /* {{{ STATUS http_check_method(char *) */
289 STATUS _http_check_method_ex(const char *method, const char *methods)
290 {
291 const char *found;
292
293 if ( (found = strstr(methods, method)) &&
294 (found == method || !HTTP_IS_CTYPE(alpha, found[-1])) &&
295 (strlen(found) >= strlen(method) && !HTTP_IS_CTYPE(alpha, found[strlen(method)]))) {
296 return SUCCESS;
297 }
298 return FAILURE;
299 }
300 /* }}} */
301
302 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
303 PHP_HTTP_API zval *_http_get_server_var_ex(const char *key, size_t key_len, zend_bool check TSRMLS_DC)
304 {
305 zval **hsv, **var;
306 char *env;
307
308 /* if available, this is a lot faster than accessing $_SERVER */
309 if (sapi_module.getenv) {
310 if ((!(env = sapi_module.getenv((char *) key, key_len TSRMLS_CC))) || (check && !*env)) {
311 return NULL;
312 }
313 if (HTTP_G->server_var) {
314 zval_ptr_dtor(&HTTP_G->server_var);
315 }
316 MAKE_STD_ZVAL(HTTP_G->server_var);
317 ZVAL_STRING(HTTP_G->server_var, env, 1);
318 return HTTP_G->server_var;
319 }
320
321 #ifdef ZEND_ENGINE_2
322 zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC);
323 #endif
324
325 if ((SUCCESS != zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void *) &hsv)) || (Z_TYPE_PP(hsv) != IS_ARRAY)) {
326 return NULL;
327 }
328 if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(hsv), HTTP_ZAPI_CONST_CAST(char *) key, key_len + 1, (void *) &var))) {
329 return NULL;
330 }
331 if (check && !((Z_TYPE_PP(var) == IS_STRING) && Z_STRVAL_PP(var) && Z_STRLEN_PP(var))) {
332 return NULL;
333 }
334 return *var;
335 }
336 /* }}} */
337
338 /* {{{ STATUS http_get_request_body(char **, size_t *) */
339 PHP_HTTP_API STATUS _http_get_request_body_ex(char **body, size_t *length, zend_bool dup TSRMLS_DC)
340 {
341 *length = 0;
342 *body = NULL;
343
344 if (SG(request_info).raw_post_data) {
345 *length = SG(request_info).raw_post_data_length;
346 *body = SG(request_info).raw_post_data;
347
348 if (dup) {
349 *body = estrndup(*body, *length);
350 }
351 return SUCCESS;
352 } else if (sapi_module.read_post && !HTTP_G->read_post_data) {
353 char buf[4096];
354 int len;
355
356 HTTP_G->read_post_data = 1;
357
358 while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
359 *body = erealloc(*body, *length + len + 1);
360 memcpy(*body + *length, buf, len);
361 *length += len;
362 (*body)[*length] = '\0';
363 if (len < (int) sizeof(buf)) {
364 break;
365 }
366 }
367
368 /* check for error */
369 if (len < 0) {
370 STR_FREE(*body);
371 *length = 0;
372 return FAILURE;
373 }
374
375 SG(request_info).raw_post_data = *body;
376 SG(request_info).raw_post_data_length = *length;
377
378 if (dup) {
379 *body = estrndup(*body, *length);
380 }
381 return SUCCESS;
382 }
383
384 return FAILURE;
385 }
386 /* }}} */
387
388 /* {{{ php_stream *http_get_request_body_stream(void) */
389 PHP_HTTP_API php_stream *_http_get_request_body_stream(TSRMLS_D)
390 {
391 php_stream *s = NULL;
392
393 if (SG(request_info).raw_post_data) {
394 s = php_stream_open_wrapper("php://input", "rb", 0, NULL);
395 } else if (sapi_module.read_post && !HTTP_G->read_post_data) {
396 HTTP_G->read_post_data = 1;
397
398 if ((s = php_stream_temp_new())) {
399 char buf[4096];
400 int len;
401
402 while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
403 php_stream_write(s, buf, len);
404 if (len < (int) sizeof(buf)) {
405 break;
406 }
407 }
408
409 if (len < 0) {
410 php_stream_close(s);
411 s = NULL;
412 } else {
413 php_stream_rewind(s);
414 }
415 }
416 }
417
418 return s;
419 }
420 /* }}} */
421
422 /* {{{ void http_parse_params_default_callback(...) */
423 PHP_HTTP_API void _http_parse_params_default_callback(void *arg, const char *key, int keylen, const char *val, int vallen TSRMLS_DC)
424 {
425 char *kdup;
426 zval tmp, *entry;
427 HashTable *ht = (HashTable *) arg;
428
429 if (ht) {
430 INIT_ZARR(tmp, ht);
431
432 if (vallen) {
433 MAKE_STD_ZVAL(entry);
434 array_init(entry);
435 if (keylen) {
436 kdup = estrndup(key, keylen);
437 add_assoc_stringl_ex(entry, kdup, keylen + 1, (char *) val, vallen, 1);
438 efree(kdup);
439 } else {
440 add_next_index_stringl(entry, (char *) val, vallen, 1);
441 }
442 add_next_index_zval(&tmp, entry);
443 } else {
444 add_next_index_stringl(&tmp, (char *) key, keylen, 1);
445 }
446 }
447 }
448 /* }}} */
449
450 /* {{{ STATUS http_parse_params(const char *, HashTable *) */
451 PHP_HTTP_API STATUS _http_parse_params_ex(const char *param, int flags, http_parse_params_callback cb, void *cb_arg TSRMLS_DC)
452 {
453 #define ST_QUOTE 1
454 #define ST_VALUE 2
455 #define ST_KEY 3
456 #define ST_ASSIGN 4
457 #define ST_ADD 5
458
459 int st = ST_KEY, keylen = 0, vallen = 0;
460 char *s, *c, *key = NULL, *val = NULL;
461
462 for(c = s = estrdup(param);;) {
463 continued:
464 #if 0
465 {
466 char *tk = NULL, *tv = NULL;
467
468 if (key) {
469 if (keylen) {
470 tk= estrndup(key, keylen);
471 } else {
472 tk = ecalloc(1, 7);
473 memcpy(tk, key, 3);
474 tk[3]='.'; tk[4]='.'; tk[5]='.';
475 }
476 }
477 if (val) {
478 if (vallen) {
479 tv = estrndup(val, vallen);
480 } else {
481 tv = ecalloc(1, 7);
482 memcpy(tv, val, 3);
483 tv[3]='.'; tv[4]='.'; tv[5]='.';
484 }
485 }
486 fprintf(stderr, "[%6s] %c \"%s=%s\"\n",
487 (
488 st == ST_QUOTE ? "QUOTE" :
489 st == ST_VALUE ? "VALUE" :
490 st == ST_KEY ? "KEY" :
491 st == ST_ASSIGN ? "ASSIGN" :
492 st == ST_ADD ? "ADD":
493 "HUH?"
494 ), *c?*c:'0', tk, tv
495 );
496 STR_FREE(tk); STR_FREE(tv);
497 }
498 #endif
499 switch (st) {
500 case ST_QUOTE:
501 quote:
502 if (*c == '"') {
503 if (*(c-1) == '\\') {
504 memmove(c-1, c, strlen(c)+1);
505 goto quote;
506 } else {
507 goto add;
508 }
509 } else {
510 if (!val) {
511 val = c;
512 }
513 if (!*c) {
514 --val;
515 st = ST_ADD;
516 }
517 }
518 break;
519
520 case ST_VALUE:
521 switch (*c) {
522 case '"':
523 if (!val) {
524 st = ST_QUOTE;
525 }
526 break;
527
528 case ' ':
529 break;
530
531 case ';':
532 case '\0':
533 goto add;
534 break;
535 case ',':
536 if (flags & HTTP_PARAMS_ALLOW_COMMA) {
537 goto add;
538 }
539 default:
540 if (!val) {
541 val = c;
542 }
543 break;
544 }
545 break;
546
547 case ST_KEY:
548 switch (*c) {
549 case ',':
550 if (flags & HTTP_PARAMS_ALLOW_COMMA) {
551 goto allow_comma;
552 }
553 case '\r':
554 case '\n':
555 case '\t':
556 case '\013':
557 case '\014':
558 goto failure;
559 break;
560
561 case ' ':
562 if (key) {
563 keylen = c - key;
564 st = ST_ASSIGN;
565 }
566 break;
567
568 case ';':
569 case '\0':
570 allow_comma:
571 if (key) {
572 keylen = c-- - key;
573 st = ST_ADD;
574 }
575 break;
576
577 case ':':
578 if (!(flags & HTTP_PARAMS_COLON_SEPARATOR)) {
579 goto not_separator;
580 }
581 if (key) {
582 keylen = c - key;
583 st = ST_VALUE;
584 } else {
585 goto failure;
586 }
587 break;
588
589 case '=':
590 if (flags & HTTP_PARAMS_COLON_SEPARATOR) {
591 goto not_separator;
592 }
593 if (key) {
594 keylen = c - key;
595 st = ST_VALUE;
596 } else {
597 goto failure;
598 }
599 break;
600
601 default:
602 not_separator:
603 if (!key) {
604 key = c;
605 }
606 break;
607 }
608 break;
609
610 case ST_ASSIGN:
611 if (*c == '=') {
612 st = ST_VALUE;
613 } else if (!*c || *c == ';' || ((flags & HTTP_PARAMS_ALLOW_COMMA) && *c == ',')) {
614 st = ST_ADD;
615 } else if (*c != ' ') {
616 goto failure;
617 }
618 break;
619
620 case ST_ADD:
621 add:
622 if (val) {
623 vallen = c - val;
624 if (st != ST_QUOTE) {
625 while (val[vallen-1] == ' ') --vallen;
626 }
627 } else {
628 val = "";
629 vallen = 0;
630 }
631
632 cb(cb_arg, key, keylen, val, vallen TSRMLS_CC);
633
634 st = ST_KEY;
635 key = val = NULL;
636 keylen = vallen = 0;
637 break;
638 }
639 if (*c) {
640 ++c;
641 } else if (st == ST_ADD) {
642 goto add;
643 } else {
644 break;
645 }
646 }
647
648 efree(s);
649 return SUCCESS;
650
651 failure:
652 if (flags & HTTP_PARAMS_RAISE_ERROR) {
653 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Unexpected character (%c) at pos %tu of %zu", *c, c-s, strlen(s));
654 }
655 if (flags & HTTP_PARAMS_ALLOW_FAILURE) {
656 if (st == ST_KEY) {
657 if (key) {
658 keylen = c - key;
659 } else {
660 key = c;
661 }
662 } else {
663 --c;
664 }
665 st = ST_ADD;
666 goto continued;
667 }
668 efree(s);
669 return FAILURE;
670 }
671 /* }}} */
672
673 /* {{{ array_join */
674 int apply_array_append_func(void *pDest HTTP_ZAPI_HASH_TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
675 {
676 int flags;
677 char *key = NULL;
678 HashTable *dst;
679 zval **data = NULL, **value = (zval **) pDest;
680
681 dst = va_arg(args, HashTable *);
682 flags = va_arg(args, int);
683
684 if ((!(flags & ARRAY_JOIN_STRONLY)) || hash_key->nKeyLength) {
685 if ((flags & ARRAY_JOIN_PRETTIFY) && hash_key->nKeyLength) {
686 key = pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 1);
687 zend_hash_find(dst, key, hash_key->nKeyLength, (void *) &data);
688 } else {
689 zend_hash_quick_find(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) &data);
690 }
691
692 ZVAL_ADDREF(*value);
693 if (data) {
694 add_next_index_zval(http_zset(IS_ARRAY, *data), *value);
695 } else if (key) {
696 zend_hash_add(dst, key, hash_key->nKeyLength, value, sizeof(zval *), NULL);
697 } else {
698 zend_hash_quick_add(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, value, sizeof(zval *), NULL);
699 }
700
701 if (key) {
702 efree(key);
703 }
704 }
705
706 return ZEND_HASH_APPLY_KEEP;
707 }
708
709 int apply_array_merge_func(void *pDest HTTP_ZAPI_HASH_TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
710 {
711 int flags;
712 char *key = NULL;
713 HashTable *dst;
714 zval **value = (zval **) pDest;
715
716 dst = va_arg(args, HashTable *);
717 flags = va_arg(args, int);
718
719 if ((!(flags & ARRAY_JOIN_STRONLY)) || hash_key->nKeyLength) {
720 ZVAL_ADDREF(*value);
721 if ((flags & ARRAY_JOIN_PRETTIFY) && hash_key->nKeyLength) {
722 key = pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 1);
723 zend_hash_update(dst, key, hash_key->nKeyLength, (void *) value, sizeof(zval *), NULL);
724 efree(key);
725 } else {
726 zend_hash_quick_update(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) value, sizeof(zval *), NULL);
727 }
728 }
729
730 return ZEND_HASH_APPLY_KEEP;
731 }
732 /* }}} */
733
734 /*
735 * Local variables:
736 * tab-width: 4
737 * c-basic-offset: 4
738 * End:
739 * vim600: noet sw=4 ts=4 fdm=marker
740 * vim<600: noet sw=4 ts=4
741 */
742