- renamed response.status to response.code
authorMichael Wallner <mike@php.net>
Tue, 12 Apr 2005 20:17:35 +0000 (20:17 +0000)
committerMichael Wallner <mike@php.net>
Tue, 12 Apr 2005 20:17:35 +0000 (20:17 +0000)
- added HttpMessage::get* methods

http_message_object.c
http_methods.c
php_http_message_object.h

index 2e968b808b600f3632c89873ad9921ec8d8048e9..fe4a628620c622f2fc9c806aff9f91404256ff87 100644 (file)
@@ -43,6 +43,16 @@ zend_function_entry http_message_object_fe[] = {
        PHP_ME(HttpMessage, setRaw, NULL, ZEND_ACC_PUBLIC)
        PHP_ME(HttpMessage, getBody, NULL, ZEND_ACC_PUBLIC)
        PHP_ME(HttpMessage, getHeaders, NULL, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessage, getType, NULL, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessage, getResponseCode, NULL, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessage, getRequestMethod, NULL, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessage, getRequestUri, NULL, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessage, getHttpVersion, NULL, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessage, toString, NULL, ZEND_ACC_PUBLIC)
+
+       ZEND_MALIAS(HttpMessage, __toString, toString, NULL, ZEND_ACC_PUBLIC)
+
+       PHP_ME(HttpMessage, fromString, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
        {NULL, NULL, NULL}
 };
 static zend_object_handlers http_message_object_handlers;
@@ -51,19 +61,28 @@ void _http_message_object_init(INIT_FUNC_ARGS)
 {
        HTTP_REGISTER_CLASS_EX(HttpMessage, http_message_object, NULL, 0);
 
+       HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE);
+       HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST);
+       HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE);
+
        http_message_object_handlers.read_property = http_message_object_read_prop;
        http_message_object_handlers.write_property = http_message_object_write_prop;
        http_message_object_handlers.get_properties = http_message_object_get_props;
 }
 
 zend_object_value _http_message_object_new(zend_class_entry *ce TSRMLS_DC)
+{
+       return http_message_object_new_ex(ce, NULL);
+}
+
+zend_object_value _http_message_object_new_ex(zend_class_entry *ce, http_message *msg TSRMLS_DC)
 {
        zend_object_value ov;
        http_message_object *o;
 
        o = ecalloc(1, sizeof(http_message_object));
        o->zo.ce = ce;
-       o->message = http_message_new();
+       o->message = msg ? msg : http_message_new();
 
        ALLOC_HASHTABLE(OBJ_PROP(o));
        zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
@@ -85,7 +104,7 @@ static inline void _http_message_object_declare_default_properties(TSRMLS_D)
 
        DCL_PROP(PROTECTED, string, requestMethod, "");
        DCL_PROP(PROTECTED, string, requestUri, "");
-       DCL_PROP(PROTECTED, long, responseStatus, 0);
+       DCL_PROP(PROTECTED, long, responseCode, 0);
 
        DCL_PROP_N(PROTECTED, httpVersion);
        DCL_PROP_N(PROTECTED, headers);
@@ -191,9 +210,9 @@ static zval *_http_message_object_read_prop(zval *object, zval *member, int type
                        }
                break;
 
-               case HTTP_MSG_PROPHASH_RESPONSE_STATUS:
+               case HTTP_MSG_PROPHASH_RESPONSE_CODE:
                        if (msg->type == HTTP_MSG_RESPONSE) {
-                               RETVAL_LONG(msg->info.response.status);
+                               RETVAL_LONG(msg->info.response.code);
                        } else {
                                RETVAL_NULL();
                        }
@@ -279,7 +298,7 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va
 
                case HTTP_MSG_PROPHASH_RESPONSE_STATUS:
                        if (msg->type == HTTP_MSG_RESPONSE) {
-                               msg->info.response.status = Z_LVAL_P(value);
+                               msg->info.response.code = Z_LVAL_P(value);
                        }
                break;
        }
@@ -327,14 +346,14 @@ static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
        {
                case HTTP_MSG_REQUEST:
                        ASSOC_PROP(obj, double, "httpVersion", msg->info.request.http_version);
-                       ASSOC_PROP(obj, long, "responseStatus", 0);
+                       ASSOC_PROP(obj, long, "responseCode", 0);
                        ASSOC_STRING(obj, "requestMethod", msg->info.request.method);
                        ASSOC_STRING(obj, "requestUri", msg->info.request.URI);
                break;
 
                case HTTP_MSG_RESPONSE:
                        ASSOC_PROP(obj, double, "httpVersion", msg->info.response.http_version);
-                       ASSOC_PROP(obj, long, "responseStatus", msg->info.response.status);
+                       ASSOC_PROP(obj, long, "responseCode", msg->info.response.code);
                        ASSOC_STRING(obj, "requestMethod", empty_string);
                        ASSOC_STRING(obj, "requestUri", empty_string);
                break;
@@ -342,7 +361,7 @@ static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
                case HTTP_MSG_NONE:
                default:
                        ASSOC_PROP(obj, double, "httpVersion", 0.0);
-                       ASSOC_PROP(obj, long, "responseStatus", 0);
+                       ASSOC_PROP(obj, long, "responseCode", 0);
                        ASSOC_STRING(obj, "requestMethod", empty_string);
                        ASSOC_STRING(obj, "requestUri", empty_string);
                break;
index f43141bbc58ba638cee039245cde6ee2b987307e..23631c5c6c4a04823c7e3b02dedfac195dd37034 100644 (file)
@@ -523,6 +523,29 @@ PHP_METHOD(HttpResponse, send)
 
 /* {{{ HttpMessage */
 
+/* {{{ static HttpMessage HttpMessage::fromString(string raw_message)
+ *
+ * Create an HttpMessage object from a string.
+ */
+PHP_METHOD(HttpMessage, fromString)
+{
+       char *string = NULL;
+       int length = 0;
+       http_message *msg = NULL;
+
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &length)) {
+               RETURN_NULL();
+       }
+
+       if (!(msg = http_message_parse(string, length))) {
+               RETURN_NULL();
+       }
+
+       Z_TYPE_P(return_value) = IS_OBJECT;
+       return_value->value.obj = http_message_object_from_msg(msg);
+}
+/* }}} */
+
 /* {{{ void HttpMessage::__construct([string raw_message])
  *
  * Instantiate a new HttpMessage object based on the optionally provided
@@ -533,14 +556,14 @@ PHP_METHOD(HttpMessage, __construct)
        zval *message = NULL;
        getObject(http_message_object, obj);
 
-       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z/", &message)) {
-               return;
-       }
-
-       if (message) {
-               convert_to_string(message);
-               SET_PROP(obj, raw, message);
+       SET_EH_THROW_HTTP();
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z/", &message)) {
+               if (message) {
+                       convert_to_string(message);
+                       SET_PROP(obj, raw, message);
+               }
        }
+       SET_EH_NORMAL();
 }
 /* }}} */
 
@@ -595,6 +618,122 @@ PHP_METHOD(HttpMessage, getHeaders)
 }
 /* }}} */
 
+/* {{{ long HttpMessage::getType()
+ *
+ * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
+ */
+PHP_METHOD(HttpMessage, getType)
+{
+       zval *type;
+       getObject(http_message_object, obj);
+
+       NO_ARGS;
+
+       type = GET_PROP(obj, type);
+       RETURN_LONG(Z_LVAL_P(type));
+}
+/* }}} */
+
+/* {{{ int HttpMessage::getResponseCode()
+ *
+ * Get the Response Code of the Message.
+ */
+PHP_METHOD(HttpMessage, getResponseCode)
+{
+       zval *status;
+       getObject(http_message_object, obj);
+
+       NO_ARGS;
+
+       if (obj->message->type != HTTP_MSG_RESPONSE) {
+               RETURN_NULL();
+       }
+
+       status = GET_PROP(obj, responseCode);
+       RETURN_LONG(Z_LVAL_P(status));
+}
+/* }}} */
+
+/* {{{ string HttpMessage::getRequestMethod()
+ *
+ * Get the Request Method of the Message.
+ */
+PHP_METHOD(HttpMessage, getRequestMethod)
+{
+       zval *method;
+       getObject(http_message_object, obj);
+
+       NO_ARGS;
+
+       if (obj->message->type != HTTP_MSG_REQUEST) {
+               RETURN_NULL();
+       }
+
+       method = GET_PROP(obj, requestMethod);
+       RETURN_STRINGL(Z_STRVAL_P(method), Z_STRLEN_P(method), 1);
+}
+/* }}} */
+
+/* {{{ string HttpMessage::getRequestUri()
+ *
+ * Get the Request URI of the Message.
+ */
+PHP_METHOD(HttpMessage, getRequestUri)
+{
+       zval *uri;
+       getObject(http_message_object, obj);
+
+       NO_ARGS;
+
+       if (obj->message->type != HTTP_MSG_REQUEST) {
+               RETURN_NULL();
+       }
+
+       uri = GET_PROP(obj, requestUri);
+       RETURN_STRINGL(Z_STRVAL_P(uri), Z_STRLEN_P(uri), 1);
+}
+/* }}} */
+
+/* {{{ string HttpMessage::getHttpVersion()
+ *
+ * Get the HTTP Protocol Version of the Message.
+ */
+PHP_METHOD(HttpMessage, getHttpVersion)
+{
+       zval *version;
+       char ver[4] = {0};
+       getObject(http_message_object, obj);
+
+       NO_ARGS;
+
+       version = GET_PROP(obj, httpVersion);
+
+       if (Z_TYPE_P(version) == IS_NULL) {
+               RETURN_NULL();
+       }
+       
+       sprintf(ver, "1.1f", Z_DVAL_P(version));
+       RETURN_STRINGL(ver, 3, 1);
+}
+/* }}} */
+
+/* {{{ string HttpMessage::toString()
+ *
+ * Get the string representation of the Message.
+ */
+PHP_METHOD(HttpMessage, toString)
+{
+       char *string;
+       size_t length;
+       getObject(http_message_object, obj);
+       
+       NO_ARGS;
+       
+       http_message_tostring(obj->message, &string, &length);
+       RETURN_STRINGL(string, length, 0);
+}
+/* }}} */
+
 /* }}} */
 
 #ifdef HTTP_HAVE_CURL
index 5e87b59b43e846d8728fdc7cea4a08c5b2f75526..484c562bec6409f69e2a591371fbba3ba9d92ea8 100644 (file)
@@ -34,6 +34,9 @@ extern zend_function_entry http_message_object_fe[];
 extern void _http_message_object_init(INIT_FUNC_ARGS);
 #define http_message_object_new _http_message_object_new
 extern zend_object_value _http_message_object_new(zend_class_entry *ce TSRMLS_DC);
+#define http_message_object_new_ex(ce, msg) _http_message_object_new_ex(ce, msg TSRMLS_CC)
+#define http_message_object_from_msg(msg) _http_message_object_new_ex(http_message_object_ce, msg TSRMLS_CC)
+extern zend_object_value _http_message_object_new_ex(zend_class_entry *ce, http_message *msg TSRMLS_DC);
 #define http_message_object_free _http_message_object_free
 extern void _http_message_object_free(zend_object *object TSRMLS_DC);
 
@@ -46,11 +49,20 @@ extern void _http_message_object_free(zend_object *object TSRMLS_DC);
 #define HTTP_MSG_PROPHASH_REQUEST_METHOD       1669022159LU
 #define HTTP_MSG_PROPHASH_REQUEST_URI          3208695486LU
 #define HTTP_MSG_PROPHASH_RESPONSE_STATUS      3857097400LU
+#define HTTP_MSG_PROPHASH_RESPONSE_CODE        1305615119LU
 
 PHP_METHOD(HttpMessage, __construct);
 PHP_METHOD(HttpMessage, setRaw);
 PHP_METHOD(HttpMessage, getBody);
 PHP_METHOD(HttpMessage, getHeaders);
+PHP_METHOD(HttpMessage, getType);
+PHP_METHOD(HttpMessage, getResponseCode);
+PHP_METHOD(HttpMessage, getRequestMethod);
+PHP_METHOD(HttpMessage, getRequestUri);
+PHP_METHOD(HttpMessage, getHttpVersion);
+PHP_METHOD(HttpMessage, toString);
+
+PHP_METHOD(HttpMessage, fromString);
 
 #endif
 #endif