X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_version.c;h=f4fcfc85367417dee7cb8f711fe39d89f18250b3;hp=4b80504cb7ad5e7be0a53b559a07c7f5383ab4f6;hb=791511f3bc18cdc68b3f27b43d9396cf56d99e5a;hpb=87db9817d428282792c8146d9c2ae9748ebf6f1e diff --git a/php_http_version.c b/php_http_version.c index 4b80504..f4fcfc8 100644 --- a/php_http_version.c +++ b/php_http_version.c @@ -6,7 +6,7 @@ | modification, are permitted provided that the conditions mentioned | | in the accompanying LICENSE file are met. | +--------------------------------------------------------------------+ - | Copyright (c) 2004-2013, Michael Wallner | + | Copyright (c) 2004-2014, Michael Wallner | +--------------------------------------------------------------------+ */ @@ -26,20 +26,38 @@ php_http_version_t *php_http_version_init(php_http_version_t *v, unsigned major, php_http_version_t *php_http_version_parse(php_http_version_t *v, const char *str TSRMLS_DC) { - php_http_version_t tmp; - char separator = 0; + long major, minor; + char separator = 0, *stop = NULL; + register const char *ptr = str; - if (3 != sscanf(str, "HTTP/%u%c%u", &tmp.major, &separator, &tmp.minor) - && 3 != sscanf(str, "%u%c%u", &tmp.major, &separator, &tmp.minor)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP protocol version '%s'", str); - return NULL; + switch (*ptr) { + case 'h': + case 'H': + ++ptr; if (*ptr != 't' && *ptr != 'T') break; + ++ptr; if (*ptr != 't' && *ptr != 'T') break; + ++ptr; if (*ptr != 'p' && *ptr != 'P') break; + ++ptr; if (*ptr != '/') break; + ++ptr; + /* no break */ + default: + major = strtol(ptr, &stop, 10); + if (stop && stop != ptr && major != LONG_MIN && major != LONG_MAX) { + separator = *stop; + if (separator) { + if (separator != '.' && separator != ',') { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Non-standard version separator '%c' in HTTP protocol version '%s'", separator, ptr); + } + ptr = stop + 1; + minor = strtol(ptr, &stop, 10); + if (minor != LONG_MIN && minor != LONG_MAX) { + return php_http_version_init(v, major, minor TSRMLS_CC); + } + } + } } - if (separator && separator != '.' && separator != ',') { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Non-standard version separator '%c' in HTTP protocol version '%s'", separator, str); - } - - return php_http_version_init(v, tmp.major, tmp.minor TSRMLS_CC); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP protocol version '%s'", str); + return NULL; } void php_http_version_to_string(php_http_version_t *v, char **str, size_t *len, const char *pre, const char *post TSRMLS_DC)