Skip to content
This repository has been archived by the owner on Jul 24, 2021. It is now read-only.

Commit

Permalink
use url_for() consistently when setting Location header
Browse files Browse the repository at this point in the history
  • Loading branch information
karenetheridge committed Jan 7, 2021
1 parent 1813ec9 commit 1a080ec
Show file tree
Hide file tree
Showing 17 changed files with 46 additions and 33 deletions.
4 changes: 4 additions & 0 deletions docs/modules/Conch.md
Expand Up @@ -27,6 +27,10 @@ and therefore other helpers).
Helper method for setting the response status code and json content. Calls
`$c->render` as a side-effect.

### res\_location

Simple helper for setting the `Location` header in the response.

### startup\_time

Stores a [Conch::Time](../modules/Conch%3A%3ATime) instance representing the time the server started accepting requests.
Expand Down
13 changes: 12 additions & 1 deletion lib/Conch.pm
Expand Up @@ -146,7 +146,7 @@ C<< $c->render >> as a side-effect.
}

if ($code == 204) {
$c->res->headers->location($payload) if defined $payload;
$c->res_location($payload) if defined $payload;
$c->rendered;
}
elsif (any { $code == $_ } 301, 302, 303, 305, 307, 308) {
Expand All @@ -163,6 +163,17 @@ C<< $c->render >> as a side-effect.
});


=head2 res_location
Simple helper for setting the C<Location> header in the response.
=cut

$self->helper(res_location => sub ($c, @args) {
$c->res->headers->location($c->url_for(@args));
});


$self->hook(before_routes => sub ($c) {
my $headers = $c->req->headers;

Expand Down
2 changes: 1 addition & 1 deletion lib/Conch/Controller/Build.pm
Expand Up @@ -91,7 +91,7 @@ sub create ($c) {
user_build_roles => [ map +{ user_id => $_->[2], role => 'admin' }, @admins ],
});
$c->log->info('created build '.$build->id.' ('.$build->name.')');
$c->res->headers->location('/build/'.$build->id);
$c->res_location('/build/'.$build->id);
$c->status(201);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Conch/Controller/Datacenter.pm
Expand Up @@ -95,7 +95,7 @@ sub create ($c) {

my $dc = $c->db_datacenters->create($input);
$c->log->debug('Created datacenter '.$dc->id);
$c->res->headers->location('/dc/'.$dc->id);
$c->res_location('/dc/'.$dc->id);
$c->status(201);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/Conch/Controller/DatacenterRoom.pm
Expand Up @@ -73,7 +73,7 @@ Response uses the DatacenterRoomDetailed json schema.

sub get_one ($c) {
my $room = $c->stash('datacenter_room_rs')->single;
$c->res->headers->location('/room/'.$room->id);
$c->res_location('/room/'.$room->id);
$c->status(200, $room);
}

Expand All @@ -97,7 +97,7 @@ sub create ($c) {

my $room = $c->db_datacenter_rooms->create($input);
$c->log->debug('Created datacenter room '.$room->id);
$c->res->headers->location('/room/'.$room->id);
$c->res_location('/room/'.$room->id);
$c->status(201);
}

Expand All @@ -124,7 +124,7 @@ sub update ($c) {
if $input->{vendor_name} and $input->{vendor_name} ne $room->vendor_name
and $c->db_datacenter_rooms->search({ vendor_name => $input->{vendor_name} })->exists;

$c->res->headers->location('/room/'.$room->id);
$c->res_location('/room/'.$room->id);

$room->set_columns($input);
return $c->status(204) if not $room->is_changed;
Expand Down
4 changes: 2 additions & 2 deletions lib/Conch/Controller/Device.pm
Expand Up @@ -113,12 +113,12 @@ sub find_device ($c) {
if (my $bad_phase = $params->{phase_earlier_than} // $c->stash('phase_earlier_than')) {
my $phase = $c->stash('device_rs')->get_column('phase')->single;
if (Conch::DB::Result::Device->phase_cmp($phase, $bad_phase) >= 0) {
$c->res->headers->location($c->url_for('/device/'.$device_id.'/links'));
$c->res_location('/device/'.$device_id.'/links');
return $c->status(409, { error => 'device is in the '.$phase.' phase' });
}
}

$c->res->headers->location('/device/'.$device_id);
$c->res_location('/device/'.$device_id);
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Conch/Controller/DeviceReport.pm
Expand Up @@ -181,7 +181,7 @@ sub process ($c) {
}
}

$c->res->headers->location($c->url_for('/validation_state/'.$validation_state->id));
$c->res_location('/validation_state/'.$validation_state->id);
$c->status(201);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/Conch/Controller/HardwareProduct.pm
Expand Up @@ -85,7 +85,7 @@ Response uses the HardwareProduct json schema.

sub get ($c) {
my $hardware_product = $c->stash('hardware_product_rs')->single;
$c->res->headers->location('/hardware_product/'.$hardware_product->id);
$c->res_location('/hardware_product/'.$hardware_product->id);
$c->status(200, $hardware_product);
}

Expand Down Expand Up @@ -122,7 +122,7 @@ sub create ($c) {
return $c->status(400) if not $hardware_product;

$c->log->debug('Created hardware product id '.$hardware_product->id);
$c->res->headers->location('/hardware_product/'.$hardware_product->id);
$c->res_location('/hardware_product/'.$hardware_product->id);
$c->status(201);
}

Expand Down Expand Up @@ -232,7 +232,7 @@ sub set_specification ($c) {

return $rendered ? () : $c->status(400) if not $result;

$c->res->headers->location('/hardware_product/'.$hardware_product_id);
$c->res_location('/hardware_product/'.$hardware_product_id);
$c->status(204);
}

Expand Down Expand Up @@ -284,7 +284,7 @@ sub delete_specification ($c) {

return $rendered ? () : $c->status(400) if not $result;

$c->res->headers->location('/hardware_product/'.$hardware_product_id);
$c->res_location('/hardware_product/'.$hardware_product_id);
$c->status(204);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Conch/Controller/HardwareVendor.pm
Expand Up @@ -83,7 +83,7 @@ sub create ($c) {
my $hardware_vendor = $c->db_hardware_vendors->create({ name => $c->stash('hardware_vendor_name') });

$c->log->debug('Created hardware vendor '.$c->stash('hardware_vendor_name'));
$c->res->headers->location('/hardware_vendor/'.$hardware_vendor->id);
$c->res_location('/hardware_vendor/'.$hardware_vendor->id);
$c->status(201);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Conch/Controller/JSONSchema.pm
Expand Up @@ -183,7 +183,7 @@ sub create ($c) {

my $path = $row->canonical_path;
$c->log->info('created json schema '.$row->id.' at '.$path);
$c->res->headers->location($c->url_for('/json_schema/'.$row->id));
$c->res_location('/json_schema/'.$row->id);
$c->res->headers->content_location($c->url_for($path));
$c->status(201);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Conch/Controller/Login.pm
Expand Up @@ -138,7 +138,7 @@ sub _check_authentication ($c) {
$user->user_session_tokens->login_only
->update({ expires => \'least(expires, now() + interval \'10 minutes\')' }) if $session_token;

$c->res->headers->location($c->url_for('/user/me/password?clear_tokens=none'));
$c->res_location('/user/me/password?clear_tokens=none');
return 0;
}

Expand Down Expand Up @@ -203,7 +203,7 @@ sub login ($c) {
$c->_update_session($user->id, $input->{set_session} ? time + 10 * 60 : 0);

# we logged the user in, but he must now change his password (within 10 minutes)
$c->res->headers->location($c->url_for('/user/me/password?clear_tokens=none'));
$c->res_location('/user/me/password?clear_tokens=none');
return $c->_respond_with_jwt($user->id, time + 10 * 60);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Conch/Controller/Organization.pm
Expand Up @@ -82,7 +82,7 @@ sub create ($c) {
user_organization_roles => [ map +{ user_id => $_->[2], role => 'admin' }, @admins ],
});
$c->log->info('created organization '.$organization->id.' ('.$organization->name.')');
$c->res->headers->location('/organization/'.$organization->id);
$c->res_location('/organization/'.$organization->id);
$c->status(201);
}

Expand Down
10 changes: 5 additions & 5 deletions lib/Conch/Controller/Rack.pm
Expand Up @@ -118,7 +118,7 @@ sub create ($c) {
my $rack = $c->db_racks->create($input);
$c->log->debug('Created rack '.$rack->id);

$c->res->headers->location('/rack/'.$rack->id);
$c->res_location('/rack/'.$rack->id);
$c->status(201);
}

Expand All @@ -131,7 +131,7 @@ Response uses the Rack json schema.
=cut

sub get ($c) {
$c->res->headers->location('/rack/'.$c->stash('rack_id'));
$c->res_location('/rack/'.$c->stash('rack_id'));
my $rs = $c->stash('rack_rs')
->with_build_name
->with_full_rack_name
Expand Down Expand Up @@ -160,7 +160,7 @@ sub get_layouts ($c) {
->all;

$c->log->debug('Found '.scalar(@layouts).' rack layouts');
$c->res->headers->location('/rack/'.$c->stash('rack_id').'/layout');
$c->res_location('/rack/'.$c->stash('rack_id').'/layout');
$c->status(200, \@layouts);
}

Expand Down Expand Up @@ -296,7 +296,7 @@ sub update ($c) {
}
}

$c->res->headers->location('/rack/'.$rack->id);
$c->res_location('/rack/'.$rack->id);

$rack->set_columns($input);
return $c->status(204) if not $rack->is_changed;
Expand Down Expand Up @@ -348,7 +348,7 @@ sub get_assignment ($c) {
->all;

$c->log->debug('Found '.scalar(@assignments).' device-rack assignments');
$c->res->headers->location('/rack/'.$c->stash('rack_id').'/assignment');
$c->res_location('/rack/'.$c->stash('rack_id').'/assignment');
$c->status(200, \@assignments);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/Conch/Controller/RackLayout.pm
Expand Up @@ -111,7 +111,7 @@ sub create ($c) {
my $layout = $c->db_rack_layouts->create($input);
$c->log->debug('Created rack layout '.$layout->id);

$c->res->headers->location('/layout/'.$layout->id);
$c->res_location('/layout/'.$layout->id);
$c->status(201);
}

Expand All @@ -130,7 +130,7 @@ sub get ($c) {
->with_sku
->single;

$c->res->headers->location('/layout/'.$layout->id);
$c->res_location('/layout/'.$layout->id);
$c->status(200, $layout);
}

Expand Down Expand Up @@ -237,7 +237,7 @@ sub update ($c) {
return $c->status(409, { error => 'rack_unit_start conflict' });
}

$c->res->headers->location('/layout/'.$layout->id);
$c->res_location('/layout/'.$layout->id);

$layout->set_columns($input);
return $c->status(204) if not $layout->is_changed;
Expand Down
4 changes: 2 additions & 2 deletions lib/Conch/Controller/RackRole.pm
Expand Up @@ -58,7 +58,7 @@ sub create ($c) {

my $rack_role = $c->db_rack_roles->create($input);
$c->log->debug('Created rack role '.$rack_role->id);
$c->res->headers->location('/rack_role/'.$rack_role->id);
$c->res_location('/rack_role/'.$rack_role->id);
$c->status(201);
}

Expand Down Expand Up @@ -125,7 +125,7 @@ sub update ($c) {
}
}

$c->res->headers->location('/rack_role/'.$rack_role->id);
$c->res_location('/rack_role/'.$rack_role->id);

$rack_role->set_columns($input);
return $c->status(204) if not $rack_role->is_changed;
Expand Down
2 changes: 1 addition & 1 deletion lib/Conch/Controller/Relay.pm
Expand Up @@ -42,7 +42,7 @@ sub register ($c) {
$c->status(204);
}

$c->res->headers->location($c->url_for('/relay/'.$relay->id));
$c->res_location('/relay/'.$relay->id);
return;
}

Expand Down
6 changes: 2 additions & 4 deletions lib/Conch/Controller/User.pm
Expand Up @@ -473,7 +473,7 @@ sub create ($c) {
);
}

$c->res->headers->location($c->url_for('/user/'.$user->id));
$c->res_location('/user/'.$user->id);
return $c->status(201, { map +($_ => $user->$_), qw(id email name) });
}

Expand Down Expand Up @@ -583,9 +583,7 @@ sub create_api_token ($c) {

$c->res->headers->last_modified(Mojo::Date->new($token->created->epoch));
$c->res->headers->expires(Mojo::Date->new($token->expires->epoch));
$c->res->headers->location($c->url_for('/user/'
.($user->id eq $c->stash('user_id') ? 'me' : $user->id)
.'/token/'.$input->{name}));
$c->res_location('/user/'.($user->id eq $c->stash('user_id') ? 'me' : $user->id).'/token/'.$input->{name});

my $token_data = $token->TO_JSON;
delete $token_data->{last_ipaddr};
Expand Down

0 comments on commit 1a080ec

Please sign in to comment.