X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_message_api.c;h=8d062b1bac600e3bb6e48a385a0f30372addd103;hp=1c977bf3dbc18798f263692c7f127754556be675;hb=d36da8206d10ff49fd0123ab67f976ca29cb5ba9;hpb=c2b4f0332ead5425b183d2487ab5f25663f1009f diff --git a/http_message_api.c b/http_message_api.c index 1c977bf..8d062b1 100644 --- a/http_message_api.c +++ b/http_message_api.c @@ -18,19 +18,21 @@ #ifdef HAVE_CONFIG_H # include "config.h" #endif - #include "php.h" + #include "php_http.h" #include "php_http_std_defs.h" #include "php_http_api.h" #include "php_http_message_api.h" #include "php_http_headers_api.h" #include "php_http_send_api.h" -#include "php_http_curl_api.h" +#include "php_http_request_api.h" #include "php_http_url_api.h" #include "phpstr/phpstr.h" +ZEND_EXTERN_MODULE_GLOBALS(http); + #define http_message_headers_cb _http_message_headers_cb static void _http_message_headers_cb(const char *http_line, HashTable **headers, void **message TSRMLS_DC) { @@ -94,17 +96,6 @@ static inline void _http_message_init_type(http_message *message, http_message_t } } -#define http_message_header(m, h) _http_message_header_ex((m), (h), sizeof(h)) -#define http_message_header_ex _http_message_header_ex -static inline zval *_http_message_header_ex(http_message *msg, char *key_str, size_t key_len) -{ - zval **header; - if (SUCCESS == zend_hash_find(&msg->hdrs, key_str, key_len, (void **) &header)) { - return *header; - } - return NULL; -} - PHP_HTTP_API http_message *_http_message_init_ex(http_message *message, http_message_type type) { if (!message) { @@ -182,8 +173,21 @@ PHP_HTTP_API http_message *_http_message_parse_ex(http_message *msg, const char char *decoded; size_t decoded_len; + /* decode and replace Transfer-Encoding with Content-Length header */ if (continue_at = http_chunked_decode(body, message + message_length - body, &decoded, &decoded_len)) { phpstr_from_string_ex(PHPSTR(msg), decoded, decoded_len); + efree(decoded); + { + zval *len; + char *tmp; + + spprintf(&tmp, 0, "%lu", (ulong) decoded_len); + MAKE_STD_ZVAL(len); + ZVAL_STRING(len, tmp, 0); + + zend_hash_del(&msg->hdrs, "Transfer-Encoding", sizeof("Transfer-Encoding")); + zend_hash_add(&msg->hdrs, "Content-Length", sizeof("Content-Length"), (void *) &len, sizeof(zval *), NULL); + } } } } else @@ -200,8 +204,10 @@ PHP_HTTP_API http_message *_http_message_parse_ex(http_message *msg, const char } else /* no headers that indicate content length */ - if (1) { + if (HTTP_MSG_TYPE(RESPONSE, msg)) { phpstr_from_string_ex(PHPSTR(msg), body, message + message_length - body); + } else { + continue_at = body; } /* check for following messages */ @@ -273,9 +279,11 @@ PHP_HTTP_API void _http_message_tostring(http_message *msg, char **string, size_ } } - phpstr_appends(&str, HTTP_CRLF); - phpstr_append(&str, PHPSTR_VAL(msg), PHPSTR_LEN(msg)); - phpstr_appends(&str, HTTP_CRLF); + if (PHPSTR_LEN(msg)) { + phpstr_appends(&str, HTTP_CRLF); + phpstr_append(&str, PHPSTR_VAL(msg), PHPSTR_LEN(msg)); + phpstr_appends(&str, HTTP_CRLF); + } data = phpstr_data(&str, string, length); if (!string) { @@ -295,7 +303,7 @@ PHP_HTTP_API void _http_message_serialize(http_message *message, char **string, do { http_message_tostring(message, &buf, &len); - phpstr_append(&str, buf, len); + phpstr_prepend(&str, buf, len); efree(buf); } while (message = message->parent); @@ -321,10 +329,17 @@ PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC) FOREACH_HASH_KEYVAL(&message->hdrs, key, idx, val) { if (key) { - char *header; - spprintf(&header, 0, "%s: %s", key, Z_STRVAL_PP(val)); - http_send_header(header); - efree(header); + if (Z_TYPE_PP(val) == IS_ARRAY) { + zend_bool first = 1; + zval **data; + + FOREACH_VAL(*val, data) { + http_send_header_ex(key, strlen(key), Z_STRVAL_PP(data), Z_STRLEN_PP(data), first); + first = 0; + } + } else { + http_send_header_ex(key, strlen(key), Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1); + } key = NULL; } } @@ -367,7 +382,8 @@ PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC) } if (!strcasecmp("POST", message->info.request.method)) { - rs = http_post_data(uri, PHPSTR_VAL(message), PHPSTR_LEN(message), Z_ARRVAL(options), NULL, NULL); + http_request_body body = {HTTP_REQUEST_BODY_CSTRING, PHPSTR_VAL(message), PHPSTR_LEN(message)}; + rs = http_post(uri, &body, Z_ARRVAL(options), NULL, NULL); } else if (!strcasecmp("GET", message->info.request.method)) { rs = http_get(uri, Z_ARRVAL(options), NULL, NULL); @@ -375,27 +391,42 @@ PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC) if (!strcasecmp("HEAD", message->info.request.method)) { rs = http_head(uri, Z_ARRVAL(options), NULL, NULL); } else { - http_error_ex(E_WARNING, HTTP_E_MSG, + http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Cannot send HttpMessage. Request method %s not supported", message->info.request.method); } efree(uri); #else - http_error(E_WARNING, HTTP_E_MSG, "HTTP requests not supported - ext/http was not linked against libcurl."); + http_error(HE_WARNING, HTTP_E_RUNTIME, "HTTP requests not supported - ext/http was not linked against libcurl."); #endif } break; case HTTP_MSG_NONE: default: - http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is neither of type HTTP_MSG_REQUEST nor HTTP_MSG_RESPONSE"); + http_error(HE_WARNING, HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type HTTP_MSG_REQUEST nor HTTP_MSG_RESPONSE"); break; } return rs; } +PHP_HTTP_API http_message *_http_message_dup(http_message *msg TSRMLS_DC) +{ + /* + * TODO: unroll + */ + http_message *new; + char *serialized_data; + size_t serialized_length; + + http_message_serialize(msg, &serialized_data, &serialized_length); + new = http_message_parse(serialized_data, serialized_length); + efree(serialized_data); + return new; +} + PHP_HTTP_API void _http_message_dtor(http_message *message) { if (message) {