- fix http_get_request_body()
authorMichael Wallner <mike@php.net>
Wed, 11 Jan 2006 16:35:22 +0000 (16:35 +0000)
committerMichael Wallner <mike@php.net>
Wed, 11 Jan 2006 16:35:22 +0000 (16:35 +0000)
- add http_get_request_body_stream()

# TODO: locate everything that accepts a stream context and fix it up

http.c
http_api.c
http_functions.c
php_http.h
php_http_api.h
tests/get_request_data_001.phpt

diff --git a/http.c b/http.c
index 05244d18287a8080193a4e7d598bcdc87ae13cd3..26ab4d3bb0981924860f060dcde9521f19f9451c 100644 (file)
--- a/http.c
+++ b/http.c
@@ -92,6 +92,7 @@ zend_function_entry http_functions[] = {
        PHP_FE(http_parse_cookie, NULL)
        PHP_FE(http_get_request_headers, NULL)
        PHP_FE(http_get_request_body, NULL)
+       PHP_FE(http_get_request_body_stream, NULL)
        PHP_FE(http_match_request_header, NULL)
 #ifdef HTTP_HAVE_CURL
        PHP_FE(http_get, http_arg_pass_ref_3)
index 7825ebdd353ebd57aaf3098577d1bff821106e66..3f4b7a66072b9af63905daee897931fffcc86b49 100644 (file)
@@ -312,18 +312,21 @@ PHP_HTTP_API STATUS _http_get_request_body_ex(char **body, size_t *length, zend_
 {
        *length = 0;
        *body = NULL;
-
+       
        if (SG(request_info).raw_post_data) {
                *length = SG(request_info).raw_post_data_length;
-               *body = (char *) (dup ? estrndup(SG(request_info).raw_post_data, *length) : SG(request_info).raw_post_data);
+               *body = SG(request_info).raw_post_data;
+               
+               if (dup) {
+                       *body = estrndup(*body, *length);
+               }
                return SUCCESS;
-       }
-       
-       /* PHP only reads POST */
-       if (sapi_module.read_post) {
+       } else if (sapi_module.read_post && !HTTP_G(read_post_data)) {
                char buf[4096];
                int len;
                
+               HTTP_G(read_post_data) = 1;
+               
                while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
                        *body = erealloc(*body, *length + len + 1);
                        memcpy(*body + *length, buf, len);
@@ -332,23 +335,55 @@ PHP_HTTP_API STATUS _http_get_request_body_ex(char **body, size_t *length, zend_
                }
                
                /* check for error */
-               if (len >= 0) {
-                       /* connect to sapi module so it'll be freed */
-                       if (!dup) {
-                               SG(request_info).raw_post_data = *body;
-                               SG(request_info).raw_post_data_length = *length;
-                       }
-                       return SUCCESS;
-               } else {
+               if (len < 0) {
                        STR_FREE(*body);
                        *length = 0;
+                       return FAILURE;
+               }
+               
+               SG(request_info).raw_post_data = *body;
+               SG(request_info).raw_post_data_length = *length;
+               
+               if (dup) {
+                       *body = estrndup(*body, *length);
                }
+               return SUCCESS;
        }
        
        return FAILURE;
 }
 /* }}} */
 
+/* {{{ php_stream *_http_get_request_body_stream(void) */
+PHP_HTTP_API php_stream *_http_get_request_body_stream(TSRMLS_D)
+{
+       php_stream *s = NULL;
+       
+       if (SG(request_info).raw_post_data) {
+               s = php_stream_open_wrapper("php://input", "rb", 0, NULL);
+       } else if (sapi_module.read_post && !HTTP_G(read_post_data)) {
+               HTTP_G(read_post_data) = 1;
+               
+               if ((s = php_stream_temp_new())) {
+                       char buf[4096];
+                       int len;
+                       
+                       while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
+                               php_stream_write(s, buf, len);
+                       }
+                       
+                       if (len < 0) {
+                               php_stream_close(s);
+                               s = NULL;
+                       } else {
+                               php_stream_rewind(s);
+                       }
+               }
+       }
+       
+       return s;
+}
+/* }}} */
 
 /*
  * Local variables:
index 69f03e17a4d75ff0b7582800ec18fc015e3fc8c5..4ece6965114f435e7312445828109f59fef155e0 100644 (file)
@@ -1020,7 +1020,10 @@ PHP_FUNCTION(http_get_request_headers)
  *
  * Get the raw request body (e.g. POST or PUT data).
  * 
- * Returns NULL when using the CLI SAPI.
+ * This function can not be used after http_get_request_body_stream() 
+ * if the request method was another than POST.
+ * 
+ * Returns the raw request body as string on success or NULL on failure.
  */
 PHP_FUNCTION(http_get_request_body)
 {
@@ -1037,6 +1040,29 @@ PHP_FUNCTION(http_get_request_body)
 }
 /* }}} */
 
+/* {{{ proto resource http_get_request_body_stream(void)
+ *
+ * Create a stream to read the raw request body (e.g. POST or PUT data).
+ * 
+ * This function can only be used once if the request method was another than POST.
+ * 
+ * Returns the raw request body as stream on success or NULL on failure.
+ */
+PHP_FUNCTION(http_get_request_body_stream)
+{
+       php_stream *s;
+       
+       NO_ARGS;
+       
+       if ((s = http_get_request_body_stream())) {
+               php_stream_to_zval(s, return_value);
+       } else {
+               http_error(HE_WARNING, HTTP_E_RUNTIME, "Failed to create request body stream");
+               RETURN_NULL();
+       }
+}
+/* }}} */
+
 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
  *
  * Match an incoming HTTP header.
index a3cb94f4083516e58fbe286d002b434ca39479a8..0fbbb55172a76353b650536c6c04c0dc0dfde6ea 100644 (file)
@@ -107,6 +107,7 @@ ZEND_BEGIN_MODULE_GLOBALS(http)
 #endif
 
        zend_bool force_exit;
+       zend_bool read_post_data;
 
 ZEND_END_MODULE_GLOBALS(http)
 
@@ -147,6 +148,7 @@ PHP_FUNCTION(http_parse_headers);
 PHP_FUNCTION(http_parse_cookie);
 PHP_FUNCTION(http_get_request_headers);
 PHP_FUNCTION(http_get_request_body);
+PHP_FUNCTION(http_get_request_body_stream);
 PHP_FUNCTION(http_match_request_header);
 #ifdef HTTP_HAVE_CURL
 PHP_FUNCTION(http_get);
index 3d708d7ba396c82e4385ecbf49ef214e9d0fcf76..2c54e2b3acfd939036e189843e52225f99c82a6a 100644 (file)
@@ -124,6 +124,8 @@ PHP_HTTP_API zval *_http_get_server_var_ex(const char *key, size_t key_size, zen
 #define http_get_request_body_ex(b, l, d) _http_get_request_body_ex((b), (l), (d) TSRMLS_CC)
 PHP_HTTP_API STATUS _http_get_request_body_ex(char **body, size_t *length, zend_bool dup TSRMLS_DC);
 
+#define http_get_request_body_stream() _http_get_request_body_stream(TSRMLS_C)
+PHP_HTTP_API php_stream *_http_get_request_body_stream(TSRMLS_D);
 
 #define http_locate_body _http_locate_body
 static inline const char *_http_locate_body(const char *message)
index 4d15265a3f90755cc17626a7cb03351cfed06252..3dabaa14b3987ba02615f8b42b3a33f6145c404a 100644 (file)
@@ -4,25 +4,26 @@ get request data
 <?php
 include 'skip.inc';
 ?>
---ENV--
-HTTP_ACCEPT_CHARSET=iso-8859-1, *
-HTTP_ACCEPT_ENCODING=none
-HTTP_USER_AGENT=Mozilla/5.0
-HTTP_HOST=localhost
 --POST--
 a=b&c=d
 --FILE--
 <?php
 echo "-TEST\n";
+
+$_SERVER['HTTP_ACCEPT_CHARSET'] = 'iso-8859-1, *';
+$_SERVER['HTTP_ACCEPT_ENCODING'] = 'none';
+$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0';
+$_SERVER['HTTP_HOST'] = 'localhost';
+
 $h = http_get_request_headers();
 ksort($h);
 print_r($h);
-$b = http_get_request_body();
-if (php_sapi_name() == 'cli' || $b == 'a=b&c=d') {
-       echo "OK\n";
-}
+var_dump(http_get_request_body());
+var_dump(http_get_request_body());
+var_dump(http_get_request_body());
+var_dump(fread(http_get_request_body_stream(), 4096));
+echo "Done\n";
 ?>
-===DONE===
 --EXPECTF--
 %sTEST
 Array
@@ -32,5 +33,8 @@ Array
     [Host] => localhost
     [User-Agent] => Mozilla/5.0
 )
-OK
-===DONE===
+string(7) "a=b&c=d"
+string(7) "a=b&c=d"
+string(7) "a=b&c=d"
+string(7) "a=b&c=d"
+Done