fix problem when finishing a chunked encoding stream
[m6w6/ext-http] / php_http_version.c
1 #include "php_http.h"
2
3 PHP_HTTP_API php_http_version_t *php_http_version_init(php_http_version_t *v, unsigned major, unsigned minor TSRMLS_DC)
4 {
5 if (!v) {
6 v = emalloc(sizeof(*v));
7 }
8
9 v->major = major;
10 v->minor = minor;
11
12 return v;
13 }
14
15 PHP_HTTP_API php_http_version_t *php_http_version_parse(php_http_version_t *v, const char *str TSRMLS_DC)
16 {
17 php_http_version_t tmp;
18 char separator = 0;
19
20 if (3 != sscanf(str, "HTTP/%u%c%u", &tmp.major, &separator, &tmp.minor)
21 && 3 != sscanf(str, "%u%c%u", &tmp.major, &separator, &tmp.minor)) {
22 php_http_error(HE_WARNING, PHP_HTTP_E_MALFORMED_HEADERS, "Could not parse HTTP protocol version '%s'", str);
23 return NULL;
24 }
25
26 if (separator && separator != '.' && separator != ',') {
27 php_http_error(HE_NOTICE, PHP_HTTP_E_MALFORMED_HEADERS, "Non-standard version separator '%c' in HTTP protocol version '%s'", separator, str);
28 }
29
30 return php_http_version_init(v, tmp.major, tmp.minor TSRMLS_CC);
31 }
32
33 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)
34 {
35 *len = spprintf(str, 0, "%s%u.%u%s", pre ? pre : "", v->major, v->minor, post ? post : "");
36 }
37
38 PHP_HTTP_API void php_http_version_to_struct(php_http_version_t *v, HashTable *strct TSRMLS_DC)
39 {
40 zval tmp;
41
42 INIT_PZVAL_ARRAY(&tmp, strct);
43 add_assoc_long(&tmp, "major", v->major);
44 add_assoc_long(&tmp, "minor", v->minor);
45 }
46
47 PHP_HTTP_API void php_http_version_dtor(php_http_version_t *v)
48 {
49 (void) v;
50 }
51
52 PHP_HTTP_API void php_http_version_free(php_http_version_t **v)
53 {
54 if (*v) {
55 php_http_version_dtor(*v);
56 efree(*v);
57 *v = NULL;
58 }
59 }