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