From: Michael Wallner Date: Tue, 12 Apr 2005 20:17:35 +0000 (+0000) Subject: - renamed response.status to response.code X-Git-Tag: RELEASE_0_8_0~72 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=325520da64f5ae1ecea2f7395fc808ade28da9ea;p=m6w6%2Fext-http - renamed response.status to response.code - added HttpMessage::get* methods --- diff --git a/http_message_object.c b/http_message_object.c index 2e968b8..fe4a628 100644 --- a/http_message_object.c +++ b/http_message_object.c @@ -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; diff --git a/http_methods.c b/http_methods.c index f43141b..23631c5 100644 --- a/http_methods.c +++ b/http_methods.c @@ -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 diff --git a/php_http_message_object.h b/php_http_message_object.h index 5e87b59..484c562 100644 --- a/php_http_message_object.h +++ b/php_http_message_object.h @@ -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