push a load of changes before holidays
[m6w6/ext-http] / php_http_curl.c
1
2 #include "php_http.h"
3 #include "php_http_request.h"
4 #include "php_http_request_pool.h"
5
6 #include <curl/curl.h>
7 #define PHP_HTTP_CURL_VERSION(x, y, z) (LIBCURL_VERSION_NUM >= (((x)<<16) + ((y)<<8) + (z)))
8
9 #ifdef PHP_HTTP_HAVE_EVENT
10 # include <event.h>
11 #endif
12
13 #include <ext/spl/spl_iterators.h>
14 #include <Zend/zend_interfaces.h>
15
16
17 typedef struct php_http_curl_request {
18 CURL *handle;
19
20 struct {
21 HashTable cache;
22
23 struct curl_slist *headers;
24 php_http_buffer_t cookies;
25
26 long redirects;
27
28 struct {
29 uint count;
30 double delay;
31 } retry;
32
33 } options;
34
35 php_http_request_progress_t progress;
36
37 } php_http_curl_request_t;
38
39 typedef struct php_http_curl_request_storage {
40 char *url;
41 char *cookiestore;
42 char errorbuffer[0x100];
43 } php_http_curl_request_storage_t;
44
45 typedef struct php_http_curl_request_pool {
46 CURLM *handle;
47
48 int unfinished; /* int because of curl_multi_perform() */
49
50 #ifdef PHP_HTTP_HAVE_EVENT
51 struct event *timeout;
52 unsigned useevents:1;
53 unsigned runsocket:1;
54 #endif
55 } php_http_curl_request_pool_t;
56
57 #ifdef ZTS
58 typedef struct php_http_curl_request_datashare_lock {
59 CURL *ch;
60 MUTEX_T mx;
61 } php_http_curl_request_datashare_lock_t;
62 #endif
63
64 typedef struct php_http_curl_request_datashare {
65 CURLSH *handle;
66
67 #ifdef ZTS
68 php_http_curl_request_datashare_lock_t *locks;
69 #endif
70 } php_http_curl_request_datashare_t;
71
72 #define PHP_HTTP_CURL_OPT_STRING(OPTION, ldiff, obdc) \
73 { \
74 char *K = #OPTION; \
75 PHP_HTTP_CURL_OPT_STRING_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION, obdc); \
76 }
77 #define PHP_HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \
78 if (!strcasecmp(key.str, keyname)) { \
79 zval *copy = cache_option(&curl->options.cache, keyname, strlen(keyname)+1, 0, php_http_zsep(IS_STRING, *param)); \
80 if (obdc) { \
81 if (SUCCESS != php_check_open_basedir(Z_STRVAL_P(copy) TSRMLS_CC)) { \
82 return FAILURE; \
83 } \
84 } \
85 curl_easy_setopt(ch, optname, Z_STRVAL_P(copy)); \
86 zval_ptr_dtor(&copy); \
87 continue; \
88 }
89 #define PHP_HTTP_CURL_OPT_LONG(OPTION, ldiff) \
90 { \
91 char *K = #OPTION; \
92 PHP_HTTP_CURL_OPT_LONG_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION); \
93 }
94 #define PHP_HTTP_CURL_OPT_LONG_EX(keyname, optname) \
95 if (!strcasecmp(key.str, keyname)) { \
96 zval *copy = php_http_zsep(IS_LONG, *param); \
97 curl_easy_setopt(ch, optname, Z_LVAL_P(copy)); \
98 zval_ptr_dtor(&copy); \
99 continue; \
100 }
101
102 static inline php_http_curl_request_storage_t *get_storage(CURL *ch) {
103 php_http_curl_request_storage_t *st = NULL;
104
105 curl_easy_getinfo(ch, CURLINFO_PRIVATE, &st);
106
107 if (!st) {
108 st = pecalloc(1, sizeof(*st), 1);
109 curl_easy_setopt(ch, CURLOPT_PRIVATE, st);
110 curl_easy_setopt(ch, CURLOPT_ERRORBUFFER, st->errorbuffer);
111 }
112
113 return st;
114 }
115
116 static size_t php_http_curl_read_callback(void *data, size_t len, size_t n, void *ctx)
117 {
118 php_http_message_body_t *body = ctx;
119
120 if (body) {
121 TSRMLS_FETCH_FROM_CTX(body->ts);
122 return php_stream_read(php_http_message_body_stream(body), data, len * n);
123 }
124 return 0;
125 }
126
127 static int php_http_curl_progress_callback(void *ctx, double dltotal, double dlnow, double ultotal, double ulnow)
128 {
129 php_http_request_t *h = ctx;
130 php_http_curl_request_t *curl = h->ctx;
131 TSRMLS_FETCH_FROM_CTX(h->ts);
132
133 curl->progress.state.dl.total = dltotal;
134 curl->progress.state.dl.now = dlnow;
135 curl->progress.state.ul.total = ultotal;
136 curl->progress.state.ul.now = ulnow;
137
138 php_http_request_progress_notify(&curl->progress TSRMLS_CC);
139
140 return 0;
141 }
142
143 static curlioerr php_http_curl_ioctl_callback(CURL *ch, curliocmd cmd, void *ctx)
144 {
145 php_http_message_body_t *body = ctx;
146
147 if (cmd != CURLIOCMD_RESTARTREAD) {
148 return CURLIOE_UNKNOWNCMD;
149 }
150
151 if (body) {
152 TSRMLS_FETCH_FROM_CTX(body->ts);
153
154 if (SUCCESS == php_stream_rewind(php_http_message_body_stream(body))) {
155 return CURLIOE_OK;
156 }
157 }
158
159 return CURLIOE_FAILRESTART;
160 }
161
162 static int php_http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size_t length, void *ctx)
163 {
164 php_http_request_t *h = ctx;
165 php_http_curl_request_t *curl = h->ctx;
166 unsigned flags = 0;
167
168 /* process data */
169 switch (type) {
170 case CURLINFO_HEADER_IN:
171 case CURLINFO_DATA_IN:
172 case CURLINFO_HEADER_OUT:
173 case CURLINFO_DATA_OUT:
174 php_http_buffer_append(h->buffer, data, length);
175
176 if (curl->options.redirects) {
177 flags |= PHP_HTTP_MESSAGE_PARSER_EMPTY_REDIRECTS;
178 }
179
180 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE == php_http_message_parser_parse(h->parser, h->buffer, flags, &h->message)) {
181 return -1;
182 }
183 break;
184 default:
185 break;
186 }
187
188 #if 0
189 /* debug */
190 _dpf(type, data, length);
191 #endif
192
193 return 0;
194 }
195
196 static int php_http_curl_dummy_callback(char *data, size_t n, size_t l, void *s)
197 {
198 return n*l;
199 }
200
201 static STATUS php_http_curl_request_prepare(php_http_request_t *h, php_http_request_method_t meth, const char *url, php_http_message_body_t *body)
202 {
203 php_http_curl_request_t *curl = h->ctx;
204 php_http_curl_request_storage_t *storage = get_storage(curl->handle);
205 TSRMLS_FETCH_FROM_CTX(h->ts);
206
207 storage->errorbuffer[0] = '\0';
208 if (storage->url) {
209 pefree(storage->url, 1);
210 }
211 storage->url = pestrdup(url, 1);
212 curl_easy_setopt(curl->handle, CURLOPT_URL, storage->url);
213
214 /* request method */
215 switch (meth) {
216 case PHP_HTTP_GET:
217 curl_easy_setopt(curl->handle, CURLOPT_HTTPGET, 1L);
218 break;
219
220 case PHP_HTTP_HEAD:
221 curl_easy_setopt(curl->handle, CURLOPT_NOBODY, 1L);
222 break;
223
224 case PHP_HTTP_POST:
225 curl_easy_setopt(curl->handle, CURLOPT_POST, 1L);
226 break;
227
228 case PHP_HTTP_PUT:
229 curl_easy_setopt(curl->handle, CURLOPT_UPLOAD, 1L);
230 break;
231
232 default: {
233 const char *name = php_http_request_method_name(meth);
234
235 if (name) {
236 curl_easy_setopt(curl->handle, CURLOPT_CUSTOMREQUEST, name);
237 } else {
238 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_METHOD, "Unsupported request method: %d (%s)", meth, url);
239 return FAILURE;
240 }
241 break;
242 }
243 }
244
245 /* attach request body */
246 if (body) {
247 /* RFC2616, section 4.3 (para. 4) states that »a message-body MUST NOT be included in a request if the
248 * specification of the request method (section 5.1.1) does not allow sending an entity-body in request.«
249 * Following the clause in section 5.1.1 (para. 2) that request methods »MUST be implemented with the
250 * same semantics as those specified in section 9« reveal that not any single defined HTTP/1.1 method
251 * does not allow a request body.
252 */
253 switch (meth) {
254 default: {
255 size_t body_size = php_http_message_body_size(body);
256
257 curl_easy_setopt(curl->handle, CURLOPT_IOCTLDATA, body);
258 curl_easy_setopt(curl->handle, CURLOPT_READDATA, body);
259 curl_easy_setopt(curl->handle, CURLOPT_INFILESIZE, body_size);
260 curl_easy_setopt(curl->handle, CURLOPT_POSTFIELDSIZE, body_size);
261 break;
262 }
263 }
264 }
265
266 return SUCCESS;
267 }
268
269 static void php_http_curl_request_pool_responsehandler(php_http_request_pool_t *pool)
270 {
271 int remaining = 0;
272 zval **requests;
273 php_http_curl_request_pool_t *curl = pool->ctx;
274 TSRMLS_FETCH_FROM_CTX(pool->ts);
275
276 do {
277 CURLMsg *msg = curl_multi_info_read(curl->handle, &remaining);
278
279 if (msg && CURLMSG_DONE == msg->msg) {
280 zval **request;
281
282 if (CURLE_OK != msg->data.result) {
283 php_http_curl_request_storage_t *st = get_storage(msg->easy_handle);
284 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(msg->data.result), STR_PTR(st->errorbuffer), STR_PTR(st->url));
285 }
286
287 php_http_request_pool_requests(pool, &requests, NULL);
288 for (request = requests; *request; ++request) {
289 php_http_request_object_t *obj = zend_object_store_get_object(*request TSRMLS_CC);
290
291 if (msg->easy_handle == ((php_http_curl_request_t *) (obj->request->ctx))->handle) {
292 Z_ADDREF_PP(request);
293 zend_llist_add_element(&pool->requests.finished, request);
294 php_http_request_object_responsehandler(obj, *request TSRMLS_CC);
295 }
296
297 zval_ptr_dtor(request);
298 }
299 efree(requests);
300 }
301 } while (remaining);
302 }
303
304
305 #ifdef PHP_HTTP_HAVE_EVENT
306
307 typedef struct php_http_request_pool_event {
308 struct event evnt;
309 php_http_request_pool_t *pool;
310 } php_http_request_pool_event_t;
311
312 static inline int etoca(short action) {
313 switch (action & (EV_READ|EV_WRITE)) {
314 case EV_READ:
315 return CURL_CSELECT_IN;
316 break;
317 case EV_WRITE:
318 return CURL_CSELECT_OUT;
319 break;
320 case EV_READ|EV_WRITE:
321 return CURL_CSELECT_IN|CURL_CSELECT_OUT;
322 break;
323 default:
324 return 0;
325 }
326 }
327
328 static void php_http_curl_request_pool_timeout_callback(int socket, short action, void *event_data)
329 {
330 php_http_request_pool_t *pool = event_data;
331 php_http_curl_request_pool_t *curl = pool->ctx;
332
333 #if DBG_EVENTS
334 fprintf(stderr, "T");
335 #endif
336 if (curl->useevents) {
337 CURLMcode rc;
338 TSRMLS_FETCH_FROM_CTX(pool->ts);
339
340 while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket_action(curl->handle, socket, etoca(action), &curl->unfinished)));
341
342 if (CURLM_OK != rc) {
343 php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "%s", curl_multi_strerror(rc));
344 }
345
346 php_http_curl_request_pool_responsehandler(pool);
347 }
348 }
349
350 static void php_http_curl_request_pool_event_callback(int socket, short action, void *event_data)
351 {
352 php_http_request_pool_t *pool = event_data;
353 php_http_curl_request_pool_t *curl = pool->ctx;
354
355 #if DBG_EVENTS
356 fprintf(stderr, "E");
357 #endif
358 if (curl->useevents) {
359 CURLMcode rc = CURLE_OK;
360 TSRMLS_FETCH_FROM_CTX(pool->ts);
361
362 while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket_action(curl->handle, socket, etoca(action), &curl->unfinished)));
363
364 if (CURLM_OK != rc) {
365 php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "%s", curl_multi_strerror(rc));
366 }
367
368 php_http_curl_request_pool_responsehandler(pool);
369
370 /* remove timeout if there are no transfers left */
371 if (!curl->unfinished && event_initialized(curl->timeout) && event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
372 event_del(curl->timeout);
373 }
374 }
375 }
376
377 static int php_http_curl_request_pool_socket_callback(CURL *easy, curl_socket_t sock, int action, void *socket_data, void *assign_data)
378 {
379 php_http_request_pool_t *pool = socket_data;
380 php_http_curl_request_pool_t *curl = pool->ctx;
381
382 #if DBG_EVENTS
383 fprintf(stderr, "S");
384 #endif
385 if (curl->useevents) {
386 int events = EV_PERSIST;
387 php_http_request_pool_event_t *ev = assign_data;
388 TSRMLS_FETCH_FROM_CTX(pool->ts);
389
390 if (!ev) {
391 ev = ecalloc(1, sizeof(php_http_request_pool_event_t));
392 ev->pool = pool;
393 curl_multi_assign(curl->handle, sock, ev);
394 event_base_set(PHP_HTTP_G->request_pool.event_base, &ev->evnt);
395 } else {
396 event_del(&ev->evnt);
397 }
398
399 switch (action) {
400 case CURL_POLL_IN:
401 events |= EV_READ;
402 break;
403 case CURL_POLL_OUT:
404 events |= EV_WRITE;
405 break;
406 case CURL_POLL_INOUT:
407 events |= EV_READ|EV_WRITE;
408 break;
409
410 case CURL_POLL_REMOVE:
411 efree(ev);
412 case CURL_POLL_NONE:
413 return 0;
414
415 default:
416 php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "Unknown socket action %d", action);
417 return -1;
418 }
419
420 event_set(&ev->evnt, sock, events, php_http_curl_request_pool_event_callback, pool);
421 event_add(&ev->evnt, NULL);
422 }
423
424 return 0;
425 }
426
427 static void php_http_curl_request_pool_timer_callback(CURLM *multi, long timeout_ms, void *timer_data)
428 {
429 php_http_request_pool_t *pool = timer_data;
430 php_http_curl_request_pool_t *curl = pool->ctx;
431
432 #if DBG_EVENTS
433 fprintf(stderr, "%ld", timeout_ms);
434 #endif
435 if (curl->useevents) {
436
437 if (timeout_ms < 0) {
438 php_http_curl_request_pool_timeout_callback(CURL_SOCKET_TIMEOUT, CURL_CSELECT_IN|CURL_CSELECT_OUT, pool);
439 } else if (timeout_ms > 0 || !event_initialized(curl->timeout) || !event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
440 struct timeval timeout;
441 TSRMLS_FETCH_FROM_CTX(pool->ts);
442
443 if (!event_initialized(curl->timeout)) {
444 event_set(curl->timeout, -1, 0, php_http_curl_request_pool_timeout_callback, pool);
445 event_base_set(PHP_HTTP_G->request_pool.event_base, curl->timeout);
446 } else if (event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
447 event_del(curl->timeout);
448 }
449
450 timeout.tv_sec = timeout_ms / 1000;
451 timeout.tv_usec = (timeout_ms % 1000) * 1000;
452
453 event_add(curl->timeout, &timeout);
454 }
455 }
456 }
457
458 #endif /* HAVE_EVENT */
459
460
461 static inline zval *cache_option(HashTable *cache, char *key, size_t keylen, ulong h, zval *opt)
462 {
463 Z_ADDREF_P(opt);
464
465 if (h) {
466 zend_hash_quick_update(cache, key, keylen, h, &opt, sizeof(zval *), NULL);
467 } else {
468 zend_hash_update(cache, key, keylen, &opt, sizeof(zval *), NULL);
469 }
470
471 return opt;
472 }
473
474 static inline zval *get_option(HashTable *cache, HashTable *options, char *key, size_t keylen, int type)
475 {
476 if (options) {
477 zval **zoption;
478 ulong h = zend_hash_func(key, keylen);
479
480 if (SUCCESS == zend_hash_quick_find(options, key, keylen, h, (void *) &zoption)) {
481 zval *option = php_http_zsep(type, *zoption);
482
483 if (cache) {
484 zval *cached = cache_option(cache, key, keylen, h, option);
485
486 zval_ptr_dtor(&option);
487 return cached;
488 }
489 return option;
490 }
491 }
492
493 return NULL;
494 }
495
496 static STATUS set_options(php_http_request_t *h, HashTable *options)
497 {
498 zval *zoption;
499 int range_req = 0;
500 php_http_curl_request_t *curl = h->ctx;
501 CURL *ch = curl->handle;
502
503 /* proxy */
504 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyhost"), IS_STRING))) {
505 curl_easy_setopt(ch, CURLOPT_PROXY, Z_STRVAL_P(zoption));
506 /* type */
507 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxytype"), IS_LONG))) {
508 curl_easy_setopt(ch, CURLOPT_PROXYTYPE, Z_LVAL_P(zoption));
509 }
510 /* port */
511 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyport"), IS_LONG))) {
512 curl_easy_setopt(ch, CURLOPT_PROXYPORT, Z_LVAL_P(zoption));
513 }
514 /* user:pass */
515 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyauth"), IS_STRING)) && Z_STRLEN_P(zoption)) {
516 curl_easy_setopt(ch, CURLOPT_PROXYUSERPWD, Z_STRVAL_P(zoption));
517 }
518 /* auth method */
519 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyauthtype"), IS_LONG))) {
520 curl_easy_setopt(ch, CURLOPT_PROXYAUTH, Z_LVAL_P(zoption));
521 }
522 /* tunnel */
523 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxytunnel"), IS_BOOL)) && Z_BVAL_P(zoption)) {
524 curl_easy_setopt(ch, CURLOPT_HTTPPROXYTUNNEL, 1L);
525 }
526 }
527 #if PHP_HTTP_CURL_VERSION(7,19,4)
528 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("noproxy"), IS_STRING))) {
529 curl_easy_setopt(ch, CURLOPT_NOPROXY, Z_STRVAL_P(zoption));
530 }
531 #endif
532
533 /* dns */
534 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("dns_cache_timeout"), IS_LONG))) {
535 curl_easy_setopt(ch, CURLOPT_DNS_CACHE_TIMEOUT, Z_LVAL_P(zoption));
536 }
537 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("ipresolve"), IS_LONG)) && Z_LVAL_P(zoption)) {
538 curl_easy_setopt(ch, CURLOPT_IPRESOLVE, Z_LVAL_P(zoption));
539 }
540
541 /* limits */
542 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("low_speed_limit"), IS_LONG))) {
543 curl_easy_setopt(ch, CURLOPT_LOW_SPEED_LIMIT, Z_LVAL_P(zoption));
544 }
545 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("low_speed_time"), IS_LONG))) {
546 curl_easy_setopt(ch, CURLOPT_LOW_SPEED_TIME, Z_LVAL_P(zoption));
547 }
548 /* LSF weirdance
549 if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_send_speed"), IS_LONG))) {
550 curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
551 }
552 if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_recv_speed"), IS_LONG))) {
553 curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
554 }
555 */
556 /* crashes
557 if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("maxconnects"), IS_LONG))) {
558 curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, Z_LVAL_P(zoption));
559 } */
560 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("fresh_connect"), IS_BOOL)) && Z_BVAL_P(zoption)) {
561 curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 1L);
562 }
563 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("forbid_reuse"), IS_BOOL)) && Z_BVAL_P(zoption)) {
564 curl_easy_setopt(ch, CURLOPT_FORBID_REUSE, 1L);
565 }
566
567 /* outgoing interface */
568 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("interface"), IS_STRING))) {
569 curl_easy_setopt(ch, CURLOPT_INTERFACE, Z_STRVAL_P(zoption));
570
571 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("portrange"), IS_ARRAY))) {
572 zval **prs, **pre;
573
574 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
575 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &prs)) {
576 zend_hash_move_forward(Z_ARRVAL_P(zoption));
577 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &pre)) {
578 zval *prs_cpy = php_http_zsep(IS_LONG, *prs);
579 zval *pre_cpy = php_http_zsep(IS_LONG, *pre);
580
581 if (Z_LVAL_P(prs_cpy) && Z_LVAL_P(pre_cpy)) {
582 curl_easy_setopt(ch, CURLOPT_LOCALPORT, MIN(Z_LVAL_P(prs_cpy), Z_LVAL_P(pre_cpy)));
583 curl_easy_setopt(ch, CURLOPT_LOCALPORTRANGE, labs(Z_LVAL_P(prs_cpy)-Z_LVAL_P(pre_cpy))+1L);
584 }
585 zval_ptr_dtor(&prs_cpy);
586 zval_ptr_dtor(&pre_cpy);
587 }
588 }
589 }
590 }
591
592 /* another port */
593 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("port"), IS_LONG))) {
594 curl_easy_setopt(ch, CURLOPT_PORT, Z_LVAL_P(zoption));
595 }
596
597 /* RFC4007 zone_id */
598 #if PHP_HTTP_CURL_VERSION(7,19,0)
599 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("address_scope"), IS_LONG))) {
600 curl_easy_setopt(ch, CURLOPT_ADDRESS_SCOPE, Z_LVAL_P(zoption));
601 }
602 #endif
603
604 /* auth */
605 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("httpauth"), IS_STRING)) && Z_STRLEN_P(zoption)) {
606 curl_easy_setopt(ch, CURLOPT_USERPWD, Z_STRVAL_P(zoption));
607 }
608 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("httpauthtype"), IS_LONG))) {
609 curl_easy_setopt(ch, CURLOPT_HTTPAUTH, Z_LVAL_P(zoption));
610 }
611
612 /* redirects, defaults to 0 */
613 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("redirect"), IS_LONG))) {
614 curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1L : 0L);
615 curl_easy_setopt(ch, CURLOPT_MAXREDIRS, curl->options.redirects = Z_LVAL_P(zoption));
616 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("unrestrictedauth"), IS_BOOL))) {
617 curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
618 }
619 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("postredir"), IS_BOOL))) {
620 #if PHP_HTTP_CURL_VERSION(7,19,1)
621 curl_easy_setopt(ch, CURLOPT_POSTREDIR, Z_BVAL_P(zoption) ? 1L : 0L);
622 #else
623 curl_easy_setopt(ch, CURLOPT_POST301, Z_BVAL_P(zoption) ? 1L : 0L);
624 #endif
625 }
626 } else {
627 curl->options.redirects = 0;
628 }
629
630 /* retries, defaults to 0 */
631 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("retrycount"), IS_LONG))) {
632 curl->options.retry.count = Z_LVAL_P(zoption);
633 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("retrydelay"), IS_DOUBLE))) {
634 curl->options.retry.delay = Z_DVAL_P(zoption);
635 } else {
636 curl->options.retry.delay = 0;
637 }
638 } else {
639 curl->options.retry.count = 0;
640 }
641
642 /* referer */
643 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("referer"), IS_STRING)) && Z_STRLEN_P(zoption)) {
644 curl_easy_setopt(ch, CURLOPT_REFERER, Z_STRVAL_P(zoption));
645 }
646
647 /* useragent, default "PECL::HTTP/version (PHP/version)" */
648 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("useragent"), IS_STRING))) {
649 /* allow to send no user agent, not even default one */
650 if (Z_STRLEN_P(zoption)) {
651 curl_easy_setopt(ch, CURLOPT_USERAGENT, Z_STRVAL_P(zoption));
652 } else {
653 curl_easy_setopt(ch, CURLOPT_USERAGENT, NULL);
654 }
655 }
656
657 /* resume */
658 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("resume"), IS_LONG)) && (Z_LVAL_P(zoption) > 0)) {
659 range_req = 1;
660 curl_easy_setopt(ch, CURLOPT_RESUME_FROM, Z_LVAL_P(zoption));
661 }
662 /* or range of kind array(array(0,499), array(100,1499)) */
663 else if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("range"), IS_ARRAY)) && zend_hash_num_elements(Z_ARRVAL_P(zoption))) {
664 HashPosition pos1, pos2;
665 zval **rr, **rb, **re;
666 php_http_buffer_t rs;
667
668 php_http_buffer_init(&rs);
669 FOREACH_VAL(pos1, zoption, rr) {
670 if (Z_TYPE_PP(rr) == IS_ARRAY) {
671 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(rr), &pos2);
672 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &rb, &pos2)) {
673 zend_hash_move_forward_ex(Z_ARRVAL_PP(rr), &pos2);
674 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &re, &pos2)) {
675 if ( ((Z_TYPE_PP(rb) == IS_LONG) || ((Z_TYPE_PP(rb) == IS_STRING) && is_numeric_string(Z_STRVAL_PP(rb), Z_STRLEN_PP(rb), NULL, NULL, 1))) &&
676 ((Z_TYPE_PP(re) == IS_LONG) || ((Z_TYPE_PP(re) == IS_STRING) && is_numeric_string(Z_STRVAL_PP(re), Z_STRLEN_PP(re), NULL, NULL, 1)))) {
677 zval *rbl = php_http_zsep(IS_LONG, *rb);
678 zval *rel = php_http_zsep(IS_LONG, *re);
679
680 if ((Z_LVAL_P(rbl) >= 0) && (Z_LVAL_P(rel) >= 0)) {
681 php_http_buffer_appendf(&rs, "%ld-%ld,", Z_LVAL_P(rbl), Z_LVAL_P(rel));
682 }
683 zval_ptr_dtor(&rbl);
684 zval_ptr_dtor(&rel);
685 }
686 }
687 }
688 }
689 }
690
691 if (PHP_HTTP_BUFFER_LEN(&rs)) {
692 zval *cached_range;
693
694 /* ditch last comma */
695 PHP_HTTP_BUFFER_VAL(&rs)[PHP_HTTP_BUFFER_LEN(&rs)-- -1] = '\0';
696 /* cache string */
697 MAKE_STD_ZVAL(cached_range);
698 ZVAL_STRINGL(cached_range, PHP_HTTP_BUFFER_VAL(&rs), PHP_HTTP_BUFFER_LEN(&rs), 0);
699 curl_easy_setopt(ch, CURLOPT_RANGE, Z_STRVAL_P(cache_option(&curl->options.cache, ZEND_STRS("range"), 0, cached_range)));
700 zval_ptr_dtor(&cached_range);
701 }
702 }
703
704 /* additional headers, array('name' => 'value') */
705 if (curl->options.headers) {
706 curl_slist_free_all(curl->options.headers);
707 curl->options.headers = NULL;
708 }
709 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("headers"), IS_ARRAY))) {
710 php_http_array_hashkey_t header_key = php_http_array_hashkey_init(0);
711 zval **header_val;
712 HashPosition pos;
713 php_http_buffer_t header;
714
715 php_http_buffer_init(&header);
716 FOREACH_KEYVAL(pos, zoption, header_key, header_val) {
717 if (header_key.type == HASH_KEY_IS_STRING) {
718 zval *header_cpy = php_http_zsep(IS_STRING, *header_val);
719
720 if (!strcasecmp(header_key.str, "range")) {
721 range_req = 1;
722 }
723
724 php_http_buffer_appendf(&header, "%s: %s", header_key.str, Z_STRVAL_P(header_cpy));
725 php_http_buffer_fix(&header);
726 curl->options.headers = curl_slist_append(curl->options.headers, PHP_HTTP_BUFFER_VAL(&header));
727 php_http_buffer_reset(&header);
728
729 zval_ptr_dtor(&header_cpy);
730 }
731 }
732 php_http_buffer_dtor(&header);
733 }
734 /* etag */
735 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("etag"), IS_STRING)) && Z_STRLEN_P(zoption)) {
736 zend_bool is_quoted = !((Z_STRVAL_P(zoption)[0] != '"') || (Z_STRVAL_P(zoption)[Z_STRLEN_P(zoption)-1] != '"'));
737 php_http_buffer_t header;
738
739 php_http_buffer_init(&header);
740 php_http_buffer_appendf(&header, is_quoted?"%s: %s":"%s: \"%s\"", range_req?"If-Match":"If-None-Match", Z_STRVAL_P(zoption));
741 php_http_buffer_fix(&header);
742 curl->options.headers = curl_slist_append(curl->options.headers, PHP_HTTP_BUFFER_VAL(&header));
743 php_http_buffer_dtor(&header);
744 }
745 /* compression */
746 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("compress"), IS_BOOL)) && Z_LVAL_P(zoption)) {
747 curl->options.headers = curl_slist_append(curl->options.headers, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
748 }
749 curl_easy_setopt(ch, CURLOPT_HTTPHEADER, curl->options.headers);
750
751 /* lastmodified */
752 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("lastmodified"), IS_LONG))) {
753 if (Z_LVAL_P(zoption)) {
754 if (Z_LVAL_P(zoption) > 0) {
755 curl_easy_setopt(ch, CURLOPT_TIMEVALUE, Z_LVAL_P(zoption));
756 } else {
757 curl_easy_setopt(ch, CURLOPT_TIMEVALUE, (long) PHP_HTTP_G->env.request.time + Z_LVAL_P(zoption));
758 }
759 curl_easy_setopt(ch, CURLOPT_TIMECONDITION, (long) (range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE));
760 } else {
761 curl_easy_setopt(ch, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
762 }
763 }
764
765 /* cookies, array('name' => 'value') */
766 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookies"), IS_ARRAY))) {
767 php_http_buffer_dtor(&curl->options.cookies);
768 if (zend_hash_num_elements(Z_ARRVAL_P(zoption))) {
769 zval *urlenc_cookies = NULL;
770 /* check whether cookies should not be urlencoded; default is to urlencode them */
771 if ((!(urlenc_cookies = get_option(&curl->options.cache, options, ZEND_STRS("encodecookies"), IS_BOOL))) || Z_BVAL_P(urlenc_cookies)) {
772 if (SUCCESS == php_http_url_encode_hash_recursive(HASH_OF(zoption), &curl->options.cookies, "; ", lenof("; "), NULL, 0 TSRMLS_CC)) {
773 php_http_buffer_fix(&curl->options.cookies);
774 curl_easy_setopt(ch, CURLOPT_COOKIE, curl->options.cookies.data);
775 }
776 } else {
777 HashPosition pos;
778 php_http_array_hashkey_t cookie_key = php_http_array_hashkey_init(0);
779 zval **cookie_val;
780
781 FOREACH_KEYVAL(pos, zoption, cookie_key, cookie_val) {
782 if (cookie_key.type == HASH_KEY_IS_STRING) {
783 zval *val = php_http_zsep(IS_STRING, *cookie_val);
784 php_http_buffer_appendf(&curl->options.cookies, "%s=%s; ", cookie_key.str, Z_STRVAL_P(val));
785 zval_ptr_dtor(&val);
786 }
787 }
788
789 php_http_buffer_fix(&curl->options.cookies);
790 if (PHP_HTTP_BUFFER_LEN(&curl->options.cookies)) {
791 curl_easy_setopt(ch, CURLOPT_COOKIE, PHP_HTTP_BUFFER_VAL(&curl->options.cookies));
792 }
793 }
794 }
795 }
796
797 /* don't load session cookies from cookiestore */
798 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookiesession"), IS_BOOL)) && Z_BVAL_P(zoption)) {
799 curl_easy_setopt(ch, CURLOPT_COOKIESESSION, 1L);
800 }
801
802 /* cookiestore, read initial cookies from that file and store cookies back into that file */
803 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookiestore"), IS_STRING))) {
804 php_http_curl_request_storage_t *storage = get_storage(curl->handle);
805
806 if (Z_STRLEN_P(zoption)) {
807 if (SUCCESS != php_check_open_basedir(Z_STRVAL_P(zoption) TSRMLS_CC)) {
808 return FAILURE;
809 }
810 }
811 if (storage->cookiestore) {
812 pefree(storage->cookiestore, 1);
813 }
814 storage->cookiestore = pestrndup(Z_STRVAL_P(zoption), Z_STRLEN_P(zoption), 1);
815 curl_easy_setopt(ch, CURLOPT_COOKIEFILE, storage->cookiestore);
816 curl_easy_setopt(ch, CURLOPT_COOKIEJAR, storage->cookiestore);
817 }
818
819 /* maxfilesize */
820 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("maxfilesize"), IS_LONG))) {
821 curl_easy_setopt(ch, CURLOPT_MAXFILESIZE, Z_LVAL_P(zoption));
822 }
823
824 /* http protocol */
825 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("protocol"), IS_LONG))) {
826 curl_easy_setopt(ch, CURLOPT_HTTP_VERSION, Z_LVAL_P(zoption));
827 }
828
829 /* timeout, defaults to 0 */
830 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("timeout"), IS_DOUBLE))) {
831 curl_easy_setopt(ch, CURLOPT_TIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000));
832 }
833 /* connecttimeout, defaults to 0 */
834 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("connecttimeout"), IS_DOUBLE))) {
835 curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000));
836 }
837
838 /* ssl */
839 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("ssl"), IS_ARRAY))) {
840 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
841 zval **param;
842 HashPosition pos;
843
844 FOREACH_KEYVAL(pos, zoption, key, param) {
845 if (key.type == HASH_KEY_IS_STRING) {
846 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERT, 0, 1);
847 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTTYPE, 0, 0);
848 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTPASSWD, 0, 0);
849
850 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEY, 0, 0);
851 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYTYPE, 0, 0);
852 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYPASSWD, 0, 0);
853
854 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLENGINE, 0, 0);
855 PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSLVERSION, 0);
856
857 PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYPEER, 1);
858 PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYHOST, 1);
859 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSL_CIPHER_LIST, 1, 0);
860
861 PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAINFO, -3, 1);
862 PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAPATH, -3, 1);
863 PHP_HTTP_CURL_OPT_STRING(CURLOPT_RANDOM_FILE, -3, 1);
864 PHP_HTTP_CURL_OPT_STRING(CURLOPT_EGDSOCKET, -3, 1);
865 #if PHP_HTTP_CURL_VERSION(7,19,0)
866 PHP_HTTP_CURL_OPT_STRING(CURLOPT_ISSUERCERT, -3, 1);
867 #if defined(PHP_HTTP_HAVE_OPENSSL)
868 PHP_HTTP_CURL_OPT_STRING(CURLOPT_CRLFILE, -3, 1);
869 #endif
870 #endif
871 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
872 PHP_HTTP_CURL_OPT_LONG(CURLOPT_CERTINFO, -3);
873 #endif
874 }
875 }
876 }
877 return SUCCESS;
878 }
879
880 static STATUS get_info(CURL *ch, HashTable *info)
881 {
882 char *c;
883 long l;
884 double d;
885 struct curl_slist *s, *p;
886 zval *subarray, array;
887 INIT_PZVAL_ARRAY(&array, info);
888
889 /* BEGIN::CURLINFO */
890 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_EFFECTIVE_URL, &c)) {
891 add_assoc_string_ex(&array, "effective_url", sizeof("effective_url"), c ? c : "", 1);
892 }
893 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &l)) {
894 add_assoc_long_ex(&array, "response_code", sizeof("response_code"), l);
895 }
896 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_TOTAL_TIME, &d)) {
897 add_assoc_double_ex(&array, "total_time", sizeof("total_time"), d);
898 }
899 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NAMELOOKUP_TIME, &d)) {
900 add_assoc_double_ex(&array, "namelookup_time", sizeof("namelookup_time"), d);
901 }
902 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONNECT_TIME, &d)) {
903 add_assoc_double_ex(&array, "connect_time", sizeof("connect_time"), d);
904 }
905 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRETRANSFER_TIME, &d)) {
906 add_assoc_double_ex(&array, "pretransfer_time", sizeof("pretransfer_time"), d);
907 }
908 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_UPLOAD, &d)) {
909 add_assoc_double_ex(&array, "size_upload", sizeof("size_upload"), d);
910 }
911 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_DOWNLOAD, &d)) {
912 add_assoc_double_ex(&array, "size_download", sizeof("size_download"), d);
913 }
914 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_DOWNLOAD, &d)) {
915 add_assoc_double_ex(&array, "speed_download", sizeof("speed_download"), d);
916 }
917 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_UPLOAD, &d)) {
918 add_assoc_double_ex(&array, "speed_upload", sizeof("speed_upload"), d);
919 }
920 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HEADER_SIZE, &l)) {
921 add_assoc_long_ex(&array, "header_size", sizeof("header_size"), l);
922 }
923 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REQUEST_SIZE, &l)) {
924 add_assoc_long_ex(&array, "request_size", sizeof("request_size"), l);
925 }
926 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_VERIFYRESULT, &l)) {
927 add_assoc_long_ex(&array, "ssl_verifyresult", sizeof("ssl_verifyresult"), l);
928 }
929 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_FILETIME, &l)) {
930 add_assoc_long_ex(&array, "filetime", sizeof("filetime"), l);
931 }
932 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) {
933 add_assoc_double_ex(&array, "content_length_download", sizeof("content_length_download"), d);
934 }
935 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_LENGTH_UPLOAD, &d)) {
936 add_assoc_double_ex(&array, "content_length_upload", sizeof("content_length_upload"), d);
937 }
938 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_STARTTRANSFER_TIME, &d)) {
939 add_assoc_double_ex(&array, "starttransfer_time", sizeof("starttransfer_time"), d);
940 }
941 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_TYPE, &c)) {
942 add_assoc_string_ex(&array, "content_type", sizeof("content_type"), c ? c : "", 1);
943 }
944 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_TIME, &d)) {
945 add_assoc_double_ex(&array, "redirect_time", sizeof("redirect_time"), d);
946 }
947 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_COUNT, &l)) {
948 add_assoc_long_ex(&array, "redirect_count", sizeof("redirect_count"), l);
949 }
950 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTP_CONNECTCODE, &l)) {
951 add_assoc_long_ex(&array, "connect_code", sizeof("connect_code"), l);
952 }
953 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTPAUTH_AVAIL, &l)) {
954 add_assoc_long_ex(&array, "httpauth_avail", sizeof("httpauth_avail"), l);
955 }
956 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PROXYAUTH_AVAIL, &l)) {
957 add_assoc_long_ex(&array, "proxyauth_avail", sizeof("proxyauth_avail"), l);
958 }
959 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_OS_ERRNO, &l)) {
960 add_assoc_long_ex(&array, "os_errno", sizeof("os_errno"), l);
961 }
962 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NUM_CONNECTS, &l)) {
963 add_assoc_long_ex(&array, "num_connects", sizeof("num_connects"), l);
964 }
965 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_ENGINES, &s)) {
966 MAKE_STD_ZVAL(subarray);
967 array_init(subarray);
968 for (p = s; p; p = p->next) {
969 if (p->data) {
970 add_next_index_string(subarray, p->data, 1);
971 }
972 }
973 add_assoc_zval_ex(&array, "ssl_engines", sizeof("ssl_engines"), subarray);
974 curl_slist_free_all(s);
975 }
976 #if PHP_HTTP_CURL_VERSION(7,14,1)
977 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_COOKIELIST, &s)) {
978 MAKE_STD_ZVAL(subarray);
979 array_init(subarray);
980 for (p = s; p; p = p->next) {
981 if (p->data) {
982 add_next_index_string(subarray, p->data, 1);
983 }
984 }
985 add_assoc_zval_ex(&array, "cookies", sizeof("cookies"), subarray);
986 curl_slist_free_all(s);
987 }
988 #endif
989 #if PHP_HTTP_CURL_VERSION(7,18,2)
990 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_URL, &c)) {
991 add_assoc_string_ex(&array, "redirect_url", sizeof("redirect_url"), c ? c : "", 1);
992 }
993 #endif
994 #if PHP_HTTP_CURL_VERSION(7,19,0)
995 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_IP, &c)) {
996 add_assoc_string_ex(&array, "primary_ip", sizeof("primary_ip"), c ? c : "", 1);
997 }
998 #endif
999 #if PHP_HTTP_CURL_VERSION(7,19,0)
1000 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_APPCONNECT_TIME, &d)) {
1001 add_assoc_double_ex(&array, "appconnect_time", sizeof("appconnect_time"), d);
1002 }
1003 #endif
1004 #if PHP_HTTP_CURL_VERSION(7,19,4)
1005 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONDITION_UNMET, &l)) {
1006 add_assoc_long_ex(&array, "condition_unmet", sizeof("condition_unmet"), l);
1007 }
1008 #endif
1009 /* END::CURLINFO */
1010 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
1011 {
1012 int i;
1013 zval *ci_array;
1014 struct curl_certinfo *ci;
1015 char *colon, *keyname;
1016
1017 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CERTINFO, &ci)) {
1018 MAKE_STD_ZVAL(ci_array);
1019 array_init(ci_array);
1020
1021 for (i = 0; i < ci->num_of_certs; ++i) {
1022 s = ci->certinfo[i];
1023
1024 MAKE_STD_ZVAL(subarray);
1025 array_init(subarray);
1026 for (p = s; p; p = p->next) {
1027 if (p->data) {
1028 if ((colon = strchr(p->data, ':'))) {
1029 keyname = estrndup(p->data, colon - p->data);
1030 add_assoc_string_ex(subarray, keyname, colon - p->data + 1, colon + 1, 1);
1031 efree(keyname);
1032 } else {
1033 add_next_index_string(subarray, p->data, 1);
1034 }
1035 }
1036 }
1037 add_next_index_zval(ci_array, subarray);
1038 }
1039 add_assoc_zval_ex(&array, "certinfo", sizeof("certinfo"), ci_array);
1040 }
1041 }
1042 #endif
1043 add_assoc_string_ex(&array, "error", sizeof("error"), get_storage(ch)->errorbuffer, 1);
1044
1045 return SUCCESS;
1046 }
1047
1048
1049 #ifdef ZTS
1050 static void *php_http_curl_request_datashare_locks_init(void)
1051 {
1052 int i;
1053 php_http_curl_request_datashare_lock_t *locks = pecalloc(CURL_LOCK_DATA_LAST, sizeof(*locks), 1);
1054
1055 if (locks) {
1056 for (i = 0; i < CURL_LOCK_DATA_LAST; ++i) {
1057 locks[i].mx = tsrm_mutex_alloc();
1058 }
1059 }
1060
1061 return locks;
1062 }
1063
1064 static void php_http_curl_request_datashare_locks_dtor(void *l)
1065 {
1066 int i;
1067 php_http_curl_request_datashare_lock_t *locks = l;
1068
1069 for (i = 0; i < CURL_LOCK_DATA_LAST; ++i) {
1070 tsrm_mutex_free(locks[i].mx);
1071 }
1072 pefree(locks, 1);
1073 }
1074
1075 static void php_http_curl_request_datashare_lock_func(CURL *handle, curl_lock_data data, curl_lock_access locktype, void *userptr)
1076 {
1077 php_http_curl_request_datashare_lock_t *locks = userptr;
1078
1079 /* TSRM can't distinguish shared/exclusive locks */
1080 tsrm_mutex_lock(locks[data].mx);
1081 locks[data].ch = handle;
1082 }
1083
1084 static void php_http_curl_request_datashare_unlock_func(CURL *handle, curl_lock_data data, void *userptr)
1085 {
1086 php_http_curl_request_datashare_lock_t *locks = userptr;
1087
1088 if (locks[data].ch == handle) {
1089 tsrm_mutex_unlock(locks[data].mx);
1090 }
1091 }
1092 #endif
1093
1094
1095
1096 /* request datashare handler ops */
1097
1098 static php_http_request_datashare_t *php_http_curl_request_datashare_init(php_http_request_datashare_t *h, void *handle TSRMLS_DC)
1099 {
1100 php_http_curl_request_datashare_t *curl;
1101
1102 if (!handle && (SUCCESS != php_http_persistent_handle_acquire(ZEND_STRL("http_request_datashare.curl"), &handle TSRMLS_CC))) {
1103 return NULL;
1104 }
1105
1106 curl = pecalloc(1, sizeof(*curl), h->persistent);
1107 curl->handle = handle;
1108 #ifdef ZTS
1109 if (h->persistent) {
1110 if (SUCCESS == php_http_persistent_handle_acquire(ZEND_STRL("http_request_datashare_lock.curl"), (void *) &curl->locks)) {
1111 curl_share_setopt(share->ch, CURLSHOPT_LOCKFUNC, php_http_curl_request_datashare_lock_func);
1112 curl_share_setopt(share->ch, CURLSHOPT_UNLOCKFUNC, php_http_curl_request_datashare_unlock_func);
1113 curl_share_setopt(share->ch, CURLSHOPT_USERDATA, curl->locks);
1114 }
1115 }
1116 #endif
1117 h->ctx = curl;
1118
1119 return h;
1120 }
1121
1122 static void php_http_curl_request_datashare_dtor(php_http_request_datashare_t *h)
1123 {
1124 php_http_curl_request_datashare_t *curl = h->ctx;
1125 TSRMLS_FETCH_FROM_CTX(h->ts);
1126
1127 php_http_persistent_handle_release(ZEND_STRL("http_request_datashare.curl"), &curl->handle TSRMLS_CC);
1128
1129 #ifdef ZTS
1130 if (h->persistent) {
1131 php_http_persistent_handle_release(ZEND_STRL("http_request_datashare_lock.curl"), (void *) &curl->locks TSRMLS_CC);
1132 }
1133 #endif
1134
1135 }
1136
1137 static STATUS php_http_curl_request_datashare_attach(php_http_request_datashare_t *h, php_http_request_t *r)
1138 {
1139 CURLcode rc;
1140 php_http_curl_request_datashare_t *curl = h->ctx;
1141 php_http_curl_request_t *recurl = r->ctx;
1142 TSRMLS_FETCH_FROM_CTX(h->ts);
1143
1144 if (CURLE_OK != (rc = curl_easy_setopt(recurl->handle, CURLOPT_SHARE, curl->handle))) {
1145 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not attach request to the datashare: %s", curl_easy_strerror(rc));
1146 return FAILURE;
1147 }
1148 return SUCCESS;
1149 }
1150
1151 static STATUS php_http_curl_request_datashare_detach(php_http_request_datashare_t *h, php_http_request_t *r)
1152 {
1153 CURLcode rc;
1154 php_http_curl_request_t *recurl = r->ctx;
1155 TSRMLS_FETCH_FROM_CTX(h->ts);
1156
1157
1158 if (CURLE_OK != (rc = curl_easy_setopt(recurl->handle, CURLOPT_SHARE, NULL))) {
1159 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not detach request from the datashare: %s", curl_share_strerror(rc));
1160 return FAILURE;
1161 }
1162 return SUCCESS;
1163 }
1164
1165 static STATUS php_http_curl_request_datashare_setopt(php_http_request_datashare_t *h, php_http_request_datashare_setopt_opt_t opt, void *arg)
1166 {
1167 CURLSHcode rc;
1168 php_http_curl_request_datashare_t *curl = h->ctx;
1169
1170 switch (opt) {
1171 case PHP_HTTP_REQUEST_DATASHARE_OPT_COOKIES:
1172 if (CURLSHE_OK != (rc = curl_share_setopt(curl->handle, *((zend_bool *) arg) ? CURLSHOPT_SHARE : CURLSHOPT_UNSHARE, CURL_LOCK_DATA_COOKIE))) {
1173 TSRMLS_FETCH_FROM_CTX(h->ts);
1174
1175 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not %s sharing of cookie data: %s", *((zend_bool *) arg) ? "enable" : "disable", curl_share_strerror(rc));
1176 return FAILURE;
1177 }
1178 break;
1179
1180 case PHP_HTTP_REQUEST_DATASHARE_OPT_RESOLVER:
1181 if (CURLSHE_OK != (rc = curl_share_setopt(curl->handle, *((zend_bool *) arg) ? CURLSHOPT_SHARE : CURLSHOPT_UNSHARE, CURL_LOCK_DATA_COOKIE))) {
1182 TSRMLS_FETCH_FROM_CTX(h->ts);
1183
1184 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not %s sharing of resolver data: %s", *((zend_bool *) arg) ? "enable" : "disable", curl_share_strerror(rc));
1185 return FAILURE;
1186 }
1187 break;
1188
1189 default:
1190 return FAILURE;
1191 }
1192
1193 return SUCCESS;
1194 }
1195
1196 static php_http_request_datashare_ops_t php_http_curl_request_datashare_ops = {
1197 php_http_curl_request_datashare_init,
1198 NULL /* copy */,
1199 php_http_curl_request_datashare_dtor,
1200 NULL /*reset */,
1201 php_http_curl_request_datashare_attach,
1202 php_http_curl_request_datashare_detach,
1203 php_http_curl_request_datashare_setopt,
1204 };
1205
1206 PHP_HTTP_API php_http_request_datashare_ops_t *php_http_curl_get_request_datashare_ops(void)
1207 {
1208 return &php_http_curl_request_datashare_ops;
1209 }
1210
1211
1212 /* request pool handler ops */
1213
1214 static php_http_request_pool_t *php_http_curl_request_pool_init(php_http_request_pool_t *h, void *handle TSRMLS_DC)
1215 {
1216 php_http_curl_request_pool_t *curl;
1217
1218 if (!handle && (SUCCESS != php_http_persistent_handle_acquire(ZEND_STRL("http_request_pool.curl"), &handle TSRMLS_CC))) {
1219 return NULL;
1220 }
1221
1222 curl = ecalloc(1, sizeof(*curl));
1223 curl->handle = handle;
1224 curl->unfinished = 0;
1225 h->ctx = curl;
1226
1227 return h;
1228 }
1229
1230 static void php_http_curl_request_pool_dtor(php_http_request_pool_t *h)
1231 {
1232 php_http_curl_request_pool_t *curl = h->ctx;
1233 TSRMLS_FETCH_FROM_CTX(h->ts);
1234
1235 #ifdef PHP_HTTP_HAVE_EVENT
1236 if (curl->timeout) {
1237 efree(curl->timeout);
1238 curl->timeout = NULL;
1239 }
1240 #endif
1241 curl->unfinished = 0;
1242 php_http_request_pool_reset(h);
1243 php_http_persistent_handle_release(ZEND_STRL("http_request_pool.curl"), &curl->handle TSRMLS_CC);
1244
1245 efree(curl);
1246 h->ctx = NULL;
1247 }
1248
1249 static STATUS php_http_curl_request_pool_attach(php_http_request_pool_t *h, php_http_request_t *r, php_http_request_method_t m, const char *url, php_http_message_body_t *body)
1250 {
1251 php_http_curl_request_pool_t *curl = h->ctx;
1252 php_http_curl_request_t *recurl = r->ctx;
1253 CURLMcode rs;
1254
1255 if (SUCCESS != php_http_curl_request_prepare(r, m, url, body)) {
1256 return FAILURE;
1257 }
1258
1259 if (CURLM_OK == (rs = curl_multi_add_handle(curl->handle, recurl->handle))) {
1260 ++curl->unfinished;
1261 return SUCCESS;
1262 } else {
1263 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_POOL, "Could not attach request to pool: %s", curl_multi_strerror(rs));
1264 return FAILURE;
1265 }
1266 }
1267
1268 static STATUS php_http_curl_request_pool_detach(php_http_request_pool_t *h, php_http_request_t *r)
1269 {
1270 php_http_curl_request_pool_t *curl = h->ctx;
1271 php_http_curl_request_t *recurl = r->ctx;
1272 CURLMcode rs = curl_multi_remove_handle(curl->handle, recurl->handle);
1273
1274 if (CURLM_OK == rs) {
1275 return SUCCESS;
1276 } else {
1277 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_POOL, "Could not detach request from pool: %s", curl_multi_strerror(rs));
1278 return FAILURE;
1279 }
1280 }
1281
1282 #ifdef PHP_WIN32
1283 # define SELECT_ERROR SOCKET_ERROR
1284 #else
1285 # define SELECT_ERROR -1
1286 #endif
1287
1288 static STATUS php_http_curl_request_pool_wait(php_http_request_pool_t *h, struct timeval *custom_timeout)
1289 {
1290 int MAX;
1291 fd_set R, W, E;
1292 struct timeval timeout;
1293 php_http_curl_request_pool_t *curl = h->ctx;
1294
1295 #ifdef PHP_HTTP_HAVE_EVENT
1296 if (curl->useevents) {
1297 TSRMLS_FETCH_FROM_CTX(pool->ts);
1298
1299 php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "not implemented");
1300 return FAILURE;
1301 }
1302 #endif
1303
1304 if (custom_timeout && timerisset(custom_timeout)) {
1305 timeout = *custom_timeout;
1306 } else {
1307 long max_tout = 1000;
1308
1309 if ((CURLM_OK == curl_multi_timeout(curl->handle, &max_tout)) && (max_tout > 0)) {
1310 timeout.tv_sec = max_tout / 1000;
1311 timeout.tv_usec = (max_tout % 1000) * 1000;
1312 } else {
1313 timeout.tv_sec = 0;
1314 timeout.tv_usec = 1000;
1315 }
1316 }
1317
1318 FD_ZERO(&R);
1319 FD_ZERO(&W);
1320 FD_ZERO(&E);
1321
1322 if (CURLM_OK == curl_multi_fdset(curl->handle, &R, &W, &E, &MAX)) {
1323 if (MAX == -1) {
1324 php_http_sleep((double) timeout.tv_sec + (double) (timeout.tv_usec / PHP_HTTP_MCROSEC));
1325 return SUCCESS;
1326 } else if (SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) {
1327 return SUCCESS;
1328 }
1329 }
1330 return FAILURE;
1331 }
1332
1333 static int php_http_curl_request_pool_once(php_http_request_pool_t *h)
1334 {
1335 php_http_curl_request_pool_t *curl = h->ctx;
1336 TSRMLS_FETCH_FROM_CTX(h->ts);
1337
1338 #ifdef PHP_HTTP_HAVE_EVENT
1339 if (curl->useevents) {
1340 php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "not implemented");
1341 return FAILURE;
1342 }
1343 #endif
1344
1345 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curl->handle, &curl->unfinished));
1346
1347 php_http_curl_request_pool_responsehandler(h);
1348
1349 return curl->unfinished;
1350
1351 }
1352 static void dolog(int i, const char *m) {
1353 fprintf(stderr, "%d: %s\n", i, m);
1354 }
1355 static STATUS php_http_curl_request_pool_exec(php_http_request_pool_t *h)
1356 {
1357 php_http_curl_request_pool_t *curl = h->ctx;
1358
1359 #ifdef PHP_HTTP_HAVE_EVENT
1360 if (curl->useevents) {
1361 event_set_log_callback(dolog);
1362 do {
1363 #if DBG_EVENTS
1364 fprintf(stderr, "X");
1365 #endif
1366 event_base_dispatch(PHP_HTTP_G->request_pool.event_base);
1367 } while (curl->unfinished);
1368 } else
1369 #endif
1370 {
1371 while (php_http_curl_request_pool_once(h)) {
1372 if (SUCCESS != php_http_curl_request_pool_wait(h, NULL)) {
1373 #ifdef PHP_WIN32
1374 /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */
1375 php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "WinSock error: %d", WSAGetLastError());
1376 #else
1377 php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, strerror(errno));
1378 #endif
1379 return FAILURE;
1380 }
1381 }
1382 }
1383
1384 return SUCCESS;
1385 }
1386
1387 static STATUS php_http_curl_request_pool_setopt(php_http_request_pool_t *h, php_http_request_pool_setopt_opt_t opt, void *arg)
1388 {
1389 php_http_curl_request_pool_t *curl = h->ctx;
1390
1391 switch (opt) {
1392 case PHP_HTTP_REQUEST_POOL_OPT_ENABLE_PIPELINING:
1393 if (CURLM_OK != curl_multi_setopt(curl->handle, CURLMOPT_PIPELINING, (long) *((zend_bool *) arg))) {
1394 return FAILURE;
1395 }
1396 break;
1397
1398 case PHP_HTTP_REQUEST_POOL_OPT_USE_EVENTS:
1399 #if PHP_HTTP_HAVE_EVENT
1400 if ((curl->useevents = *((zend_bool *) arg))) {
1401 if (!curl->timeout) {
1402 curl->timeout = ecalloc(1, sizeof(struct event));
1403 }
1404 curl_multi_setopt(curl->handle, CURLMOPT_SOCKETDATA, h);
1405 curl_multi_setopt(curl->handle, CURLMOPT_SOCKETFUNCTION, php_http_curl_request_pool_socket_callback);
1406 curl_multi_setopt(curl->handle, CURLMOPT_TIMERDATA, h);
1407 curl_multi_setopt(curl->handle, CURLMOPT_TIMERFUNCTION, php_http_curl_request_pool_timer_callback);
1408 } else {
1409 curl_multi_setopt(curl->handle, CURLMOPT_SOCKETDATA, NULL);
1410 curl_multi_setopt(curl->handle, CURLMOPT_SOCKETFUNCTION, NULL);
1411 curl_multi_setopt(curl->handle, CURLMOPT_TIMERDATA, NULL);
1412 curl_multi_setopt(curl->handle, CURLMOPT_TIMERFUNCTION, NULL);
1413 }
1414 break;
1415 #endif
1416
1417 default:
1418 return FAILURE;
1419 }
1420 return SUCCESS;
1421 }
1422
1423 static php_http_request_pool_ops_t php_http_curl_request_pool_ops = {
1424 php_http_curl_request_pool_init,
1425 NULL /* copy */,
1426 php_http_curl_request_pool_dtor,
1427 NULL /*reset */,
1428 php_http_curl_request_pool_exec,
1429 php_http_curl_request_pool_wait,
1430 php_http_curl_request_pool_once,
1431 php_http_curl_request_pool_attach,
1432 php_http_curl_request_pool_detach,
1433 php_http_curl_request_pool_setopt,
1434 };
1435
1436 PHP_HTTP_API php_http_request_pool_ops_t *php_http_curl_get_request_pool_ops(void)
1437 {
1438 return &php_http_curl_request_pool_ops;
1439 }
1440
1441 /* request handler ops */
1442
1443 static STATUS php_http_curl_request_reset(php_http_request_t *h);
1444
1445 static php_http_request_t *php_http_curl_request_init(php_http_request_t *h, void *handle)
1446 {
1447 php_http_curl_request_t *ctx;
1448 TSRMLS_FETCH_FROM_CTX(h->ts);
1449
1450 if (!handle && (SUCCESS != php_http_persistent_handle_acquire(ZEND_STRL("http_request.curl"), &handle TSRMLS_CC))) {
1451 return NULL;
1452 }
1453
1454 ctx = ecalloc(1, sizeof(*ctx));
1455 ctx->handle = handle;
1456 php_http_buffer_init(&ctx->options.cookies);
1457 zend_hash_init(&ctx->options.cache, 0, NULL, ZVAL_PTR_DTOR, 0);
1458 h->ctx = ctx;
1459
1460 #if defined(ZTS)
1461 curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L);
1462 #endif
1463 curl_easy_setopt(handle, CURLOPT_HEADER, 0L);
1464 curl_easy_setopt(handle, CURLOPT_FILETIME, 1L);
1465 curl_easy_setopt(handle, CURLOPT_AUTOREFERER, 1L);
1466 curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L);
1467 curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L);
1468 curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, NULL);
1469 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, php_http_curl_dummy_callback);
1470 curl_easy_setopt(handle, CURLOPT_DEBUGFUNCTION, php_http_curl_raw_callback);
1471 curl_easy_setopt(handle, CURLOPT_READFUNCTION, php_http_curl_read_callback);
1472 curl_easy_setopt(handle, CURLOPT_IOCTLFUNCTION, php_http_curl_ioctl_callback);
1473 curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, php_http_curl_progress_callback);
1474 curl_easy_setopt(handle, CURLOPT_DEBUGDATA, h);
1475 curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, h);
1476
1477 php_http_curl_request_reset(h TSRMLS_CC);
1478
1479 return h;
1480 }
1481
1482 static php_http_request_t *php_http_curl_request_copy(php_http_request_t *from, php_http_request_t *to)
1483 {
1484 php_http_curl_request_t *ctx = from->ctx;
1485 void *copy;
1486 TSRMLS_FETCH_FROM_CTX(from->ts);
1487
1488 if (SUCCESS != php_http_persistent_handle_accrete(ZEND_STRL("http_request.curl"), ctx->handle, &copy TSRMLS_CC)) {
1489 return NULL;
1490 }
1491
1492 if (to) {
1493 return php_http_curl_request_init(to, copy);
1494 } else {
1495 return php_http_request_init(NULL, from->ops, copy TSRMLS_CC);
1496 }
1497 }
1498
1499 static void php_http_curl_request_dtor(php_http_request_t *h)
1500 {
1501 php_http_curl_request_t *ctx = h->ctx;
1502
1503 curl_easy_setopt(ctx->handle, CURLOPT_NOPROGRESS, 1L);
1504 curl_easy_setopt(ctx->handle, CURLOPT_PROGRESSFUNCTION, NULL);
1505 curl_easy_setopt(ctx->handle, CURLOPT_VERBOSE, 0L);
1506 curl_easy_setopt(ctx->handle, CURLOPT_DEBUGFUNCTION, NULL);
1507
1508 php_http_persistent_handle_release(ZEND_STRL("http_request.curl"), &ctx->handle TSRMLS_CC);
1509
1510 php_http_buffer_dtor(&ctx->options.cookies);
1511 zend_hash_destroy(&ctx->options.cache);
1512
1513 if (ctx->options.headers) {
1514 curl_slist_free_all(ctx->options.headers);
1515 ctx->options.headers = NULL;
1516 }
1517
1518 if (ctx->progress.callback) {
1519 zval_ptr_dtor(&ctx->progress.callback);
1520 ctx->progress.callback = NULL;
1521 }
1522
1523 efree(ctx);
1524 h->ctx = NULL;
1525 }
1526 static STATUS php_http_curl_request_reset(php_http_request_t *h)
1527 {
1528 CURL *ch = ((php_http_curl_request_t *) h->ctx)->handle;
1529 php_http_curl_request_storage_t *st;
1530
1531 if ((st = get_storage(ch))) {
1532 if (st->url) {
1533 pefree(st->url, 1);
1534 st->url = NULL;
1535 }
1536 if (st->cookiestore) {
1537 pefree(st->cookiestore, 1);
1538 st->cookiestore = NULL;
1539 }
1540 st->errorbuffer[0] = '\0';
1541 }
1542
1543 curl_easy_setopt(ch, CURLOPT_URL, NULL);
1544 #if PHP_HTTP_CURL_VERSION(7,19,4)
1545 curl_easy_setopt(ch, CURLOPT_NOPROXY, NULL);
1546 #endif
1547 curl_easy_setopt(ch, CURLOPT_PROXY, NULL);
1548 curl_easy_setopt(ch, CURLOPT_PROXYPORT, 0L);
1549 curl_easy_setopt(ch, CURLOPT_PROXYTYPE, 0L);
1550 /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
1551 #if PHP_HTTP_CURL_VERSION(7,19,1)
1552 curl_easy_setopt(ch, CURLOPT_PROXYUSERNAME, NULL);
1553 curl_easy_setopt(ch, CURLOPT_PROXYPASSWORD, NULL);
1554 #endif
1555 curl_easy_setopt(ch, CURLOPT_PROXYAUTH, 0L);
1556 curl_easy_setopt(ch, CURLOPT_HTTPPROXYTUNNEL, 0L);
1557 curl_easy_setopt(ch, CURLOPT_DNS_CACHE_TIMEOUT, 60L);
1558 curl_easy_setopt(ch, CURLOPT_IPRESOLVE, 0);
1559 curl_easy_setopt(ch, CURLOPT_LOW_SPEED_LIMIT, 0L);
1560 curl_easy_setopt(ch, CURLOPT_LOW_SPEED_TIME, 0L);
1561 /* LFS weirdance
1562 curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) 0);
1563 curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) 0);
1564 */
1565 /* crashes
1566 curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, 5L); */
1567 curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 0L);
1568 curl_easy_setopt(ch, CURLOPT_FORBID_REUSE, 0L);
1569 curl_easy_setopt(ch, CURLOPT_INTERFACE, NULL);
1570 curl_easy_setopt(ch, CURLOPT_PORT, 0L);
1571 #if PHP_HTTP_CURL_VERSION(7,19,0)
1572 curl_easy_setopt(ch, CURLOPT_ADDRESS_SCOPE, 0L);
1573 #endif
1574 curl_easy_setopt(ch, CURLOPT_LOCALPORT, 0L);
1575 curl_easy_setopt(ch, CURLOPT_LOCALPORTRANGE, 0L);
1576 /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
1577 #if PHP_HTTP_CURL_VERSION(7,19,1)
1578 curl_easy_setopt(ch, CURLOPT_USERNAME, NULL);
1579 curl_easy_setopt(ch, CURLOPT_PASSWORD, NULL);
1580 #endif
1581 curl_easy_setopt(ch, CURLOPT_HTTPAUTH, 0L);
1582 curl_easy_setopt(ch, CURLOPT_ENCODING, NULL);
1583 /* we do this ourself anyway */
1584 curl_easy_setopt(ch, CURLOPT_HTTP_CONTENT_DECODING, 0L);
1585 curl_easy_setopt(ch, CURLOPT_HTTP_TRANSFER_DECODING, 0L);
1586 curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 0L);
1587 #if PHP_HTTP_CURL_VERSION(7,19,1)
1588 curl_easy_setopt(ch, CURLOPT_POSTREDIR, 0L);
1589 #else
1590 curl_easy_setopt(ch, CURLOPT_POST301, 0L);
1591 #endif
1592 curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, 0L);
1593 curl_easy_setopt(ch, CURLOPT_REFERER, NULL);
1594 curl_easy_setopt(ch, CURLOPT_USERAGENT, "PECL::HTTP/" PHP_HTTP_EXT_VERSION " (PHP/" PHP_VERSION ")");
1595 curl_easy_setopt(ch, CURLOPT_HTTPHEADER, NULL);
1596 curl_easy_setopt(ch, CURLOPT_COOKIE, NULL);
1597 curl_easy_setopt(ch, CURLOPT_COOKIESESSION, 0L);
1598 /* these options would enable curl's cookie engine by default which we don't want
1599 curl_easy_setopt(ch, CURLOPT_COOKIEFILE, NULL);
1600 curl_easy_setopt(ch, CURLOPT_COOKIEJAR, NULL); */
1601 curl_easy_setopt(ch, CURLOPT_COOKIELIST, NULL);
1602 curl_easy_setopt(ch, CURLOPT_RANGE, NULL);
1603 curl_easy_setopt(ch, CURLOPT_RESUME_FROM, 0L);
1604 curl_easy_setopt(ch, CURLOPT_MAXFILESIZE, 0L);
1605 curl_easy_setopt(ch, CURLOPT_TIMECONDITION, 0L);
1606 curl_easy_setopt(ch, CURLOPT_TIMEVALUE, 0L);
1607 curl_easy_setopt(ch, CURLOPT_TIMEOUT, 0L);
1608 curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT, 3);
1609 curl_easy_setopt(ch, CURLOPT_SSLCERT, NULL);
1610 curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, NULL);
1611 curl_easy_setopt(ch, CURLOPT_SSLCERTPASSWD, NULL);
1612 curl_easy_setopt(ch, CURLOPT_SSLKEY, NULL);
1613 curl_easy_setopt(ch, CURLOPT_SSLKEYTYPE, NULL);
1614 curl_easy_setopt(ch, CURLOPT_SSLKEYPASSWD, NULL);
1615 curl_easy_setopt(ch, CURLOPT_SSLENGINE, NULL);
1616 curl_easy_setopt(ch, CURLOPT_SSLVERSION, 0L);
1617 curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 0L);
1618 curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, 0L);
1619 curl_easy_setopt(ch, CURLOPT_SSL_CIPHER_LIST, NULL);
1620 #if PHP_HTTP_CURL_VERSION(7,19,0)
1621 curl_easy_setopt(ch, CURLOPT_ISSUERCERT, NULL);
1622 #if defined(PHP_HTTP_HAVE_OPENSSL)
1623 curl_easy_setopt(ch, CURLOPT_CRLFILE, NULL);
1624 #endif
1625 #endif
1626 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
1627 curl_easy_setopt(ch, CURLOPT_CERTINFO, NULL);
1628 #endif
1629 #ifdef PHP_HTTP_CURL_CAINFO
1630 curl_easy_setopt(ch, CURLOPT_CAINFO, PHP_HTTP_CURL_CAINFO);
1631 #else
1632 curl_easy_setopt(ch, CURLOPT_CAINFO, NULL);
1633 #endif
1634 curl_easy_setopt(ch, CURLOPT_CAPATH, NULL);
1635 curl_easy_setopt(ch, CURLOPT_RANDOM_FILE, NULL);
1636 curl_easy_setopt(ch, CURLOPT_EGDSOCKET, NULL);
1637 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, NULL);
1638 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, 0L);
1639 curl_easy_setopt(ch, CURLOPT_HTTPPOST, NULL);
1640 curl_easy_setopt(ch, CURLOPT_IOCTLDATA, NULL);
1641 curl_easy_setopt(ch, CURLOPT_READDATA, NULL);
1642 curl_easy_setopt(ch, CURLOPT_INFILESIZE, 0L);
1643 curl_easy_setopt(ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);
1644 curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, NULL);
1645 curl_easy_setopt(ch, CURLOPT_NOBODY, 0L);
1646 curl_easy_setopt(ch, CURLOPT_POST, 0L);
1647 curl_easy_setopt(ch, CURLOPT_UPLOAD, 0L);
1648 curl_easy_setopt(ch, CURLOPT_HTTPGET, 1L);
1649
1650 return SUCCESS;
1651 }
1652
1653 static STATUS php_http_curl_request_exec(php_http_request_t *h, php_http_request_method_t meth, const char *url, php_http_message_body_t *body)
1654 {
1655 uint tries = 0;
1656 CURLcode result;
1657 php_http_curl_request_t *curl = h->ctx;
1658 php_http_curl_request_storage_t *storage = get_storage(curl->handle);
1659 TSRMLS_FETCH_FROM_CTX(h->ts);
1660
1661 if (SUCCESS != php_http_curl_request_prepare(h, meth, url, body)) {
1662 return FAILURE;
1663 }
1664
1665 retry:
1666 if (CURLE_OK != (result = curl_easy_perform(curl->handle))) {
1667 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(result), storage->errorbuffer, storage->url);
1668
1669 if (EG(exception)) {
1670 add_property_long(EG(exception), "curlCode", result);
1671 }
1672
1673 if (curl->options.retry.count > tries++) {
1674 switch (result) {
1675 case CURLE_COULDNT_RESOLVE_PROXY:
1676 case CURLE_COULDNT_RESOLVE_HOST:
1677 case CURLE_COULDNT_CONNECT:
1678 case CURLE_WRITE_ERROR:
1679 case CURLE_READ_ERROR:
1680 case CURLE_OPERATION_TIMEDOUT:
1681 case CURLE_SSL_CONNECT_ERROR:
1682 case CURLE_GOT_NOTHING:
1683 case CURLE_SSL_ENGINE_SETFAILED:
1684 case CURLE_SEND_ERROR:
1685 case CURLE_RECV_ERROR:
1686 case CURLE_SSL_ENGINE_INITFAILED:
1687 case CURLE_LOGIN_DENIED:
1688 if (curl->options.retry.delay >= PHP_HTTP_DIFFSEC) {
1689 php_http_sleep(curl->options.retry.delay);
1690 }
1691 goto retry;
1692 default:
1693 break;
1694 }
1695 } else {
1696 return FAILURE;
1697 }
1698 }
1699
1700 return SUCCESS;
1701 }
1702
1703 static STATUS php_http_curl_request_setopt(php_http_request_t *h, php_http_request_setopt_opt_t opt, void *arg)
1704 {
1705 php_http_curl_request_t *curl = h->ctx;
1706
1707 switch (opt) {
1708 case PHP_HTTP_REQUEST_OPT_SETTINGS:
1709 return set_options(h, arg);
1710 break;
1711
1712 case PHP_HTTP_REQUEST_OPT_PROGRESS_CALLBACK:
1713 if (curl->progress.in_cb) {
1714 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "Cannot change progress callback while executing it");
1715 return FAILURE;
1716 }
1717 if (arg) {
1718 Z_ADDREF_P(arg);
1719 }
1720 if (curl->progress.callback) {
1721 zval_ptr_dtor(&curl->progress.callback);
1722 }
1723 curl->progress.callback = arg;
1724 break;
1725
1726 case PHP_HTTP_REQUEST_OPT_PROGRESS_CALLBACK_WANTS_STATE:
1727 curl->progress.pass_state = *((int *)arg);
1728 break;
1729
1730 case PHP_HTTP_REQUEST_OPT_COOKIES_ENABLE:
1731 /* are cookies already enabled anyway? */
1732 if (!get_storage(curl->handle)->cookiestore) {
1733 if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIEFILE, "")) {
1734 return FAILURE;
1735 }
1736 }
1737 break;
1738
1739 case PHP_HTTP_REQUEST_OPT_COOKIES_RESET:
1740 if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIELIST, "ALL")) {
1741 return FAILURE;
1742 }
1743 break;
1744
1745 case PHP_HTTP_REQUEST_OPT_COOKIES_RESET_SESSION:
1746 if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIELIST, "SESS")) {
1747 return FAILURE;
1748 }
1749 break;
1750
1751 case PHP_HTTP_REQUEST_OPT_COOKIES_FLUSH:
1752 if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIELIST, "FLUSH")) {
1753 return FAILURE;
1754 }
1755 break;
1756
1757 default:
1758 return FAILURE;
1759 }
1760
1761 return SUCCESS;
1762 }
1763
1764 static STATUS php_http_curl_request_getopt(php_http_request_t *h, php_http_request_getopt_opt_t opt, void *arg)
1765 {
1766 php_http_curl_request_t *curl = h->ctx;
1767
1768 switch (opt) {
1769 case PHP_HTTP_REQUEST_OPT_PROGRESS_INFO:
1770 *((php_http_request_progress_t **) arg) = &curl->progress;
1771 break;
1772
1773 case PHP_HTTP_REQUEST_OPT_TRANSFER_INFO:
1774 get_info(curl->handle, arg);
1775 break;
1776
1777 default:
1778 return FAILURE;
1779 }
1780
1781 return SUCCESS;
1782 }
1783
1784 static php_http_request_ops_t php_http_curl_request_ops = {
1785 php_http_curl_request_init,
1786 php_http_curl_request_copy,
1787 php_http_curl_request_dtor,
1788 php_http_curl_request_reset,
1789 php_http_curl_request_exec,
1790 php_http_curl_request_setopt,
1791 php_http_curl_request_getopt
1792 };
1793
1794 PHP_HTTP_API php_http_request_ops_t *php_http_curl_get_request_ops(void)
1795 {
1796 return &php_http_curl_request_ops;
1797 }
1798
1799 /* safe curl wrappers */
1800 static void *safe_curl_init(void)
1801 {
1802 void *ch;
1803
1804 if ((ch = curl_easy_init())) {
1805 get_storage(ch);
1806 return ch;
1807 }
1808 return NULL;
1809 }
1810
1811 static void *safe_curl_copy(void *p)
1812 {
1813 void *ch;
1814
1815 if ((ch = curl_easy_duphandle(p))) {
1816 get_storage(ch);
1817 return ch;
1818 }
1819 return NULL;
1820 }
1821
1822 static void safe_curl_dtor(void *p) {
1823 php_http_curl_request_storage_t *st = get_storage(p);
1824
1825 curl_easy_cleanup(p);
1826
1827 if (st) {
1828 if (st->url) {
1829 pefree(st->url, 1);
1830 }
1831 if (st->cookiestore) {
1832 pefree(st->cookiestore, 1);
1833 }
1834 pefree(st, 1);
1835 }
1836 }
1837
1838 #if defined(ZTS) && defined(PHP_HTTP_HAVE_SSL)
1839 # ifdef PHP_WIN32
1840 # define PHP_HTTP_NEED_OPENSSL_TSL
1841 # include <openssl/crypto.h>
1842 # else /* !PHP_WIN32 */
1843 # if defined(PHP_HTTP_HAVE_OPENSSL)
1844 # define PHP_HTTP_NEED_OPENSSL_TSL
1845 # include <openssl/crypto.h>
1846 # elif defined(PHP_HTTP_HAVE_GNUTLS)
1847 # define PHP_HTTP_NEED_GNUTLS_TSL
1848 # include <gcrypt.h>
1849 # else
1850 # warning \
1851 "libcurl was compiled with SSL support, but configure could not determine which" \
1852 "library was used; thus no SSL crypto locking callbacks will be set, which may " \
1853 "cause random crashes on SSL requests"
1854 # endif /* PHP_HTTP_HAVE_OPENSSL || PHP_HTTP_HAVE_GNUTLS */
1855 # endif /* PHP_WIN32 */
1856 #endif /* ZTS && PHP_HTTP_HAVE_SSL */
1857
1858
1859 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
1860 static MUTEX_T *php_http_openssl_tsl = NULL;
1861
1862 static void php_http_openssl_thread_lock(int mode, int n, const char * file, int line)
1863 {
1864 if (mode & CRYPTO_LOCK) {
1865 tsrm_mutex_lock(php_http_openssl_tsl[n]);
1866 } else {
1867 tsrm_mutex_unlock(php_http_openssl_tsl[n]);
1868 }
1869 }
1870
1871 static ulong php_http_openssl_thread_id(void)
1872 {
1873 return (ulong) tsrm_thread_id();
1874 }
1875 #endif
1876 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
1877 static int php_http_gnutls_mutex_create(void **m)
1878 {
1879 if (*((MUTEX_T *) m) = tsrm_mutex_alloc()) {
1880 return SUCCESS;
1881 } else {
1882 return FAILURE;
1883 }
1884 }
1885
1886 static int php_http_gnutls_mutex_destroy(void **m)
1887 {
1888 tsrm_mutex_free(*((MUTEX_T *) m));
1889 return SUCCESS;
1890 }
1891
1892 static int php_http_gnutls_mutex_lock(void **m)
1893 {
1894 return tsrm_mutex_lock(*((MUTEX_T *) m));
1895 }
1896
1897 static int php_http_gnutls_mutex_unlock(void **m)
1898 {
1899 return tsrm_mutex_unlock(*((MUTEX_T *) m));
1900 }
1901
1902 static struct gcry_thread_cbs php_http_gnutls_tsl = {
1903 GCRY_THREAD_OPTION_USER,
1904 NULL,
1905 php_http_gnutls_mutex_create,
1906 php_http_gnutls_mutex_destroy,
1907 php_http_gnutls_mutex_lock,
1908 php_http_gnutls_mutex_unlock
1909 };
1910 #endif
1911
1912
1913
1914 PHP_MINIT_FUNCTION(http_curl)
1915 {
1916 php_http_request_factory_driver_t driver = {
1917 &php_http_curl_request_ops,
1918 &php_http_curl_request_pool_ops,
1919 &php_http_curl_request_datashare_ops
1920 };
1921
1922 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
1923 /* mod_ssl, libpq or ext/curl might already have set thread lock callbacks */
1924 if (!CRYPTO_get_id_callback()) {
1925 int i, c = CRYPTO_num_locks();
1926
1927 php_http_openssl_tsl = malloc(c * sizeof(MUTEX_T));
1928
1929 for (i = 0; i < c; ++i) {
1930 php_http_openssl_tsl[i] = tsrm_mutex_alloc();
1931 }
1932
1933 CRYPTO_set_id_callback(php_http_openssl_thread_id);
1934 CRYPTO_set_locking_callback(php_http_openssl_thread_lock);
1935 }
1936 #endif
1937 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
1938 gcry_control(GCRYCTL_SET_THREAD_CBS, &php_http_gnutls_tsl);
1939 #endif
1940
1941 if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
1942 return FAILURE;
1943 }
1944
1945 if (SUCCESS != php_http_persistent_handle_provide(ZEND_STRL("http_request_datashare.curl"), curl_share_init, (php_http_persistent_handle_dtor_t) curl_share_cleanup, NULL TSRMLS_CC)) {
1946 return FAILURE;
1947 }
1948
1949 if (SUCCESS != php_http_persistent_handle_provide(ZEND_STRL("http_request_pool.curl"), curl_multi_init, (php_http_persistent_handle_dtor_t) curl_multi_cleanup, NULL TSRMLS_CC)) {
1950 return FAILURE;
1951 }
1952
1953 if (SUCCESS != php_http_persistent_handle_provide(ZEND_STRL("http_request.curl"), safe_curl_init, safe_curl_dtor, safe_curl_copy)) {
1954 return FAILURE;
1955 }
1956
1957 if (SUCCESS != php_http_request_factory_add_driver(ZEND_STRL("curl"), &driver)) {
1958 return FAILURE;
1959 }
1960
1961 /*
1962 * HTTP Protocol Version Constants
1963 */
1964 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("VERSION_1_0"), CURL_HTTP_VERSION_1_0 TSRMLS_CC);
1965 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("VERSION_1_1"), CURL_HTTP_VERSION_1_1 TSRMLS_CC);
1966 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("VERSION_NONE"), CURL_HTTP_VERSION_NONE TSRMLS_CC); /* to be removed */
1967 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("VERSION_ANY"), CURL_HTTP_VERSION_NONE TSRMLS_CC);
1968
1969 /*
1970 * SSL Version Constants
1971 */
1972 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("SSL_VERSION_TLSv1"), CURL_SSLVERSION_TLSv1 TSRMLS_CC);
1973 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("SSL_VERSION_SSLv2"), CURL_SSLVERSION_SSLv2 TSRMLS_CC);
1974 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("SSL_VERSION_SSLv3"), CURL_SSLVERSION_SSLv3 TSRMLS_CC);
1975 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("SSL_VERSION_ANY"), CURL_SSLVERSION_DEFAULT TSRMLS_CC);
1976
1977 /*
1978 * DNS IPvX resolving
1979 */
1980 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("IPRESOLVE_V4"), CURL_IPRESOLVE_V4 TSRMLS_CC);
1981 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("IPRESOLVE_V6"), CURL_IPRESOLVE_V6 TSRMLS_CC);
1982 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("IPRESOLVE_ANY"), CURL_IPRESOLVE_WHATEVER TSRMLS_CC);
1983
1984 /*
1985 * Auth Constants
1986 */
1987 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("AUTH_BASIC"), CURLAUTH_BASIC TSRMLS_CC);
1988 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("AUTH_DIGEST"), CURLAUTH_DIGEST TSRMLS_CC);
1989 #if PHP_HTTP_CURL_VERSION(7,19,3)
1990 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("AUTH_DIGEST_IE"), CURLAUTH_DIGEST_IE TSRMLS_CC);
1991 #endif
1992 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("AUTH_NTLM"), CURLAUTH_NTLM TSRMLS_CC);
1993 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("AUTH_GSSNEG"), CURLAUTH_GSSNEGOTIATE TSRMLS_CC);
1994 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("AUTH_ANY"), CURLAUTH_ANY TSRMLS_CC);
1995
1996 /*
1997 * Proxy Type Constants
1998 */
1999 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("PROXY_SOCKS4"), CURLPROXY_SOCKS4 TSRMLS_CC);
2000 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("PROXY_SOCKS4A"), CURLPROXY_SOCKS5 TSRMLS_CC);
2001 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("PROXY_SOCKS5_HOSTNAME"), CURLPROXY_SOCKS5 TSRMLS_CC);
2002 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("PROXY_SOCKS5"), CURLPROXY_SOCKS5 TSRMLS_CC);
2003 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("PROXY_HTTP"), CURLPROXY_HTTP TSRMLS_CC);
2004 # if PHP_HTTP_CURL_VERSION(7,19,4)
2005 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("PROXY_HTTP_1_0"), CURLPROXY_HTTP_1_0 TSRMLS_CC);
2006 # endif
2007
2008 /*
2009 * Post Redirection Constants
2010 */
2011 #if PHP_HTTP_CURL_VERSION(7,19,1)
2012 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("POSTREDIR_301"), CURL_REDIR_POST_301 TSRMLS_CC);
2013 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("POSTREDIR_302"), CURL_REDIR_POST_302 TSRMLS_CC);
2014 zend_declare_class_constant_long(php_http_request_class_entry, ZEND_STRL("POSTREDIR_ALL"), CURL_REDIR_POST_ALL TSRMLS_CC);
2015 #endif
2016
2017 return SUCCESS;
2018 }
2019
2020 PHP_MSHUTDOWN_FUNCTION(http_curl)
2021 {
2022 curl_global_cleanup();
2023 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
2024 if (php_http_openssl_tsl) {
2025 int i, c = CRYPTO_num_locks();
2026
2027 CRYPTO_set_id_callback(NULL);
2028 CRYPTO_set_locking_callback(NULL);
2029
2030 for (i = 0; i < c; ++i) {
2031 tsrm_mutex_free(php_http_openssl_tsl[i]);
2032 }
2033
2034 free(php_http_openssl_tsl);
2035 php_http_openssl_tsl = NULL;
2036 }
2037 #endif
2038 return SUCCESS;
2039 }
2040
2041 PHP_RINIT_FUNCTION(http_curl)
2042 {
2043 #ifdef PHP_HTTP_HAVE_EVENT
2044 if (!PHP_HTTP_G->request_pool.event_base && !(PHP_HTTP_G->request_pool.event_base = event_init())) {
2045 return FAILURE;
2046 }
2047 #endif
2048
2049 return SUCCESS;
2050 }