RETVAL_FALSE;
- body.type = HTTP_REQUEST_BODY_CSTRING;
- body.data = postdata;
- body.size = postdata_len;
-
http_request_init_ex(&request, NULL, HTTP_POST, URL);
- request.body = &body;
+ request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_CSTRING, postdata, postdata_len, 0);
if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
http_request_exec(&request);
if (info) {
}
RETVAL_RESPONSE_OR_BODY(request);
}
- request.body = NULL;
http_request_dtor(&request);
}
/* }}} */
RETURN_FALSE;
}
- if (SUCCESS != http_request_body_fill(&body, Z_ARRVAL_P(fields), files ? Z_ARRVAL_P(files) : NULL)) {
+ if (!http_request_body_fill(&body, Z_ARRVAL_P(fields), files ? Z_ARRVAL_P(files) : NULL)) {
RETURN_FALSE;
}
request.body = &body;
if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
http_request_exec(&request);
- http_request_body_dtor(&body);
if (info) {
http_request_info(&request, Z_ARRVAL_P(info));
}
RETVAL_RESPONSE_OR_BODY(request);
}
- http_request_body_dtor(&body);
- request.body = NULL;
http_request_dtor(&request);
}
/* }}} */
}
}
-PHP_HTTP_API http_message *_http_message_init_ex(http_message *message, http_message_type type)
+PHP_HTTP_API http_message *_http_message_init_ex(http_message *message, http_message_type type ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
{
if (!message) {
- message = ecalloc(1, sizeof(http_message));
+ message = ecalloc_rel(1, sizeof(http_message));
}
http_message_init_type(message, type);
}
}
-PHP_HTTP_API http_message *_http_message_parse_ex(http_message *msg, const char *message, size_t message_length TSRMLS_DC)
+PHP_HTTP_API http_message *_http_message_parse_ex(http_message *msg, const char *message, size_t message_length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
{
const char *body = NULL;
zend_bool free_msg = msg ? 0 : 1;
}
if ((request.meth = http_request_method_exists(1, 0, message->http.info.request.method))) {
- http_request_body body = {HTTP_REQUEST_BODY_CSTRING, PHPSTR_VAL(message), PHPSTR_LEN(message)};
+ http_request_body body;
http_request_init_ex(&request, NULL, request.meth, uri);
- request.body = &body;
+ request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_CSTRING, PHPSTR_VAL(message), PHPSTR_LEN(message), 0);
if (SUCCESS == (rs = http_request_prepare(&request, NULL))) {
http_request_exec(&request);
}
- request.body = NULL;
http_request_dtor(&request);
} else {
http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD,
/* }}} */
/* {{{ http_request *http_request_init(http_request *) */
-PHP_HTTP_API http_request *_http_request_init_ex(http_request *request, CURL *ch, http_request_method meth, const char *url TSRMLS_DC)
+PHP_HTTP_API http_request *_http_request_init_ex(http_request *request, CURL *ch, http_request_method meth, const char *url ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
{
http_request *r;
if (request) {
r = request;
} else {
- r = emalloc(sizeof(http_request));
+ r = emalloc_rel(sizeof(http_request));
}
memset(r, 0, sizeof(http_request));
phpstr_init(&r->_cache.cookies);
zend_hash_init(&r->_cache.options, 0, NULL, ZVAL_PTR_DTOR, 0);
- TSRMLS_SET_CTX(request->tsrm_ls);
+ TSRMLS_SET_CTX(r->tsrm_ls);
return r;
}
{
if (*request) {
TSRMLS_FETCH_FROM_CTX((*request)->tsrm_ls);
+ http_request_body_free(&(*request)->body);
http_request_dtor(*request);
efree(*request);
*request = NULL;
request->conv.last_type = 0;
phpstr_dtor(&request->conv.request);
phpstr_dtor(&request->conv.response);
- http_request_body_free(&request->body);
+ http_request_body_dtor(request->body);
}
/* }}} */
ZEND_EXTERN_MODULE_GLOBALS(http);
/* {{{ http_request_body *http_request_body_new() */
-PHP_HTTP_API http_request_body *_http_request_body_new(ZEND_FILE_LINE_D ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
+PHP_HTTP_API http_request_body *_http_request_body_init_ex(http_request_body *body, int type, void *data, size_t size, zend_bool free ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
{
- return (http_request_body *) ecalloc_rel(1, sizeof(http_request_body));
+ if (!body) {
+ body = emalloc_rel(sizeof(http_request_body));
+ }
+
+ body->type = type;
+ body->free = free;
+ body->data = data;
+ body->size = size;
+
+ return body;
}
/* }}} */
-/* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
-PHP_HTTP_API STATUS _http_request_body_fill(http_request_body *body, HashTable *fields, HashTable *files TSRMLS_DC)
+/* {{{ http_request_body *http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
+PHP_HTTP_API http_request_body *_http_request_body_fill(http_request_body *body, HashTable *fields, HashTable *files ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
{
if (files && (zend_hash_num_elements(files) > 0)) {
char *key = NULL;
if (CURLE_OK != err) {
http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not encode post fields: %s", curl_easy_strerror(err));
curl_formfree(http_post_data[0]);
- return FAILURE;
+ return NULL;
}
/* reset */
if (CURLE_OK != err) {
http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not encode post files: %s", curl_easy_strerror(err));
curl_formfree(http_post_data[0]);
- return FAILURE;
+ return NULL;
}
}
}
-
- body->type = HTTP_REQUEST_BODY_CURLPOST;
- body->data = http_post_data[0];
- body->size = 0;
+
+ return http_request_body_init_ex(body, HTTP_REQUEST_BODY_CURLPOST, http_post_data[0], 0, 1);
} else {
char *encoded;
if (SUCCESS != http_urlencode_hash_ex(fields, 1, NULL, 0, &encoded, &encoded_len)) {
http_error(HE_WARNING, HTTP_E_ENCODING, "Could not encode post data");
- return FAILURE;
+ return NULL;
}
-
- body->type = HTTP_REQUEST_BODY_CSTRING;
- body->data = encoded;
- body->size = encoded_len;
+
+ return http_request_body_init_ex(body, HTTP_REQUEST_BODY_CSTRING, encoded, encoded_len, 1);
}
-
- return SUCCESS;
}
/* }}} */
/* {{{ void http_request_body_dtor(http_request_body *) */
PHP_HTTP_API void _http_request_body_dtor(http_request_body *body TSRMLS_DC)
{
- if (body) {
+ if (body && body->free) {
switch (body->type)
{
case HTTP_REQUEST_BODY_CSTRING:
o = ecalloc(1, sizeof(http_request_object));
o->zo.ce = ce;
- http_request_init_ex(&o->request, ch, 0, NULL);
+ o->request = http_request_init_ex(NULL, ch, 0, NULL);
phpstr_init(&o->history);
if (ptr) {
getObject(http_request_object, old_obj);
old_zo = zend_objects_get_address(this_ptr TSRMLS_CC);
- new_ov = http_request_object_new_ex(old_zo->ce, curl_easy_duphandle(old_obj->request.ch), &new_obj);
+ new_ov = http_request_object_new_ex(old_zo->ce, curl_easy_duphandle(old_obj->request->ch), &new_obj);
zend_objects_clone_members(&new_obj->zo, new_ov, old_zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
phpstr_append(&new_obj->history, old_obj->history.data, old_obj->history.used);
- phpstr_append(&new_obj->request.conv.request, old_obj->request.conv.request.data, old_obj->request.conv.request.used);
- phpstr_append(&new_obj->request.conv.response, old_obj->request.conv.response.data, old_obj->request.conv.response.used);
+ phpstr_append(&new_obj->request->conv.request, old_obj->request->conv.request.data, old_obj->request->conv.request.used);
+ phpstr_append(&new_obj->request->conv.response, old_obj->request->conv.response.data, old_obj->request->conv.response.used);
return new_ov;
}
zend_hash_destroy(OBJ_PROP(o));
FREE_HASHTABLE(OBJ_PROP(o));
}
- http_request_dtor(&o->request);
+ http_request_free(&o->request);
phpstr_dtor(&o->history);
efree(o);
}
zval *URL, *URL_p, *meth_p;
STATUS status = SUCCESS;
- http_request_reset(&obj->request);
- HTTP_CHECK_CURL_INIT(obj->request.ch, curl_easy_init(), return FAILURE);
+ http_request_reset(obj->request);
+ HTTP_CHECK_CURL_INIT(obj->request->ch, curl_easy_init(), return FAILURE);
URL = convert_to_type_ex(IS_STRING, GET_PROP(obj, url), &URL_p);
- obj->request.url = http_absolute_uri_ex(Z_STRVAL_P(URL), Z_STRLEN_P(URL), NULL, 0, NULL, 0, 0);
+ obj->request->url = http_absolute_uri_ex(Z_STRVAL_P(URL), Z_STRLEN_P(URL), NULL, 0, NULL, 0, 0);
if (URL_p) {
zval_ptr_dtor(&URL_p);
}
- if (!obj->request.url) {
+ if (!obj->request->url) {
return FAILURE;
}
- switch (obj->request.meth = Z_LVAL_P(convert_to_type_ex(IS_LONG, GET_PROP(obj, method), &meth_p)))
+ switch (obj->request->meth = Z_LVAL_P(convert_to_type_ex(IS_LONG, GET_PROP(obj, method), &meth_p)))
{
case HTTP_GET:
case HTTP_HEAD:
php_stream *stream = php_stream_open_wrapper(Z_STRVAL_P(GET_PROP(obj, putFile)), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
if (stream && !php_stream_stat(stream, &ssb)) {
- obj->request.body = http_request_body_new();
- obj->request.body->type = HTTP_REQUEST_BODY_UPLOADFILE;
- obj->request.body->data = stream;
- obj->request.body->size = ssb.sb.st_size;
+ http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 1);
} else {
status = FAILURE;
}
/* check for raw post data */
zval *raw_data_p, *raw_data = convert_to_type_ex(IS_STRING, GET_PROP(obj, rawPostData), &raw_data_p);
- obj->request.body = http_request_body_new();
if (Z_STRLEN_P(raw_data)) {
zval *ctype_p, *ctype = convert_to_type_ex(IS_STRING, GET_PROP(obj, contentType), &ctype_p);
zval_ptr_dtor(&ctype_p);
}
- obj->request.body->type = HTTP_REQUEST_BODY_CSTRING;
- obj->request.body->data = estrndup(Z_STRVAL_P(raw_data), Z_STRLEN_P(raw_data));
- obj->request.body->size = Z_STRLEN_P(raw_data);
+ 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 {
- status = http_request_body_fill(obj->request.body, Z_ARRVAL_P(GET_PROP(obj, postFields)), Z_ARRVAL_P(GET_PROP(obj, postFiles)));
+ zval *fields_cpy, *files_cpy;
+ HashTable *fields = Z_ARRVAL_P(convert_to_type_ex(IS_ARRAY, GET_PROP(obj, postFields), &fields_cpy));
+ HashTable *files = Z_ARRVAL_P(convert_to_type_ex(IS_ARRAY, GET_PROP(obj, postFiles), &files_cpy));
+
+ if (!(obj->request->body = http_request_body_fill(obj->request->body, fields, files))) {
+ status = FAILURE;
+ }
+
+ if (fields_cpy) {
+ zval_ptr_dtor(&fields_cpy);
+ }
+ if (files_cpy) {
+ zval_ptr_dtor(&files_cpy);
+ }
}
if (raw_data_p) {
zval *opt_p, *options = convert_to_type_ex(IS_ARRAY, GET_PROP(obj, options), &opt_p);
if (Z_STRLEN_P(qdata)) {
- if (!strchr(obj->request.url, '?')) {
- strlcat(obj->request.url, "?", HTTP_URI_MAXLEN);
+ if (!strchr(obj->request->url, '?')) {
+ strlcat(obj->request->url, "?", HTTP_URI_MAXLEN);
} else {
- strlcat(obj->request.url, "&", HTTP_URI_MAXLEN);
+ strlcat(obj->request->url, "&", HTTP_URI_MAXLEN);
}
- strlcat(obj->request.url, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN);
+ strlcat(obj->request->url, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN);
}
- http_request_prepare(&obj->request, Z_ARRVAL_P(options));
+ http_request_prepare(obj->request, Z_ARRVAL_P(options));
if (opt_p) {
zval_ptr_dtor(&opt_p);
{
http_message *msg;
- phpstr_fix(&obj->request.conv.request);
- phpstr_fix(&obj->request.conv.response);
+ phpstr_fix(&obj->request->conv.request);
+ phpstr_fix(&obj->request->conv.response);
- msg = http_message_parse(PHPSTR_VAL(&obj->request.conv.response), PHPSTR_LEN(&obj->request.conv.response));
+ msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response));
if (!msg) {
return FAILURE;
* the requests and the responses in separate chains
* for redirects
*/
- http_message *response = msg, *request = http_message_parse(PHPSTR_VAL(&obj->request.conv.request), PHPSTR_LEN(&obj->request.conv.request));
+ http_message *response = msg, *request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request));
http_message *free_msg = request;
do {
SET_PROP(obj, responseMessage, message);
zval_ptr_dtor(&message);
- http_request_info(&obj->request, Z_ARRVAL_P(info));
+ http_request_info(obj->request, Z_ARRVAL_P(info));
SET_PROP(obj, responseInfo, info);
return SUCCESS;
getObject(http_request_object, obj);
SET_EH_THROW_HTTP();
- if ((msg = http_message_parse(PHPSTR_VAL(&obj->request.conv.request), PHPSTR_LEN(&obj->request.conv.request)))) {
+ if ((msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request)))) {
ZVAL_OBJVAL(return_value, http_message_object_new_ex(http_message_object_ce, msg, NULL));
}
SET_EH_NORMAL();
RETVAL_NULL();
if (SUCCESS == http_request_object_requesthandler(obj, getThis())) {
- http_request_exec(&obj->request);
+ http_request_exec(obj->request);
if (SUCCESS == http_request_object_responsehandler(obj, getThis())) {
RETVAL_OBJECT(GET_PROP(obj, responseMessage));
}
} else if (SUCCESS != http_request_object_requesthandler(req, request)) {
http_error_ex(HE_WARNING, HTTP_E_REQUEST, "Could not initialize HttpRequest object for attaching to the HttpRequestPool");
} else {
- CURLMcode code = curl_multi_add_handle(pool->ch, req->request.ch);
+ CURLMcode code = curl_multi_add_handle(pool->ch, req->request->ch);
if ((CURLM_OK != code) && (CURLM_CALL_MULTI_PERFORM != code)) {
http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "Could not attach HttpRequest object to the HttpRequestPool: %s", curl_multi_strerror(code));
#endif
} else if (req->pool != pool) {
http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "HttpRequest object(#%d) is not attached to this HttpRequestPool", Z_OBJ_HANDLE_P(request));
- } else if (CURLM_OK != (code = curl_multi_remove_handle(pool->ch, req->request.ch))) {
+ } else if (CURLM_OK != (code = curl_multi_remove_handle(pool->ch, req->request->ch))) {
http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "Could not detach HttpRequest object from the HttpRequestPool: %s", curl_multi_strerror(code));
} else {
req->pool = NULL;
{
getObjectEx(http_request_object, obj, *req);
- if (obj->request.ch == ch) {
+ if (obj->request->ch == ch) {
#if HTTP_DEBUG_REQPOOLS
fprintf(stderr, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_PP(req), obj, obj->pool);
/* shorthand for type checks */
#define HTTP_MSG_TYPE(TYPE, msg) ((msg) && ((msg)->type == HTTP_MSG_ ##TYPE))
-#define http_message_new() _http_message_init_ex(NULL, 0)
-#define http_message_init(m) _http_message_init_ex((m), 0)
-#define http_message_init_ex(m, t) _http_message_init_ex((m), (t))
-PHP_HTTP_API http_message *_http_message_init_ex(http_message *m, http_message_type t);
+#define http_message_new() http_message_init_ex(NULL, 0)
+#define http_message_init(m) http_message_init_ex((m), 0)
+#define http_message_init_ex(m, t) _http_message_init_ex((m), (t) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
+PHP_HTTP_API http_message *_http_message_init_ex(http_message *m, http_message_type t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
#define http_message_set_type(m, t) _http_message_set_type((m), (t))
PHP_HTTP_API void _http_message_set_type(http_message *m, http_message_type t);
}
#define http_message_parse(m, l) http_message_parse_ex(NULL, (m), (l))
-#define http_message_parse_ex(h, m, l) _http_message_parse_ex((h), (m), (l) TSRMLS_CC)
-PHP_HTTP_API http_message *_http_message_parse_ex(http_message *msg, const char *message, size_t length TSRMLS_DC);
+#define http_message_parse_ex(h, m, l) _http_message_parse_ex((h), (m), (l) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC TSRMLS_CC)
+PHP_HTTP_API http_message *_http_message_parse_ex(http_message *msg, const char *message, size_t length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC);
#define http_message_tostring(m, s, l) _http_message_tostring((m), (s), (l))
PHP_HTTP_API void _http_message_tostring(http_message *msg, char **string, size_t *length);
} http_request;
-#define http_request_init(r) _http_request_init_ex((r), NULL, 0, NULL TSRMLS_CC)
-#define http_request_init_ex(r, c, m, u) _http_request_init_ex((r), (c), (m), (u) TSRMLS_CC)
-PHP_HTTP_API http_request *_http_request_init_ex(http_request *request, CURL *ch, http_request_method meth, const char *url TSRMLS_DC);
+#define http_request_init(r) _http_request_init_ex((r), NULL, 0, NULL ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC TSRMLS_CC)
+#define http_request_init_ex(r, c, m, u) _http_request_init_ex((r), (c), (m), (u) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC TSRMLS_CC)
+PHP_HTTP_API http_request *_http_request_init_ex(http_request *request, CURL *ch, http_request_method meth, const char *url ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC);
#define http_request_dtor(r) _http_request_dtor((r))
PHP_HTTP_API void _http_request_dtor(http_request *request);
#define HTTP_REQUEST_BODY_CURLPOST 2
#define HTTP_REQUEST_BODY_UPLOADFILE 3
typedef struct {
- int type;
+ uint type:31;
+ uint free:1;
void *data;
size_t size;
} http_request_body;
-PHP_HTTP_API http_request_body *_http_request_body_new(ZEND_FILE_LINE_D ZEND_FILE_LINE_ORIG_DC TSRMLS_DC);
-#define http_request_body_new() _http_request_body_new(ZEND_FILE_LINE_C ZEND_FILE_LINE_EMPTY_CC TSRMLS_CC)
+#define http_request_body_new() http_request_body_init(NULL)
+#define http_request_body_init(b) http_request_body_init_ex((b), 0, NULL, 0, 0)
+#define http_request_body_init_ex(b, t, d, l, f) _http_request_body_init_ex((b), (t), (d), (l), (f) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC TSRMLS_CC)
+PHP_HTTP_API http_request_body *_http_request_body_init_ex(http_request_body *body, int type, void *data, size_t len, zend_bool free ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC);
-#define http_request_body_fill(b, fields, files) _http_request_body_fill((b), (fields), (files) TSRMLS_CC)
-PHP_HTTP_API STATUS _http_request_body_fill(http_request_body *body, HashTable *fields, HashTable *files TSRMLS_DC);
+#define http_request_body_fill(b, fields, files) _http_request_body_fill((b), (fields), (files) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC TSRMLS_CC)
+PHP_HTTP_API http_request_body *_http_request_body_fill(http_request_body *body, HashTable *fields, HashTable *files ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC);
#define http_request_body_dtor(b) _http_request_body_dtor((b) TSRMLS_CC)
PHP_HTTP_API void _http_request_body_dtor(http_request_body *body TSRMLS_DC);
typedef struct {
zend_object zo;
- http_request request;
+ http_request *request;
http_request_pool *pool;
phpstr history;
} http_request_object;
printf("%s (%d)\n", $x->getMessage(), $x->getCode());
}
} else {
- echo "URL using bad/illegal format or missing URL ((null)) (8)\n";
+ echo "URL using bad/illegal format or missing URL; ((null)) (8)\n";
}
echo "Done\n";
?>
10: HttpSocketException
11: HttpResponseException
12: HttpUrlException
-URL using bad/illegal format or missing URL ((null)) (8)
+URL using bad/illegal format or missing URL; ((null)) (8)
Done