From: Michael Wallner Date: Fri, 24 Feb 2006 20:31:53 +0000 (+0000) Subject: - add http_put_data() and HttpRequest::(set|get|add)PutData() X-Git-Tag: RELEASE_1_0_0RC1~24 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=6086d2a97b6959531650c886b10dc814ad2392f9;p=m6w6%2Fext-http - add http_put_data() and HttpRequest::(set|get|add)PutData() - fix HttpRequest::addRawPostData() - properly initialize request body in http_(post|put)_*() --- diff --git a/docs/functions.html b/docs/functions.html index 02b4840..1488408 100644 --- a/docs/functions.html +++ b/docs/functions.html @@ -386,7 +386,12 @@ See http_get() for a full list of available options.

Expects the second parameter to be a resource referencing an already
opened stream, from which the data to upload should be read.
See http_get() for a full list of available options.

-

Returns the HTTP response(s) as string on success. or FALSE on failure.

+

Returns the HTTP response(s) as string on success, or FALSE on failure.

+

string http_put_data(string url, string data[, array options[, array &info]])

+

Performs an HTTP PUT request on the supplied url.

+

Expects the second parameter to be a string containing the data to upload.
+See http_get() for a full list of available options.

+

Returns the HTTP response(s) as string on success, or FALSE on failure.

int http_request_method_register(string method)

Register a custom request method.

Expects a string parameter containing the request method name to register.

@@ -777,6 +782,22 @@ If the parameter is empty or omitted the put file will be unset.

string HttpRequest::getPutFile()

Get previously set put file.

Returns a string containing the path to the currently set put file.

+

bool HttpRequest::setPutData([string put_data])

+

Set PUT data to send, overwriting previously set PUT data.
+Affects only PUT requests.
+Only either PUT data or PUT file can be used for each request.
+PUT data has higher precedence and will be used even if a PUT
+file is set.

+

Accepts a string as parameter containing the data to upload.

+

Returns TRUE on success, or FALSE on failure.

+

bool HttpRequest::addPutData(string put_data)

+

Add PUT data, leaving previously set PUT data unchanged.
+Affects only PUT requests.

+

Expects a string as parameter containing the data to concatenate.

+

Returns TRUE on success, or FALSE on failure.

+

string HttpRequest::getPutData()

+

Get previously set PUT data.

+

Returns a string containing the currently set raw post data.

array HttpRequest::getResponseData()

Get all response data after the request has been sent.

Returns an associative array with the key "headers" containing an associative
@@ -1171,6 +1192,8 @@ http.cache_log is set.

  • http_put_stream
  • +
  • http_put_data +
  • http_request_method_register
  • http_request_method_unregister @@ -1295,6 +1318,9 @@ http.cache_log is set.

  • HttpRequest::getPostFiles()
  • HttpRequest::setPutFile()
  • HttpRequest::getPutFile()
  • +
  • HttpRequest::setPutData()
  • +
  • HttpRequest::addPutData()
  • +
  • HttpRequest::getPutData()
  • HttpRequest::getResponseData()
  • HttpRequest::getResponseHeader()
  • HttpRequest::getResponseCookies()
  • @@ -1369,7 +1395,7 @@ http.cache_log is set.

    -

    Generated at: Mon, 20 Feb 2006 17:41:32 +0100

    +

    Generated at: Fri, 24 Feb 2006 21:29:29 +0100

    diff --git a/http.c b/http.c index 75ca088..2f2011f 100644 --- a/http.c +++ b/http.c @@ -102,6 +102,7 @@ zend_function_entry http_functions[] = { PHP_FE(http_head, http_arg_pass_ref_3) PHP_FE(http_post_data, http_arg_pass_ref_4) PHP_FE(http_post_fields, http_arg_pass_ref_5) + PHP_FE(http_put_data, http_arg_pass_ref_4) PHP_FE(http_put_file, http_arg_pass_ref_4) PHP_FE(http_put_stream, http_arg_pass_ref_4) #endif diff --git a/http_functions.c b/http_functions.c index 8ce961f..8f91300 100644 --- a/http_functions.c +++ b/http_functions.c @@ -1517,12 +1517,8 @@ PHP_FUNCTION(http_put_file) RETVAL_FALSE; - body.type = HTTP_REQUEST_BODY_UPLOADFILE; - body.data = stream; - body.size = ssb.sb.st_size; - http_request_init_ex(&request, NULL, HTTP_PUT, URL); - request.body = &body; + request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 1); if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) { http_request_exec(&request); if (info) { @@ -1530,8 +1526,6 @@ PHP_FUNCTION(http_put_file) } RETVAL_RESPONSE_OR_BODY(request); } - http_request_body_dtor(&body); - request.body = NULL; http_request_dtor(&request); } /* }}} */ @@ -1544,7 +1538,7 @@ PHP_FUNCTION(http_put_file) * opened stream, from which the data to upload should be read. * See http_get() for a full list of available options. * - * Returns the HTTP response(s) as string on success. or FALSE on failure. + * Returns the HTTP response(s) as string on success, or FALSE on failure. */ PHP_FUNCTION(http_put_stream) { @@ -1572,12 +1566,49 @@ PHP_FUNCTION(http_put_stream) RETVAL_FALSE; - body.type = HTTP_REQUEST_BODY_UPLOADFILE; - body.data = stream; - body.size = ssb.sb.st_size; + http_request_init_ex(&request, NULL, HTTP_PUT, URL); + request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 0); + if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) { + http_request_exec(&request); + if (info) { + http_request_info(&request, Z_ARRVAL_P(info)); + } + RETVAL_RESPONSE_OR_BODY(request); + } + http_request_dtor(&request); +} +/* }}} */ - http_request_init_ex(&request, NULL, HTTP_POST, URL); - request.body = &body; +/* {{{ proto string http_put_data(string url, string data[, array options[, array &info]]) + * + * Performs an HTTP PUT request on the supplied url. + * + * Expects the second parameter to be a string containing the data to upload. + * See http_get() for a full list of available options. + * + * Returns the HTTP response(s) as string on success, or FALSE on failure. + */ +PHP_FUNCTION(http_put_data) +{ + char *URL, *data; + int URL_len, data_len; + zval *options = NULL, *info = NULL; + http_request_body body; + http_request request; + + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &data, &data_len, &options, &info)) { + RETURN_FALSE; + } + + if (info) { + zval_dtor(info); + array_init(info); + } + + RETVAL_FALSE; + + http_request_init_ex(&request, NULL, HTTP_PUT, URL); + request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_CSTRING, data, data_len, 0); if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) { http_request_exec(&request); if (info) { @@ -1585,7 +1616,6 @@ PHP_FUNCTION(http_put_stream) } RETVAL_RESPONSE_OR_BODY(request); } - request.body = NULL; http_request_dtor(&request); } /* }}} */ diff --git a/http_request_api.c b/http_request_api.c index e34b866..8e8fa9c 100644 --- a/http_request_api.c +++ b/http_request_api.c @@ -827,15 +827,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 +912,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, 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; } /* }}} */ @@ -953,12 +972,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; } /* }}} */ diff --git a/http_request_body_api.c b/http_request_body_api.c index 14a235d..dd76edb 100644 --- a/http_request_body_api.c +++ b/http_request_body_api.c @@ -30,6 +30,7 @@ PHP_HTTP_API http_request_body *_http_request_body_init_ex(http_request_body *bo body->type = type; body->free = free; + body->priv = 0; body->data = data; body->size = size; diff --git a/http_request_object.c b/http_request_object.c index 3bf2f98..87bfb65 100644 --- a/http_request_object.c +++ b/http_request_object.c @@ -130,6 +130,15 @@ HTTP_BEGIN_ARGS(setPutFile, 0) HTTP_ARG_VAL(filename, 0) HTTP_END_ARGS; +HTTP_EMPTY_ARGS(getPutData); +HTTP_BEGIN_ARGS(setPutData, 0) + HTTP_ARG_VAL(put_data, 0) +HTTP_END_ARGS; + +HTTP_BEGIN_ARGS(addPutData, 1) + HTTP_ARG_VAL(put_data, 0) +HTTP_END_ARGS; + HTTP_EMPTY_ARGS(getResponseData); HTTP_BEGIN_ARGS(getResponseHeader, 0) HTTP_ARG_VAL(name, 0) @@ -261,6 +270,10 @@ zend_function_entry http_request_object_fe[] = { HTTP_REQUEST_ME(setPutFile, ZEND_ACC_PUBLIC) HTTP_REQUEST_ME(getPutFile, ZEND_ACC_PUBLIC) + HTTP_REQUEST_ME(setPutData, ZEND_ACC_PUBLIC) + HTTP_REQUEST_ME(getPutData, ZEND_ACC_PUBLIC) + HTTP_REQUEST_ME(addPutData, ZEND_ACC_PUBLIC) + HTTP_REQUEST_ME(send, ZEND_ACC_PUBLIC) HTTP_REQUEST_ME(getResponseData, ZEND_ACC_PUBLIC) @@ -366,6 +379,7 @@ static inline void _http_request_object_declare_default_properties(TSRMLS_D) DCL_PROP(PRIVATE, string, rawPostData, ""); DCL_PROP(PRIVATE, string, queryData, ""); DCL_PROP(PRIVATE, string, putFile, ""); + DCL_PROP(PRIVATE, string, putData, ""); DCL_PROP_N(PRIVATE, history); DCL_PROP(PUBLIC, bool, recordHistory, 0); @@ -432,6 +446,35 @@ void _http_request_object_free(zend_object *object TSRMLS_DC) efree(o); } +#define http_request_object_check_request_content_type(t) _http_request_object_check_request_content_type((t) TSRMLS_CC) +static inline void _http_request_object_check_request_content_type(zval *this_ptr TSRMLS_DC) +{ + zval *ctype = GET_PROP(contentType); + + if (Z_STRLEN_P(ctype)) { + zval **headers, *opts = GET_PROP(options); + + if ( (Z_TYPE_P(opts) == IS_ARRAY) && + (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) && + (Z_TYPE_PP(headers) == IS_ARRAY)) { + zval **ct_header; + + /* only override if not already set */ + if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(headers), "Content-Type", sizeof("Content-Type"), (void **) &ct_header))) { + add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1); + } + } else { + zval *headers; + + MAKE_STD_ZVAL(headers); + array_init(headers); + add_assoc_stringl(headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1); + zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, headers); + zval_ptr_dtor(&headers); + } + } +} + STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ptr TSRMLS_DC) { STATUS status = SUCCESS; @@ -449,13 +492,21 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ case HTTP_PUT: { - php_stream_statbuf ssb; - php_stream *stream = php_stream_open_wrapper_ex(Z_STRVAL_P(GET_PROP(putFile)), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT); + zval *put_data = GET_PROP(putData); - if (stream && !php_stream_stat(stream, &ssb)) { - obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 1); + http_request_object_check_request_content_type(getThis()); + if (Z_STRLEN_P(put_data)) { + obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_CSTRING, + estrndup(Z_STRVAL_P(put_data), Z_STRLEN_P(put_data)), Z_STRLEN_P(put_data), 1); } else { - status = FAILURE; + php_stream_statbuf ssb; + php_stream *stream = php_stream_open_wrapper_ex(Z_STRVAL_P(GET_PROP(putFile)), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT); + + if (stream && !php_stream_stat(stream, &ssb)) { + obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 1); + } else { + status = FAILURE; + } } } break; @@ -467,34 +518,9 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ zval *raw_data = GET_PROP(rawPostData); if (Z_STRLEN_P(raw_data)) { - zval *ctype = GET_PROP(contentType); - - if (Z_STRLEN_P(ctype)) { - zval **headers, *opts = GET_PROP(options); - - if ( (Z_TYPE_P(opts) == IS_ARRAY) && - (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) && - (Z_TYPE_PP(headers) == IS_ARRAY)) { - zval **ct_header; - - /* only override if not already set */ - if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(headers), "Content-Type", sizeof("Content-Type"), (void **) &ct_header))) { - add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1); - } - } else { - zval *headers; - - MAKE_STD_ZVAL(headers); - array_init(headers); - add_assoc_stringl(headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1); - zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, headers); - zval_ptr_dtor(&headers); - } - } - + http_request_object_check_request_content_type(getThis()); obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_CSTRING, estrndup(Z_STRVAL_P(raw_data), Z_STRLEN_P(raw_data)), Z_STRLEN_P(raw_data), 1); - } else { zval *zfields = GET_PROP(postFields), *zfiles = GET_PROP(postFiles); HashTable *fields; @@ -1292,13 +1318,15 @@ PHP_METHOD(HttpRequest, addRawPostData) } if (data_len) { - zval *data = zval_copy(IS_STRING, GET_PROP(rawPostData)); + zval *data = GET_PROP(rawPostData); - Z_STRVAL_P(data) = erealloc(Z_STRVAL_P(data), (Z_STRLEN_P(data) += data_len) + 1); - Z_STRVAL_P(data)[Z_STRLEN_P(data)] = '\0'; - memcpy(Z_STRVAL_P(data) + Z_STRLEN_P(data) - data_len, raw_data, data_len); - SET_PROP(rawPostData, data); - zval_free(&data); + if (Z_STRLEN_P(data)) { + Z_STRVAL_P(data) = erealloc(Z_STRVAL_P(data), (Z_STRLEN_P(data) += data_len) + 1); + Z_STRVAL_P(data)[Z_STRLEN_P(data)] = '\0'; + memcpy(Z_STRVAL_P(data) + Z_STRLEN_P(data) - data_len, raw_data, data_len); + } else { + UPD_STRL(putData, raw_data, data_len); + } } RETURN_TRUE; @@ -1458,6 +1486,88 @@ PHP_METHOD(HttpRequest, getPutFile) } /* }}} */ +/* {{{ proto bool HttpRequest::setPutData([string put_data]) + * + * Set PUT data to send, overwriting previously set PUT data. + * Affects only PUT requests. + * Only either PUT data or PUT file can be used for each request. + * PUT data has higher precedence and will be used even if a PUT + * file is set. + * + * Accepts a string as parameter containing the data to upload. + * + * Returns TRUE on success, or FALSE on failure. + */ +PHP_METHOD(HttpRequest, setPutData) +{ + char *put_data = NULL; + int data_len = 0; + + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &put_data, &data_len)) { + RETURN_FALSE; + } + + if (!put_data) { + put_data = ""; + } + + UPD_STRL(putData, put_data, data_len); + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto bool HttpRequest::addPutData(string put_data) + * + * Add PUT data, leaving previously set PUT data unchanged. + * Affects only PUT requests. + * + * Expects a string as parameter containing the data to concatenate. + * + * Returns TRUE on success, or FALSE on failure. + */ +PHP_METHOD(HttpRequest, addPutData) +{ + char *put_data; + int data_len; + + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &put_data, &data_len)) { + RETURN_FALSE; + } + + if (data_len) { + char *new_data; + size_t new_data_len; + zval *data = GET_PROP(putData); + + if (Z_STRLEN_P(data)) { + Z_STRVAL_P(data) = erealloc(Z_STRVAL_P(data), (Z_STRLEN_P(data) += data_len) + 1); + Z_STRVAL_P(data)[Z_STRLEN_P(data)] = '\0'; + memcpy(Z_STRVAL_P(data) + Z_STRLEN_P(data) - data_len, put_data, data_len); + } else { + UPD_STRL(putData, put_data, data_len); + } + } + + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto string HttpRequest::getPutData() + * + * Get previously set PUT data. + * + * Returns a string containing the currently set raw post data. + */ +PHP_METHOD(HttpRequest, getPutData) +{ + NO_ARGS; + + IF_RETVAL_USED { + RETURN_PROP(putData); + } +} +/* }}} */ + /* {{{ proto array HttpRequest::getResponseData() * * Get all response data after the request has been sent. diff --git a/package2.xml b/package2.xml index fe0878e..df982fc 100644 --- a/package2.xml +++ b/package2.xml @@ -37,8 +37,8 @@ HttpResponse 2006-00-00 - 0.24.1 - 0.24.0 + 0.25.0 + 0.25.0 beta @@ -46,8 +46,10 @@ HttpResponse BSD, revised diff --git a/php_http.h b/php_http.h index beddf2a..f8f3e96 100644 --- a/php_http.h +++ b/php_http.h @@ -15,7 +15,7 @@ #ifndef PHP_EXT_HTTP_H #define PHP_EXT_HTTP_H -#define PHP_EXT_HTTP_VERSION "0.24.1" +#define PHP_EXT_HTTP_VERSION "0.25.0dev" #ifdef HAVE_CONFIG_H # include "config.h" @@ -178,6 +178,7 @@ PHP_FUNCTION(http_get); PHP_FUNCTION(http_head); PHP_FUNCTION(http_post_data); PHP_FUNCTION(http_post_fields); +PHP_FUNCTION(http_put_data); PHP_FUNCTION(http_put_file); PHP_FUNCTION(http_put_stream); #endif /* HTTP_HAVE_CURL */ diff --git a/php_http_request_body_api.h b/php_http_request_body_api.h index 5786d6c..346544d 100644 --- a/php_http_request_body_api.h +++ b/php_http_request_body_api.h @@ -22,8 +22,9 @@ #define HTTP_REQUEST_BODY_CURLPOST 2 #define HTTP_REQUEST_BODY_UPLOADFILE 3 typedef struct { - uint type:31; + uint type:3; uint free:1; + uint priv:28; void *data; size_t size; } http_request_body; diff --git a/php_http_request_object.h b/php_http_request_object.h index 4d2ab24..00c2eb0 100644 --- a/php_http_request_object.h +++ b/php_http_request_object.h @@ -76,6 +76,9 @@ PHP_METHOD(HttpRequest, setPostFiles); PHP_METHOD(HttpRequest, getPostFiles); PHP_METHOD(HttpRequest, setPutFile); PHP_METHOD(HttpRequest, getPutFile); +PHP_METHOD(HttpRequest, getPutData); +PHP_METHOD(HttpRequest, setPutData); +PHP_METHOD(HttpRequest, addPutData); PHP_METHOD(HttpRequest, send); PHP_METHOD(HttpRequest, getResponseData); PHP_METHOD(HttpRequest, getResponseHeader);