- increment refcount for HttpRequest object put into hashtable as debug wrapper
[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 #include "php.h"
22
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_request_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", (ulong) 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 (HTTP_MSG_TYPE(RESPONSE, msg)) {
217 phpstr_from_string_ex(PHPSTR(msg), body, message + message_length - body);
218 } else {
219 continue_at = body;
220 }
221
222 /* check for following messages */
223 if (continue_at) {
224 while (isspace(*continue_at)) ++continue_at;
225 if (continue_at < (message + message_length)) {
226 http_message *next = NULL, *most = NULL;
227
228 /* set current message to parent of most parent following messages and return deepest */
229 if (most = next = http_message_parse(continue_at, message + message_length - continue_at)) {
230 while (most->parent) most = most->parent;
231 most->parent = msg;
232 msg = next;
233 }
234 }
235 }
236 }
237
238 return msg;
239 }
240
241 PHP_HTTP_API void _http_message_tostring(http_message *msg, char **string, size_t *length)
242 {
243 phpstr str;
244 char *key, *data;
245 ulong idx;
246 zval **header;
247
248 phpstr_init_ex(&str, 4096, 0);
249
250 switch (msg->type)
251 {
252 case HTTP_MSG_REQUEST:
253 phpstr_appendf(&str, "%s %s HTTP/%1.1f" HTTP_CRLF,
254 msg->info.request.method,
255 msg->info.request.URI,
256 msg->info.request.http_version);
257 break;
258
259 case HTTP_MSG_RESPONSE:
260 phpstr_appendf(&str, "HTTP/%1.1f %d" HTTP_CRLF,
261 msg->info.response.http_version,
262 msg->info.response.code);
263 break;
264
265 case HTTP_MSG_NONE:
266 default:
267 break;
268 }
269
270 FOREACH_HASH_KEYVAL(&msg->hdrs, key, idx, header) {
271 if (key) {
272 zval **single_header;
273
274 switch (Z_TYPE_PP(header))
275 {
276 case IS_STRING:
277 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key, Z_STRVAL_PP(header));
278 break;
279
280 case IS_ARRAY:
281 FOREACH_VAL(*header, single_header) {
282 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key, Z_STRVAL_PP(single_header));
283 }
284 break;
285 }
286
287 key = NULL;
288 }
289 }
290
291 if (PHPSTR_LEN(msg)) {
292 phpstr_appends(&str, HTTP_CRLF);
293 phpstr_append(&str, PHPSTR_VAL(msg), PHPSTR_LEN(msg));
294 phpstr_appends(&str, HTTP_CRLF);
295 }
296
297 data = phpstr_data(&str, string, length);
298 if (!string) {
299 efree(data);
300 }
301
302 phpstr_dtor(&str);
303 }
304
305 PHP_HTTP_API void _http_message_serialize(http_message *message, char **string, size_t *length)
306 {
307 char *buf;
308 size_t len;
309 phpstr str;
310
311 phpstr_init(&str);
312
313 do {
314 http_message_tostring(message, &buf, &len);
315 phpstr_prepend(&str, buf, len);
316 efree(buf);
317 } while (message = message->parent);
318
319 buf = phpstr_data(&str, string, length);
320 if (!string) {
321 efree(buf);
322 }
323
324 phpstr_dtor(&str);
325 }
326
327 PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC)
328 {
329 STATUS rs = FAILURE;
330
331 switch (message->type)
332 {
333 case HTTP_MSG_RESPONSE:
334 {
335 char *key;
336 ulong idx;
337 zval **val;
338
339 FOREACH_HASH_KEYVAL(&message->hdrs, key, idx, val) {
340 if (key) {
341 char *header;
342 spprintf(&header, 0, "%s: %s", key, Z_STRVAL_PP(val));
343 http_send_header(header);
344 efree(header);
345 key = NULL;
346 }
347 }
348 rs = SUCCESS == http_send_status(message->info.response.code) &&
349 SUCCESS == http_send_data(PHPSTR_VAL(message), PHPSTR_LEN(message)) ?
350 SUCCESS : FAILURE;
351 }
352 break;
353
354 case HTTP_MSG_REQUEST:
355 {
356 #ifdef HTTP_HAVE_CURL
357 char *uri = NULL;
358 zval **zhost, options, headers;
359
360 array_init(&options);
361 array_init(&headers);
362 zend_hash_copy(Z_ARRVAL(headers), &message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
363 add_assoc_zval(&options, "headers", &headers);
364
365 /* check host header */
366 if (SUCCESS == zend_hash_find(&message->hdrs, "Host", sizeof("Host"), (void **) &zhost)) {
367 char *colon = NULL, *host = NULL;
368 size_t host_len = 0;
369 int port = 0;
370
371 /* check for port */
372 if (colon = strchr(Z_STRVAL_PP(zhost), ':')) {
373 port = atoi(colon + 1);
374 host = estrndup(Z_STRVAL_PP(zhost), host_len = (Z_STRVAL_PP(zhost) - colon - 1));
375 } else {
376 host = estrndup(Z_STRVAL_PP(zhost), host_len = Z_STRLEN_PP(zhost));
377 }
378 uri = http_absolute_uri_ex(
379 message->info.request.URI, strlen(message->info.request.URI),
380 NULL, 0, host, host_len, port);
381 efree(host);
382 } else {
383 uri = http_absolute_uri(message->info.request.URI);
384 }
385
386 if (!strcasecmp("POST", message->info.request.method)) {
387 http_request_body body = {HTTP_REQUEST_BODY_CSTRING, PHPSTR_VAL(message), PHPSTR_LEN(message)};
388 rs = http_post(uri, &body, Z_ARRVAL(options), NULL, NULL);
389 } else
390 if (!strcasecmp("GET", message->info.request.method)) {
391 rs = http_get(uri, Z_ARRVAL(options), NULL, NULL);
392 } else
393 if (!strcasecmp("HEAD", message->info.request.method)) {
394 rs = http_head(uri, Z_ARRVAL(options), NULL, NULL);
395 } else {
396 http_error_ex(E_WARNING, HTTP_E_MSG,
397 "Cannot send HttpMessage. Request method %s not supported",
398 message->info.request.method);
399 }
400
401 efree(uri);
402 #else
403 http_error(E_WARNING, HTTP_E_MSG, "HTTP requests not supported - ext/http was not linked against libcurl.");
404 #endif
405 }
406 break;
407
408 case HTTP_MSG_NONE:
409 default:
410 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is neither of type HTTP_MSG_REQUEST nor HTTP_MSG_RESPONSE");
411 break;
412 }
413
414 return rs;
415 }
416
417 PHP_HTTP_API http_message *_http_message_dup(http_message *msg TSRMLS_DC)
418 {
419 /*
420 * TODO: unroll
421 */
422 http_message *new;
423 char *serialized_data;
424 size_t serialized_length;
425
426 http_message_serialize(msg, &serialized_data, &serialized_length);
427 new = http_message_parse(serialized_data, serialized_length);
428 efree(serialized_data);
429 return new;
430 }
431
432 PHP_HTTP_API void _http_message_dtor(http_message *message)
433 {
434 if (message) {
435 zend_hash_destroy(&message->hdrs);
436 phpstr_dtor(PHPSTR(message));
437 if (HTTP_MSG_TYPE(REQUEST, message)) {
438 if (message->info.request.method) {
439 efree(message->info.request.method);
440 message->info.request.method = NULL;
441 }
442 if (message->info.request.URI) {
443 efree(message->info.request.URI);
444 message->info.request.URI = NULL;
445 }
446 }
447 }
448 }
449
450 PHP_HTTP_API void _http_message_free(http_message *message)
451 {
452 if (message) {
453 if (message->parent) {
454 http_message_free(message->parent);
455 message->parent = NULL;
456 }
457 http_message_dtor(message);
458 efree(message);
459 }
460 }
461
462 /*
463 * Local variables:
464 * tab-width: 4
465 * c-basic-offset: 4
466 * End:
467 * vim600: noet sw=4 ts=4 fdm=marker
468 * vim<600: noet sw=4 ts=4
469 */