Line data Source code
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 170 : php_http_version_t *php_http_version_init(php_http_version_t *v, unsigned major, unsigned minor TSRMLS_DC)
16 : {
17 170 : if (!v) {
18 0 : v = emalloc(sizeof(*v));
19 : }
20 :
21 170 : v->major = major;
22 170 : v->minor = minor;
23 :
24 170 : return v;
25 : }
26 :
27 160 : php_http_version_t *php_http_version_parse(php_http_version_t *v, const char *str TSRMLS_DC)
28 : {
29 : long major, minor;
30 160 : char separator = 0;
31 160 : register const char *ptr = str;
32 :
33 160 : switch (*ptr) {
34 : case 'h':
35 : case 'H':
36 136 : ++ptr; if (*ptr != 't' && *ptr != 'T') break;
37 136 : ++ptr; if (*ptr != 't' && *ptr != 'T') break;
38 136 : ++ptr; if (*ptr != 'p' && *ptr != 'P') break;
39 136 : ++ptr; if (*ptr != '/') break;
40 136 : ++ptr;
41 : /* no break */
42 : default:
43 : /* rfc7230#2.6 The HTTP version number consists of two decimal digits separated by a "." (period or decimal point) */
44 160 : major = *ptr++ - '0';
45 160 : if (major >= 0 && major <= 9) {
46 159 : separator = *ptr++;
47 159 : if (separator) {
48 159 : if (separator != '.' && separator != ',') {
49 1 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Non-standard version separator '%c' in HTTP protocol version '%s'", separator, ptr - 2);
50 : }
51 159 : minor = *ptr - '0';
52 159 : if (minor >= 0 && minor <= 9) {
53 159 : return php_http_version_init(v, major, minor TSRMLS_CC);
54 : }
55 : }
56 : }
57 : }
58 :
59 1 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP protocol version '%s'", str);
60 1 : return NULL;
61 : }
62 :
63 38 : void php_http_version_to_string(php_http_version_t *v, char **str, size_t *len, const char *pre, const char *post TSRMLS_DC)
64 : {
65 38 : *len = spprintf(str, 0, "%s%u.%u%s", pre ? pre : "", v->major, v->minor, post ? post : "");
66 38 : }
67 :
68 20 : void php_http_version_dtor(php_http_version_t *v)
69 : {
70 : (void) v;
71 20 : }
72 :
73 0 : void php_http_version_free(php_http_version_t **v)
74 : {
75 0 : if (*v) {
76 0 : php_http_version_dtor(*v);
77 0 : efree(*v);
78 0 : *v = NULL;
79 : }
80 0 : }
81 :
82 : /*
83 : * Local variables:
84 : * tab-width: 4
85 : * c-basic-offset: 4
86 : * End:
87 : * vim600: noet sw=4 ts=4 fdm=marker
88 : * vim<600: noet sw=4 ts=4
89 : */
90 :
|