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