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;
{
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);
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);
}
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();
}
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;
}
{
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;
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;
/* {{{ 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
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();
}
/* }}} */
}
/* }}} */
+/* {{{ 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
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);
#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