Example #1
0
 private function routeRequest(Request $request, Service $service)
 {
     $desired_method = String::underscoreToCamelCase($request->getMethod());
     $requested_method = $request->getRequestMethod() . $desired_method;
     $method = $requested_method;
     $arguments = $request->getArguments();
     if (!method_exists($service, $method)) {
         $method = $request->getRequestMethod() . 'Router';
         if (!method_exists($service, $method)) {
             throw new MethodNotSupported($request->getService(), $requested_method);
         }
         array_splice($arguments, 0, 0, array($request->getMethod()));
     }
     $output = call_user_func_array(array($service, $method), $arguments);
     $this->response->setResponse($output);
 }
Example #2
0
 public static function route(\Request $request)
 {
     $method = $request->getRequestMethod();
     $req = $request->getRequestedUri();
     $routes = new \Config("routes");
     $routes = $routes->{$method};
     foreach ($routes as $url => $handler) {
         $url = preg_replace("|\\:([a-z0-9\\_\\-]+)|iu", "(?P<\$1>.*)", $url);
         if (preg_match("|{$url}|iu", $request->getRequestedUri(), $params)) {
             list($controller, $method) = explode("::", $handler);
             $request->setParams($params);
             break;
         }
     }
     if (!isset($controller)) {
         $controller = "Controller\\BadRequest";
         $method = "output404";
     }
     $controller = new $controller();
     $auth = new Authentication\Auth();
     if ($controller instanceof Controller\AbAuthController && !$auth->isLoggedIn()) {
         return array($controller, "checkLogin");
     }
     return array($controller, $method);
 }
 public function output404(\Request $req, \Response $res)
 {
     $view = new \Output\View("404");
     $view->request = $req->getRequestedUri();
     $view->method = $req->getRequestMethod();
     $res->addView("content", $view);
 }
Example #4
0
 public function testGetRequestMethod()
 {
     $this->assertEquals(null, Request::getRequestMethod());
     $_SERVER['REQUEST_METHOD'] = 'get';
     $this->assertEquals('GET', Request::getRequestMethod());
     $_SERVER['REQUEST_METHOD'] = 'post';
     $this->assertEquals('POST', Request::getRequestMethod());
 }
 /**
  * Check if an HTTP request can be cached by a private local cache.
  *
  * @static
  * @param Request $resp
  * @return bool True if the request is cacheable.
  * False if the request is uncacheable.
  */
 public static function isRequestCacheable(Request $resp)
 {
     $method = $resp->getRequestMethod();
     if (!in_array($method, self::$CACHEABLE_HTTP_METHODS)) {
         return false;
     }
     // Don't cache authorized requests/responses.
     // [rfc2616-14.8] When a shared cache receives a request containing an
     // Authorization field, it MUST NOT return the corresponding response
     // as a reply to any other request...
     if ($resp->getRequestHeader("authorization")) {
         return false;
     }
     return true;
 }
Example #6
0
 /**
  * Decode an HTTP Response.
  * @static
  * @throws Exception
  * @param Request $response The http response to be decoded.
  * @param Client $client
  * @return mixed|null
  */
 public static function decodeHttpResponse($response, Client $client = null)
 {
     $code = $response->getResponseHttpCode();
     $body = $response->getResponseBody();
     $decoded = null;
     if (intVal($code) >= 300) {
         $decoded = json_decode($body, true);
         $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl();
         if (isset($decoded['error']) && isset($decoded['error']['message']) && isset($decoded['error']['code'])) {
             // if we're getting a json encoded error definition, use that instead of the raw response
             // body for improved readability
             $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}";
         } else {
             $err .= ": ({$code}) {$body}";
         }
         $errors = null;
         // Specific check for APIs which don't return error details, such as Blogger.
         if (isset($decoded['error']) && isset($decoded['error']['errors'])) {
             $errors = $decoded['error']['errors'];
         }
         $map = null;
         if ($client) {
             $client->getLogger()->error($err, array('code' => $code, 'errors' => $errors));
             $map = $client->getClassConfig('Exception', 'retry_map');
         }
         throw new Exception($err, $code, null, $errors, $map);
     }
     // Only attempt to decode the response, if the response code wasn't (204) 'no content'
     if ($code != '204') {
         if ($response->getExpectedRaw()) {
             return $body;
         }
         $decoded = json_decode($body, true);
         if ($decoded === null || $decoded === "") {
             $error = "Invalid json in service response: {$body}";
             if ($client) {
                 $client->getLogger()->error($error);
             }
             throw new Exception($error);
         }
         if ($response->getExpectedClass()) {
             $class = $response->getExpectedClass();
             $decoded = new $class($decoded);
         }
     }
     return $decoded;
 }
Example #7
0
 /**
  * Determine whether the request is a GET request
  * @return boolean
  */
 function isGet()
 {
     return Request::getRequestMethod() == 'GET';
 }
Example #8
0
 /**
  * Processes an access control request
  *
  * @param Request $request Access control request to process
  * @return void
  * @throws Exception\AccessDeniedException If access is not allowed
  */
 public function processRequest(Request $request, Response $response)
 {
     if (!$request->isCrossOrigin()) {
         return;
     }
     if ($request->isPreflight()) {
         if (!$request->getRequestMethod()) {
             throw new Exception\AccessDeniedException('Missing request method', 1413983849);
         }
         if (!$this->isMethodAllowed($request->getRequestMethod())) {
             throw new Exception\AccessDeniedException('Request method "' . $request->getRequestMethod() . '" not allowed', 1413983927);
         }
         foreach ($request->getRequestHeaders() as $header) {
             if (!$this->isHeaderAllowed($header)) {
                 throw new Exception\AccessDeniedException('Request header "' . $header . '" not allowed', 1413988013);
             }
         }
         $response->setPreflight(TRUE);
         $response->setAllowedMethods([$request->getRequestMethod()]);
         $response->setAllowedHeaders($request->getRequestHeaders());
         $response->setMaximumAge($this->getMaximumAge());
     }
     $origin = $request->getOrigin();
     $originUri = $origin->getScheme() . '://' . $origin->getHostname() . ($origin->getPort() ? ':' . $origin->getPort() : '');
     if ($this->isOriginUriAllowed('*') && !$request->hasCredentials()) {
         $response->setAllowedOrigin('*');
     } elseif ($this->isOriginUriAllowed($originUri)) {
         $response->setAllowedOrigin($originUri);
     } else {
         throw new Exception\AccessDeniedException('Access not allowed for origin "' . $originUri . '"', 1413983266);
     }
     if ($request->hasCredentials()) {
         $response->setAllowCredentials($this->getAllowCredentials());
     }
     $response->setExposedHeaders($this->getExposedHeaders());
 }
Example #9
0
<?php

require_once 'config.php';
// configuration values
require_once 'loader.php';
// spl_autoload_register
// default module and action
$module = 'addresses';
$action = 'get';
$rqst = new Request();
if ($uri = $rqst->getUrlElements()) {
    $action = $rqst->getRequestMethod();
    $module = ucfirst($uri[0]);
    if (strlen($uri[1]) > 0) {
        $params = $uri[1];
    }
}
if (!file_exists('./controllers/' . $module . 'Controller.php')) {
    $module = 'Error';
    $action = 'index';
}
$classname = $module . 'Controller';
$methodname = $action . 'Action';
if (!class_exists($classname) || !method_exists($classname, $methodname)) {
    $classname = 'ErrorController';
    $methodname = 'indexAction';
}
try {
    $object = new $classname();
    $object->{$methodname}($params);
} catch (Exception $e) {