Skip to content

Commit

Permalink
Add support for &default=1 (#371)
Browse files Browse the repository at this point in the history
If there is a problem loading the image, it will redirect to the
URL specified in `?url=` (if that's a valid URI as per RFC 3986).
  • Loading branch information
kleisauke committed Jun 30, 2023
1 parent a82438c commit adc6b9a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
19 changes: 12 additions & 7 deletions src/nginx/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ using ::weserv::api::utils::Status;

namespace weserv::nginx {

ngx_int_t ngx_weserv_return_error(ngx_http_request_t *r, Status status,
ngx_chain_t *out) {
ngx_int_t ngx_weserv_return_error(ngx_http_request_t *r,
ngx_weserv_upstream_ctx_t *upstream_ctx,
Status status, ngx_chain_t *out) {
ngx_uint_t http_status = status.http_code();

// Redirect if the 'default' (or 'errorredirect') query parameter is given.
Expand All @@ -21,12 +22,16 @@ ngx_int_t ngx_weserv_return_error(ngx_http_request_t *r, Status status,
if (ngx_http_arg(r, (u_char *)"default", 7, &redirect_uri) == NGX_OK ||
ngx_http_arg(r, (u_char *)"errorredirect", 13, &redirect_uri) ==
NGX_OK) {
ngx_str_t parsed_redirect;
if (parse_url(r->pool, redirect_uri, &parsed_redirect) == NGX_OK) {
if (set_location_header(r, &parsed_redirect) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_str_t parsed_redirect = ngx_null_string;
if (upstream_ctx != nullptr && redirect_uri.len == 1 &&
redirect_uri.data[0] == '1') {
parsed_redirect = upstream_ctx->request->url();
} else {
(void)parse_url(r->pool, redirect_uri, &parsed_redirect);
}

if (parsed_redirect.len > 0 &&
set_location_header(r, &parsed_redirect) == NGX_OK) {
http_status = NGX_HTTP_MOVED_TEMPORARILY;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/nginx/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ extern "C" {
#include <ngx_http.h>
}

#include "module.h"

#include <weserv/utils/status.h>

namespace weserv::nginx {

ngx_int_t ngx_weserv_return_error(ngx_http_request_t *r,
ngx_weserv_upstream_ctx_t *upstream_ctx,
api::utils::Status status, ngx_chain_t *out);

} // namespace weserv::nginx
5 changes: 3 additions & 2 deletions src/nginx/handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ngx_int_t ngx_weserv_request_handler(ngx_http_request_t *r) {
Status::ErrorCause::Application};

ngx_chain_t out;
if (ngx_weserv_return_error(r, status, &out) != NGX_OK) {
if (ngx_weserv_return_error(r, nullptr, status, &out) != NGX_OK) {
return NGX_ERROR;
}

Expand Down Expand Up @@ -82,7 +82,8 @@ ngx_int_t ngx_weserv_request_handler(ngx_http_request_t *r) {

if (rc == NGX_ERROR) {
ngx_chain_t out;
if (ngx_weserv_return_error(r, ctx->response_status, &out) != NGX_OK) {
if (ngx_weserv_return_error(r, ctx, ctx->response_status, &out) !=
NGX_OK) {
return NGX_ERROR;
}

Expand Down
6 changes: 4 additions & 2 deletions src/nginx/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,8 @@ ngx_int_t ngx_weserv_image_body_filter(ngx_http_request_t *r, ngx_chain_t *in) {
#endif
) {
ngx_chain_t out;
if (ngx_weserv_return_error(r, upstream_ctx->response_status,
if (ngx_weserv_return_error(r, upstream_ctx,
upstream_ctx->response_status,
&out) != NGX_OK) {
return NGX_ERROR;
}
Expand Down Expand Up @@ -886,7 +887,8 @@ ngx_int_t ngx_weserv_image_body_filter(ngx_http_request_t *r, ngx_chain_t *in) {

if (!status.ok()) {
ngx_chain_t error;
if (ngx_weserv_return_error(r, status, &error) != NGX_OK) {
if (ngx_weserv_return_error(r, upstream_ctx, status, &error) !=
NGX_OK) {
return NGX_ERROR;
}

Expand Down

0 comments on commit adc6b9a

Please sign in to comment.