back to dev
[m6w6/ext-http] / php_http_version.c
index 8c1637c122002709b6c707a477062bc789b1aef4..f4fcfc85367417dee7cb8f711fe39d89f18250b3 100644 (file)
@@ -1,6 +1,18 @@
-#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-2014, Michael Wallner <mike@php.net>            |
+    +--------------------------------------------------------------------+
+*/
 
-PHP_HTTP_API php_http_version_t *php_http_version_init(php_http_version_t *v, unsigned major, unsigned minor TSRMLS_DC)
+#include "php_http_api.h"
+
+php_http_version_t *php_http_version_init(php_http_version_t *v, unsigned major, unsigned minor TSRMLS_DC)
 {
        if (!v) {
                v = emalloc(sizeof(*v));
@@ -12,44 +24,53 @@ PHP_HTTP_API php_http_version_t *php_http_version_init(php_http_version_t *v, un
        return v;
 }
 
-PHP_HTTP_API php_http_version_t *php_http_version_parse(php_http_version_t *v, const char *str TSRMLS_DC)
+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;
-
-       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_http_error(HE_WARNING, PHP_HTTP_E_MALFORMED_HEADERS, "Could not parse HTTP protocol version '%s'", str);
-               return NULL;
-       }
+       long major, minor;
+       char separator = 0, *stop = NULL;
+       register const char *ptr = str;
 
-       if (separator && separator != '.' && separator != ',') {
-               php_http_error(HE_NOTICE, PHP_HTTP_E_MALFORMED_HEADERS, "Non-standard version separator '%c' in HTTP protocol version '%s'", separator, str);
+       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);
+                               }
+                       }
+               }
        }
 
-       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;
 }
 
-PHP_HTTP_API void php_http_version_to_string(php_http_version_t *v, char **str, size_t *len, const char *pre, const char *post TSRMLS_DC)
+void php_http_version_to_string(php_http_version_t *v, char **str, size_t *len, const char *pre, const char *post TSRMLS_DC)
 {
        *len = spprintf(str, 0, "%s%u.%u%s", pre ? pre : "", v->major, v->minor, post ? post : "");
 }
 
-PHP_HTTP_API void php_http_version_to_struct(php_http_version_t *v, HashTable *strct TSRMLS_DC)
-{
-       zval tmp;
-
-       INIT_PZVAL_ARRAY(&tmp, strct);
-       add_assoc_long(&tmp, "major", v->major);
-       add_assoc_long(&tmp, "minor", v->minor);
-}
-
-PHP_HTTP_API void php_http_version_dtor(php_http_version_t *v)
+void php_http_version_dtor(php_http_version_t *v)
 {
        (void) v;
 }
 
-PHP_HTTP_API void php_http_version_free(php_http_version_t **v)
+void php_http_version_free(php_http_version_t **v)
 {
        if (*v) {
                php_http_version_dtor(*v);
@@ -57,3 +78,13 @@ PHP_HTTP_API void php_http_version_free(php_http_version_t **v)
                *v = NULL;
        }
 }
+
+/*
+ * 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
+ */
+