Skip to content

Commit

Permalink
Merge branch 'master' into service-exception-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
robotpony committed Sep 3, 2013
2 parents 841a542 + 91e2664 commit a6e90d5
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 238 deletions.
1 change: 0 additions & 1 deletion _tests/index.php

This file was deleted.

187 changes: 0 additions & 187 deletions _tests/run-all-tests.php

This file was deleted.

20 changes: 20 additions & 0 deletions lib/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ public function restrictTo($types) {
return $this->supports_contentType($types);
}

/* Set necessary CORS headers for this API route.
* Sets `Access-Control-Allow-Origin` appropriately with respect to referer (necessary for `Access-Control-Allow-Credentials`)
* Sets `Access-Control-Allow-Credentials: true`: allow `rx-auth` cookie to be included in request (authentication)
* Sets `Access-Control-Allow-Methods: GET`: only GETs supported for now
* sets some other useful default headers
*/
public function crossOrigin() {

if (empty($_SERVER['HTTP_ORIGIN']))
throw new Exception("'HTTP_ORIGIN' unknown. Failed to set required 'Access-Control-Allow-Origin' header for CORS handshake.", 500);

$this->add_header('Access-Control-Allow-Origin', $_SERVER['HTTP_ORIGIN']);
$this->add_header('Access-Control-Allow-Credentials', 'true');
$this->add_header('Access-Control-Allow-Methods', 'GET');
$this->add_header('Access-Control-Allow-Headers', 'Content-Type,Accept');
$this->add_header('Access-Control-Max-Age', '10');
}

/* Get a filtered variable (get filter_var_array + exceptions) */
static function filtered($thing, $rules, $defaults = null) {
if ($defaults) $thing = array_merge($defaults, (array)$thing);
Expand Down
6 changes: 3 additions & 3 deletions lib/autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ function autoload_delegate($call) {
$in = $call->container;
$error = "API `$call->class` not found";

if ( !file_exists($call->file) ) {
if ( !stream_resolve_include_path($call->file) ) {
$extra = " ({$call->file} not found).";

if ( !empty($in) && !is_dir($in) )
$extra = " ({$call->file} not found, $in missing)."; // aid debugging of routes-in-folders

throw new Exception($error .$extra, 404);
throw new Exception($error . $extra, 404);
}

if ( !(require_once $call->file) )
Expand Down
Loading

0 comments on commit a6e90d5

Please sign in to comment.