X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_methods.c;h=23631c5c6c4a04823c7e3b02dedfac195dd37034;hp=f43141bbc58ba638cee039245cde6ee2b987307e;hb=325520da64f5ae1ecea2f7395fc808ade28da9ea;hpb=3fae71eb51f3b0f09cf61639991a44051da8c0bc 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