release 2.6.0
[m6w6/ext-http] / src / php_http_info.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_info_t *php_http_info_init(php_http_info_t *i TSRMLS_DC)
16 {
17 if (!i) {
18 i = emalloc(sizeof(*i));
19 }
20
21 memset(i, 0, sizeof(*i));
22
23 return i;
24 }
25
26 void php_http_info_dtor(php_http_info_t *i)
27 {
28 switch (i->type) {
29 case PHP_HTTP_REQUEST:
30 PTR_SET(PHP_HTTP_INFO(i).request.method, NULL);
31 PTR_SET(PHP_HTTP_INFO(i).request.url, NULL);
32 break;
33
34 case PHP_HTTP_RESPONSE:
35 PTR_SET(PHP_HTTP_INFO(i).response.status, NULL);
36 break;
37
38 default:
39 break;
40 }
41 }
42
43 void php_http_info_free(php_http_info_t **i)
44 {
45 if (*i) {
46 php_http_info_dtor(*i);
47 efree(*i);
48 *i = NULL;
49 }
50 }
51
52 void php_http_info_to_string(php_http_info_t *info, char **str, size_t *len, const char *eol TSRMLS_DC)
53 {
54 char *tmp = NULL;
55
56 if (info->http.version.major == 2) {
57 if (info->type == PHP_HTTP_REQUEST) {
58 *len = spprintf(str, 0, "%s %s HTTP/2%s",
59 info->http.info.request.method?info->http.info.request.method:"UNKNOWN",
60 info->http.info.request.method&&!strcasecmp(info->http.info.request.method,"CONNECT")?(
61 info->http.info.request.url?php_http_url_authority_to_string(info->http.info.request.url, &(tmp), NULL):"0"):(
62 info->http.info.request.url?php_http_url_to_string(info->http.info.request.url, &(tmp), NULL, 0):"/"),
63 eol);
64 } else if (info->type == PHP_HTTP_RESPONSE) {
65 *len = spprintf(str, 0, "HTTP/2 %d%s%s%s",
66 info->http.info.response.code?info->http.info.response.code:200,
67 info->http.info.response.status&&*info->http.info.response.status ? " ":"",
68 STR_PTR(info->http.info.response.status),
69 eol);
70 }
71 } else if (info->type == PHP_HTTP_REQUEST) {
72 *len = spprintf(str, 0, "%s %s HTTP/%u.%u%s",
73 info->http.info.request.method?info->http.info.request.method:"UNKNOWN",
74 info->http.info.request.method&&!strcasecmp(info->http.info.request.method,"CONNECT")?(
75 info->http.info.request.url?php_http_url_authority_to_string(info->http.info.request.url, &(tmp), NULL):"0"):(
76 info->http.info.request.url?php_http_url_to_string(info->http.info.request.url, &(tmp), NULL, 0):"/"),
77 info->http.version.major||info->http.version.major?info->http.version.major:1,
78 info->http.version.major||info->http.version.minor?info->http.version.minor:1,
79 eol);
80 } else if (info->type == PHP_HTTP_RESPONSE){
81 *len = spprintf(str, 0, "HTTP/%u.%u %d%s%s%s", info->http.version.major||info->http.version.major?info->http.version.major:1,
82 info->http.version.major||info->http.version.minor?info->http.version.minor:1,
83 info->http.info.response.code?info->http.info.response.code:200,
84 info->http.info.response.status&&*info->http.info.response.status ? " ":"",
85 STR_PTR(info->http.info.response.status),
86 eol);
87 }
88
89 STR_FREE(tmp);
90 }
91
92 php_http_info_t *php_http_info_parse(php_http_info_t *info, const char *pre_header TSRMLS_DC)
93 {
94 const char *end, *http, *off;
95 zend_bool free_info = !info;
96
97 /* sane parameter */
98 if ((!pre_header) || (!*pre_header)) {
99 return NULL;
100 }
101
102 /* where's the end of the line */
103 if (!(end = php_http_locate_eol(pre_header, NULL))) {
104 end = pre_header + strlen(pre_header);
105 }
106
107 /* there must be HTTP/1.x in the line */
108 if (!(http = php_http_locate_str(pre_header, end - pre_header, "HTTP/", lenof("HTTP/")))) {
109 return NULL;
110 }
111
112 info = php_http_info_init(info TSRMLS_CC);
113
114 if (!php_http_version_parse(&info->http.version, http TSRMLS_CC)) {
115 if (free_info) {
116 php_http_info_free(&info);
117 }
118 return NULL;
119 }
120
121 /* clumsy fix for changed libcurl behaviour in 7.49.1, see https://github.com/curl/curl/issues/888 */
122 off = &http[lenof("HTTP/X")];
123 if (info->http.version.major < 2 || (info->http.version.major == 2 && *off == '.')) {
124 off += 2;
125 }
126
127 /* and nothing than SPACE or NUL after HTTP/X(.x) */
128 if (*off && (!PHP_HTTP_IS_CTYPE(space, *off))) {
129 if (free_info) {
130 php_http_info_free(&info);
131 }
132 return NULL;
133 }
134
135 #if 0
136 {
137 char *line = estrndup(pre_header, end - pre_header);
138 fprintf(stderr, "http_parse_info('%s')\n", line);
139 efree(line);
140 }
141 #endif
142
143 /* is response */
144 if (pre_header == http) {
145 const char *status = NULL, *code = off;
146
147 info->type = PHP_HTTP_RESPONSE;
148 while (' ' == *code) ++code;
149 if (end > code) {
150 /* rfc7230#3.1.2 The status-code element is a 3-digit integer code */
151 PHP_HTTP_INFO(info).response.code = 100*(*code++ - '0');
152 PHP_HTTP_INFO(info).response.code += 10*(*code++ - '0');
153 PHP_HTTP_INFO(info).response.code += *code++ - '0';
154 if (PHP_HTTP_INFO(info).response.code < 100 || PHP_HTTP_INFO(info).response.code > 599) {
155 if (free_info) {
156 php_http_info_free(&info);
157 }
158 return NULL;
159 }
160 status = code;
161 } else {
162 PHP_HTTP_INFO(info).response.code = 0;
163 }
164 if (status && end > status) {
165 while (' ' == *status) ++status;
166 PHP_HTTP_INFO(info).response.status = estrndup(status, end - status);
167 } else {
168 PHP_HTTP_INFO(info).response.status = NULL;
169 }
170
171 return info;
172 }
173
174 /* is request */
175 else if (*(http - 1) == ' ' && (!*off || *off == '\r' || *off == '\n')) {
176 const char *url = strchr(pre_header, ' ');
177
178 info->type = PHP_HTTP_REQUEST;
179 if (url && http > url) {
180 size_t url_len = url - pre_header;
181
182 PHP_HTTP_INFO(info).request.method = estrndup(pre_header, url_len);
183
184 while (' ' == *url) ++url;
185 while (' ' == *(http-1)) --http;
186
187 if (http > url) {
188 /* CONNECT presents an authority only */
189 if (strcasecmp(PHP_HTTP_INFO(info).request.method, "CONNECT")) {
190 PHP_HTTP_INFO(info).request.url = php_http_url_parse(url, http - url, PHP_HTTP_URL_STDFLAGS TSRMLS_CC);
191 } else {
192 PHP_HTTP_INFO(info).request.url = php_http_url_parse_authority(url, http - url, PHP_HTTP_URL_STDFLAGS TSRMLS_CC);
193 }
194 if (!PHP_HTTP_INFO(info).request.url) {
195 PTR_SET(PHP_HTTP_INFO(info).request.method, NULL);
196 return NULL;
197 }
198 } else {
199 PTR_SET(PHP_HTTP_INFO(info).request.method, NULL);
200 return NULL;
201 }
202 } else {
203 PHP_HTTP_INFO(info).request.method = NULL;
204 PHP_HTTP_INFO(info).request.url = NULL;
205 }
206
207 return info;
208 }
209
210 /* some darn header containing HTTP/X(.x) */
211 else {
212 if (free_info) {
213 php_http_info_free(&info);
214 }
215 return NULL;
216 }
217 }
218
219 /*
220 * Local variables:
221 * tab-width: 4
222 * c-basic-offset: 4
223 * End:
224 * vim600: noet sw=4 ts=4 fdm=marker
225 * vim<600: noet sw=4 ts=4
226 */
227