/**
  * @phutil-external-symbol class PhabricatorStartup
  */
 public static function runHTTPRequest(AphrontHTTPSink $sink)
 {
     $multimeter = MultimeterControl::newInstance();
     $multimeter->setEventContext('<http-init>');
     $multimeter->setEventViewer('<none>');
     // Build a no-op write guard for the setup phase. We'll replace this with a
     // real write guard later on, but we need to survive setup and build a
     // request object first.
     $write_guard = new AphrontWriteGuard('id');
     PhabricatorEnv::initializeWebEnvironment();
     $multimeter->setSampleRate(PhabricatorEnv::getEnvConfig('debug.sample-rate'));
     $debug_time_limit = PhabricatorEnv::getEnvConfig('debug.time-limit');
     if ($debug_time_limit) {
         PhabricatorStartup::setDebugTimeLimit($debug_time_limit);
     }
     // This is the earliest we can get away with this, we need env config first.
     PhabricatorAccessLog::init();
     $access_log = PhabricatorAccessLog::getLog();
     PhabricatorStartup::setAccessLog($access_log);
     $access_log->setData(array('R' => AphrontRequest::getHTTPHeader('Referer', '-'), 'r' => idx($_SERVER, 'REMOTE_ADDR', '-'), 'M' => idx($_SERVER, 'REQUEST_METHOD', '-')));
     DarkConsoleXHProfPluginAPI::hookProfiler();
     DarkConsoleErrorLogPluginAPI::registerErrorHandler();
     $response = PhabricatorSetupCheck::willProcessRequest();
     if ($response) {
         PhabricatorStartup::endOutputCapture();
         $sink->writeResponse($response);
         return;
     }
     $host = AphrontRequest::getHTTPHeader('Host');
     $path = $_REQUEST['__path__'];
     switch ($host) {
         default:
             $config_key = 'aphront.default-application-configuration-class';
             $application = PhabricatorEnv::newObjectFromConfig($config_key);
             break;
     }
     $application->setHost($host);
     $application->setPath($path);
     $application->willBuildRequest();
     $request = $application->buildRequest();
     // Now that we have a request, convert the write guard into one which
     // actually checks CSRF tokens.
     $write_guard->dispose();
     $write_guard = new AphrontWriteGuard(array($request, 'validateCSRF'));
     // Build the server URI implied by the request headers. If an administrator
     // has not configured "phabricator.base-uri" yet, we'll use this to generate
     // links.
     $request_protocol = $request->isHTTPS() ? 'https' : 'http';
     $request_base_uri = "{$request_protocol}://{$host}/";
     PhabricatorEnv::setRequestBaseURI($request_base_uri);
     $access_log->setData(array('U' => (string) $request->getRequestURI()->getPath()));
     $processing_exception = null;
     try {
         $response = $application->processRequest($request, $access_log, $sink, $multimeter);
         $response_code = $response->getHTTPResponseCode();
     } catch (Exception $ex) {
         $processing_exception = $ex;
         $response_code = 500;
     }
     $write_guard->dispose();
     $access_log->setData(array('c' => $response_code, 'T' => PhabricatorStartup::getMicrosecondsSinceStart()));
     $multimeter->newEvent(MultimeterEvent::TYPE_REQUEST_TIME, $multimeter->getEventContext(), PhabricatorStartup::getMicrosecondsSinceStart());
     $access_log->write();
     $multimeter->saveEvents();
     DarkConsoleXHProfPluginAPI::saveProfilerSample($access_log);
     // Add points to the rate limits for this request.
     if (isset($_SERVER['REMOTE_ADDR'])) {
         $user_ip = $_SERVER['REMOTE_ADDR'];
         // The base score for a request allows users to make 30 requests per
         // minute.
         $score = 1000 / 30;
         // If the user was logged in, let them make more requests.
         if ($request->getUser() && $request->getUser()->getPHID()) {
             $score = $score / 5;
         }
         PhabricatorStartup::addRateLimitScore($user_ip, $score);
     }
     if ($processing_exception) {
         throw $processing_exception;
     }
 }
Ejemplo n.º 2
0
if (file_exists($preamble_path)) {
    require_once $preamble_path;
}
PhabricatorStartup::didStartup();
$show_unexpected_traces = false;
try {
    PhabricatorStartup::loadCoreLibraries();
    PhabricatorEnv::initializeWebEnvironment();
    $show_unexpected_traces = PhabricatorEnv::getEnvConfig('phabricator.developer-mode');
    // This is the earliest we can get away with this, we need env config first.
    PhabricatorAccessLog::init();
    $access_log = PhabricatorAccessLog::getLog();
    PhabricatorStartup::setGlobal('log.access', $access_log);
    $access_log->setData(array('R' => AphrontRequest::getHTTPHeader('Referer', '-'), 'r' => idx($_SERVER, 'REMOTE_ADDR', '-'), 'M' => idx($_SERVER, 'REQUEST_METHOD', '-')));
    DarkConsoleXHProfPluginAPI::hookProfiler();
    DarkConsoleErrorLogPluginAPI::registerErrorHandler();
    $sink = new AphrontPHPHTTPSink();
    $response = PhabricatorSetupCheck::willProcessRequest();
    if ($response) {
        PhabricatorStartup::endOutputCapture();
        $sink->writeResponse($response);
        return;
    }
    $host = AphrontRequest::getHTTPHeader('Host');
    $path = $_REQUEST['__path__'];
    switch ($host) {
        default:
            $config_key = 'aphront.default-application-configuration-class';
            $application = PhabricatorEnv::newObjectFromConfig($config_key);
            break;
    }