X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_header_parser.c;h=0dbaf848f9e72f02d59f55f27e87ef7c4ba79eee;hp=a70d853f6a061e6c90dac877ef4bb932bdf47e47;hb=d926a12aeec96752456eddfbf9a04a002809a0d2;hpb=d3485e3b28336153dca690e872ffe1ddc60fedd2 diff --git a/php_http_header_parser.c b/php_http_header_parser.c index a70d853..0dbaf84 100644 --- a/php_http_header_parser.c +++ b/php_http_header_parser.c @@ -1,5 +1,16 @@ - -#include "php_http.h" +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#include "php_http_api.h" typedef struct php_http_header_parser_state_spec { php_http_header_parser_state_t state; @@ -15,7 +26,7 @@ static const php_http_header_parser_state_spec_t php_http_header_parser_states[] }; -PHP_HTTP_API php_http_header_parser_t *php_http_header_parser_init(php_http_header_parser_t *parser TSRMLS_CC) +PHP_HTTP_API php_http_header_parser_t *php_http_header_parser_init(php_http_header_parser_t *parser TSRMLS_DC) { if (!parser) { parser = emalloc(sizeof(*parser)); @@ -82,15 +93,15 @@ PHP_HTTP_API void php_http_header_parser_free(php_http_header_parser_t **parser) } } -PHP_HTTP_API STATUS php_http_header_parser_parse(php_http_header_parser_t *parser, php_http_buffer *buffer, unsigned flags, HashTable *headers, php_http_info_callback_t callback_func, void *callback_arg) +PHP_HTTP_API STATUS php_http_header_parser_parse(php_http_header_parser_t *parser, php_http_buffer_t *buffer, unsigned flags, HashTable *headers, php_http_info_callback_t callback_func, void *callback_arg) { TSRMLS_FETCH_FROM_CTX(parser->ts); while (buffer->used || !php_http_header_parser_states[php_http_header_parser_state_is(parser)].need_data) { #if 0 const char *state[] = {"START", "KEY", "VALUE", "HEADER_DONE", "DONE"}; - fprintf(stderr, "#HP: %s (%d) %zu\n", - state[php_http_header_parser_state_is(parser)], zend_hash_num_elements(headers), buffer->used); + fprintf(stderr, "#HP: %s (%d)\n", php_http_header_parser_state_is(parser) < 0 ? "FAILURE" : state[php_http_header_parser_state_is(parser)], zend_hash_num_elements(headers)); + _dpf(0, buffer->data, buffer->used); #endif switch (php_http_header_parser_state_pop(parser)) { case PHP_HTTP_HEADER_PARSER_STATE_FAILURE: @@ -109,8 +120,8 @@ PHP_HTTP_API STATUS php_http_header_parser_parse(php_http_header_parser_t *parse } case PHP_HTTP_HEADER_PARSER_STATE_KEY: { - const char *colon, *eol_str; - int eol_len; + const char *colon, *eol_str = NULL; + int eol_len = 0; if (buffer->data == (eol_str = php_http_locate_bin_eol(buffer->data, buffer->used, &eol_len))) { /* end of headers */ @@ -135,46 +146,24 @@ PHP_HTTP_API STATUS php_http_header_parser_parse(php_http_header_parser_t *parse return php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_FAILURE); } break; - /* - if (colon && (!eol_str || colon < eol_str)) { - parser->_key.str = estrndup(buffer->data, parser->_key.len = colon - buffer->data); - while (PHP_HTTP_IS_CTYPE(space, *++colon)); - php_http_buffer_cut(buffer, 0, colon - buffer->data); - php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_VALUE); - } else if (eol_str) { - if (eol_str == buffer->data) { - php_http_buffer_cut(buffer, 0, eol_len); - php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_DONE); - } else if (php_http_info_parse(&parser->info, php_http_buffer_fix(buffer)->data TSRMLS_CC)) { - if (callback_func) { - callback_func(callback_arg, &headers, &parser->info TSRMLS_CC); - } - php_http_info_dtor(&parser->info); - php_http_buffer_cut(buffer, 0, eol_str + eol_len - buffer->data); - php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_HEADER_DONE); - } else { - return PHP_HTTP_HEADER_PARSER_STATE_FAILURE; - } - } else { - php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_KEY); - return PHP_HTTP_HEADER_PARSER_STATE_KEY; - } - break; - */ } case PHP_HTTP_HEADER_PARSER_STATE_VALUE: { const char *eol_str; int eol_len; - do { + line_split: { + if ((eol_str = php_http_locate_bin_eol(buffer->data, buffer->used, &eol_len))) { if (eol_str + eol_len - buffer->data < buffer->used) { - char nextline = *(eol_str + eol_len); - - if (nextline == '\t' || nextline == ' ') { - php_http_buffer_cut(buffer, eol_str - buffer->data, eol_len); - continue; + char *nextline = eol_str + eol_len; + + if (*nextline == '\t' || *nextline == ' ') { + while (nextline < buffer->data + buffer->used && (*nextline == '\t' || *nextline == ' ')) { + ++nextline; + } + php_http_buffer_cut(buffer, eol_str - buffer->data, nextline - eol_str); + goto line_split; } } @@ -190,7 +179,7 @@ PHP_HTTP_API STATUS php_http_header_parser_parse(php_http_header_parser_t *parse } else { return php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_VALUE); } - } while (0); + } break; } @@ -201,9 +190,9 @@ PHP_HTTP_API STATUS php_http_header_parser_parse(php_http_header_parser_t *parse INIT_PZVAL_ARRAY(&array, headers); php_http_pretty_key(parser->_key.str, parser->_key.len, 1, 1); - if (SUCCESS == zend_hash_find(headers, parser->_key.str, parser->_key.len + 1, (void *) &exist)) { + if (SUCCESS == zend_symtable_find(headers, parser->_key.str, parser->_key.len + 1, (void *) &exist)) { convert_to_array(*exist); - add_next_index_stringl(&array, parser->_val.str, parser->_val.len, 0); + add_next_index_stringl(*exist, parser->_val.str, parser->_val.len, 0); } else { add_assoc_stringl_ex(&array, parser->_key.str, parser->_key.len + 1, parser->_val.str, parser->_val.len, 0); } @@ -223,3 +212,13 @@ PHP_HTTP_API STATUS php_http_header_parser_parse(php_http_header_parser_t *parse return php_http_header_parser_state_is(parser); } + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ +