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