merge php_http.h with touch
[m6w6/ext-http] / php_http_version.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 php_http_version_t *php_http_version_init(php_http_version_t *v, unsigned major, unsigned minor TSRMLS_DC)
16 {
17 if (!v) {
18 v = emalloc(sizeof(*v));
19 }
20
21 v->major = major;
22 v->minor = minor;
23
24 return v;
25 }
26
27 php_http_version_t *php_http_version_parse(php_http_version_t *v, const char *str TSRMLS_DC)
28 {
29 php_http_version_t tmp;
30 char separator = 0;
31
32 if (3 != sscanf(str, "HTTP/%u%c%u", &tmp.major, &separator, &tmp.minor)
33 && 3 != sscanf(str, "%u%c%u", &tmp.major, &separator, &tmp.minor)) {
34 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP protocol version '%s'", str);
35 return NULL;
36 }
37
38 if (separator && separator != '.' && separator != ',') {
39 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Non-standard version separator '%c' in HTTP protocol version '%s'", separator, str);
40 }
41
42 return php_http_version_init(v, tmp.major, tmp.minor TSRMLS_CC);
43 }
44
45 void php_http_version_to_string(php_http_version_t *v, char **str, size_t *len, const char *pre, const char *post TSRMLS_DC)
46 {
47 *len = spprintf(str, 0, "%s%u.%u%s", pre ? pre : "", v->major, v->minor, post ? post : "");
48 }
49
50 void php_http_version_dtor(php_http_version_t *v)
51 {
52 (void) v;
53 }
54
55 void php_http_version_free(php_http_version_t **v)
56 {
57 if (*v) {
58 php_http_version_dtor(*v);
59 efree(*v);
60 *v = NULL;
61 }
62 }
63
64 /*
65 * Local variables:
66 * tab-width: 4
67 * c-basic-offset: 4
68 * End:
69 * vim600: noet sw=4 ts=4 fdm=marker
70 * vim<600: noet sw=4 ts=4
71 */
72