e4ded1e799ca0204817826e47401436508d52e83
[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 PHP_HTTP_API http_message *_http_message_init_ex(http_message *message, http_message_type type)
98 {
99 if (!message) {
100 message = ecalloc(1, sizeof(http_message));
101 }
102
103 http_message_init_type(message, type);
104 message->parent = NULL;
105 phpstr_init(&message->body);
106 zend_hash_init(&message->hdrs, 0, NULL, ZVAL_PTR_DTOR, 0);
107
108 return message;
109 }
110
111
112 PHP_HTTP_API void _http_message_set_type(http_message *message, http_message_type type)
113 {
114 /* just act if different */
115 if (type != message->type) {
116
117 /* free request info */
118 if (message->type == HTTP_MSG_REQUEST) {
119 if (message->info.request.method) {
120 efree(message->info.request.method);
121 }
122 if (message->info.request.URI) {
123 efree(message->info.request.URI);
124 }
125 }
126
127 /* init */
128 http_message_init_type(message, type);
129 }
130 }
131
132 PHP_HTTP_API http_message *_http_message_parse_ex(http_message *msg, const char *message, size_t message_length TSRMLS_DC)
133 {
134 char *body = NULL;
135 zend_bool free_msg = msg ? 0 : 1;
136
137 if (message_length < HTTP_MSG_MIN_SIZE) {
138 return NULL;
139 }
140
141 if (!message) {
142 return NULL;
143 }
144
145 msg = http_message_init(msg);
146
147 if (SUCCESS != http_parse_headers_cb(message, &msg->hdrs, 1, http_message_headers_cb, (void **) &msg)) {
148 if (free_msg) {
149 http_message_free(msg);
150 }
151 return NULL;
152 }
153
154 /* header parsing stops at CRLF CRLF */
155 if (body = strstr(message, HTTP_CRLF HTTP_CRLF)) {
156 zval *c;
157 const char *continue_at = NULL;
158
159 body += lenof(HTTP_CRLF HTTP_CRLF);
160
161 /* message has content-length header */
162 if (c = http_message_header(msg, "Content-Length")) {
163 long len = atol(Z_STRVAL_P(c));
164 phpstr_from_string_ex(PHPSTR(msg), body, len);
165 continue_at = body + len;
166 } else
167
168 /* message has chunked transfer encoding */
169 if (c = http_message_header(msg, "Transfer-Encoding")) {
170 if (!strcasecmp("chunked", Z_STRVAL_P(c))) {
171 char *decoded;
172 size_t decoded_len;
173
174 /* decode and replace Transfer-Encoding with Content-Length header */
175 if (continue_at = http_chunked_decode(body, message + message_length - body, &decoded, &decoded_len)) {
176 phpstr_from_string_ex(PHPSTR(msg), decoded, decoded_len);
177 efree(decoded);
178 {
179 zval *len;
180 char *tmp;
181
182 spprintf(&tmp, 0, "%lu", (ulong) decoded_len);
183 MAKE_STD_ZVAL(len);
184 ZVAL_STRING(len, tmp, 0);
185
186 zend_hash_del(&msg->hdrs, "Transfer-Encoding", sizeof("Transfer-Encoding"));
187 zend_hash_add(&msg->hdrs, "Content-Length", sizeof("Content-Length"), (void *) &len, sizeof(zval *), NULL);
188 }
189 }
190 }
191 } else
192
193 /* message has content-range header */
194 if (c = http_message_header(msg, "Content-Range")) {
195 ulong start = 0, end = 0;
196
197 sscanf(Z_STRVAL_P(c), "bytes=%lu-%lu", &start, &end);
198 if (end > start) {
199 phpstr_from_string_ex(PHPSTR(msg), body, (size_t) (end - start));
200 continue_at = body + (end - start);
201 }
202 } else
203
204 /* no headers that indicate content length */
205 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
206 phpstr_from_string_ex(PHPSTR(msg), body, message + message_length - body);
207 } else {
208 continue_at = body;
209 }
210
211 /* check for following messages */
212 if (continue_at) {
213 while (isspace(*continue_at)) ++continue_at;
214 if (continue_at < (message + message_length)) {
215 http_message *next = NULL, *most = NULL;
216
217 /* set current message to parent of most parent following messages and return deepest */
218 if (most = next = http_message_parse(continue_at, message + message_length - continue_at)) {
219 while (most->parent) most = most->parent;
220 most->parent = msg;
221 msg = next;
222 }
223 }
224 }
225 }
226
227 return msg;
228 }
229
230 PHP_HTTP_API void _http_message_tostring(http_message *msg, char **string, size_t *length)
231 {
232 phpstr str;
233 char *key, *data;
234 ulong idx;
235 zval **header;
236
237 phpstr_init_ex(&str, 4096, 0);
238
239 switch (msg->type)
240 {
241 case HTTP_MSG_REQUEST:
242 phpstr_appendf(&str, "%s %s HTTP/%1.1f" HTTP_CRLF,
243 msg->info.request.method,
244 msg->info.request.URI,
245 msg->info.request.http_version);
246 break;
247
248 case HTTP_MSG_RESPONSE:
249 phpstr_appendf(&str, "HTTP/%1.1f %d" HTTP_CRLF,
250 msg->info.response.http_version,
251 msg->info.response.code);
252 break;
253
254 case HTTP_MSG_NONE:
255 default:
256 break;
257 }
258
259 FOREACH_HASH_KEYVAL(&msg->hdrs, key, idx, header) {
260 if (key) {
261 zval **single_header;
262
263 switch (Z_TYPE_PP(header))
264 {
265 case IS_STRING:
266 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key, Z_STRVAL_PP(header));
267 break;
268
269 case IS_ARRAY:
270 FOREACH_VAL(*header, single_header) {
271 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key, Z_STRVAL_PP(single_header));
272 }
273 break;
274 }
275
276 key = NULL;
277 }
278 }
279
280 if (PHPSTR_LEN(msg)) {
281 phpstr_appends(&str, HTTP_CRLF);
282 phpstr_append(&str, PHPSTR_VAL(msg), PHPSTR_LEN(msg));
283 phpstr_appends(&str, HTTP_CRLF);
284 }
285
286 data = phpstr_data(&str, string, length);
287 if (!string) {
288 efree(data);
289 }
290
291 phpstr_dtor(&str);
292 }
293
294 PHP_HTTP_API void _http_message_serialize(http_message *message, char **string, size_t *length)
295 {
296 char *buf;
297 size_t len;
298 phpstr str;
299
300 phpstr_init(&str);
301
302 do {
303 http_message_tostring(message, &buf, &len);
304 phpstr_prepend(&str, buf, len);
305 efree(buf);
306 } while (message = message->parent);
307
308 buf = phpstr_data(&str, string, length);
309 if (!string) {
310 efree(buf);
311 }
312
313 phpstr_dtor(&str);
314 }
315
316 PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC)
317 {
318 STATUS rs = FAILURE;
319
320 switch (message->type)
321 {
322 case HTTP_MSG_RESPONSE:
323 {
324 char *key;
325 ulong idx;
326 zval **val;
327
328 FOREACH_HASH_KEYVAL(&message->hdrs, key, idx, val) {
329 if (key) {
330 char *header;
331 spprintf(&header, 0, "%s: %s", key, Z_STRVAL_PP(val));
332 http_send_header(header);
333 efree(header);
334 key = NULL;
335 }
336 }
337 rs = SUCCESS == http_send_status(message->info.response.code) &&
338 SUCCESS == http_send_data(PHPSTR_VAL(message), PHPSTR_LEN(message)) ?
339 SUCCESS : FAILURE;
340 }
341 break;
342
343 case HTTP_MSG_REQUEST:
344 {
345 #ifdef HTTP_HAVE_CURL
346 char *uri = NULL;
347 zval **zhost, options, headers;
348
349 array_init(&options);
350 array_init(&headers);
351 zend_hash_copy(Z_ARRVAL(headers), &message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
352 add_assoc_zval(&options, "headers", &headers);
353
354 /* check host header */
355 if (SUCCESS == zend_hash_find(&message->hdrs, "Host", sizeof("Host"), (void **) &zhost)) {
356 char *colon = NULL, *host = NULL;
357 size_t host_len = 0;
358 int port = 0;
359
360 /* check for port */
361 if (colon = strchr(Z_STRVAL_PP(zhost), ':')) {
362 port = atoi(colon + 1);
363 host = estrndup(Z_STRVAL_PP(zhost), host_len = (Z_STRVAL_PP(zhost) - colon - 1));
364 } else {
365 host = estrndup(Z_STRVAL_PP(zhost), host_len = Z_STRLEN_PP(zhost));
366 }
367 uri = http_absolute_uri_ex(
368 message->info.request.URI, strlen(message->info.request.URI),
369 NULL, 0, host, host_len, port);
370 efree(host);
371 } else {
372 uri = http_absolute_uri(message->info.request.URI);
373 }
374
375 if (!strcasecmp("POST", message->info.request.method)) {
376 http_request_body body = {HTTP_REQUEST_BODY_CSTRING, PHPSTR_VAL(message), PHPSTR_LEN(message)};
377 rs = http_post(uri, &body, Z_ARRVAL(options), NULL, NULL);
378 } else
379 if (!strcasecmp("GET", message->info.request.method)) {
380 rs = http_get(uri, Z_ARRVAL(options), NULL, NULL);
381 } else
382 if (!strcasecmp("HEAD", message->info.request.method)) {
383 rs = http_head(uri, Z_ARRVAL(options), NULL, NULL);
384 } else {
385 http_error_ex(E_WARNING, HTTP_E_MSG,
386 "Cannot send HttpMessage. Request method %s not supported",
387 message->info.request.method);
388 }
389
390 efree(uri);
391 #else
392 http_error(E_WARNING, HTTP_E_MSG, "HTTP requests not supported - ext/http was not linked against libcurl.");
393 #endif
394 }
395 break;
396
397 case HTTP_MSG_NONE:
398 default:
399 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is neither of type HTTP_MSG_REQUEST nor HTTP_MSG_RESPONSE");
400 break;
401 }
402
403 return rs;
404 }
405
406 PHP_HTTP_API http_message *_http_message_dup(http_message *msg TSRMLS_DC)
407 {
408 /*
409 * TODO: unroll
410 */
411 http_message *new;
412 char *serialized_data;
413 size_t serialized_length;
414
415 http_message_serialize(msg, &serialized_data, &serialized_length);
416 new = http_message_parse(serialized_data, serialized_length);
417 efree(serialized_data);
418 return new;
419 }
420
421 PHP_HTTP_API void _http_message_dtor(http_message *message)
422 {
423 if (message) {
424 zend_hash_destroy(&message->hdrs);
425 phpstr_dtor(PHPSTR(message));
426 if (HTTP_MSG_TYPE(REQUEST, message)) {
427 if (message->info.request.method) {
428 efree(message->info.request.method);
429 message->info.request.method = NULL;
430 }
431 if (message->info.request.URI) {
432 efree(message->info.request.URI);
433 message->info.request.URI = NULL;
434 }
435 }
436 }
437 }
438
439 PHP_HTTP_API void _http_message_free(http_message *message)
440 {
441 if (message) {
442 if (message->parent) {
443 http_message_free(message->parent);
444 message->parent = NULL;
445 }
446 http_message_dtor(message);
447 efree(message);
448 }
449 }
450
451 /*
452 * Local variables:
453 * tab-width: 4
454 * c-basic-offset: 4
455 * End:
456 * vim600: noet sw=4 ts=4 fdm=marker
457 * vim<600: noet sw=4 ts=4
458 */