- serialize HttpMessage in the same order as deserialized
[m6w6/ext-http] / http_message_api.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #include "php.h"
23 #include "php_http.h"
24 #include "php_http_std_defs.h"
25 #include "php_http_api.h"
26 #include "php_http_message_api.h"
27 #include "php_http_headers_api.h"
28 #include "php_http_send_api.h"
29 #include "php_http_curl_api.h"
30 #include "php_http_url_api.h"
31
32 #include "phpstr/phpstr.h"
33
34 #define http_message_headers_cb _http_message_headers_cb
35 static void _http_message_headers_cb(const char *http_line, HashTable **headers, void **message TSRMLS_DC)
36 {
37 size_t line_length;
38 char *crlf = NULL;
39 http_message *new, *old = (http_message *) *message;
40
41 if (crlf = strstr(http_line, HTTP_CRLF)) {
42 line_length = crlf - http_line;
43 } else {
44 line_length = strlen(http_line);
45 }
46
47 if (old->type || zend_hash_num_elements(&old->hdrs) || PHPSTR_LEN(old)) {
48 new = http_message_new();
49
50 new->parent = old;
51 *message = new;
52 *headers = &new->hdrs;
53 } else {
54 new = old;
55 }
56
57 while (isspace(http_line[line_length-1])) --line_length;
58
59 // response
60 if (!strncmp(http_line, "HTTP/1.", lenof("HTTP/1."))) {
61 new->type = HTTP_MSG_RESPONSE;
62 new->info.response.http_version = atof(http_line + lenof("HTTP/"));
63 new->info.response.code = atoi(http_line + lenof("HTTP/1.1 "));
64 } else
65 // request
66 if (!strncmp(http_line + line_length - lenof("HTTP/1.1"), "HTTP/1.", lenof("HTTP/1."))) {
67 const char *method_sep_uri = strchr(http_line, ' ');
68 new->type = HTTP_MSG_REQUEST;
69 new->info.request.http_version = atof(http_line + line_length - lenof("1.1"));
70 new->info.request.method = estrndup(http_line, method_sep_uri - http_line);
71 new->info.request.URI = estrndup(method_sep_uri + 1, http_line + line_length - method_sep_uri - 1 - lenof(" HTTP/1.1"));
72 }
73 }
74
75 #define http_message_init_type _http_message_init_type
76 static inline void _http_message_init_type(http_message *message, http_message_type type)
77 {
78 switch (message->type = type)
79 {
80 case HTTP_MSG_RESPONSE:
81 message->info.response.http_version = .0;
82 message->info.response.code = 0;
83 break;
84
85 case HTTP_MSG_REQUEST:
86 message->info.request.http_version = .0;
87 message->info.request.method = NULL;
88 message->info.request.URI = NULL;
89 break;
90
91 case HTTP_MSG_NONE:
92 default:
93 break;
94 }
95 }
96
97 #define http_message_header(m, h) _http_message_header_ex((m), (h), sizeof(h))
98 #define http_message_header_ex _http_message_header_ex
99 static inline zval *_http_message_header_ex(http_message *msg, char *key_str, size_t key_len)
100 {
101 zval **header;
102 if (SUCCESS == zend_hash_find(&msg->hdrs, key_str, key_len, (void **) &header)) {
103 return *header;
104 }
105 return NULL;
106 }
107
108 PHP_HTTP_API http_message *_http_message_init_ex(http_message *message, http_message_type type)
109 {
110 if (!message) {
111 message = ecalloc(1, sizeof(http_message));
112 }
113
114 http_message_init_type(message, type);
115 message->parent = NULL;
116 phpstr_init(&message->body);
117 zend_hash_init(&message->hdrs, 0, NULL, ZVAL_PTR_DTOR, 0);
118
119 return message;
120 }
121
122
123 PHP_HTTP_API void _http_message_set_type(http_message *message, http_message_type type)
124 {
125 /* just act if different */
126 if (type != message->type) {
127
128 /* free request info */
129 if (message->type == HTTP_MSG_REQUEST) {
130 if (message->info.request.method) {
131 efree(message->info.request.method);
132 }
133 if (message->info.request.URI) {
134 efree(message->info.request.URI);
135 }
136 }
137
138 /* init */
139 http_message_init_type(message, type);
140 }
141 }
142
143 PHP_HTTP_API http_message *_http_message_parse_ex(http_message *msg, const char *message, size_t message_length TSRMLS_DC)
144 {
145 char *body = NULL;
146 zend_bool free_msg = msg ? 0 : 1;
147
148 if (message_length < HTTP_MSG_MIN_SIZE) {
149 return NULL;
150 }
151
152 if (!message) {
153 return NULL;
154 }
155
156 msg = http_message_init(msg);
157
158 if (SUCCESS != http_parse_headers_cb(message, &msg->hdrs, 1, http_message_headers_cb, (void **) &msg)) {
159 if (free_msg) {
160 http_message_free(msg);
161 }
162 return NULL;
163 }
164
165 /* header parsing stops at CRLF CRLF */
166 if (body = strstr(message, HTTP_CRLF HTTP_CRLF)) {
167 zval *c;
168 const char *continue_at = NULL;
169
170 body += lenof(HTTP_CRLF HTTP_CRLF);
171
172 /* message has content-length header */
173 if (c = http_message_header(msg, "Content-Length")) {
174 long len = atol(Z_STRVAL_P(c));
175 phpstr_from_string_ex(PHPSTR(msg), body, len);
176 continue_at = body + len;
177 } else
178
179 /* message has chunked transfer encoding */
180 if (c = http_message_header(msg, "Transfer-Encoding")) {
181 if (!strcasecmp("chunked", Z_STRVAL_P(c))) {
182 char *decoded;
183 size_t decoded_len;
184
185 /* decode and replace Transfer-Encoding with Content-Length header */
186 if (continue_at = http_chunked_decode(body, message + message_length - body, &decoded, &decoded_len)) {
187 phpstr_from_string_ex(PHPSTR(msg), decoded, decoded_len);
188 efree(decoded);
189 {
190 zval *len;
191 char *tmp;
192
193 spprintf(&tmp, 0, "%lu", decoded_len);
194 MAKE_STD_ZVAL(len);
195 ZVAL_STRING(len, tmp, 0);
196
197 zend_hash_del(&msg->hdrs, "Transfer-Encoding", sizeof("Transfer-Encoding"));
198 zend_hash_add(&msg->hdrs, "Content-Length", sizeof("Content-Length"), (void *) &len, sizeof(zval *), NULL);
199 }
200 }
201 }
202 } else
203
204 /* message has content-range header */
205 if (c = http_message_header(msg, "Content-Range")) {
206 ulong start = 0, end = 0;
207
208 sscanf(Z_STRVAL_P(c), "bytes=%lu-%lu", &start, &end);
209 if (end > start) {
210 phpstr_from_string_ex(PHPSTR(msg), body, (size_t) (end - start));
211 continue_at = body + (end - start);
212 }
213 } else
214
215 /* no headers that indicate content length */
216 if (1) {
217 phpstr_from_string_ex(PHPSTR(msg), body, message + message_length - body);
218 }
219
220 /* check for following messages */
221 if (continue_at) {
222 while (isspace(*continue_at)) ++continue_at;
223 if (continue_at < (message + message_length)) {
224 http_message *next = NULL, *most = NULL;
225
226 /* set current message to parent of most parent following messages and return deepest */
227 if (most = next = http_message_parse(continue_at, message + message_length - continue_at)) {
228 while (most->parent) most = most->parent;
229 most->parent = msg;
230 msg = next;
231 }
232 }
233 }
234 }
235
236 return msg;
237 }
238
239 PHP_HTTP_API void _http_message_tostring(http_message *msg, char **string, size_t *length)
240 {
241 phpstr str;
242 char *key, *data;
243 ulong idx;
244 zval **header;
245
246 phpstr_init_ex(&str, 4096, 0);
247
248 switch (msg->type)
249 {
250 case HTTP_MSG_REQUEST:
251 phpstr_appendf(&str, "%s %s HTTP/%1.1f" HTTP_CRLF,
252 msg->info.request.method,
253 msg->info.request.URI,
254 msg->info.request.http_version);
255 break;
256
257 case HTTP_MSG_RESPONSE:
258 phpstr_appendf(&str, "HTTP/%1.1f %d" HTTP_CRLF,
259 msg->info.response.http_version,
260 msg->info.response.code);
261 break;
262
263 case HTTP_MSG_NONE:
264 default:
265 break;
266 }
267
268 FOREACH_HASH_KEYVAL(&msg->hdrs, key, idx, header) {
269 if (key) {
270 zval **single_header;
271
272 switch (Z_TYPE_PP(header))
273 {
274 case IS_STRING:
275 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key, Z_STRVAL_PP(header));
276 break;
277
278 case IS_ARRAY:
279 FOREACH_VAL(*header, single_header) {
280 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key, Z_STRVAL_PP(single_header));
281 }
282 break;
283 }
284
285 key = NULL;
286 }
287 }
288
289 if (PHPSTR_LEN(msg)) {
290 phpstr_appends(&str, HTTP_CRLF);
291 phpstr_append(&str, PHPSTR_VAL(msg), PHPSTR_LEN(msg));
292 phpstr_appends(&str, HTTP_CRLF);
293 }
294
295 data = phpstr_data(&str, string, length);
296 if (!string) {
297 efree(data);
298 }
299
300 phpstr_dtor(&str);
301 }
302
303 PHP_HTTP_API void _http_message_serialize(http_message *message, char **string, size_t *length)
304 {
305 char *buf;
306 size_t len;
307 phpstr str;
308
309 phpstr_init(&str);
310
311 do {
312 http_message_tostring(message, &buf, &len);
313 phpstr_prepend(&str, buf, len);
314 efree(buf);
315 } while (message = message->parent);
316
317 buf = phpstr_data(&str, string, length);
318 if (!string) {
319 efree(buf);
320 }
321
322 phpstr_dtor(&str);
323 }
324
325 PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC)
326 {
327 STATUS rs = FAILURE;
328
329 switch (message->type)
330 {
331 case HTTP_MSG_RESPONSE:
332 {
333 char *key;
334 ulong idx;
335 zval **val;
336
337 FOREACH_HASH_KEYVAL(&message->hdrs, key, idx, val) {
338 if (key) {
339 char *header;
340 spprintf(&header, 0, "%s: %s", key, Z_STRVAL_PP(val));
341 http_send_header(header);
342 efree(header);
343 key = NULL;
344 }
345 }
346 rs = SUCCESS == http_send_status(message->info.response.code) &&
347 SUCCESS == http_send_data(PHPSTR_VAL(message), PHPSTR_LEN(message)) ?
348 SUCCESS : FAILURE;
349 }
350 break;
351
352 case HTTP_MSG_REQUEST:
353 {
354 #ifdef HTTP_HAVE_CURL
355 char *uri = NULL;
356 zval **zhost, options, headers;
357
358 array_init(&options);
359 array_init(&headers);
360 zend_hash_copy(Z_ARRVAL(headers), &message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
361 add_assoc_zval(&options, "headers", &headers);
362
363 /* check host header */
364 if (SUCCESS == zend_hash_find(&message->hdrs, "Host", sizeof("Host"), (void **) &zhost)) {
365 char *colon = NULL, *host = NULL;
366 size_t host_len = 0;
367 int port = 0;
368
369 /* check for port */
370 if (colon = strchr(Z_STRVAL_PP(zhost), ':')) {
371 port = atoi(colon + 1);
372 host = estrndup(Z_STRVAL_PP(zhost), host_len = (Z_STRVAL_PP(zhost) - colon - 1));
373 } else {
374 host = estrndup(Z_STRVAL_PP(zhost), host_len = Z_STRLEN_PP(zhost));
375 }
376 uri = http_absolute_uri_ex(
377 message->info.request.URI, strlen(message->info.request.URI),
378 NULL, 0, host, host_len, port);
379 efree(host);
380 } else {
381 uri = http_absolute_uri(message->info.request.URI);
382 }
383
384 if (!strcasecmp("POST", message->info.request.method)) {
385 rs = http_post_data(uri, PHPSTR_VAL(message), PHPSTR_LEN(message), Z_ARRVAL(options), NULL, NULL);
386 } else
387 if (!strcasecmp("GET", message->info.request.method)) {
388 rs = http_get(uri, Z_ARRVAL(options), NULL, NULL);
389 } else
390 if (!strcasecmp("HEAD", message->info.request.method)) {
391 rs = http_head(uri, Z_ARRVAL(options), NULL, NULL);
392 } else {
393 http_error_ex(E_WARNING, HTTP_E_MSG,
394 "Cannot send HttpMessage. Request method %s not supported",
395 message->info.request.method);
396 }
397
398 efree(uri);
399 #else
400 http_error(E_WARNING, HTTP_E_MSG, "HTTP requests not supported - ext/http was not linked against libcurl.");
401 #endif
402 }
403 break;
404
405 case HTTP_MSG_NONE:
406 default:
407 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is neither of type HTTP_MSG_REQUEST nor HTTP_MSG_RESPONSE");
408 break;
409 }
410
411 return rs;
412 }
413
414 PHP_HTTP_API void _http_message_dtor(http_message *message)
415 {
416 if (message) {
417 zend_hash_destroy(&message->hdrs);
418 phpstr_dtor(PHPSTR(message));
419 if (HTTP_MSG_TYPE(REQUEST, message)) {
420 if (message->info.request.method) {
421 efree(message->info.request.method);
422 message->info.request.method = NULL;
423 }
424 if (message->info.request.URI) {
425 efree(message->info.request.URI);
426 message->info.request.URI = NULL;
427 }
428 }
429 }
430 }
431
432 PHP_HTTP_API void _http_message_free(http_message *message)
433 {
434 if (message) {
435 if (message->parent) {
436 http_message_free(message->parent);
437 message->parent = NULL;
438 }
439 http_message_dtor(message);
440 efree(message);
441 }
442 }
443
444 /*
445 * Local variables:
446 * tab-width: 4
447 * c-basic-offset: 4
448 * End:
449 * vim600: noet sw=4 ts=4 fdm=marker
450 * vim<600: noet sw=4 ts=4
451 */
452