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