- print doesn't like commas
[m6w6/ext-http] / http_request_object.c
index d6447ca066b31fe664c7f32ff9be151985680725..6527cce5cefaefac8ba6293bf3ebb80d378862a1 100644 (file)
@@ -126,6 +126,15 @@ HTTP_BEGIN_ARGS(addPostFile, 0, 2)
        HTTP_ARG_VAL(content_type, 0)
 HTTP_END_ARGS;
 
+HTTP_EMPTY_ARGS(getRawPostData, 0);
+HTTP_BEGIN_ARGS(setRawPostData, 0, 0)
+       HTTP_ARG_VAL(raw_post_data, 0)
+HTTP_END_ARGS;
+
+HTTP_BEGIN_ARGS(addRawPostData, 0, 1)
+       HTTP_ARG_VAL(raw_post_data, 0)
+HTTP_END_ARGS;
+
 HTTP_EMPTY_ARGS(getPutFile, 0);
 HTTP_BEGIN_ARGS(setPutFile, 0, 0)
        HTTP_ARG_VAL(filename, 0)
@@ -244,6 +253,10 @@ zend_function_entry http_request_object_fe[] = {
        HTTP_REQUEST_ME(setPostFields, ZEND_ACC_PUBLIC)
        HTTP_REQUEST_ME(getPostFields, ZEND_ACC_PUBLIC)
        HTTP_REQUEST_ME(addPostFields, ZEND_ACC_PUBLIC)
+       
+       HTTP_REQUEST_ME(setRawPostData, ZEND_ACC_PUBLIC)
+       HTTP_REQUEST_ME(getRawPostData, ZEND_ACC_PUBLIC)
+       HTTP_REQUEST_ME(addRawPostData, ZEND_ACC_PUBLIC)
 
        HTTP_REQUEST_ME(setPostFiles, ZEND_ACC_PUBLIC)
        HTTP_REQUEST_ME(addPostFile, ZEND_ACC_PUBLIC)
@@ -276,7 +289,7 @@ zend_function_entry http_request_object_fe[] = {
        HTTP_REQUEST_ALIAS(methodName, http_request_method_name)
        HTTP_REQUEST_ALIAS(methodExists, http_request_method_exists)
 
-       {NULL, NULL, NULL}
+       EMPTY_FUNCTION_ENTRY
 };
 static zend_object_handlers http_request_object_handlers;
 
@@ -364,6 +377,7 @@ static inline void _http_request_object_declare_default_properties(TSRMLS_D)
 
        DCL_PROP(PROTECTED, string, url, "");
        DCL_PROP(PROTECTED, string, contentType, "");
+       DCL_PROP(PROTECTED, string, rawPostData, "");
        DCL_PROP(PROTECTED, string, queryData, "");
        DCL_PROP(PROTECTED, string, putFile, "");
 
@@ -394,9 +408,9 @@ void _http_request_object_free(zend_object *object TSRMLS_DC)
 
 STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ptr, http_request_body *body TSRMLS_DC)
 {
-       zval *meth, *URL, *qdata, *opts;
+       zval *meth, *URL;
        char *request_uri;
-       STATUS status;
+       STATUS status = SUCCESS;
 
        if (!body) {
                return FAILURE;
@@ -406,46 +420,30 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
                return FAILURE;
        }
 
-       meth  = GET_PROP(obj, method);
-       URL   = GET_PROP(obj, url);
-       qdata = GET_PROP(obj, queryData);
-       opts  = GET_PROP(obj, options);
-
+       URL = GET_PROP(obj, url);
        // HTTP_URI_MAXLEN+1 long char *
        if (!(request_uri = http_absolute_uri_ex(Z_STRVAL_P(URL), Z_STRLEN_P(URL), NULL, 0, NULL, 0, 0))) {
                return FAILURE;
        }
-
-       if (Z_STRLEN_P(qdata) && (strlen(request_uri) < HTTP_URI_MAXLEN)) {
-               if (!strchr(request_uri, '?')) {
-                       strcat(request_uri, "?");
-               } else {
-                       strcat(request_uri, "&");
-               }
-               strncat(request_uri, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN - strlen(request_uri));
-       }
-
+       
+       meth = GET_PROP(obj, method);
        switch (Z_LVAL_P(meth))
        {
                case HTTP_GET:
                case HTTP_HEAD:
                        body->type = -1;
-                       status = http_request_init(obj->ch, Z_LVAL_P(meth), request_uri, NULL, Z_ARRVAL_P(opts));
+                       body = NULL;
                break;
 
                case HTTP_PUT:
                {
-                       php_stream *stream;
                        php_stream_statbuf ssb;
-                       zval *file = GET_PROP(obj, putFile);
-
-                       if (    (stream = php_stream_open_wrapper(Z_STRVAL_P(file), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL)) &&
-                                       !php_stream_stat(stream, &ssb)) {
+                       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)) {
                                body->type = HTTP_REQUEST_BODY_UPLOADFILE;
                                body->data = stream;
                                body->size = ssb.sb.st_size;
-
-                               status = http_request_init(obj->ch, HTTP_PUT, request_uri, body, Z_ARRVAL_P(opts));
                        } else {
                                status = FAILURE;
                        }
@@ -453,34 +451,65 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
                break;
 
                case HTTP_POST:
+               default:
                {
-                       zval *fields = GET_PROP(obj, postFields), *files = GET_PROP(obj, postFiles);
-
-                       if (SUCCESS == (status = http_request_body_fill(body, Z_ARRVAL_P(fields), Z_ARRVAL_P(files)))) {
-                               status = http_request_init(obj->ch, HTTP_POST, request_uri, body, Z_ARRVAL_P(opts));
+                       /* check for raw post data */
+                       zval *raw_data = GET_PROP(obj, rawPostData);
+                       
+                       if (Z_STRLEN_P(raw_data)) {
+                               zval *ctype = GET_PROP(obj, contentType);
+                               
+                               if (Z_STRLEN_P(ctype)) {
+                                       zval **headers, *opts = GET_PROP(obj, options);
+                                       
+                                       if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
+                                               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);
+                                               add_assoc_zval(opts, "headers", headers);
+                                       }
+                               }
+                               
+                               body->type = HTTP_REQUEST_BODY_CSTRING;
+                               body->data = estrndup(Z_STRVAL_P(raw_data), Z_STRLEN_P(raw_data));
+                               body->size = Z_STRLEN_P(raw_data);
+                       } else {
+                               status = http_request_body_fill(body, Z_ARRVAL_P(GET_PROP(obj, postFields)), Z_ARRVAL_P(GET_PROP(obj, postFiles)));
                        }
                }
                break;
-
-               default:
-               {
-                       zval *post = GET_PROP(obj, postData);
-
-                       body->type = HTTP_REQUEST_BODY_CSTRING;
-                       body->data = Z_STRVAL_P(post);
-                       body->size = Z_STRLEN_P(post);
-
-                       status = http_request_init(obj->ch, Z_LVAL_P(meth), request_uri, body, Z_ARRVAL_P(opts));
+       }
+       
+       if (status == SUCCESS) {
+               zval *qdata = GET_PROP(obj, queryData);
+               
+               if (Z_STRLEN_P(qdata) && (strlen(request_uri) < HTTP_URI_MAXLEN)) {
+                       if (!strchr(request_uri, '?')) {
+                               strcat(request_uri, "?");
+                       } else {
+                               strcat(request_uri, "&");
+                       }
+                       strncat(request_uri, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN - strlen(request_uri));
                }
-               break;
+               
+               status = http_request_init(obj->ch, Z_LVAL_P(meth), request_uri, body, Z_ARRVAL_P(GET_PROP(obj, options)));
        }
+       efree(request_uri);
 
        /* clean previous response */
        phpstr_dtor(&obj->response);
        /* clean previous request */
        phpstr_dtor(&obj->request);
 
-       efree(request_uri);
        return status;
 }
 
@@ -490,8 +519,12 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this
 
        phpstr_fix(&obj->request);
        phpstr_fix(&obj->response);
-
-       if (msg = http_message_parse(PHPSTR_VAL(&obj->response), PHPSTR_LEN(&obj->response))) {
+       
+       msg = http_message_parse(PHPSTR_VAL(&obj->response), PHPSTR_LEN(&obj->response));
+       
+       if (!msg) {
+               return FAILURE;
+       } else {
                char *body;
                size_t body_len;
                zval *headers, *message,
@@ -501,7 +534,7 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this
 
                /* should we record history? */
                if (Z_TYPE_P(hist) != IS_BOOL) {
-                       convert_to_boolean_ex(&hist);
+                       convert_to_boolean(hist);
                }
                if (Z_LVAL_P(hist)) {
                        /* we need to act like a zipper, as we'll receive
@@ -525,11 +558,11 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this
 
                        } while ((response = response->parent) && (request = request->parent));
 
-                       http_message_free(free_msg);
+                       http_message_free(&free_msg);
                        phpstr_fix(&obj->history);
                }
 
-               UPD_PROP(obj, long, responseCode, msg->info.response.code);
+               UPD_PROP(obj, long, responseCode, msg->http.info.response.code);
 
                MAKE_STD_ZVAL(headers)
                array_init(headers);
@@ -541,9 +574,7 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this
                add_assoc_stringl(resp, "body", body, body_len, 0);
 
                MAKE_STD_ZVAL(message);
-               message->type = IS_OBJECT;
-               message->is_ref = 1;
-               message->value.obj = http_message_object_from_msg(msg);
+               ZVAL_OBJVAL(message, http_message_object_from_msg(msg));
                SET_PROP(obj, responseMessage, message);
                zval_ptr_dtor(&message);
 
@@ -552,7 +583,6 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this
 
                return SUCCESS;
        }
-       return FAILURE;
 }
 
 #define http_request_object_set_options_subr(key, ow) \
@@ -631,7 +661,7 @@ PHP_METHOD(HttpRequest, __construct)
                INIT_PARR(obj, postFiles);
 
                if (URL) {
-                       UPD_PROP(obj, string, url, URL);
+                       UPD_STRL(obj, url, URL, URL_len);
                }
                if (meth > -1) {
                        UPD_PROP(obj, long, method, meth);
@@ -706,7 +736,7 @@ PHP_METHOD(HttpRequest, setOptions)
                                if (Z_TYPE_PP(opt) != IS_STRING) {
                                        convert_to_string_ex(opt);
                                }
-                               UPD_PROP(obj, string, url, Z_STRVAL_PP(opt));
+                               UPD_STRL(obj, url, Z_STRVAL_PP(opt), Z_STRLEN_PP(opt));
                                continue;
                        } else if (!strcmp(key, "method")) {
                                if (Z_TYPE_PP(opt) != IS_LONG) {
@@ -850,7 +880,7 @@ PHP_METHOD(HttpRequest, setUrl)
                RETURN_FALSE;
        }
 
-       UPD_PROP(obj, string, url, URL);
+       UPD_STRL(obj, url, URL, URL_len);
        RETURN_TRUE;
 }
 /* }}} */
@@ -930,7 +960,7 @@ PHP_METHOD(HttpRequest, setContentType)
                RETURN_FALSE;
        }
 
-       UPD_PROP(obj, string, contentType, ctype);
+       UPD_STRL(obj, contentType, ctype, ct_len);
        RETURN_TRUE;
 }
 /* }}} */
@@ -969,7 +999,7 @@ PHP_METHOD(HttpRequest, setQueryData)
        }
 
        if ((!qdata) || Z_TYPE_P(qdata) == IS_NULL) {
-               UPD_PROP(obj, string, queryData, "");
+               UPD_STRL(obj, queryData, "", 0);
        } else if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
                char *query_data = NULL;
                
@@ -981,7 +1011,7 @@ PHP_METHOD(HttpRequest, setQueryData)
                efree(query_data);
        } else {
                convert_to_string(qdata);
-               UPD_PROP(obj, string, queryData, Z_STRVAL_P(qdata));
+               UPD_STRL(obj, queryData, Z_STRVAL_P(qdata), Z_STRLEN_P(qdata));
        }
        RETURN_TRUE;
 }
@@ -1014,6 +1044,7 @@ PHP_METHOD(HttpRequest, addQueryData)
 {
        zval *qdata, *old_qdata;
        char *query_data = NULL;
+       size_t query_data_len = 0;
        getObject(http_request_object, obj);
 
        if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &qdata)) {
@@ -1022,11 +1053,11 @@ PHP_METHOD(HttpRequest, addQueryData)
 
        old_qdata = GET_PROP(obj, queryData);
 
-       if (SUCCESS != http_urlencode_hash_ex(HASH_OF(qdata), 1, Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata), &query_data, NULL)) {
+       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_PROP(obj, string, queryData, query_data);
+       UPD_STRL(obj, queryData, query_data, query_data_len);
        efree(query_data);
 
        RETURN_TRUE;
@@ -1099,6 +1130,80 @@ PHP_METHOD(HttpRequest, getPostFields)
 }
 /* }}} */
 
+/* {{{ proto bool HttpRequest::setRawPostData([string raw_post_data])
+ *
+ * Set raw post data to send.  Don't forget to specify a content type.
+ * Affects only POST requests.
+ */
+PHP_METHOD(HttpRequest, setRawPostData)
+{
+       char *raw_data = NULL;
+       int data_len = 0;
+       getObject(http_request_object, obj);
+       
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &raw_data, &data_len)) {
+               RETURN_FALSE;
+       }
+       
+       if (!raw_data) {
+               raw_data = "";
+       }
+       
+       UPD_STRL(obj, rawPostData, raw_data, data_len);
+       RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::addRawPostData(string raw_post_data)
+ *
+ * Add raw post data.
+ * Affects only POST requests.
+ */
+PHP_METHOD(HttpRequest, addRawPostData)
+{
+       char *raw_data, *new_data;
+       int data_len;
+       getObject(http_request_object, obj);
+       
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &raw_data, &data_len)) {
+               RETURN_FALSE;
+       }
+       
+       if (data_len) {
+               zval *zdata = GET_PROP(obj, rawPostData);
+               
+               new_data = emalloc(Z_STRLEN_P(zdata) + data_len + 1);
+               new_data[Z_STRLEN_P(zdata) + data_len] = '\0';
+               
+               if (Z_STRLEN_P(zdata)) {
+                       memcpy(new_data, Z_STRVAL_P(zdata), Z_STRLEN_P(zdata));
+               }
+               
+               memcpy(new_data + Z_STRLEN_P(zdata), raw_data, data_len);
+               UPD_STRL(obj, rawPostData, new_data, Z_STRLEN_P(zdata) + data_len);
+       }
+       
+       RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto string HttpRequest::getRawPostData()
+ *
+ * Get previously set raw post data.
+ */
+PHP_METHOD(HttpRequest, getRawPostData)
+{
+       NO_ARGS;
+       
+       IF_RETVAL_USED {
+               getObject(http_request_object, obj);
+               zval *raw_data = GET_PROP(obj, rawPostData);
+               
+               RETURN_STRINGL(Z_STRVAL_P(raw_data), Z_STRLEN_P(raw_data), 1);
+       }
+}
+/* }}} */
+
 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
  *
  * Add a file to the POST request.
@@ -1200,7 +1305,7 @@ PHP_METHOD(HttpRequest, setPutFile)
                RETURN_FALSE;
        }
 
-       UPD_PROP(obj, string, putFile, file);
+       UPD_STRL(obj, putFile, file, file_len);
        RETURN_TRUE;
 }
 /* }}} */
@@ -1425,7 +1530,7 @@ PHP_METHOD(HttpRequest, getResponseInfo)
 
                if (info_len && info_name) {
                        if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {
-                               RETURN_ZVAL(*infop, 1, ZVAL_PTR_DTOR);
+                               RETURN_ZVAL(*infop, 1, 0);
                        } else {
                                http_error_ex(HE_NOTICE, HTTP_E_INVALID_PARAM, "Could not find response info named %s", info_name);
                                RETURN_FALSE;
@@ -1537,7 +1642,6 @@ PHP_METHOD(HttpRequest, getHistory)
  */
 PHP_METHOD(HttpRequest, send)
 {
-       STATUS status = FAILURE;
        http_request_body body = {0, NULL, 0};
        getObject(http_request_object, obj);
 
@@ -1551,17 +1655,15 @@ PHP_METHOD(HttpRequest, send)
                RETURN_FALSE;
        }
 
-       if (SUCCESS == (status = http_request_object_requesthandler(obj, getThis(), &body))) {
-               status = http_request_exec(obj->ch, NULL, &obj->response, &obj->request);
+       RETVAL_NULL();
+       
+       if (    (SUCCESS == http_request_object_requesthandler(obj, getThis(), &body)) &&
+                       (SUCCESS == http_request_exec(obj->ch, NULL, &obj->response, &obj->request)) &&
+                       (SUCCESS == http_request_object_responsehandler(obj, getThis()))) {
+               RETVAL_OBJECT(GET_PROP(obj, responseMessage));
        }
        http_request_body_dtor(&body);
 
-       /* final data handling */
-       if (SUCCESS == status) {
-               status = http_request_object_responsehandler(obj, getThis());
-       }
-
-       RETVAL_OBJECT(GET_PROP(obj, responseMessage));
        SET_EH_NORMAL();
 }
 /* }}} */