Skip to content
Snippets Groups Projects
Commit ac0de60c authored by Andy Yates's avatar Andy Yates
Browse files

[ENSCORESW-741]. Support Accepts content

We now look at the type of method and will switch to looking at the Accept header.
parent b083bd7d
No related branches found
No related tags found
No related merge requests found
package EnsEMBL::REST::Role::Content;
###############################################################
####### HUGE WARNING
####### I'm not sure the following code will work all the time.
####### However it's working most of the time. Be aware!
###############################################################
use Moose::Role;
use Bio::EnsEMBL::Utils::Scalar qw/check_ref/;
sub is_content_type {
my ($self, $c, $content_type) = @_;
my $request_content_type = $c->request()->params->{'content-type'};
my $headers_content_type = $c->request()->headers()->content_type();
if( (defined $request_content_type && $request_content_type eq $content_type) ||
(defined $headers_content_type && $headers_content_type eq $content_type)) {
return 1;
my $request = $c->request();
my $headers = $request->headers();
my $param_content_type = $request->parameters()->{'content-type'};
my $headers_accept = $headers->header('Accept');
my $headers_content_type = $headers->content_type();
# If it's a GET then Accepts gets bumped down to the bottom otherwise things go wrong
if($request->method() eq 'GET') {
if(check_ref($content_type, 'Regexp')) {
if( (defined $headers_content_type && $headers_content_type =~ $content_type) ||
(defined $param_content_type && $param_content_type =~ $content_type) ||
(defined $headers_accept && $headers_accept =~ $content_type) ) {
return 1;
}
}
else {
if( (defined $headers_content_type && $headers_content_type eq $content_type) ||
(defined $param_content_type && $param_content_type eq $content_type) ||
(defined $headers_accept && $headers_accept =~ /$content_type/i) ) {
return 1;
}
}
}
# Must be a POST so do the Accepts take precedence thang
else {
# If it is already a regexp then apply against the values
if(check_ref($content_type, 'Regexp')) {
if( (defined $headers_accept && $headers_accept =~ $content_type) ||
(defined $headers_content_type && $headers_content_type =~ $content_type) ||
(defined $param_content_type && $param_content_type =~ $content_type)) {
return 1;
}
}
# Otherwise do the old code using eq & regex
else {
# Accepts ALWAYS WINS
if( (defined $headers_accept && $headers_accept =~ /$content_type/i) ||
(defined $param_content_type && $param_content_type eq $content_type) ||
(defined $headers_content_type && $headers_content_type eq $content_type)) {
return 1;
}
}
}
return 0;
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment