+/* {{{ 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;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
+ RETURN_FALSE;
+ }
+
+ if (ct_len) {
+ HTTP_CHECK_CONTENT_TYPE(ctype, RETURN_FALSE);
+ }
+ UPD_STRL(contentType, ctype, ct_len);
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto string HttpRequest::getContentType()
+ *
+ * Get the previously content type.
+ *
+ * Returns the previously set content type as string.
+ */
+PHP_METHOD(HttpRequest, getContentType)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ RETURN_PROP(contentType);
+ }
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
+ *
+ * Set the URL query parameters to use, overwriting previously set query parameters.
+ * Affects any request types.
+ *
+ * Accepts a string or associative array parameter containing the pre-encoded
+ * query string or to be encoded query fields. If the parameter is empty or
+ * omitted, the query data will be unset.
+ *
+ * Returns TRUE on success, or FALSE on failure.
+ */
+PHP_METHOD(HttpRequest, setQueryData)
+{
+ zval *qdata = NULL;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!", &qdata)) {
+ RETURN_FALSE;
+ }
+
+ if ((!qdata) || Z_TYPE_P(qdata) == IS_NULL) {
+ UPD_STRL(queryData, "", 0);
+ } else if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
+ char *query_data = NULL;
+
+ if (SUCCESS != http_urlencode_hash(HASH_OF(qdata), &query_data)) {
+ RETURN_FALSE;
+ }
+
+ UPD_PROP(string, queryData, query_data);
+ efree(query_data);
+ } else {
+ zval *orig = qdata;
+
+ convert_to_string_ex(&qdata);
+ UPD_STRL(queryData, Z_STRVAL_P(qdata), Z_STRLEN_P(qdata));
+ if (orig != qdata) {
+ zval_ptr_dtor(&qdata);
+ }
+ }
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto string HttpRequest::getQueryData()
+ *
+ * Get the current query data in form of an urlencoded query string.
+ *
+ * Returns a string containing the urlencoded query.
+ */
+PHP_METHOD(HttpRequest, getQueryData)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ RETURN_PROP(queryData);
+ }
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::addQueryData(array query_params)
+ *
+ * Add parameters to the query parameter list, leaving previously set unchanged.
+ * Affects any request type.
+ *
+ * Expects an associative array as parameter containing the query fields to add.
+ *
+ * Returns TRUE on success, or FALSE on failure.
+ */
+PHP_METHOD(HttpRequest, addQueryData)
+{
+ zval *qdata, *old_qdata;
+ char *query_data = NULL;
+ size_t query_data_len = 0;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &qdata)) {
+ RETURN_FALSE;
+ }
+
+ old_qdata = GET_PROP(queryData);
+
+ if (SUCCESS != http_urlencode_hash_ex(HASH_OF(qdata), 1, Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata), &query_data, &query_data_len)) {
+ RETURN_FALSE;
+ }
+
+ UPD_STRL(queryData, query_data, query_data_len);
+ efree(query_data);
+
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::addPostFields(array post_data)
+ *
+ * Adds POST data entries, leaving previously set unchanged, unless a
+ * post entry with the same name already exists.
+ * Affects only POST and custom requests.
+ *
+ * Expects an associative array as parameter containing the post fields.
+ *
+ * Returns TRUE on success, or FALSE on failure.
+ */
+PHP_METHOD(HttpRequest, addPostFields)
+{
+ zval *post_data, *old_post, *new_post;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &post_data)) {
+ RETURN_FALSE;
+ }
+
+ if (zend_hash_num_elements(Z_ARRVAL_P(post_data))) {
+ MAKE_STD_ZVAL(new_post);
+ array_init(new_post);
+ old_post = GET_PROP(postFields);
+ if (Z_TYPE_P(old_post) == IS_ARRAY) {
+ array_copy(old_post, new_post);
+ }
+ array_merge(post_data, new_post);
+ SET_PROP(postFields, new_post);
+ zval_ptr_dtor(&new_post);
+ }
+
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::setPostFields([array post_data])
+ *
+ * Set the POST data entries, overwriting previously set POST data.
+ * Affects only POST and custom requests.
+ *
+ * Accepts an associative array as parameter containing the post fields.
+ * If the parameter is empty or omitted, the post data will be unset.
+ *
+ * Returns TRUE on success, or FALSE on failure.
+ */
+PHP_METHOD(HttpRequest, setPostFields)
+{
+ zval *post, *post_data = NULL;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/!", &post_data)) {
+ RETURN_FALSE;
+ }
+
+ MAKE_STD_ZVAL(post);
+ array_init(post);
+ if (post_data && zend_hash_num_elements(Z_ARRVAL_P(post_data))) {
+ array_copy(post_data, post);
+ }
+ SET_PROP(postFields, post);
+ zval_ptr_dtor(&post);
+
+ RETURN_TRUE;
+}
+/* }}}*/
+
+/* {{{ proto array HttpRequest::getPostFields()
+ *
+ * Get previously set POST data.
+ *
+ * Returns the currently set post fields as associative array.
+ */
+PHP_METHOD(HttpRequest, getPostFields)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ RETURN_PROP(postFields);
+ }
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
+ *
+ * Set raw post data to send, overwriting previously set raw post data. Don't
+ * forget to specify a content type. Affects only POST and custom requests.
+ * Only either post fields or raw post data can be used for each request.
+ * Raw post data has higher precedence and will be used even if post fields
+ * are set.
+ *
+ * Accepts a string as parameter containing the *raw* post data.
+ *
+ * Returns TRUE on success, or FALSE on failure.
+ */
+PHP_METHOD(HttpRequest, setRawPostData)
+{
+ char *raw_data = NULL;
+ int data_len = 0;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &raw_data, &data_len)) {
+ RETURN_FALSE;
+ }
+
+ if (!raw_data) {
+ raw_data = "";
+ }
+
+ UPD_STRL(rawPostData, raw_data, data_len);
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
+ *
+ * Add raw post data, leaving previously set raw post data unchanged.
+ * Affects only POST and custom requests.
+ *
+ * Expects a string as parameter containing the raw post data to concatenate.
+ *
+ * Returns TRUE on success, or FALSE on failure.
+ */
+PHP_METHOD(HttpRequest, addRawPostData)
+{
+ char *raw_data;
+ int data_len;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &raw_data, &data_len)) {
+ RETURN_FALSE;
+ }
+
+ if (data_len) {
+ zval *data = GET_PROP(rawPostData);
+
+ 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);