X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_request_api.c;h=41801656e82849fad99c28d5c2bca5a24ee05be0;hp=e34b866330acc551cb4dccc08b36eef94f40616a;hb=e37040ebf8a470c77c7ae3498ee582ca20db259c;hpb=60086d086e7f0dbfccf61931c1b2e2b410ccb5bb diff --git a/http_request_api.c b/http_request_api.c index e34b866..4180165 100644 --- a/http_request_api.c +++ b/http_request_api.c @@ -147,6 +147,11 @@ PHP_MINIT_FUNCTION(http_request) HTTP_LONG_CONSTANT("HTTP_VERSION_1_0", CURL_HTTP_VERSION_1_0); HTTP_LONG_CONSTANT("HTTP_VERSION_1_1", CURL_HTTP_VERSION_1_1); +#if HTTP_CURL_VERSION(7,15,2) + HTTP_LONG_CONSTANT("HTTP_PROXY_SOCKS4", CURLPROXY_SOCKS4); +#endif + HTTP_LONG_CONSTANT("HTTP_PROXY_SOCKS5", CURLPROXY_SOCKS5); + HTTP_LONG_CONSTANT("HTTP_PROXY_HTTP", CURLPROXY_HTTP); return SUCCESS; } /* }}} */ @@ -425,6 +430,7 @@ PHP_HTTP_API void _http_request_defaults(http_request *request) HTTP_CURL_OPT(CURLOPT_NOPROGRESS, 1); HTTP_CURL_OPT(CURLOPT_PROXY, NULL); HTTP_CURL_OPT(CURLOPT_PROXYPORT, 0); + HTTP_CURL_OPT(CURLOPT_PROXYTYPE, 0); HTTP_CURL_OPT(CURLOPT_PROXYUSERPWD, NULL); HTTP_CURL_OPT(CURLOPT_PROXYAUTH, 0); HTTP_CURL_OPT(CURLOPT_INTERFACE, NULL); @@ -447,6 +453,7 @@ PHP_HTTP_API void _http_request_defaults(http_request *request) #endif HTTP_CURL_OPT(CURLOPT_COOKIEFILE, NULL); HTTP_CURL_OPT(CURLOPT_COOKIEJAR, NULL); + HTTP_CURL_OPT(CURLOPT_RANGE, NULL); HTTP_CURL_OPT(CURLOPT_RESUME_FROM, 0); HTTP_CURL_OPT(CURLOPT_MAXFILESIZE, 0); HTTP_CURL_OPT(CURLOPT_TIMECONDITION, 0); @@ -526,7 +533,10 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti if (Z_STRLEN_P(zoption)) { HTTP_CURL_OPT(CURLOPT_PROXY, Z_STRVAL_P(zoption)); } - + /* type */ + if ((zoption = http_request_option(request, options, "proxytype", IS_LONG))) { + HTTP_CURL_OPT(CURLOPT_PROXYTYPE, Z_LVAL_P(zoption)); + } /* port */ if ((zoption = http_request_option(request, options, "proxyport", IS_LONG))) { HTTP_CURL_OPT(CURLOPT_PROXYPORT, Z_LVAL_P(zoption)); @@ -547,17 +557,17 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti #if HTTP_CURL_VERSION(7,15,2) if ((zoption = http_request_option(request, options, "portrange", IS_ARRAY))) { - zval *prs, *pre; + zval **prs, **pre; zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption)); - if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &prs)) { + if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &prs)) { zend_hash_move_forward(Z_ARRVAL_P(zoption)); - if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &pre)) { + if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &pre)) { zval *prs_cpy = zval_copy(IS_LONG, *prs), *pre_cpy = zval_copy(IS_LONG, *pre); if (Z_LVAL_P(prs_cpy) && Z_LVAL_P(pre_cpy)) { HTTP_CURL_OPT(CURLOPT_LOCALPORT, MIN(Z_LVAL_P(prs_cpy), Z_LVAL_P(pre_cpy))); - HTTP_CURL_OPT(CURLOPT_LOCALPORTRANGE, ABS(Z_LVAL_P(prs_cpy)-Z_LVAL_P(pre_cpy))+1L); + HTTP_CURL_OPT(CURLOPT_LOCALPORTRANGE, labs(Z_LVAL_P(prs_cpy)-Z_LVAL_P(pre_cpy))+1L); } zval_free(&prs_cpy); zval_free(&pre_cpy); @@ -609,6 +619,46 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti range_req = 1; HTTP_CURL_OPT(CURLOPT_RESUME_FROM, Z_LVAL_P(zoption)); } + /* or range of kind array(array(0,499), array(100,1499)) */ + else if ((zoption = http_request_option(request, options, "range", IS_ARRAY)) && zend_hash_num_elements(Z_ARRVAL_P(zoption))) { + HashPosition pos1, pos2; + zval **rr, **rb, **re; + phpstr rs; + + phpstr_init(&rs); + FOREACH_VAL(pos1, zoption, rr) { + if (Z_TYPE_PP(rr) == IS_ARRAY) { + zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(rr), &pos2); + if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &rb, &pos2)) { + zend_hash_move_forward_ex(Z_ARRVAL_PP(rr), &pos2); + if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &re, &pos2)) { + 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))) && + ((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)))) { + zval *rbl = zval_copy(IS_LONG, *rb), *rel = zval_copy(IS_LONG, *re); + + if ((Z_LVAL_P(rbl) >= 0) && (Z_LVAL_P(rel) >= 0)) { + phpstr_appendf(&rs, "%ld-%ld,", Z_LVAL_P(rbl), Z_LVAL_P(rel)); + } + zval_free(&rbl); + zval_free(&rel); + } + } + } + } + } + + if (PHPSTR_LEN(&rs)) { + zval *cached_range; + + /* ditch last comma */ + PHPSTR_VAL(&rs)[PHPSTR_LEN(&rs)-- -1] = '\0'; + /* cache string */ + MAKE_STD_ZVAL(cached_range); + ZVAL_STRINGL(cached_range, PHPSTR_VAL(&rs), PHPSTR_LEN(&rs), 0); + HTTP_CURL_OPT(CURLOPT_RANGE, Z_STRVAL_P(http_request_option_cache(request, "range", cached_range))); + zval_ptr_dtor(&cached_range); + } + } /* additional headers, array('name' => 'value') */ if (request->_cache.headers) { @@ -623,7 +673,7 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti FOREACH_KEY(pos, zoption, header_key, header_idx) { if (header_key) { zval **header_val; - if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void **) &header_val, &pos)) { + if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void *) &header_val, &pos)) { char header[1024] = {0}; ZVAL_ADDREF(*header_val); @@ -691,7 +741,7 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti FOREACH_KEY(pos, zoption, cookie_key, cookie_idx) { if (cookie_key) { zval **cookie_val; - if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void **) &cookie_val, &pos)) { + if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void *) &cookie_val, &pos)) { zval *val = zval_copy(IS_STRING, *cookie_val); phpstr_appendf(&request->_cache.cookies, "%s=%s; ", cookie_key, Z_STRVAL_P(val)); zval_free(&val); @@ -827,15 +877,17 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti /* nothing */ break; - case HTTP_REQUEST_BODY_CSTRING: - HTTP_CURL_OPT(CURLOPT_POSTFIELDS, request->body->data); - HTTP_CURL_OPT(CURLOPT_POSTFIELDSIZE, request->body->size); - break; - case HTTP_REQUEST_BODY_CURLPOST: HTTP_CURL_OPT(CURLOPT_HTTPPOST, (struct curl_httppost *) request->body->data); break; + case HTTP_REQUEST_BODY_CSTRING: + if (request->meth != HTTP_PUT) { + HTTP_CURL_OPT(CURLOPT_POSTFIELDS, request->body->data); + HTTP_CURL_OPT(CURLOPT_POSTFIELDSIZE, request->body->size); + break; + } + /* fallthrough, PUT/UPLOAD _needs_ READDATA */ case HTTP_REQUEST_BODY_UPLOADFILE: HTTP_CURL_OPT(CURLOPT_IOCTLDATA, request); HTTP_CURL_OPT(CURLOPT_READDATA, request); @@ -910,11 +962,28 @@ static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *ct { http_request *request = (http_request *) ctx; TSRMLS_FETCH_FROM_CTX(request->tsrm_ls); - - if (request->body == NULL || request->body->type != HTTP_REQUEST_BODY_UPLOADFILE) { - return 0; + + if (request->body) { + switch (request->body->type) + { + case HTTP_REQUEST_BODY_CSTRING: + { + size_t out = MIN(len * n, request->body->size - request->body->priv); + + if (out) { + memcpy(data, ((char *) request->body->data) + request->body->priv, out); + request->body->priv += out; + return out; + } + } + break; + + case HTTP_REQUEST_BODY_UPLOADFILE: + return php_stream_read((php_stream *) request->body->data, data, len * n); + break; + } } - return php_stream_read((php_stream *) request->body->data, data, len * n); + return 0; } /* }}} */ @@ -935,7 +1004,9 @@ static int http_curl_progress_callback(void *ctx, double dltotal, double dlnow, add_assoc_double(param, "ultotal", ultotal); add_assoc_double(param, "ulnow", ulnow); - call_user_function(EG(function_table), NULL, request->_progress_callback, &retval, 1, ¶m TSRMLS_CC); + with_error_handling(EH_NORMAL, NULL) { + call_user_function(EG(function_table), NULL, request->_progress_callback, &retval, 1, ¶m TSRMLS_CC); + } end_error_handling(); zval_ptr_dtor(¶m); zval_dtor(&retval); @@ -953,12 +1024,24 @@ static curlioerr http_curl_ioctl_callback(CURL *ch, curliocmd cmd, void *ctx) if (cmd != CURLIOCMD_RESTARTREAD) { return CURLIOE_UNKNOWNCMD; } - if ( request->body == NULL || - request->body->type != HTTP_REQUEST_BODY_UPLOADFILE || - SUCCESS != php_stream_rewind((php_stream *) request->body->data)) { - return CURLIOE_FAILRESTART; + + if (request->body) { + switch (request->body->type) + { + case HTTP_REQUEST_BODY_CSTRING: + request->body->priv = 0; + return CURLIOE_OK; + break; + + case HTTP_REQUEST_BODY_UPLOADFILE: + if (SUCCESS == php_stream_rewind((php_stream *) request->body->data)) { + return CURLIOE_OK; + } + break; + } } - return CURLIOE_OK; + + return CURLIOE_FAILRESTART; } /* }}} */ @@ -1024,9 +1107,9 @@ static inline zval *_http_request_option_ex(http_request *r, HashTable *options, if (!options || #ifdef ZEND_ENGINE_2 - (SUCCESS != zend_hash_quick_find(options, key, keylen, h, (void **) &zoption)) + (SUCCESS != zend_hash_quick_find(options, key, keylen, h, (void *) &zoption)) #else - (SUCCESS != zend_hash_find(options, key, keylen, (void **) &zoption)) + (SUCCESS != zend_hash_find(options, key, keylen, (void *) &zoption)) #endif ) { return NULL;