From 5ee43b639ad2dba53b58e6feca5bec46912195e4 Mon Sep 17 00:00:00 2001 From: Stefan de Konink Date: Thu, 13 May 2010 22:39:04 +0200 Subject: [PATCH] Integration of the code that implements the use of the new parser. --- mistral.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 97 insertions(+), 23 deletions(-) diff --git a/mistral.c b/mistral.c index 6645dbb..5c5fe77 100644 --- a/mistral.c +++ b/mistral.c @@ -22,6 +22,7 @@ #include +#include "parser.h" #include "mistral.h" /* @@ -56,6 +57,74 @@ typedef struct _php_event_callback_t { /* {{{ */ php_event_callback_t *callback = NULL; +http_parser parser; + +void http_field_cb(void *data, const char *field, size_t flen, const char *value, size_t vlen) { + zval *server_vars = (zval *)data; + char *fld = malloc(5 + flen + 1); + char *val = strndup(value, vlen); + size_t i; + + strncpy(fld, "HTTP_", 5); + for (i = 0; i < flen; i++) { + if (field[i] == '-') + fld[5+i] = '_'; + else + fld[5+i] = toupper(field[i]); + } + fld[5+flen] = '\0'; + + add_assoc_string(server_vars, (char *)fld, (char *)val, 1); + free(fld); + free(val); +} + +void http_on_element(void *data, int type, const char *at, size_t length) { + char *line, *val; + zval *server_vars = (zval *)data; + + switch( type ) { + case MONGREL_CONTENT_LENGTH: + line = "CONTENT_LENGTH"; + break; + + case MONGREL_CONTENT_TYPE: + line = "CONTENT_TYPE"; + break; + + case MONGREL_FRAGMENT: + line = "FRAGMENT"; + break; + + case MONGREL_HTTP_VERSION: + line = "HTTP_VERSION"; + break; + + case MONGREL_QUERY_STRING: + line = "QUERY_STRING"; + break; + + case MONGREL_REQUEST_PATH: + line = "REQUEST_PATH"; + break; + + case MONGREL_REQUEST_METHOD: + line = "REQUEST_METHOD"; + break; + + case MONGREL_REQUEST_URI: + line = "REQUEST_URI"; + break; + + default: + line = "UNKNOWN"; + } + + val = strndup(at, length); + add_assoc_string(server_vars, (char *) line, (char *) val, 1); + free(val); +} + static int setnonblock(int fd) { int flags; @@ -72,14 +141,13 @@ static int setnonblock(int fd) static inline void _php_event_callback_free(php_event_callback_t *this_callback) /* {{{ */ { - if (!this_callback) { + if (!this_callback) return; - } zval_ptr_dtor(&this_callback->func); - if (this_callback->arg) { + if (this_callback->arg) zval_ptr_dtor(&this_callback->arg); - } + efree(this_callback); } @@ -127,22 +195,28 @@ static void write_cb(struct ev_loop *loop, struct ev_io *w, int revents) } static inline void* execute_php(void *_cli) { - int result; - struct client *cli = (struct client *) _cli; - zval retval; + int result; + struct client *cli = (struct client *) _cli; + zval retval; + zval *headers[1]; + MAKE_STD_ZVAL(headers[0]); + array_init(headers[0]); - zval *http_headers[1]; + cli->rbuff[cli->rlen] = '\0'; - cli->rbuff[cli->rlen] = '\0'; + http_parser_init(&parser); + parser.http_field = &http_field_cb; + parser.on_element = &http_on_element; + parser.data = headers[0]; + http_parser_execute(&parser, cli->rbuff, cli->rlen, 0); - MAKE_STD_ZVAL(http_headers[0]); - ZVAL_STRINGL (http_headers[0], cli->rbuff, (cli->rlen + 1), 0); + result = call_user_function(EG(function_table), NULL, callback->func, &retval, 1, headers TSRMLS_CC); + free(cli->rbuff); + cli->rbuff = NULL; + cli->rlen = 0; - result = call_user_function(EG(function_table), NULL, callback->func, &retval, 1, http_headers TSRMLS_CC); - free(cli->rbuff); - cli->rbuff = NULL; - cli->rlen = 0; - FREE_ZVAL(http_headers[0]); /* Free's the object itself */ + zval_dtor(headers[0]); /* Free's the object contents */ + FREE_ZVAL(headers[0]); /* Free's the object itself */ if (result == SUCCESS) { if (Z_TYPE_P(&retval) == IS_STRING) { @@ -321,12 +395,12 @@ static void accept_cb(struct ev_loop *loop, struct ev_io *w, int revents) } /* {{{ mistral_functions[] */ - zend_function_entry mistral_functions[] = { - PHP_FE(mistral_init, NULL) - PHP_FE(mistral_register_callback, NULL) - PHP_FE(mistral_start, NULL) - { NULL, NULL, NULL } - }; +zend_function_entry mistral_functions[] = { + PHP_FE(mistral_init, NULL) + PHP_FE(mistral_register_callback, NULL) + PHP_FE(mistral_start, NULL) + { NULL, NULL, NULL } +}; /* }}} */ /* {{{ mistral_module_entry @@ -349,7 +423,7 @@ zend_module_entry mistral_module_entry = { ZEND_GET_MODULE(mistral) #endif - /* {{{ PHP_MINFO_FUNCTION */ +/* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(mistral) { php_info_print_table_start(); -- 2.11.4.GIT