Permalink
Browse files

feature: added new directive echo_status which can be used to specify…

… a different default response status code other than 200. thanks Maxime Corbeau for requesting this.
  • Loading branch information...
1 parent 349c193 commit 6d5066d3f18018e59744f640be34b953674c2b71 @agentzh agentzh committed Jun 24, 2012
Showing with 159 additions and 3 deletions.
  1. +11 −0 src/ngx_http_echo_module.c
  2. +1 −0 src/ngx_http_echo_module.h
  3. +5 −3 src/ngx_http_echo_util.c
  4. +142 −0 t/status.t
View
@@ -200,6 +200,13 @@ static ngx_command_t ngx_http_echo_commands[] = {
offsetof(ngx_http_echo_loc_conf_t, handler_cmds),
NULL },
+ { ngx_string("echo_status"),
+ NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
+ ngx_conf_set_num_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_echo_loc_conf_t, status),
+ NULL },
+
ngx_null_command
};
@@ -238,6 +245,8 @@ ngx_http_echo_create_loc_conf(ngx_conf_t *cf)
* conf->seen_trailing_output = 0
*/
+ conf->status = NGX_CONF_UNSET;
+
return conf;
}
@@ -261,6 +270,8 @@ ngx_http_echo_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
conf->after_body_cmds = prev->after_body_cmds;
}
+ ngx_conf_merge_value(conf->status, prev->status, 200);
+
return NGX_CONF_OK;
}
@@ -82,6 +82,7 @@ typedef struct {
unsigned seen_leading_output;
+ ngx_int_t status;
} ngx_http_echo_loc_conf_t;
View
@@ -130,10 +130,12 @@ ngx_int_t
ngx_http_echo_send_header_if_needed(ngx_http_request_t* r,
ngx_http_echo_ctx_t *ctx)
{
- /* ngx_int_t rc; */
+ ngx_http_echo_loc_conf_t *elcf;
- if ( ! ctx->headers_sent ) {
- r->headers_out.status = NGX_HTTP_OK;
+ if (!ctx->headers_sent) {
+ elcf = ngx_http_get_module_loc_conf(r, ngx_http_echo_module);
+
+ r->headers_out.status = (ngx_uint_t) elcf->status;
if (ngx_http_set_content_type(r) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
View
@@ -0,0 +1,142 @@
+# vi:filetype=
+
+use lib 'lib';
+use Test::Nginx::Socket;
+
+repeat_each(1);
+
+plan tests => repeat_each() * (2 * blocks());
+
+#$Test::Nginx::LWP::LogLevel = 'debug';
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: 200
+--- config
+ location /echo {
+ echo_status 200;
+ echo hello;
+ }
+--- request
+ GET /echo
+--- response_body
+hello
+--- error_code: 200
+
+
+
+=== TEST 2: if location (200)
+--- config
+ location /echo {
+ set $true 1;
+ if ($true) {
+ }
+ echo_status 200;
+ echo hello;
+ }
+--- request
+ GET /echo
+--- response_body
+hello
+--- error_code: 200
+
+
+
+=== TEST 3: 404
+--- config
+ location /echo {
+ echo_status 404;
+ echo hello;
+ }
+--- request
+ GET /echo
+--- response_body
+hello
+--- error_code: 404
+
+
+
+=== TEST 4: if location (404)
+--- config
+ location /echo {
+ set $true 1;
+ if ($true) {
+ }
+ echo_status 404;
+ echo hello;
+ }
+--- request
+ GET /echo
+--- response_body
+hello
+--- error_code: 404
+
+
+
+=== TEST 5: 500
+--- config
+ location /echo {
+ echo_status 500;
+ echo hello;
+ }
+--- request
+ GET /echo
+--- response_body
+hello
+--- error_code: 500
+
+
+
+=== TEST 6: if location (500)
+--- config
+ location /echo {
+ set $true 1;
+ if ($true) {
+ }
+ echo_status 500;
+ echo hello;
+ }
+--- request
+ GET /echo
+--- response_body
+hello
+--- error_code: 500
+
+
+
+=== TEST 7: if location (500) no inherit
+--- config
+ location /echo {
+ set $true 1;
+ if ($true) {
+ echo_status 503;
+ }
+ echo_status 500;
+ echo hello;
+ }
+--- request
+ GET /echo
+--- response_body
+hello
+--- error_code: 503
+
+
+
+=== TEST 8: subrequest
+--- config
+ location /echo {
+ echo_location /sub;
+ echo_status 503;
+ }
+
+ location /sub {
+ echo blah blah;
+ }
+--- request
+ GET /echo
+--- response_body
+blah blah
+--- error_code: 503
+

0 comments on commit 6d5066d

Please sign in to comment.