#endif
/* }}} */
+/* safe curl wrappers */
+#define init_curl_storage(ch) \
+ {\
+ http_request_storage *st = pecalloc(1, sizeof(http_request_storage), 1); \
+ curl_easy_setopt(ch, CURLOPT_PRIVATE, st); \
+ curl_easy_setopt(ch, CURLOPT_ERRORBUFFER, st->errorbuffer); \
+ }
+
+static void *safe_curl_init(void)
+{
+ CURL *ch;
+
+ if ((ch = curl_easy_init())) {
+ init_curl_storage(ch);
+ return ch;
+ }
+ return NULL;
+}
+static void *safe_curl_copy(void *p)
+{
+ CURL *ch;
+
+ if ((ch = curl_easy_duphandle(p))) {
+ init_curl_storage(ch);
+ return ch;
+ }
+ return NULL;
+}
+static void safe_curl_dtor(void *p) {
+ http_request_storage *st = http_request_storage_get(p);
+
+ curl_easy_cleanup(p);
+
+ if (st) {
+ if (st->url) {
+ pefree(st->url, 1);
+ }
+ if (st->cookiestore) {
+ pefree(st->cookiestore, 1);
+ }
+ pefree(st, 1);
+ }
+}
+/* }}} */
+
/* {{{ MINIT */
PHP_MINIT_FUNCTION(http_request)
{
return FAILURE;
}
- if (SUCCESS != http_persistent_handle_provide("http_request", curl_easy_init, curl_easy_cleanup, curl_easy_duphandle)) {
+ if (SUCCESS != http_persistent_handle_provide("http_request", safe_curl_init, safe_curl_dtor, safe_curl_copy)) {
return FAILURE;
}
/* set context */
if (request) {
- curl_easy_setopt(ch, CURLOPT_PRIVATE, request);
curl_easy_setopt(ch, CURLOPT_DEBUGDATA, request);
- curl_easy_setopt(ch, CURLOPT_ERRORBUFFER, request->_error);
/* attach curl handle */
request->ch = ch;
phpstr_dtor(&request->conv.request);
phpstr_dtor(&request->conv.response);
http_request_body_dtor(request->body);
+ http_request_defaults(request);
if (request->ch) {
- http_request_defaults(request);
+ http_request_storage *st = http_request_storage_get(request->ch);
+
+ if (st) {
+ if (st->url) {
+ pefree(st->url, 1);
+ st->url = NULL;
+ }
+ if (st->cookiestore) {
+ pefree(st->cookiestore, 1);
+ st->cookiestore = NULL;
+ }
+ st->errorbuffer[0] = '\0';
+ }
}
- request->_error[0] = '\0';
}
/* }}} */
{
zval *zoption;
zend_bool range_req = 0;
+ http_request_storage *storage;
TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
HTTP_CHECK_CURL_INIT(request->ch, http_curl_init(request), return FAILURE);
+ if (!(storage = http_request_storage_get(request->ch))) {
+ return FAILURE;
+ }
+
/* set options */
- HTTP_CURL_OPT(CURLOPT_URL, request->url);
+ if (storage->url) {
+ pefree(storage->url, 1);
+ }
+ storage->url = pestrdup(request->url, 1);
+ HTTP_CURL_OPT(CURLOPT_URL, storage->url);
/* progress callback */
if ((zoption = http_request_option(request, options, "onprogress", -1))) {
if (Z_STRLEN_P(zoption)) {
HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(zoption), return FAILURE);
}
- HTTP_CURL_OPT(CURLOPT_COOKIEFILE, Z_STRVAL_P(zoption));
- HTTP_CURL_OPT(CURLOPT_COOKIEJAR, Z_STRVAL_P(zoption));
+ if (storage->cookiestore) {
+ pefree(storage->cookiestore, 1);
+ }
+ storage->cookiestore = pestrndup(Z_STRVAL_P(zoption), Z_STRLEN_P(zoption), 1);
+ HTTP_CURL_OPT(CURLOPT_COOKIEFILE, storage->cookiestore);
+ HTTP_CURL_OPT(CURLOPT_COOKIEJAR, storage->cookiestore);
}
/* maxfilesize */
retry:
if (CURLE_OK != (result = curl_easy_perform(request->ch))) {
- http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(result), request->_error, request->url);
+ http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(result), http_request_storage_get(request->ch)->errorbuffer, request->url);
if (request->_retry.count > tries++) {
switch (result) {
msg = curl_multi_info_read(pool->ch, &remaining);
if (msg && CURLMSG_DONE == msg->msg) {
if (CURLE_OK != msg->data.result) {
- http_request *r = NULL;
- curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &r);
- http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(msg->data.result), r?r->_error:"", r?r->url:"");
+ http_request_storage *st = http_request_storage_get(msg->easy_handle);
+ http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(msg->data.result), st?st->errorbuffer:"", st?st->url:"");
}
http_request_pool_apply_with_arg(pool, _http_request_pool_apply_responsehandler, msg->easy_handle);
}
} http_request;
+#ifndef pestrndup
+# define pestrndup(s,l,p) _pestrndup((s),(l),(p))
+static inline void *_pestrndup(const void *s, size_t l, int p)
+{
+ void *d = pemalloc(l+1, p);
+ memcpy(d, s, l);
+ ((char *) d)[l] = '\0';
+ return d;
+}
+#endif
+
+/* CURLOPT_PRIVATE storage living as long as a CURL handle */
+typedef struct _http_request_storage_t {
+ char *url;
+ char *cookiestore;
+ char errorbuffer[CURL_ERROR_SIZE];
+} http_request_storage;
+
+static inline http_request_storage *http_request_storage_get(CURL *ch)
+{
+ http_request_storage *st = NULL;
+ curl_easy_getinfo(ch, CURLINFO_PRIVATE, &st);
+ return st;
+}
+
#define http_curl_init(r) http_curl_init_ex(NULL, (r))
#define http_curl_init_ex(c, r) _http_curl_init_ex((c), (r) TSRMLS_CC)
PHP_HTTP_API CURL *_http_curl_init_ex(CURL *ch, http_request *request TSRMLS_DC);