Пример #1
0
 public function __invoke($context)
 {
     // _GET vars
     $context['_GET'] = array();
     if (isset($context['env']['QUERY_STRING'])) {
         parse_str($context['env']['QUERY_STRING'], $context['_GET']);
     }
     // _COOKIE vars
     if (isset($context['env']['HTTP_COOKIE'])) {
         $ck = new Cookies($context['env']['HTTP_COOKIE']);
     } else {
         $ck = new Cookies(null);
     }
     $context['_COOKIE'] = $ck;
     // _POST and _FILES
     if ($context['env']['REQUEST_METHOD'] == 'POST') {
         $context['_POST'] = array();
         $context['_FILES'] = array();
         echo "getting buffer\n";
         $buffer = stream_get_contents($context['stdin'], $context['env']['CONTENT_LENGTH']);
         echo "got buffer\n";
         if (isset($this->options['forward_stream']) and $this->options['forward_stream'] === true) {
             // user asks us to provide a valid stream to app
             $stream_name = StringStreamKeeper::keep($buffer);
             $_old_stdin = $context['stdin'];
             $context['stdin'] = fopen($stream_name, 'r');
         }
         if (isset($context['env']['CONTENT_TYPE']) and strpos($context['env']['CONTENT_TYPE'], 'multipart/form-data') === 0) {
             self::parseMultipart($context['env']['CONTENT_TYPE'], $buffer, $context['_POST'], $context['_FILES']);
         } else {
             echo "parsing…\n";
             parse_str($buffer, $context['_POST']);
         }
         unset($buffer);
         // free memory
     }
     // EXECUTE
     $result = call_user_func($this->app, $context);
     if (!is_array($result)) {
         return $result;
     }
     // Append cookie-headers
     $result[1] = array_merge($result[1], $ck->_getHeaders());
     // Cleanup
     if (isset($_old_stdin)) {
         // remove our "fake" stream
         fclose($context['stdin']);
         StringStreamKeeper::cleanup();
         $context['stdin'] = $_old_stdin;
     }
     if (isset($context['env']['_FILES'])) {
         // remove created files, if there were any
         foreach ($context['env']['_FILES'] as $file) {
             if ($file['error'] == UPLOAD_ERR_OK and file_exists($file['tmp_name'])) {
                 unlink($file['tmp_name']);
             }
         }
     }
     return $result;
 }
Пример #2
0
 public function stream_open($path, $mode, $options, &$opened_path)
 {
     if ($mode != 'r') {
         throw new InvalidArgumentException('StringStream is a read-only stream');
     }
     $this->buffer = StringStreamKeeper::get();
     $this->position = 0;
     return true;
 }