/**
  * Creates an options response and writes it to the connection.
  *
  * @access public
  * @return void
  */
 public function options()
 {
     $response = new HTTPResponse(200);
     $headers = array('Allow' => 'GET,POST,OPTIONS', 'Content-Length' => 0, 'Content-Type' => 'text/plain; charset=utf-8', 'Access-Control-Allow-Methods' => 'GET,POST,OPTIONS', 'Access-Control-Allow-Headers' => 'x-prototype-version,x-requested-with,waterspout-request', 'Access-Control-Allow-Credentials' => 'true');
     $response->add_headers($headers);
     $config = $this->dispatcher->get_config();
     if (isset($config['ACCESS_CONTROL_ALLOW_ORIGIN'])) {
         $response->add_header('Access-Control-Allow-Origin', $config['ACCESS_CONTROL_ALLOW_ORIGIN']);
     } else {
         $response->add_header('Access-Control-Allow-Origin', $this->request->get_headers()->get('Origin'));
     }
     $this->write($response);
 }
 /**
  * Sends a 304 not modified response.
  *
  * @access private
  * @return httpresponse
  */
 private function _304()
 {
     $config = $this->dispatcher->get_config();
     $response = new HTTPResponse(304);
     $headers = array('Date' => date('D, d M Y H:i:s \\G\\M\\T'), 'Expires' => date('D, d M Y H:i:s \\G\\M\\T', time() + $config['CACHE_EXPIRATION']), 'Last-Modified' => date('D, d M Y H:i:s \\G\\M\\T', filemtime($this->_path)), 'Cache-Control' => 'max-age=' . $config['CACHE_EXPIRATION']);
     $response->add_headers($headers);
     return $response;
 }
Пример #3
0
 /**
  * Writes the background contents to the response.
  *
  * @access public
  * @param  resource $socket
  * @param  integer  $ioevent
  * @param  resource $process
  * @param  boolean  $split_headers
  * @return void
  */
 public function background_write($socket, $ioevent, $process, $split_headers = false)
 {
     $attempts = 0;
     $chunk = '';
     while ($tmp = @fread($socket, IOStream::MAX_BUFFER_SIZE)) {
         $chunk .= $tmp;
     }
     if (empty($chunk) && empty($this->_background_content)) {
         // Write an error response.
         $response = new HTTPResponse(500);
         $response->set_body('Trouble generating content.');
     } else {
         $this->_background_content .= $chunk;
         // Check to see if the process has finished.
         $info = proc_get_status($process);
         if (!$info['running']) {
             // Make sure the proccess finished successfully.
             if ($info['exitcode'] !== 0) {
                 $response = new HTTPResponse(500);
             } else {
                 $response = new HTTPResponse($this->status);
                 $config = $this->dispatcher->get_config();
                 $headers = array('Content-Type' => $this->_mime, 'Date' => date('D, d M Y H:i:s \\G\\M\\T'), 'Expires' => date('D, d M Y H:i:s \\G\\M\\T', time() + $config['CACHE_EXPIRATION']), 'Last-Modified' => date('D, d M Y H:i:s \\G\\M\\T', filemtime($this->_path)), 'Cache-Control' => 'max-age=' . $config['CACHE_EXPIRATION']);
                 $response->add_headers($headers);
             }
             if ($split_headers) {
                 // Split the chunk into headers and content.
                 list($headers, $content) = explode("\r\n\r\n", $this->_background_content, 2);
                 // Set the headers.
                 foreach (explode("\r\n", $headers) as $header) {
                     list($h, $v) = explode(':', $header);
                     $response->add_header($h, trim($v));
                 }
             } else {
                 $content = $this->_background_content;
             }
             // Add the body content.
             $response->set_body($content, false);
         }
     }
     if (isset($response) && $response instanceof HTTPResponse) {
         // Send the response.
         $this->write($response);
         // Remove the handler.
         $loop = $this->dispatcher->get_server()->get_loop();
         $loop->remove_handler($socket);
         // Close the sockets.
         if (is_resource($socket)) {
             fclose($socket);
         }
         // Close the process.
         proc_close($process);
         // Clear the content.
         $this->_background_content = '';
     }
 }