- add second host header
[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 if (continue_at = http_chunked_decode(body, message + message_length - body, &decoded, &decoded_len)) {
186 phpstr_from_string_ex(PHPSTR(msg), decoded, decoded_len);
187 }
188 }
189 } else
190
191 /* message has content-range header */
192 if (c = http_message_header(msg, "Content-Range")) {
193 ulong start = 0, end = 0;
194
195 sscanf(Z_STRVAL_P(c), "bytes=%lu-%lu", &start, &end);
196 if (end > start) {
197 phpstr_from_string_ex(PHPSTR(msg), body, (size_t) (end - start));
198 continue_at = body + (end - start);
199 }
200 } else
201
202 /* no headers that indicate content length */
203 if (1) {
204 phpstr_from_string_ex(PHPSTR(msg), body, message + message_length - body);
205 }
206
207 /* check for following messages */
208 if (continue_at) {
209 while (isspace(*continue_at)) ++continue_at;
210 if (continue_at < (message + message_length)) {
211 http_message *next = NULL, *most = NULL;
212
213 /* set current message to parent of most parent following messages and return deepest */
214 if (most = next = http_message_parse(continue_at, message + message_length - continue_at)) {
215 while (most->parent) most = most->parent;
216 most->parent = msg;
217 msg = next;
218 }
219 }
220 }
221 }
222
223 return msg;
224 }
225
226 PHP_HTTP_API void _http_message_tostring(http_message *msg, char **string, size_t *length)
227 {
228 phpstr str;
229 char *key, *data;
230 ulong idx;
231 zval **header;
232
233 phpstr_init_ex(&str, 4096, 0);
234
235 switch (msg->type)
236 {
237 case HTTP_MSG_REQUEST:
238 phpstr_appendf(&str, "%s %s HTTP/%1.1f" HTTP_CRLF,
239 msg->info.request.method,
240 msg->info.request.URI,
241 msg->info.request.http_version);
242 break;
243
244 case HTTP_MSG_RESPONSE:
245 phpstr_appendf(&str, "HTTP/%1.1f %d" HTTP_CRLF,
246 msg->info.response.http_version,
247 msg->info.response.code);
248 break;
249
250 case HTTP_MSG_NONE:
251 default:
252 break;
253 }
254
255 FOREACH_HASH_KEYVAL(&msg->hdrs, key, idx, header) {
256 if (key) {
257 zval **single_header;
258
259 switch (Z_TYPE_PP(header))
260 {
261 case IS_STRING:
262 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key, Z_STRVAL_PP(header));
263 break;
264
265 case IS_ARRAY:
266 FOREACH_VAL(*header, single_header) {
267 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key, Z_STRVAL_PP(single_header));
268 }
269 break;
270 }
271
272 key = NULL;
273 }
274 }
275
276 phpstr_appends(&str, HTTP_CRLF);
277 phpstr_append(&str, PHPSTR_VAL(msg), PHPSTR_LEN(msg));
278 phpstr_appends(&str, HTTP_CRLF);
279
280 data = phpstr_data(&str, string, length);
281 if (!string) {
282 efree(data);
283 }
284
285 phpstr_dtor(&str);
286 }
287
288 PHP_HTTP_API void _http_message_serialize(http_message *message, char **string, size_t *length)
289 {
290 char *buf;
291 size_t len;
292 phpstr str;
293
294 phpstr_init(&str);
295
296 do {
297 http_message_tostring(message, &buf, &len);
298 phpstr_append(&str, buf, len);
299 efree(buf);
300 } while (message = message->parent);
301
302 buf = phpstr_data(&str, string, length);
303 if (!string) {
304 efree(buf);
305 }
306
307 phpstr_dtor(&str);
308 }
309
310 PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC)
311 {
312 STATUS rs = FAILURE;
313
314 switch (message->type)
315 {
316 case HTTP_MSG_RESPONSE:
317 {
318 char *key;
319 ulong idx;
320 zval **val;
321
322 FOREACH_HASH_KEYVAL(&message->hdrs, key, idx, val) {
323 if (key) {
324 char *header;
325 spprintf(&header, 0, "%s: %s", key, Z_STRVAL_PP(val));
326 http_send_header(header);
327 efree(header);
328 key = NULL;
329 }
330 }
331 rs = SUCCESS == http_send_status(message->info.response.code) &&
332 SUCCESS == http_send_data(PHPSTR_VAL(message), PHPSTR_LEN(message)) ?
333 SUCCESS : FAILURE;
334 }
335 break;
336
337 case HTTP_MSG_REQUEST:
338 {
339 #ifdef HTTP_HAVE_CURL
340 char *uri = NULL;
341 zval **zhost, options, headers;
342
343 array_init(&options);
344 array_init(&headers);
345 zend_hash_copy(Z_ARRVAL(headers), &message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
346 add_assoc_zval(&options, "headers", &headers);
347
348 /* check host header */
349 if (SUCCESS == zend_hash_find(&message->hdrs, "Host", sizeof("Host"), (void **) &zhost)) {
350 char *colon = NULL, *host = NULL;
351 size_t host_len = 0;
352 int port = 0;
353
354 /* check for port */
355 if (colon = strchr(Z_STRVAL_PP(zhost), ':')) {
356 port = atoi(colon + 1);
357 host = estrndup(Z_STRVAL_PP(zhost), host_len = (Z_STRVAL_PP(zhost) - colon - 1));
358 } else {
359 host = estrndup(Z_STRVAL_PP(zhost), host_len = Z_STRLEN_PP(zhost));
360 }
361 uri = http_absolute_uri_ex(
362 message->info.request.URI, strlen(message->info.request.URI),
363 NULL, 0, host, host_len, port);
364 efree(host);
365 } else {
366 uri = http_absolute_uri(message->info.request.URI);
367 }
368
369 if (!strcasecmp("POST", message->info.request.method)) {
370 rs = http_post_data(uri, PHPSTR_VAL(message), PHPSTR_LEN(message), Z_ARRVAL(options), NULL, NULL);
371 } else
372 if (!strcasecmp("GET", message->info.request.method)) {
373 rs = http_get(uri, Z_ARRVAL(options), NULL, NULL);
374 } else
375 if (!strcasecmp("HEAD", message->info.request.method)) {
376 rs = http_head(uri, Z_ARRVAL(options), NULL, NULL);
377 } else {
378 http_error_ex(E_WARNING, HTTP_E_MSG,
379 "Cannot send HttpMessage. Request method %s not supported",
380 message->info.request.method);
381 }
382
383 efree(uri);
384 #else
385 http_error(E_WARNING, HTTP_E_MSG, "HTTP requests not supported - ext/http was not linked against libcurl.");
386 #endif
387 }
388 break;
389
390 case HTTP_MSG_NONE:
391 default:
392 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is neither of type HTTP_MSG_REQUEST nor HTTP_MSG_RESPONSE");
393 break;
394 }
395
396 return rs;
397 }
398
399 PHP_HTTP_API void _http_message_dtor(http_message *message)
400 {
401 if (message) {
402 zend_hash_destroy(&message->hdrs);
403 phpstr_dtor(PHPSTR(message));
404 if (HTTP_MSG_TYPE(REQUEST, message)) {
405 if (message->info.request.method) {
406 efree(message->info.request.method);
407 message->info.request.method = NULL;
408 }
409 if (message->info.request.URI) {
410 efree(message->info.request.URI);
411 message->info.request.URI = NULL;
412 }
413 }
414 }
415 }
416
417 PHP_HTTP_API void _http_message_free(http_message *message)
418 {
419 if (message) {
420 if (message->parent) {
421 http_message_free(message->parent);
422 message->parent = NULL;
423 }
424 http_message_dtor(message);
425 efree(message);
426 }
427 }
428
429 /*
430 * Local variables:
431 * tab-width: 4
432 * c-basic-offset: 4
433 * End:
434 * vim600: noet sw=4 ts=4 fdm=marker
435 * vim<600: noet sw=4 ts=4
436 */
437