示例#1
0
    /**
     * Handles a http request, and execute a method based on its name
     *
     * @param RequestInterface $request
     * @param ResponseInterface $response
     * @param $sendResponse Whether to send the HTTP response to the DAV client.
     * @return void
     */
    function invokeMethod(RequestInterface $request, ResponseInterface $response, $sendResponse = true) {

        $method = $request->getMethod();

        if (!$this->emit('beforeMethod:' . $method, [$request, $response])) return;
        if (!$this->emit('beforeMethod', [$request, $response])) return;

        if (self::$exposeVersion) {
            $response->setHeader('X-Sabre-Version', Version::VERSION);
        }

        $this->transactionType = strtolower($method);

        if (!$this->checkPreconditions($request, $response)) {
            $this->sapi->sendResponse($response);
            return;
        }

        if ($this->emit('method:' . $method, [$request, $response])) {
            if ($this->emit('method', [$request, $response])) {
                // Unsupported method
                throw new Exception\NotImplemented('There was no handler found for this "' . $method . '" method');
            }
        }

        if (!$this->emit('afterMethod:' . $method, [$request, $response])) return;
        if (!$this->emit('afterMethod', [$request, $response])) return;

        if ($sendResponse) {
            $this->sapi->sendResponse($response);
            $this->emit('afterResponse', [$request, $response]);
        }

    }
示例#2
0
 /**
  * Handles a http request, and execute a method based on its name
  *
  * @param RequestInterface $request
  * @param ResponseInterface $response
  * @param $sendResponse Whether to send the HTTP response to the DAV client.
  * @return void
  */
 function invokeMethod(RequestInterface $request, ResponseInterface $response, $sendResponse = true)
 {
     $method = $request->getMethod();
     if (!$this->emit('beforeMethod:' . $method, [$request, $response])) {
         return;
     }
     if (!$this->emit('beforeMethod', [$request, $response])) {
         return;
     }
     if (self::$exposeVersion) {
         $response->setHeader('X-Sabre-Version', Version::VERSION);
     }
     $this->transactionType = strtolower($method);
     if (!$this->checkPreconditions($request, $response)) {
         $this->sapi->sendResponse($response);
         return;
     }
     if ($this->emit('method:' . $method, [$request, $response])) {
         if ($this->emit('method', [$request, $response])) {
             $exMessage = "There was no plugin in the system that was willing to handle this " . $method . " method.";
             if ($method === "GET") {
                 $exMessage .= " Enable the Browser plugin to get a better result here.";
             }
             // Unsupported method
             throw new Exception\NotImplemented($exMessage);
         }
     }
     if (!$this->emit('afterMethod:' . $method, [$request, $response])) {
         return;
     }
     if (!$this->emit('afterMethod', [$request, $response])) {
         return;
     }
     if ($response->getStatus() === null) {
         throw new Exception('No subsystem set a valid HTTP status code. Something must have interrupted the request without providing further detail.');
     }
     if ($sendResponse) {
         $this->sapi->sendResponse($response);
         $this->emit('afterResponse', [$request, $response]);
     }
 }
 /**
  *
  */
 public function process()
 {
     $this->emit('process:before', [['request' => $this->httpRequest]]);
     // set Content Security Policy and CORS headers
     $this->httpResponse->addHeader('Content-Security-Policy', "default-src *");
     $this->httpResponse->addHeader('X-Content-Security-Policy', "default-src *");
     if ($this->httpRequest->hasHeader('Origin')) {
         // TODO: allow to configure allowed origins
         $this->httpResponse->addHeader('Access-Control-Allow-Origin', "*");
     }
     // FIXME: respond to OPTIONS requests directly and without validation
     if ($this->httpRequest->getMethod() == 'OPTIONS') {
         $this->httpResponse->addHeader('Access-Control-Request-Method', 'GET, POST, OPTIONS');
         $this->httpResponse->addHeader('Access-Control-Allow-Headers', $this->httpRequest->getHeader('Access-Control-Request-Headers'));
         $this->httpResponse->setStatus(204);
         $this->sapi->sendResponse($this->httpResponse);
         return;
     }
     // extract route from request (jmap, auth|.well-known/jmap, upload)
     if ($route = $this->getRouteMatch($this->httpRequest->getPath())) {
         try {
             call_user_func($this->routes[$route], $this->httpRequest, $this->httpResponse);
         } catch (\RuntimeException $e) {
             if ($e instanceof Exception\ProcessorException) {
                 $this->httpResponse->setStatus($e->getStatusCode());
             } else {
                 $this->httpResponse->setStatus(500);
             }
             $this->logger->err(strval($e));
             $this->emit('process:error', [['request' => $this->httpRequest, 'exception' => $e]]);
         }
     } else {
         // TODO: throw invalid route error
         $this->httpResponse->setStatus(404);
     }
     $this->emit('process:after', [['response' => $this->httpResponse]]);
     $this->sapi->sendResponse($this->httpResponse);
 }