- do not naivly trust headers about content length
[m6w6/ext-http] / http_encoding_api.c
index ccf52d3fbc6f546c81040fecac01483636d51f2e..0062623fec3526d368d27cb61bdbbae41055369c 100644 (file)
@@ -1,16 +1,13 @@
 /*
-   +----------------------------------------------------------------------+
-   | PECL :: http                                                         |
-   +----------------------------------------------------------------------+
-   | This source file is subject to version 3.0 of the PHP license, that  |
-   | is bundled with this package in the file LICENSE, and is available   |
-   | through the world-wide-web at http://www.php.net/license/3_0.txt.    |
-   | If you did not receive a copy of the PHP license and are unable to   |
-   | obtain it through the world-wide-web, please send a note to          |
-   | license@php.net so we can mail you a copy immediately.               |
-   +----------------------------------------------------------------------+
-   | Copyright (c) 2004-2005 Michael Wallner <mike@php.net>               |
-   +----------------------------------------------------------------------+
+    +--------------------------------------------------------------------+
+    | PECL :: http                                                       |
+    +--------------------------------------------------------------------+
+    | Redistribution and use in source and binary forms, with or without |
+    | modification, are permitted provided that the conditions mentioned |
+    | in the accompanying LICENSE file are met.                          |
+    +--------------------------------------------------------------------+
+    | Copyright (c) 2004-2005, Michael Wallner <mike@php.net>            |
+    +--------------------------------------------------------------------+
 */
 
 /* $Id$ */
 
 ZEND_EXTERN_MODULE_GLOBALS(http);
 
+static inline int eol_match(char **line, int *EOL_len)
+{
+       char *ptr = *line;
+       
+       while (0x20 == *ptr) ++ptr;
+
+       if (ptr == http_locate_eol(*line, EOL_len)) {
+               *line = ptr;
+               return 1;
+       } else {
+               return 0;
+       }
+}
+                       
 /* {{{ char *http_encoding_dechunk(char *, size_t, char **, size_t *) */
 PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
 {
@@ -58,7 +69,7 @@ PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t enco
                 * - chunk size is not followed by (CR)LF|NUL
                 */
                if (    (n_ptr == e_ptr) ||     (chunk_len < 0) || (chunk_len > rest) || 
-                               (*n_ptr && (eol_mismatch = (n_ptr != http_locate_eol(e_ptr, &EOL_len))))) {
+                               (*n_ptr && (eol_mismatch = !eol_match(&n_ptr, &EOL_len)))) {
                        /* don't fail on apperently not encoded data */
                        if (e_ptr == encoded) {
                                memcpy(*decoded, encoded, encoded_len);
@@ -68,13 +79,13 @@ PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t enco
                                efree(*decoded);
                                if (eol_mismatch) {
                                        if (EOL_len == 2) {
-                                               http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid character (expected 0x0D 0x0A; got: 0x%X 0x%X)", *n_ptr, *(n_ptr + 1));
+                                               http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid character (expected 0x0D 0x0A; got: 0x%02X 0x%02X)", *n_ptr, *(n_ptr + 1));
                                        } else {
-                                               http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid character (expected 0x0A; got: 0x%X)", *n_ptr);
+                                               http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid character (expected 0x0A; got: 0x%02X)", *n_ptr);
                                        }
                                } else {
-                                       char *error = estrndup(n_ptr, strcspn(n_ptr, "\r\n "));
-                                       http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid chunk size: '%s' at pos %d", error, n_ptr - encoded);
+                                       char *error = estrndup(e_ptr, strcspn(n_ptr, "\r\n "));
+                                       http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid chunk size: '%s' at pos %ld of %lu", error, (long) (n_ptr - encoded), (unsigned long) encoded_len);
                                        efree(error);
                                }
                                return NULL;