Subject: | Custom headers ignored with http status 401 [patch] |
Hi,
in a module I implemented http basic authentication and during the process I return http 401 along with a WWW-Authenticate header. The HTTP::Response is_success() method does return false in this case. If is_success() is false, Brick doesn't use the response object but instead uses send_error(). The WWW-Authenticate header then gets lost.
In the attached patch I added another parameter to ::_send_error(), the response object. ::_send_error() will use it with send_response() if present. It's only a quick workaround, maybe it could be solved in a somewhat smarter way...
- Tom
Subject: | 401.patch |
*** ~Brick.pm 2014-10-15 14:00:40.680299785 +0200
--- Brick.pm 2014-10-14 23:57:18.000000000 +0200
***************
*** 414,427 ****
# default mime type to text/html
$res->header( 'Content-Type' ) || $res->header( 'Content-Type', 'text/html' );
!
if ($res->is_success) {
$conn->send_response( $res );
$self->_log( access => "[$code] $match->{full_path}" );
} elsif ($res->is_error) {
# TODO: should allow a way to specify custom error content
! $self->_send_error( $conn, $req, $res->code, $res->message );
} elsif ($res->is_redirect) {
if (UNIVERSAL::can($res->{target_uri}, 'path')) {
--- 414,427 ----
# default mime type to text/html
$res->header( 'Content-Type' ) || $res->header( 'Content-Type', 'text/html' );
!
if ($res->is_success) {
$conn->send_response( $res );
$self->_log( access => "[$code] $match->{full_path}" );
} elsif ($res->is_error) {
# TODO: should allow a way to specify custom error content
! $self->_send_error( $conn, $req, $res->code, $res->message, $res );
} elsif ($res->is_redirect) {
if (UNIVERSAL::can($res->{target_uri}, 'path')) {
***************
*** 482,490 ****
}
sub _send_error {
! my ($self, $conn, $req, $code, $text) = @_;
! $conn->send_error($code, $text);
$self->_log_status($req, $code, $text);
}
--- 482,496 ----
}
sub _send_error {
! my ($self, $conn, $req, $code, $text, $res) = @_;
! if ($res) {
! $res->add_content($res->message);
! $conn->send_response( $res );
! }
! else {
! $conn->send_error($code, $text);
! }
$self->_log_status($req, $code, $text);
}