- no pecl/event for ya
[m6w6/ext-http] / http_message_object.c
index 32a0e325fe4d72023cd1edd0bf592ddb39873294..45be49b489774b604eaa61666695941cfa2ad7de 100644 (file)
@@ -6,7 +6,7 @@
     | modification, are permitted provided that the conditions mentioned |
     | in the accompanying LICENSE file are met.                          |
     +--------------------------------------------------------------------+
-    | Copyright (c) 2004-2006, Michael Wallner <mike@php.net>            |
+    | Copyright (c) 2004-2007, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
@@ -21,6 +21,7 @@
 
 #include "zend_interfaces.h"
 #include "ext/standard/url.h"
+#include "php_variables.h"
 
 #include "php_http_api.h"
 #include "php_http_send_api.h"
@@ -81,6 +82,11 @@ HTTP_BEGIN_ARGS(setType, 1)
        HTTP_ARG_VAL(type, 0)
 HTTP_END_ARGS;
 
+HTTP_EMPTY_ARGS(getInfo);
+HTTP_BEGIN_ARGS(setInfo, 1)
+       HTTP_ARG_VAL(http_info, 0)
+HTTP_END_ARGS;
+
 HTTP_EMPTY_ARGS(getResponseCode);
 HTTP_BEGIN_ARGS(setResponseCode, 1)
        HTTP_ARG_VAL(response_code, 0)
@@ -145,7 +151,7 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va
 #define http_message_object_get_props _http_message_object_get_props
 static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC);
 
-#define OBJ_PROP_CE http_message_object_ce
+#define THIS_CE http_message_object_ce
 zend_class_entry *http_message_object_ce;
 zend_function_entry http_message_object_fe[] = {
        HTTP_MESSAGE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
@@ -157,6 +163,8 @@ zend_function_entry http_message_object_fe[] = {
        HTTP_MESSAGE_ME(addHeaders, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(getType, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(setType, ZEND_ACC_PUBLIC)
+       HTTP_MESSAGE_ME(getInfo, ZEND_ACC_PUBLIC)
+       HTTP_MESSAGE_ME(setInfo, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(getResponseCode, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(setResponseCode, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(getResponseStatus, ZEND_ACC_PUBLIC)
@@ -221,20 +229,20 @@ PHP_MINIT_FUNCTION(http_message_object)
        http_message_object_handlers.get_properties = http_message_object_get_props;
        http_message_object_handlers.get_property_ptr_ptr = NULL;
        
-       DCL_PROP(PROTECTED, long, type, HTTP_MSG_NONE);
-       DCL_PROP(PROTECTED, string, body, "");
-       DCL_PROP(PROTECTED, string, requestMethod, "");
-       DCL_PROP(PROTECTED, string, requestUrl, "");
-       DCL_PROP(PROTECTED, string, responseStatus, "");
-       DCL_PROP(PROTECTED, long, responseCode, 0);
-       DCL_PROP_N(PROTECTED, httpVersion);
-       DCL_PROP_N(PROTECTED, headers);
-       DCL_PROP_N(PROTECTED, parentMessage);
+       zend_declare_property_long(THIS_CE, ZEND_STRS("type")-1, HTTP_MSG_NONE, ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_string(THIS_CE, ZEND_STRS("body")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_string(THIS_CE, ZEND_STRS("requestMethod")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_string(THIS_CE, ZEND_STRS("requestUrl")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_string(THIS_CE, ZEND_STRS("responseStatus")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_long(THIS_CE, ZEND_STRS("responseCode")-1, 0, ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_null(THIS_CE, ZEND_STRS("httpVersion")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_null(THIS_CE, ZEND_STRS("headers")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_null(THIS_CE, ZEND_STRS("parentMessage")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
        
 #ifndef WONKY
-       DCL_CONST(long, "TYPE_NONE", HTTP_MSG_NONE);
-       DCL_CONST(long, "TYPE_REQUEST", HTTP_MSG_REQUEST);
-       DCL_CONST(long, "TYPE_RESPONSE", HTTP_MSG_RESPONSE);
+       zend_declare_class_constant_long(THIS_CE, ZEND_STRS("TYPE_NONE")-1, HTTP_MSG_NONE TSRMLS_CC);
+       zend_declare_class_constant_long(THIS_CE, ZEND_STRS("TYPE_REQUEST")-1, HTTP_MSG_REQUEST TSRMLS_CC);
+       zend_declare_class_constant_long(THIS_CE, ZEND_STRS("TYPE_RESPONSE")-1, HTTP_MSG_RESPONSE TSRMLS_CC);
 #endif
        
        HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE);
@@ -405,7 +413,7 @@ static zval *_http_message_object_read_prop(zval *object, zval *member, int type
        http_message *msg = obj->message;
        zval *return_value;
 #ifdef WONKY
-       ulong h = zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member)+1);
+       ulong h = zend_hash_func(Z_STRVAL_P(member), Z_STRLEN_P(member)+1);
 #else
        zend_property_info *pinfo = zend_get_property_info(obj->zo.ce, member, 1 TSRMLS_CC);
        
@@ -513,7 +521,7 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va
        http_message *msg = obj->message;
        zval *cpy = NULL;
 #ifdef WONKY
-       ulong h = zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
+       ulong h = zend_hash_func(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
 #else
        zend_property_info *pinfo = zend_get_property_info(obj->zo.ce, member, 1 TSRMLS_CC);
        
@@ -891,6 +899,50 @@ PHP_METHOD(HttpMessage, setType)
 }
 /* }}} */
 
+/* {{{ proto string HttpMessage::getInfo(void)
+       Get the HTTP request/response line */
+PHP_METHOD(HttpMessage, getInfo)
+{
+       NO_ARGS;
+       
+       if (return_value_used) {
+               getObject(http_message_object, obj);
+               
+               switch (obj->message->type) {
+                       case HTTP_MSG_REQUEST:
+                               Z_STRLEN_P(return_value) = spprintf(&Z_STRVAL_P(return_value), 0, HTTP_INFO_REQUEST_FMT_ARGS(&obj->message->http, ""));
+                               break;
+                       case HTTP_MSG_RESPONSE:
+                               Z_STRLEN_P(return_value) = spprintf(&Z_STRVAL_P(return_value), 0, HTTP_INFO_RESPONSE_FMT_ARGS(&obj->message->http, ""));
+                               break;
+                       default:
+                               RETURN_NULL();
+                               break;
+               }
+               Z_TYPE_P(return_value) = IS_STRING;
+       }
+}
+/* }}} */
+
+/* {{{ proto bool HttpMessage::setInfo(string http_info)
+       Set type and request or response info with a standard HTTP request or response line */
+PHP_METHOD(HttpMessage, setInfo)
+{
+       char *str;
+       int len;
+       http_info inf;
+       
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len) && SUCCESS == http_info_parse_ex(str, &inf, 0)) {
+               getObject(http_message_object, obj);
+               
+               http_message_set_info(obj->message, &inf);
+               http_info_dtor(&inf);
+               RETURN_TRUE;
+       }
+       RETURN_FALSE;
+}
+/* }}} */
+
 /* {{{ proto int HttpMessage::getResponseCode()
        Get the Response Code of the Message. */
 PHP_METHOD(HttpMessage, getResponseCode)
@@ -936,7 +988,11 @@ PHP_METHOD(HttpMessage, getResponseStatus)
        if (return_value_used) {
                getObject(http_message_object, obj);
                HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
-               RETURN_STRING(obj->message->http.info.response.status, 1);
+               if (obj->message->http.info.response.status) {
+                       RETURN_STRING(obj->message->http.info.response.status, 1);
+               } else {
+                       RETURN_EMPTY_STRING();
+               }
        }
 }
 /* }}} */
@@ -954,7 +1010,7 @@ PHP_METHOD(HttpMessage, setResponseStatus)
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &status, &status_len)) {
                RETURN_FALSE;
        }
-       STR_SET(obj->message->http.info.response.status, estrdup(status));
+       STR_SET(obj->message->http.info.response.status, estrndup(status, status_len));
        RETURN_TRUE;
 }
 /* }}} */
@@ -968,7 +1024,11 @@ PHP_METHOD(HttpMessage, getRequestMethod)
        if (return_value_used) {
                getObject(http_message_object, obj);
                HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
-               RETURN_STRING(obj->message->http.info.request.method, 1);
+               if (obj->message->http.info.request.method) {
+                       RETURN_STRING(obj->message->http.info.request.method, 1);
+               } else {
+                       RETURN_EMPTY_STRING();
+               }
        }
 }
 /* }}} */
@@ -1009,7 +1069,11 @@ PHP_METHOD(HttpMessage, getRequestUrl)
        if (return_value_used) {
                getObject(http_message_object, obj);
                HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
-               RETURN_STRING(obj->message->http.info.request.url, 1);
+               if (obj->message->http.info.request.url) {
+                       RETURN_STRING(obj->message->http.info.request.url, 1);
+               } else {
+                       RETURN_EMPTY_STRING();
+               }
        }
 }
 /* }}} */
@@ -1172,21 +1236,24 @@ PHP_METHOD(HttpMessage, toMessageTypeObject)
 #ifdef HTTP_HAVE_CURL
                                int method;
                                char *url;
-                               zval body, *array, *headers, *host = http_message_header(obj->message, "Host");
-                               php_url hurl, *purl = php_url_parse(obj->message->http.info.request.url);
+                               zval post, body, *array, *headers, *host = http_message_header(obj->message, "Host");
+                               php_url hurl, *purl = php_url_parse(STR_PTR(obj->message->http.info.request.url));
                                
                                MAKE_STD_ZVAL(array);
                                array_init(array);
                                
                                memset(&hurl, 0, sizeof(php_url));
-                               hurl.host = host ? Z_STRVAL_P(host) : NULL;
-                               zval_ptr_dtor(&host);
+                               if (host) {
+                                       hurl.host = Z_STRVAL_P(host);
+                                       zval_ptr_dtor(&host);
+                               }
                                http_build_url(HTTP_URL_REPLACE, purl, &hurl, NULL, &url, NULL);
                                php_url_free(purl);
                                add_assoc_string(array, "url", url, 0);
                                
-                               if (    (method = http_request_method_exists(1, 0, obj->message->http.info.request.method)) ||
-                                               (method = http_request_method_register(obj->message->http.info.request.method, strlen(obj->message->http.info.request.method)))) {
+                               if (    obj->message->http.info.request.method &&
+                                                       ((method = http_request_method_exists(1, 0, obj->message->http.info.request.method)) ||
+                                                       (method = http_request_method_register(obj->message->http.info.request.method, strlen(obj->message->http.info.request.method))))) {
                                        add_assoc_long(array, "method", method);
                                }
                                
@@ -1203,9 +1270,20 @@ PHP_METHOD(HttpMessage, toMessageTypeObject)
                                zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setoptions", NULL, array);
                                zval_ptr_dtor(&array);
                                
-                               INIT_PZVAL(&body);
-                               ZVAL_STRINGL(&body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message), 0);
-                               zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setrawpostdata", NULL, &body);
+                               if (PHPSTR_VAL(obj->message) && PHPSTR_LEN(obj->message)) {
+                                       INIT_PZVAL(&body);
+                                       ZVAL_STRINGL(&body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message), 0);
+                                       if (method != HTTP_POST) {
+                                               zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setbody", NULL, &body);
+                                       } else {
+                                               INIT_PZVAL(&post);
+                                               array_init(&post);
+                                               
+                                               zval_copy_ctor(&body);
+                                               sapi_module.treat_data(PARSE_STRING, Z_STRVAL(body), &post TSRMLS_CC);
+                                               zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setpostfields", NULL, &post);
+                                       }
+                               }
 #else
                                http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot transform HttpMessage to HttpRequest (missing curl support)");
 #endif