Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Fixes #63 Conditionally permit additional GET request in POST co… #64

Merged
merged 1 commit into from
May 31, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions src/RestfulServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
*/
class RestfulServer extends Controller
{
/**
* @config
* @var array
*/
private static $url_handlers = array(
'$ClassName!/$ID/$Relation' => 'handleAction',
'' => 'notFound'
Expand All @@ -62,10 +66,24 @@ class RestfulServer extends Controller
* If no extension is given in the request, resolve to this extension
* (and subsequently the {@link self::$default_mimetype}.
*
* @config
* @var string
*/
private static $default_extension = "xml";

/**
* Whether or not to send an additional "Location" header for POST requests
* to satisfy HTTP 1.1: https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
*
* Note: With this enabled (the default), no POST request for resource creation
* will return an HTTP 201. Because of the addition of the "Location" header,
* all responses become a straight HTTP 200.
*
* @config
* @var boolean
*/
private static $location_header_on_create = true;

/**
* If no extension is given, resolve the request to this mimetype.
*
Expand Down Expand Up @@ -584,10 +602,15 @@ protected function postHandler($className, $id, $relation)
$type = ".{$types[0]}";
}

$urlSafeClassName = $this->sanitiseClassName(get_class($obj));
$apiBase = $this->config()->api_base;
$objHref = Director::absoluteURL($apiBase . "$urlSafeClassName/$obj->ID" . $type);
$this->getResponse()->addHeader('Location', $objHref);
// Deviate slightly from the spec: Helps datamodel API access restrict
// to consulting just canCreate(), not canView() as a result of the additional
// "Location" header.
if ($this->config()->get('location_header_on_create')) {
$urlSafeClassName = $this->sanitiseClassName(get_class($obj));
$apiBase = $this->config()->api_base;
$objHref = Director::absoluteURL($apiBase . "$urlSafeClassName/$obj->ID" . $type);
$this->getResponse()->addHeader('Location', $objHref);
}

return $responseFormatter->convertDataObject($obj);
}
Expand Down