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