/** * Stops the server cleanly. * * @access public * @return void */ public function stop() { if (strpos($this->request->get_remote_ip(), '127.0.0.') === 0) { // Send back a success message. We may not be successful, but this is our // last chance to send anything. $response = new HTTPResponse(200); $response->set_body('Server shut down.'); $this->write($response); // Close all listeners. $this->dispatcher->close_listeners(); // Stop the server loop. $this->dispatcher->get_server()->stop(time() - 1); } }
/** * Returns the contents of a static file. * * @access private * @return mixed */ private function _found_static() { // Backgrounding for Windows is not working well (2010-05-30) if (substr(PHP_OS, 0, 3) === 'WIN') { $response = new HTTPResponse(200); $response->set_body(file_get_contents($this->_path), false); return $response; } // Background the process. $command = 'cat'; $pipe = $this->background($command . ' ' . $this->_path); // If we couldn't open the process, freak out. if (!is_resource($pipe)) { return $this->_notfound(); } // Close the pipe. fclose($pipe); }
/** * Handle short polling requests. * * @access public * @return void */ public function short_polling() { $ip = $this->request->get_remote_ip(); $response = new HTTPResponse(200); // Determine if this is a JSONP request. $body = array('sp_requests' => self::$_sp_requests[$ip], 'sp_bytes_received' => self::$_sp_bytes_received[$ip], 'sp_bytes_sent' => self::$_sp_bytes_sent[$ip]); if ($this->request->get_request_var('callback')) { $response->set_body($this->request->get_request_var('callback') . '(' . json_encode($body) . ');', false); } else { $response->set_body($body, true); } $this->_log_response($response); $this->write($response); }
/** * Processes the given event. * * @access public * @return void */ public function process_event(Controller $mover = null) { $key = array_search((int) $this->_cursor, array_keys(self::$_commands)); if ($key === false && !is_null($this->_cursor)) { return; } $commands = array_slice(self::$_commands, $key); if (empty($commands)) { return; } $response = new HTTPResponse(200); $body = array('__URI__' => $this->uri, 'cursor' => end(array_keys(self::$_commands)) + 1, 'commands' => $commands); if ($this->request->get_request_var('callback')) { $response->set_body($this->request->get_request_var('callback') . '(' . json_encode($body) . ');', false); } else { $response->set_body($body, true); } $this->write($response); $this->_cursor = (int) end(array_keys(self::$_commands)) + 1; }
/** * 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 = ''; } }