- lotta changes asked by Ilia
[m6w6/ext-http] / http_message_api.c
index 77f18e017c25aea336472b50aa50217fd7e10fde..ac056ad7e4cc437c8f0b1fc674c2ceb736cb902c 100644 (file)
 #ifdef HAVE_CONFIG_H
 #      include "config.h"
 #endif
-#include "php.h"
 
+#define HTTP_WANT_CURL
 #include "php_http.h"
-#include "php_http_std_defs.h"
+
+#include "SAPI.h"
+
 #include "php_http_api.h"
-#include "php_http_message_api.h"
+#include "php_http_encoding_api.h"
 #include "php_http_headers_api.h"
-#include "php_http_send_api.h"
+#include "php_http_message_api.h"
 #include "php_http_request_api.h"
+#include "php_http_send_api.h"
 #include "php_http_url_api.h"
-#include "php_http_encoding_api.h"
-
-#include "phpstr/phpstr.h"
 
 ZEND_EXTERN_MODULE_GLOBALS(http);
 
@@ -189,9 +189,9 @@ PHP_HTTP_API http_message *_http_message_parse_ex(http_message *msg, const char
                        ulong total = 0, start = 0, end = 0, len = 0;
                        
                        if (!strncasecmp(Z_STRVAL_P(c), "bytes", lenof("bytes")) && 
-                                       (Z_STRVAL_P(c)[lenof("bytes")] == '=' || Z_STRVAL_P(c)[lenof("bytes")] == ' ')) {
+                                       (Z_STRVAL_P(c)[lenof("bytes")] == ':' || Z_STRVAL_P(c)[lenof("bytes")] == ' ')) {
                                char *total_at = NULL, *end_at = NULL;
-                               char *start_at = Z_STRVAL_P(c) + lenof("bytes=");
+                               char *start_at = Z_STRVAL_P(c) + sizeof("bytes");
                                
                                start = strtoul(start_at, &end_at, 10);
                                if (end_at) {
@@ -224,7 +224,7 @@ PHP_HTTP_API http_message *_http_message_parse_ex(http_message *msg, const char
                
 #if defined(HTTP_HAVE_ZLIB) || defined(HAVE_ZLIB)
                /* check for compressed data */
-               if ((c = http_message_header(msg, "Content-Encoding"))) {
+               if (http_message_header(msg, "Vary") && (c = http_message_header(msg, "Content-Encoding"))) {
                        char *decoded = NULL;
                        size_t decoded_len = 0;
 #      if defined(HAVE_ZLIB) && !defined(HTTP_HAVE_ZLIB)
@@ -243,6 +243,12 @@ PHP_HTTP_API http_message *_http_message_parse_ex(http_message *msg, const char
                                                decoded_len = Z_STRLEN(retval); \
                                        } \
                                }
+
+#      define REMEMBER_ENCODING() \
+                               if (decoded) {  \
+                                       ZVAL_ADDREF(c); \
+                                       zend_hash_add(&msg->hdrs, "X-Original-Content-Encoding", sizeof("X-Original-Content-Encoding"), (void *) &c, sizeof(zval *), NULL);     \
+                               }
                        
                        if (!strcasecmp(Z_STRVAL_P(c), "gzip") || !strcasecmp(Z_STRVAL_P(c), "x-gzip")) {
 #      ifdef HTTP_HAVE_ZLIB
@@ -251,6 +257,7 @@ PHP_HTTP_API http_message *_http_message_parse_ex(http_message *msg, const char
                                ZVAL_STRINGL(&arg, PHPSTR_VAL(msg) + 10, PHPSTR_LEN(msg) - 18, 0);
                                DECODE_WITH_EXT_ZLIB();
 #      endif /* HTTP_HAVE_ZLIB */
+                               REMEMBER_ENCODING();
                        } else if (!strcasecmp(Z_STRVAL_P(c), "deflate")) {
 #      ifdef HTTP_HAVE_ZLIB
                                http_encoding_inflate(PHPSTR_VAL(msg), PHPSTR_LEN(msg), &decoded, &decoded_len);
@@ -258,10 +265,11 @@ PHP_HTTP_API http_message *_http_message_parse_ex(http_message *msg, const char
                                ZVAL_STRINGL(&arg, PHPSTR_VAL(msg), PHPSTR_LEN(msg), 0);
                                DECODE_WITH_EXT_ZLIB();
 #      endif /* HTTP_HAVE_ZLIB */
+                               REMEMBER_ENCODING();
                        }
                        
                        if (decoded) {
-                               zval *len;
+                               zval *len, **original_len;
                                char *tmp;
                                int tmp_len;
                                
@@ -270,9 +278,14 @@ PHP_HTTP_API http_message *_http_message_parse_ex(http_message *msg, const char
                                ZVAL_STRINGL(len, tmp, tmp_len, 0);
 
                                zend_hash_del(&msg->hdrs, "Content-Encoding", sizeof("Content-Encoding"));
-                               zend_hash_del(&msg->hdrs, "Content-Length", sizeof("Content-Length"));
-                               zend_hash_add(&msg->hdrs, "Content-Length", sizeof("Content-Length"), (void *) &len, sizeof(zval *), NULL);
-                               
+                               if (zend_hash_find(&msg->hdrs, "Content-Length", sizeof("Content-Length"), (void **) &original_len) == SUCCESS) {
+                                       ZVAL_ADDREF(*original_len);                                     
+                                       zend_hash_add(&msg->hdrs, "X-Original-Content-Length", sizeof("X-Original-Content-Length"), (void *) original_len, sizeof(zval *), NULL);
+                                       zend_hash_update(&msg->hdrs, "Content-Length", sizeof("Content-Length"), (void *) &len, sizeof(zval *), NULL);
+                               } else {
+                                       zend_hash_add(&msg->hdrs, "Content-Length", sizeof("Content-Length"), (void *) &len, sizeof(zval *), NULL);
+                               }
+
                                phpstr_dtor(PHPSTR(msg));
                                PHPSTR(msg)->data = decoded;
                                PHPSTR(msg)->used = decoded_len;