+ STATUS status = SUCCESS;
+
+ http_request_reset(obj->request);
+ HTTP_CHECK_CURL_INIT(obj->request->ch, http_curl_init(obj->request), return FAILURE);
+
+ obj->request->url = http_absolute_url(Z_STRVAL_P(GET_PROP(url)));
+
+ switch (obj->request->meth = Z_LVAL_P(GET_PROP(method)))
+ {
+ case HTTP_GET:
+ case HTTP_HEAD:
+ break;
+
+ case HTTP_PUT:
+ {
+ zval *put_data = GET_PROP(putData);
+
+ 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 {
+ 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;
+ }
+
+ case HTTP_POST:
+ default:
+ {
+ /* check for raw post data */
+ zval *raw_data = GET_PROP(rawPostData);
+
+ if (Z_STRLEN_P(raw_data)) {
+ 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;
+ HashTable *files;
+
+ fields = (Z_TYPE_P(zfields) == IS_ARRAY) ? Z_ARRVAL_P(zfields) : NULL;
+ files = (Z_TYPE_P(zfiles) == IS_ARRAY) ? Z_ARRVAL_P(zfiles) : NULL;
+
+ if ((fields && zend_hash_num_elements(fields)) || (files && zend_hash_num_elements(files))) {
+ if (!(obj->request->body = http_request_body_fill(obj->request->body, fields, files))) {
+ status = FAILURE;
+ }
+ }
+ }
+ break;
+ }
+ }
+
+ if (status == SUCCESS) {
+ zval *qdata = GET_PROP(queryData);
+ zval *options = GET_PROP(options);
+
+ if (Z_STRLEN_P(qdata)) {
+ if (!strchr(obj->request->url, '?')) {
+ strlcat(obj->request->url, "?", HTTP_URL_MAXLEN);
+ } else {
+ strlcat(obj->request->url, "&", HTTP_URL_MAXLEN);
+ }
+ strlcat(obj->request->url, Z_STRVAL_P(qdata), HTTP_URL_MAXLEN);
+ }
+
+ http_request_prepare(obj->request, Z_ARRVAL_P(options));
+
+ /* check if there's a onProgress method and add it as progress callback if one isn't already set */
+ if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onprogress", sizeof("onprogress"))) {
+ zval **entry, *pcb;
+
+ if ( (Z_TYPE_P(options) != IS_ARRAY)
+ || (SUCCESS != zend_hash_find(Z_ARRVAL_P(options), "onprogress", sizeof("onprogress"), (void *) &entry)
+ || (!zval_is_true(*entry)))) {
+ MAKE_STD_ZVAL(pcb);
+ array_init(pcb);
+ ZVAL_ADDREF(getThis());
+ add_next_index_zval(pcb, getThis());
+ add_next_index_stringl(pcb, "onprogress", lenof("onprogress"), 1);
+ http_request_set_progress_callback(obj->request, pcb);
+ zval_ptr_dtor(&pcb);
+ }
+ }
+ }
+
+ return status;
+}
+
+STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this_ptr TSRMLS_DC)
+{
+ STATUS ret;
+ zval *info;
+ http_message *msg;
+
+ /* always fetch info */
+ MAKE_STD_ZVAL(info);
+ array_init(info);
+ http_request_info(obj->request, Z_ARRVAL_P(info));
+ SET_PROP(responseInfo, info);
+ zval_ptr_dtor(&info);
+
+ /* parse response message */
+ phpstr_fix(&obj->request->conv.request);
+ phpstr_fix(&obj->request->conv.response);
+
+ if ((msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response)))) {
+ char *body;
+ size_t body_len;
+ zval *headers, *message, *resp;
+
+ if (zval_is_true(GET_PROP(recordHistory))) {
+ zval *hist, *history = GET_PROP(history);
+ http_message *response = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response));
+ http_message *request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request));
+
+ MAKE_STD_ZVAL(hist);
+ ZVAL_OBJVAL(hist, http_message_object_new_ex(http_message_object_ce, http_message_interconnect(response, request), NULL), 0);
+ if (Z_TYPE_P(history) == IS_OBJECT) {
+ http_message_object_prepend(hist, history);
+ }
+ SET_PROP(history, hist);
+ zval_ptr_dtor(&hist);
+ }
+
+ UPD_PROP(long, responseCode, msg->http.info.response.code);
+ UPD_PROP(string, responseStatus, msg->http.info.response.status ? msg->http.info.response.status : "");
+
+ MAKE_STD_ZVAL(resp);
+ array_init(resp);
+ MAKE_STD_ZVAL(headers);
+ array_init(headers);
+ zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
+ add_assoc_zval(resp, "headers", headers);
+ phpstr_data(PHPSTR(msg), &body, &body_len);
+ add_assoc_stringl(resp, "body", body, body_len, 0);
+ SET_PROP(responseData, resp);
+ zval_ptr_dtor(&resp);
+
+ MAKE_STD_ZVAL(message);
+ ZVAL_OBJVAL(message, http_message_object_new_ex(http_message_object_ce, msg, NULL), 0);
+ SET_PROP(responseMessage, message);
+ zval_ptr_dtor(&message);
+
+ ret = SUCCESS;
+ } else {
+ /* update properties with empty values*/
+ zval *resp = GET_PROP(responseData), *znull;
+
+ MAKE_STD_ZVAL(znull);
+ ZVAL_NULL(znull);
+ SET_PROP(responseMessage, znull);
+ zval_ptr_dtor(&znull);
+
+ if (Z_TYPE_P(resp) == IS_ARRAY) {
+ zend_hash_clean(Z_ARRVAL_P(resp));
+ }
+
+ UPD_PROP(long, responseCode, 0);
+ UPD_PROP(string, responseStatus, "");
+
+ /* append request message to history */
+ if (zval_is_true(GET_PROP(recordHistory))) {
+ http_message *request;
+
+ if ((request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request)))) {
+ zval *hist, *history = GET_PROP(history);
+
+ MAKE_STD_ZVAL(hist);
+ ZVAL_OBJVAL(hist, http_message_object_new_ex(http_message_object_ce, request, NULL), 0);
+ if (Z_TYPE_P(history) == IS_OBJECT) {
+ http_message_object_prepend(hist, history);
+ }
+ SET_PROP(history, hist);
+ zval_ptr_dtor(&hist);
+ }
+ }
+
+ ret = FAILURE;
+ }
+
+ if (!EG(exception) && zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onfinish", sizeof("onfinish"))) {
+ zval *param;
+
+ MAKE_STD_ZVAL(param);
+ ZVAL_BOOL(param, ret == SUCCESS);
+ with_error_handling(EH_NORMAL, NULL) {
+ zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "onfinish", NULL, param);
+ } end_error_handling();
+ zval_ptr_dtor(¶m);
+ }
+
+ return ret;
+}
+
+static int apply_pretty_key(void *pDest, int num_args, va_list args, zend_hash_key *hash_key)
+{
+ if (hash_key->nKeyLength > 1) {
+ hash_key->h = zend_get_hash_value(pretty_key(hash_key->arKey, hash_key->nKeyLength - 1, 1, 0), hash_key->nKeyLength);
+ }
+ return ZEND_HASH_APPLY_KEEP;
+}
+
+#define http_request_object_set_options_subr(key, ow, pk) \
+ _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow), (pk))
+static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len, int overwrite, int prettify_keys)
+{
+ zval *old_opts, *new_opts, *opts = NULL, **entry = NULL;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &opts)) {
+ RETURN_FALSE;
+ }
+
+ MAKE_STD_ZVAL(new_opts);
+ array_init(new_opts);
+ old_opts = GET_PROP(options);
+ if (Z_TYPE_P(old_opts) == IS_ARRAY) {
+ array_copy(old_opts, new_opts);
+ }
+
+ if (prettify_keys && opts) {
+ zend_hash_apply_with_arguments(Z_ARRVAL_P(opts), apply_pretty_key, 0);
+ }
+ if (SUCCESS == zend_hash_find(Z_ARRVAL_P(new_opts), key, len, (void *) &entry)) {
+ if (overwrite) {
+ zend_hash_clean(Z_ARRVAL_PP(entry));
+ }
+ if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
+ if (overwrite) {
+ array_copy(opts, *entry);
+ } else {
+ array_merge(opts, *entry);
+ }
+ }
+ } else if (opts) {
+ ZVAL_ADDREF(opts);
+ add_assoc_zval(new_opts, key, opts);
+ }
+ SET_PROP(options, new_opts);
+ zval_ptr_dtor(&new_opts);
+
+ RETURN_TRUE;
+}
+
+#define http_request_object_get_options_subr(key) \
+ _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
+static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ zval *opts, **options;
+
+ opts = GET_PROP(options);
+ array_init(return_value);
+
+ if ( (Z_TYPE_P(opts) == IS_ARRAY) &&
+ (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void *) &options))) {
+ convert_to_array(*options);
+ array_copy(*options, return_value);
+ }
+ }
+}
+
+
+/* ### USERLAND ### */
+
+/* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
+ *
+ * Instantiate a new HttpRequest object.
+ *
+ * Accepts a string as optional parameter containing the target request url.
+ * Additionally accepts an optional int parameter specifying the request method
+ * to use and an associative array as optional third parameter which will be
+ * passed to HttpRequest::setOptions().
+ *
+ * Throws HttpException.
+ */
+PHP_METHOD(HttpRequest, __construct)
+{
+ char *URL = NULL;
+ int URL_len;
+ long meth = -1;
+ zval *options = NULL;
+
+ SET_EH_THROW_HTTP();
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sla", &URL, &URL_len, &meth, &options)) {
+ if (URL) {
+ UPD_STRL(url, URL, URL_len);
+ }
+ if (meth > -1) {
+ UPD_PROP(long, method, meth);
+ }
+ if (options) {
+ zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setoptions", NULL, options);
+ }
+ }
+ SET_EH_NORMAL();
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::setOptions([array options])
+ *
+ * Set the request options to use. See http_get() for a full list of available options.
+ *
+ * Accepts an array as optional parameters, which values will overwrite the
+ * currently set request options. If the parameter is empty or omitted,
+ * the options of the HttpRequest object will be reset.
+ *
+ * Returns TRUE on success, or FALSE on failure.
+ */
+PHP_METHOD(HttpRequest, setOptions)
+{
+ char *key = NULL;
+ ulong idx = 0;
+ HashPosition pos;
+ zval *opts = NULL, *old_opts, *new_opts, *add_opts, **opt;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
+ RETURN_FALSE;
+ }
+
+ MAKE_STD_ZVAL(new_opts);
+ array_init(new_opts);
+
+ if (!opts || !zend_hash_num_elements(Z_ARRVAL_P(opts))) {
+ SET_PROP(options, new_opts);
+ zval_ptr_dtor(&new_opts);
+ RETURN_TRUE;
+ }
+
+ MAKE_STD_ZVAL(add_opts);
+ array_init(add_opts);
+ /* some options need extra attention -- thus cannot use array_merge() directly */
+ FOREACH_KEYVAL(pos, opts, key, idx, opt) {
+ if (key) {
+ if (!strcmp(key, "headers")) {
+ zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, *opt);
+ } else if (!strcmp(key, "cookies")) {
+ zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addcookies", NULL, *opt);
+ } else if (!strcmp(key, "ssl")) {
+ zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addssloptions", NULL, *opt);
+ } else if ((!strcasecmp(key, "url")) || (!strcasecmp(key, "uri"))) {
+ zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "seturl", NULL, *opt);
+ } else if (!strcmp(key, "method")) {
+ zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setmethod", NULL, *opt);
+#if HTTP_CURL_VERSION(7,14,1)
+ } else if (!strcmp(key, "resetcookies")) {
+ getObject(http_request_object, obj);
+ http_request_reset_cookies(obj->request, 0);
+#endif
+ } else if (!strcmp(key, "enablecookies")) {
+ getObject(http_request_object, obj);
+ http_request_enable_cookies(obj->request);
+ } else if (!strcasecmp(key, "recordHistory")) {
+ UPD_PROP(bool, recordHistory, 1);
+ } else {
+ ZVAL_ADDREF(*opt);
+ add_assoc_zval(add_opts, key, *opt);
+ }
+ /* reset */
+ key = NULL;
+ }
+ }
+
+ old_opts = GET_PROP(options);
+ if (Z_TYPE_P(old_opts) == IS_ARRAY) {
+ array_copy(old_opts, new_opts);
+ }
+ array_merge(add_opts, new_opts);
+ SET_PROP(options, new_opts);
+ zval_ptr_dtor(&new_opts);
+ zval_ptr_dtor(&add_opts);
+
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto array HttpRequest::getOptions()
+ *
+ * Get currently set options.
+ *
+ * Returns an associative array containing currently set options.
+ */
+PHP_METHOD(HttpRequest, getOptions)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ RETURN_PROP(options);
+ }
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::setSslOptions([array options])
+ *
+ * Set SSL options.
+ *
+ * Accepts an associative array as parameter containing any SSL specific options.
+ * If the parameter is empty or omitted, the SSL options will be reset.
+ *
+ * Returns TRUE on success, or FALSE on failure.
+ */
+PHP_METHOD(HttpRequest, setSslOptions)
+{
+ http_request_object_set_options_subr("ssl", 1, 0);
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::addSslOptions(array options)
+ *
+ * Set additional SSL options.
+ *
+ * Expects an associative array as parameter containing additional SSL specific options.
+ *
+ * Returns TRUE on success, or FALSE on failure.
+ */
+PHP_METHOD(HttpRequest, addSslOptions)
+{
+ http_request_object_set_options_subr("ssl", 0, 0);
+}
+/* }}} */
+
+/* {{{ proto array HttpRequest::getSslOtpions()
+ *
+ * Get previously set SSL options.
+ *
+ * Returns an associative array containing any previously set SSL options.
+ */
+PHP_METHOD(HttpRequest, getSslOptions)
+{
+ http_request_object_get_options_subr("ssl");
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::addHeaders(array headers)
+ *
+ * Add request header name/value pairs.
+ *
+ * Expects an associative array as parameter containing additional header
+ * name/value pairs.
+ *
+ * Returns TRUE on success, or FALSE on failure.
+ */
+PHP_METHOD(HttpRequest, addHeaders)
+{
+ http_request_object_set_options_subr("headers", 0, 1);
+}
+
+/* {{{ proto bool HttpRequest::setHeaders([array headers])
+ *
+ * Set request header name/value pairs.
+ *
+ * Accepts an associative array as parameter containing header name/value pairs.
+ * If the parameter is empty or omitted, all previously set headers will be unset.
+ *
+ * Returns TRUE on success, or FALSE on failure.
+ */
+PHP_METHOD(HttpRequest, setHeaders)
+{
+ http_request_object_set_options_subr("headers", 1, 1);
+}
+/* }}} */
+
+/* {{{ proto array HttpRequest::getHeaders()
+ *
+ * Get previously set request headers.
+ *
+ * Returns an associative array containing all currently set headers.
+ */
+PHP_METHOD(HttpRequest, getHeaders)
+{
+ http_request_object_get_options_subr("headers");
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::setCookies([array cookies])
+ *
+ * Set cookies.
+ *
+ * Accepts an associative array as parameter containing cookie name/value pairs.
+ * If the parameter is empty or omitted, all previously set cookies will be unset.
+ *
+ * Returns TRUE on success, or FALSE on failure.
+ */
+PHP_METHOD(HttpRequest, setCookies)
+{
+ http_request_object_set_options_subr("cookies", 1, 0);
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::addCookies(array cookies)
+ *
+ * Add cookies.
+ *
+ * Expects an associative array as parameter containing any cookie name/value
+ * pairs to add.
+ *
+ * Returns TRUE on success, or FALSE on failure.
+ */
+PHP_METHOD(HttpRequest, addCookies)
+{
+ http_request_object_set_options_subr("cookies", 0, 0);
+}
+/* }}} */
+
+/* {{{ proto array HttpRequest::getCookies()
+ *
+ * Get previously set cookies.
+ *
+ * Returns an associative array containing any previously set cookies.
+ */
+PHP_METHOD(HttpRequest, getCookies)
+{
+ http_request_object_get_options_subr("cookies");
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::enableCookies()
+ *
+ * Enable automatic sending of received cookies.
+ * Note that cuutomly set cookies will be sent anyway.
+ */
+PHP_METHOD(HttpRequest, enableCookies)
+{
+ NO_ARGS {
+ getObject(http_request_object, obj);
+ RETURN_SUCCESS(http_request_enable_cookies(obj->request));
+ }
+
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::resetCookies([bool session_only = FALSE])
+ *
+ * Reset all automatically received/sent cookies.
+ * Note that customly set cookies are not affected.
+ *
+ * Accepts an optional bool parameter specifying
+ * whether only session cookies should be reset
+ * (needs libcurl >= v7.15.4, else libcurl >= v7.14.1).
+ *
+ * Returns TRUE on success, or FALSE on failure.
+ */
+PHP_METHOD(HttpRequest, resetCookies)
+{
+ zend_bool session_only = 0;
+ getObject(http_request_object, obj);
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &session_only)) {
+ RETURN_FALSE;
+ }
+ RETURN_SUCCESS(http_request_reset_cookies(obj->request, session_only));
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::setUrl(string url)
+ *
+ * Set the request URL.
+ *
+ * Expects a string as parameter specifying the request url.
+ *
+ * Returns TRUE on success, or FALSE on failure.
+ */
+PHP_METHOD(HttpRequest, setUrl)
+{
+ char *URL = NULL;
+ int URL_len;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
+ RETURN_FALSE;
+ }
+
+ UPD_STRL(url, URL, URL_len);
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto string HttpRequest::getUrl()
+ *
+ * Get the previously set request URL.
+ *
+ * Returns the currently set request url as string.
+ */
+PHP_METHOD(HttpRequest, getUrl)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ RETURN_PROP(url);
+ }
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::setMethod(int request_method)
+ *
+ * Set the request method.
+ *
+ * Expects an int as parameter specifying the request method to use.
+ * In PHP 5.1+ HttpRequest::METH_*, otherwise the HTTP_METH_* constants can be used.
+ *
+ * Returns TRUE on success, or FALSE on failure.
+ */
+PHP_METHOD(HttpRequest, setMethod)
+{
+ long meth;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
+ RETURN_FALSE;
+ }
+
+ UPD_PROP(long, method, meth);
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto int HttpRequest::getMethod()
+ *
+ * Get the previously set request method.
+ *
+ * Returns the currently set request method.
+ */
+PHP_METHOD(HttpRequest, getMethod)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ RETURN_PROP(method);
+ }
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::setContentType(string content_type)
+ *
+ * Set the content type the post request should have.
+ *
+ * Expects a string as parameters containing the content type of the request
+ * (primary/secondary).
+ *
+ * Returns TRUE on success, or FALSE if the content type does not seem to
+ * contain a primary and a secondary part.
+ */
+PHP_METHOD(HttpRequest, setContentType)
+{
+ char *ctype;
+ int ct_len;