3 +----------------------------------------------------------------------+
7 +----------------------------------------------------------------------+
9 | This source file is subject to version 3.0 of the PHP license, that |
11 | is bundled with this package in the file LICENSE, and is available |
13 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
15 | If you did not receive a copy of the PHP license and are unable to |
17 | obtain it through the world-wide-web, please send a note to |
19 | license@php.net so we can mail you a copy immediately. |
21 +----------------------------------------------------------------------+
23 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
25 +----------------------------------------------------------------------+
47 #include "php_http_std_defs.h"
49 #include "php_http_api.h"
51 #include "php_http_message_api.h"
53 #include "php_http_headers_api.h"
55 #include "php_http_send_api.h"
57 #include "php_http_request_api.h"
59 #include "php_http_url_api.h"
63 #include "phpstr/phpstr.h"
67 #define http_message_headers_cb _http_message_headers_cb
69 static void _http_message_headers_cb(const char *http_line
, HashTable
**headers
, void **message TSRMLS_DC
)
77 http_message
*new, *old
= (http_message
*) *message
;
81 if (crlf
= strstr(http_line
, HTTP_CRLF
)) {
83 line_length
= crlf
- http_line
;
87 line_length
= strlen(http_line
);
93 if (old
->type
|| zend_hash_num_elements(&old
->hdrs
) || PHPSTR_LEN(old
)) {
95 new = http_message_new();
103 *headers
= &new->hdrs
;
113 while (isspace(http_line
[line_length
-1])) --line_length
;
119 if (!strncmp(http_line
, "HTTP/1.", lenof("HTTP/1."))) {
121 new->type
= HTTP_MSG_RESPONSE
;
123 new->info
.response
.http_version
= atof(http_line
+ lenof("HTTP/"));
125 new->info
.response
.code
= atoi(http_line
+ lenof("HTTP/1.1 "));
131 if (!strncmp(http_line
+ line_length
- lenof("HTTP/1.1"), "HTTP/1.", lenof("HTTP/1."))) {
133 const char *method_sep_uri
= strchr(http_line
, ' ');
135 new->type
= HTTP_MSG_REQUEST
;
137 new->info
.request
.http_version
= atof(http_line
+ line_length
- lenof("1.1"));
139 new->info
.request
.method
= estrndup(http_line
, method_sep_uri
- http_line
);
141 new->info
.request
.URI
= estrndup(method_sep_uri
+ 1, http_line
+ line_length
- method_sep_uri
- 1 - lenof(" HTTP/1.1"));
149 #define http_message_init_type _http_message_init_type
151 static inline void _http_message_init_type(http_message
*message
, http_message_type type
)
155 switch (message
->type
= type
)
159 case HTTP_MSG_RESPONSE
:
161 message
->info
.response
.http_version
= .0;
163 message
->info
.response
.code
= 0;
169 case HTTP_MSG_REQUEST
:
171 message
->info
.request
.http_version
= .0;
173 message
->info
.request
.method
= NULL
;
175 message
->info
.request
.URI
= NULL
;
193 #define http_message_header(m, h) _http_message_header_ex((m), (h), sizeof(h))
195 #define http_message_header_ex _http_message_header_ex
197 static inline zval
*_http_message_header_ex(http_message
*msg
, char *key_str
, size_t key_len
)
203 if (SUCCESS
== zend_hash_find(&msg
->hdrs
, key_str
, key_len
, (void **) &header
)) {
215 PHP_HTTP_API http_message
*_http_message_init_ex(http_message
*message
, http_message_type type
)
221 message
= ecalloc(1, sizeof(http_message
));
227 http_message_init_type(message
, type
);
229 message
->parent
= NULL
;
231 phpstr_init(&message
->body
);
233 zend_hash_init(&message
->hdrs
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
245 PHP_HTTP_API
void _http_message_set_type(http_message
*message
, http_message_type type
)
249 /* just act if different */
251 if (type
!= message
->type
) {
255 /* free request info */
257 if (message
->type
== HTTP_MSG_REQUEST
) {
259 if (message
->info
.request
.method
) {
261 efree(message
->info
.request
.method
);
265 if (message
->info
.request
.URI
) {
267 efree(message
->info
.request
.URI
);
277 http_message_init_type(message
, type
);
285 PHP_HTTP_API http_message
*_http_message_parse_ex(http_message
*msg
, const char *message
, size_t message_length TSRMLS_DC
)
291 zend_bool free_msg
= msg
? 0 : 1;
295 if (message_length
< HTTP_MSG_MIN_SIZE
) {
311 msg
= http_message_init(msg
);
315 if (SUCCESS
!= http_parse_headers_cb(message
, &msg
->hdrs
, 1, http_message_headers_cb
, (void **) &msg
)) {
319 http_message_free(msg
);
329 /* header parsing stops at CRLF CRLF */
331 if (body
= strstr(message
, HTTP_CRLF HTTP_CRLF
)) {
335 const char *continue_at
= NULL
;
339 body
+= lenof(HTTP_CRLF HTTP_CRLF
);
343 /* message has content-length header */
345 if (c
= http_message_header(msg
, "Content-Length")) {
347 long len
= atol(Z_STRVAL_P(c
));
349 phpstr_from_string_ex(PHPSTR(msg
), body
, len
);
351 continue_at
= body
+ len
;
357 /* message has chunked transfer encoding */
359 if (c
= http_message_header(msg
, "Transfer-Encoding")) {
361 if (!strcasecmp("chunked", Z_STRVAL_P(c
))) {
369 /* decode and replace Transfer-Encoding with Content-Length header */
371 if (continue_at
= http_chunked_decode(body
, message
+ message_length
- body
, &decoded
, &decoded_len
)) {
373 phpstr_from_string_ex(PHPSTR(msg
), decoded
, decoded_len
);
385 spprintf(&tmp
, 0, "%lu", decoded_len
);
389 ZVAL_STRING(len
, tmp
, 0);
393 zend_hash_del(&msg
->hdrs
, "Transfer-Encoding", sizeof("Transfer-Encoding"));
395 zend_hash_add(&msg
->hdrs
, "Content-Length", sizeof("Content-Length"), (void *) &len
, sizeof(zval
*), NULL
);
407 /* message has content-range header */
409 if (c
= http_message_header(msg
, "Content-Range")) {
411 ulong start
= 0, end
= 0;
415 sscanf(Z_STRVAL_P(c
), "bytes=%lu-%lu", &start
, &end
);
419 phpstr_from_string_ex(PHPSTR(msg
), body
, (size_t) (end
- start
));
421 continue_at
= body
+ (end
- start
);
429 /* no headers that indicate content length */
433 phpstr_from_string_ex(PHPSTR(msg
), body
, message
+ message_length
- body
);
439 /* check for following messages */
443 while (isspace(*continue_at
)) ++continue_at
;
445 if (continue_at
< (message
+ message_length
)) {
447 http_message
*next
= NULL
, *most
= NULL
;
451 /* set current message to parent of most parent following messages and return deepest */
453 if (most
= next
= http_message_parse(continue_at
, message
+ message_length
- continue_at
)) {
455 while (most
->parent
) most
= most
->parent
;
477 PHP_HTTP_API
void _http_message_tostring(http_message
*msg
, char **string
, size_t *length
)
491 phpstr_init_ex(&str
, 4096, 0);
499 case HTTP_MSG_REQUEST
:
501 phpstr_appendf(&str
, "%s %s HTTP/%1.1f" HTTP_CRLF
,
503 msg
->info
.request
.method
,
505 msg
->info
.request
.URI
,
507 msg
->info
.request
.http_version
);
513 case HTTP_MSG_RESPONSE
:
515 phpstr_appendf(&str
, "HTTP/%1.1f %d" HTTP_CRLF
,
517 msg
->info
.response
.http_version
,
519 msg
->info
.response
.code
);
535 FOREACH_HASH_KEYVAL(&msg
->hdrs
, key
, idx
, header
) {
539 zval
**single_header
;
543 switch (Z_TYPE_PP(header
))
549 phpstr_appendf(&str
, "%s: %s" HTTP_CRLF
, key
, Z_STRVAL_PP(header
));
557 FOREACH_VAL(*header
, single_header
) {
559 phpstr_appendf(&str
, "%s: %s" HTTP_CRLF
, key
, Z_STRVAL_PP(single_header
));
577 if (PHPSTR_LEN(msg
)) {
579 phpstr_appends(&str
, HTTP_CRLF
);
581 phpstr_append(&str
, PHPSTR_VAL(msg
), PHPSTR_LEN(msg
));
583 phpstr_appends(&str
, HTTP_CRLF
);
589 data
= phpstr_data(&str
, string
, length
);
605 PHP_HTTP_API
void _http_message_serialize(http_message
*message
, char **string
, size_t *length
)
623 http_message_tostring(message
, &buf
, &len
);
625 phpstr_prepend(&str
, buf
, len
);
629 } while (message
= message
->parent
);
633 buf
= phpstr_data(&str
, string
, length
);
649 PHP_HTTP_API STATUS
_http_message_send(http_message
*message TSRMLS_DC
)
657 switch (message
->type
)
661 case HTTP_MSG_RESPONSE
:
673 FOREACH_HASH_KEYVAL(&message
->hdrs
, key
, idx
, val
) {
679 spprintf(&header
, 0, "%s: %s", key
, Z_STRVAL_PP(val
));
681 http_send_header(header
);
691 rs
= SUCCESS
== http_send_status(message
->info
.response
.code
) &&
693 SUCCESS
== http_send_data(PHPSTR_VAL(message
), PHPSTR_LEN(message
)) ?
703 case HTTP_MSG_REQUEST
:
707 #ifdef HTTP_HAVE_CURL
711 zval
**zhost
, options
, headers
;
715 array_init(&options
);
717 array_init(&headers
);
719 zend_hash_copy(Z_ARRVAL(headers
), &message
->hdrs
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
721 add_assoc_zval(&options
, "headers", &headers
);
725 /* check host header */
727 if (SUCCESS
== zend_hash_find(&message
->hdrs
, "Host", sizeof("Host"), (void **) &zhost
)) {
729 char *colon
= NULL
, *host
= NULL
;
739 if (colon
= strchr(Z_STRVAL_PP(zhost
), ':')) {
741 port
= atoi(colon
+ 1);
743 host
= estrndup(Z_STRVAL_PP(zhost
), host_len
= (Z_STRVAL_PP(zhost
) - colon
- 1));
747 host
= estrndup(Z_STRVAL_PP(zhost
), host_len
= Z_STRLEN_PP(zhost
));
751 uri
= http_absolute_uri_ex(
753 message
->info
.request
.URI
, strlen(message
->info
.request
.URI
),
755 NULL
, 0, host
, host_len
, port
);
761 uri
= http_absolute_uri(message
->info
.request
.URI
);
767 if (!strcasecmp("POST", message
->info
.request
.method
)) {
769 http_request_body body
= {HTTP_REQUEST_BODY_CSTRING
, PHPSTR_VAL(message
), PHPSTR_LEN(message
)};
771 rs
= http_post(uri
, &body
, Z_ARRVAL(options
), NULL
, NULL
);
775 if (!strcasecmp("GET", message
->info
.request
.method
)) {
777 rs
= http_get(uri
, Z_ARRVAL(options
), NULL
, NULL
);
781 if (!strcasecmp("HEAD", message
->info
.request
.method
)) {
783 rs
= http_head(uri
, Z_ARRVAL(options
), NULL
, NULL
);
787 http_error_ex(E_WARNING
, HTTP_E_MSG
,
789 "Cannot send HttpMessage. Request method %s not supported",
791 message
->info
.request
.method
);
801 http_error(E_WARNING
, HTTP_E_MSG
, "HTTP requests not supported - ext/http was not linked against libcurl.");
815 http_error(E_WARNING
, HTTP_E_MSG
, "HttpMessage is neither of type HTTP_MSG_REQUEST nor HTTP_MSG_RESPONSE");
829 PHP_HTTP_API
void _http_message_dtor(http_message
*message
)
835 zend_hash_destroy(&message
->hdrs
);
837 phpstr_dtor(PHPSTR(message
));
839 if (HTTP_MSG_TYPE(REQUEST
, message
)) {
841 if (message
->info
.request
.method
) {
843 efree(message
->info
.request
.method
);
845 message
->info
.request
.method
= NULL
;
849 if (message
->info
.request
.URI
) {
851 efree(message
->info
.request
.URI
);
853 message
->info
.request
.URI
= NULL
;
865 PHP_HTTP_API
void _http_message_free(http_message
*message
)
871 if (message
->parent
) {
873 http_message_free(message
->parent
);
875 message
->parent
= NULL
;
879 http_message_dtor(message
);
899 * vim600: noet sw=4 ts=4 fdm=marker
901 * vim<600: noet sw=4 ts=4