Ejemplo n.º 1
0
 public function run()
 {
     //set_error_handler(array('\Slim\Slim', 'handleErrors'));
     //Fetch status, header, and body
     list($status, $headers, $body) = $this->response->finalize();
     // Serialize cookies (with optional encryption)
     \Slim\Http\Util::serializeCookies($headers, $this->response->cookies, $this->settings);
     //Send headers
     if (headers_sent() === false) {
         //Send status
         if (strpos(PHP_SAPI, 'cgi') === 0) {
             header(sprintf('Status: %s', \Slim\Http\Response::getMessageForCode($status)));
         } else {
             header(sprintf('HTTP/%s %s', Config::get('http.version'), \Slim\Http\Response::getMessageForCode($status)));
         }
         //Send headers
         foreach ($headers as $name => $value) {
             $hValues = explode("\n", $value);
             foreach ($hValues as $hVal) {
                 header("{$name}: {$hVal}", false);
             }
         }
     }
     //Send body, but only if it isn't a HEAD request
     if (!$this->request->isHead()) {
         echo $body;
     }
 }
Ejemplo n.º 2
0
 /**
  * Return a string representation of the complete http response.
  *
  * @return string
  */
 public function __toString()
 {
     //Fetch status, header, and body
     list($status, $headers, $body) = $this->finalize();
     Util::serializeCookies($headers, $this->cookies, ['cookies.encrypt' => false]);
     $version = '1.1';
     $text = self::getMessageForCode($status);
     return sprintf("HTTP/%s %s\n", $version, $text) . $this->stringifyHeaders($headers) . "\n" . $body;
 }
Ejemplo n.º 3
0
 /**
  * Redefine the run method
  *
  * We no need to use a Slim handler
  */
 public function run()
 {
     //set_error_handler(array('\Slim\Slim', 'handleErrors')); //Espo: no needs to use this handler
     //Apply final outer middleware layers
     if ($this->config('debug')) {
         //Apply pretty exceptions only in debug to avoid accidental information leakage in production
         //$this->add(new \Slim\Middleware\PrettyExceptions()); //Espo: no needs to use this handler
     }
     //Invoke middleware and application stack
     $this->middleware[0]->call();
     //Fetch status, header, and body
     list($status, $headers, $body) = $this->response->finalize();
     // Serialize cookies (with optional encryption)
     \Slim\Http\Util::serializeCookies($headers, $this->response->cookies, $this->settings);
     //Send headers
     if (headers_sent() === false) {
         //Send status
         if (strpos(PHP_SAPI, 'cgi') === 0) {
             header(sprintf('Status: %s', \Slim\Http\Response::getMessageForCode($status)));
         } else {
             header(sprintf('HTTP/%s %s', $this->config('http.version'), \Slim\Http\Response::getMessageForCode($status)));
         }
         //Send headers
         foreach ($headers as $name => $value) {
             $hValues = explode("\n", $value);
             foreach ($hValues as $hVal) {
                 header("{$name}: {$hVal}", false);
             }
         }
     }
     //Send body, but only if it isn't a HEAD request
     if (!$this->request->isHead()) {
         echo $body;
     }
     //restore_error_handler(); //Espo: no needs to use this handler
 }
Ejemplo n.º 4
0
 /**
  * Test serializeCookies and decrypt with string expires
  *
  * In this test a cookie with a string typed value for 'expires' is set,
  * which should be parsed by `strtotime` to a timestamp when it's added to
  * the headers; this timestamp should then be correctly parsed, and the
  * value correctly decrypted, by `decodeSecureCookie`.
  */
 public function testSerializeCookiesAndDecryptWithStringExpires()
 {
     $value = 'bar';
     $headers = new \Slim\Http\Headers();
     $settings = array('cookies.encrypt' => true, 'cookies.secret_key' => 'secret', 'cookies.cipher' => MCRYPT_RIJNDAEL_256, 'cookies.cipher_mode' => MCRYPT_MODE_CBC);
     $cookies = new \Slim\Http\Cookies();
     $cookies->set('foo', array('value' => $value, 'expires' => '1 hour'));
     \Slim\Http\Util::serializeCookies($headers, $cookies, $settings);
     $encrypted = $headers->get('Set-Cookie');
     $encrypted = strstr($encrypted, ';', true);
     $encrypted = urldecode(substr(strstr($encrypted, '='), 1));
     $decrypted = \Slim\Http\Util::decodeSecureCookie($encrypted, $settings['cookies.secret_key'], $settings['cookies.cipher'], $settings['cookies.cipher_mode']);
     $this->assertEquals($value, $decrypted);
     $this->assertTrue($value !== $encrypted);
 }
Ejemplo n.º 5
0
 /**
  * Run
  *
  * This method invokes the middleware stack, including the core Slim application;
  * the result is an array of HTTP status, header, and body. These three items
  * are returned to the HTTP client.
  */
 public function run()
 {
     /**
      * set time zone for log and message queue message body
      * @author Mustafa Zeynel Dağlı
      */
     date_default_timezone_set($this->container['settings']['time.zone']);
     /**
      * if rest service entry logging conf. true, publish to message queue
      * @author Mustafa Zeynel Dağlı
      */
     if ($this->container['settings']['restEntry.rabbitMQ'] == true) {
         $this->publishMessage();
     }
     set_error_handler(array('\\Slim\\Slim', 'handleErrors'));
     //Apply final outer middleware layers
     if ($this->config('debug')) {
         //Apply pretty exceptions only in debug to avoid accidental information leakage in production
         $this->add(new \Slim\Middleware\PrettyExceptions());
     }
     /**
      * zeynel dağlı
      */
     if ($this->container['settings']['log.level'] <= \Slim\Log::ERROR) {
         //print_r('--slim run kontrolor--');
         $this->add(new \Slim\Middleware\PrettyExceptions());
     }
     //Invoke middleware and application stack
     $this->middleware[0]->call();
     //print_r('--slim run kontrolor2--');
     //Fetch status, header, and body
     list($status, $headers, $body) = $this->response->finalize();
     // Serialize cookies (with optional encryption)
     \Slim\Http\Util::serializeCookies($headers, $this->response->cookies, $this->settings);
     //Send headers
     if (headers_sent() === false) {
         //Send status
         if (strpos(PHP_SAPI, 'cgi') === 0) {
             header(sprintf('Status: %s', \Slim\Http\Response::getMessageForCode($status)));
         } else {
             header(sprintf('HTTP/%s %s', $this->config('http.version'), \Slim\Http\Response::getMessageForCode($status)));
         }
         //Send headers
         foreach ($headers as $name => $value) {
             $hValues = explode("\n", $value);
             foreach ($hValues as $hVal) {
                 header("{$name}: {$hVal}", false);
             }
         }
     }
     //Send body, but only if it isn't a HEAD request
     if (!$this->request->isHead()) {
         echo $body;
     }
     $this->applyHook('slim.after');
     restore_error_handler();
 }
Ejemplo n.º 6
0
 /**
  * Run
  *
  * This method invokes the middleware stack, including the core Slim application;
  * the result is an array of HTTP status, header, and body. These three items
  * are returned to the HTTP client.
  */
 public function run()
 {
     set_error_handler(array('\\Slim\\Slim', 'handleErrors'));
     //Apply final outer middleware layers
     $this->add(new \Slim\Middleware\PrettyExceptions());
     //Invoke middleware and application stack
     $this->middleware[0]->call();
     //Fetch status, header, and body
     list($status, $headers, $body) = $this->response->finalize();
     // Serialize cookies (with optional encryption)
     \Slim\Http\Util::serializeCookies($headers, $this->response->cookies, $this->settings);
     //Send headers
     if (headers_sent() === false) {
         //Send status
         if (strpos(PHP_SAPI, 'cgi') === 0) {
             header(sprintf('Status: %s', \Slim\Http\Response::getMessageForCode($status)));
         } else {
             header(sprintf('HTTP/%s %s', $this->config('http.version'), \Slim\Http\Response::getMessageForCode($status)));
         }
         //Send headers
         foreach ($headers as $name => $value) {
             $hValues = explode("\n", $value);
             foreach ($hValues as $hVal) {
                 header("{$name}: {$hVal}", false);
             }
         }
     }
     //Send body
     echo $body;
     restore_error_handler();
 }
Ejemplo n.º 7
0
 /**
  * Run
  *
  * This method invokes the middleware stack, including the core Slim application;
  * the result is an array of HTTP status, header, and body. These three items
  * are returned to the HTTP client.
  */
 public function run()
 {
     /**
      * set time zone for log and message queue message body
      * @author Mustafa Zeynel Dağlı
      */
     date_default_timezone_set($this->container['settings']['time.zone']);
     /**
      * MQMAnager middle ware katmanından önce inject
      * ediliyor/ test amaçlı değiştirilecek
      */
     $MQManagerConfigObject = new \Utill\MQ\MQManagerConfig();
     $managerConfig = new \Zend\ServiceManager\Config($MQManagerConfigObject->getConfig());
     $MQManager = new \Utill\MQ\MQManager($managerConfig);
     $MQManager->setService('slimApp', $this);
     $this->setMQManager($MQManager);
     set_error_handler(array('\\Slim\\Slim', 'handleErrors'));
     /**
      * MQmanager exceptions has been tested  by changing error handler function
      * @author Zeynel Dağlı
      * @todo first tests did not work, after further tests if not work
      * this code can be removed
      */
     //set_error_handler(array($this, 'handleErrorsCustom'));
     //Apply final outer middleware layers
     if ($this->config('debug')) {
         //Apply pretty exceptions only in debug to avoid accidental information leakage in production
         $this->add(new \Slim\Middleware\PrettyExceptions());
     }
     /**
      * zeynel dağlı
      */
     if ($this->container['settings']['log.level'] <= \Slim\Log::ERROR) {
         //print_r('--slim run kontrolor--');
         $this->add(new \Slim\Middleware\PrettyExceptions());
     }
     //Invoke middleware and application stack
     $this->middleware[0]->call();
     //print_r('--slim run kontrolor2--');
     /**
      * if rest service entry logging conf. true, publish to message queue
      * @since 07/12/2015 this functionality is being called from MQ manager
      * @author Mustafa Zeynel Dağlı
      */
     //if($this->container['settings']['restEntry.rabbitMQ'] == true) $this->publishMessage();
     if ($this->container['settings']['restEntry.rabbitMQ'] == true) {
         $this->getMQManager()->get('MQRestCallLog');
     }
     //Fetch status, header, and body
     list($status, $headers, $body) = $this->response->finalize();
     // Serialize cookies (with optional encryption)
     \Slim\Http\Util::serializeCookies($headers, $this->response->cookies, $this->settings);
     //Send headers
     if (headers_sent() === false) {
         //Send status
         if (strpos(PHP_SAPI, 'cgi') === 0) {
             header(sprintf('Status: %s', \Slim\Http\Response::getMessageForCode($status)));
         } else {
             header(sprintf('HTTP/%s %s', $this->config('http.version'), \Slim\Http\Response::getMessageForCode($status)));
         }
         //Send headers
         foreach ($headers as $name => $value) {
             $hValues = explode("\n", $value);
             foreach ($hValues as $hVal) {
                 header("{$name}: {$hVal}", false);
             }
         }
     }
     //Send body, but only if it isn't a HEAD request
     if (!$this->request->isHead()) {
         echo $body;
     }
     $this->applyHook('slim.after');
     restore_error_handler();
 }
Ejemplo n.º 8
0
 public function childServer($addr)
 {
     $output = $this->output;
     $bindTo = json_decode($addr, true);
     $this->childHost = $bindTo['host'];
     $this->port = $bindTo['port'];
     cli_set_process_title("mpcmf/console server:run/child -b {$this->childHost} -p {$this->port}");
     posix_setgid(99);
     posix_setuid(99);
     posix_seteuid(99);
     posix_setegid(99);
     $loop = Factory::create();
     $socket = new reactSocketServer($loop);
     $http = new reactHttpServer($socket);
     $http->on('request', function (reactRequest $request, reactResponse $response) use($output) {
         //MPCMF_DEBUG && $output->writeln("<info>[CHILD:{$this->port}]</info> New connection");
         //MPCMF_DEBUG && $clientName = $request->getRemoteAddress() . '#' . spl_object_hash($request);
         //MPCMF_DEBUG && $output->writeln("<info>[{$clientName}] Client connected");
         profiler::resetStack();
         if (!$this->prepare($request, $response, $output)) {
             return;
         }
         //MPCMF_DEBUG && $output->writeln("<info>[{$clientName}] Starting application</info>");
         try {
             $app = $this->app();
             $slim = $app->slim();
             $originApplication = $this->applicationInstance->getCurrentApplication();
             $this->applicationInstance->setApplication($app);
             $slim->call();
         } catch (\Exception $e) {
             $response->writeHead(500);
             $response->end("Exception: {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}\n{$e->getTraceAsString()}");
             return;
         }
         /** @var int[]|Headers[]|string[] $content */
         $content = $slim->response->finalize();
         Util::serializeCookies($content[1], $slim->response->cookies, $slim->settings);
         $content[1] = $content[1]->all();
         $this->applicationInstance->setApplication($originApplication);
         //MPCMF_DEBUG && $output->writeln("<info>[{$clientName}] Ending application</info>");
         //MPCMF_DEBUG && $output->writeln("<info>[CHILD:{$this->port}]</info> Writing data and closing connection");
         static $serverSoftware;
         if ($serverSoftware === null) {
             $serverSoftware = 'MPCMF Async PHP ' . phpversion();
         }
         if (array_key_exists('HTTP_ACCEPT_ENCODING', $_SERVER) && strpos($_SERVER["HTTP_ACCEPT_ENCODING"], 'gzip') !== false) {
             $content[1]['Content-Encoding'] = 'gzip';
             $content[2] = gzencode($content[2], 9);
         }
         $content[1]['X-PHP-Server'] = $serverSoftware;
         $content[1]['X-PHP-Server-Addr'] = "{$this->childHost}:{$this->port}";
         $response->writeHead($content[0], $content[1]);
         $response->end($content[2]);
         //MPCMF_DEBUG && $output->writeln("<info>[CHILD:{$this->port}]</info> Connection closed");
     });
     $output->writeln("<error>[CHILD]</error> Starting child server on {$this->childHost}:{$this->port}");
     $socket->listen($this->port, $this->childHost);
     $loop->run();
 }