Skip to content

Commit cf474bc

Browse files
committed
refactor: improved status code handling in content/access/rewrite_by_lua*.
1 parent 213acb9 commit cf474bc

File tree

3 files changed

+28
-52
lines changed

3 files changed

+28
-52
lines changed

src/ngx_http_lua_accessby.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ ngx_http_lua_access_handler(ngx_http_request_t *r)
9898
rc = ctx->resume_handler(r);
9999
dd("wev handler returns %d", (int) rc);
100100

101-
return rc;
101+
if (rc == NGX_ERROR || rc == NGX_DONE || rc >= NGX_OK) {
102+
return rc;
103+
}
104+
105+
return NGX_DECLINED;
102106
}
103107

104108
if (llcf->force_read_body && !ctx->read_body_done) {
@@ -279,7 +283,7 @@ ngx_http_lua_access_by_chunk(lua_State *L, ngx_http_request_t *r)
279283

280284
dd("returned %d", (int) rc);
281285

282-
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
286+
if (rc == NGX_ERROR || rc >= NGX_OK) {
283287
return rc;
284288
}
285289

@@ -292,14 +296,6 @@ ngx_http_lua_access_by_chunk(lua_State *L, ngx_http_request_t *r)
292296
return NGX_DONE;
293297
}
294298

295-
if (rc >= NGX_HTTP_OK && rc < NGX_HTTP_SPECIAL_RESPONSE) {
296-
return rc;
297-
}
298-
299-
if (rc == NGX_OK) {
300-
return NGX_OK;
301-
}
302-
303299
return NGX_DECLINED;
304300
}
305301

src/ngx_http_lua_contentby.c

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ngx_int_t
1919
ngx_http_lua_content_by_chunk(lua_State *L, ngx_http_request_t *r)
2020
{
2121
int co_ref;
22+
ngx_int_t rc;
2223
lua_State *co;
2324
ngx_http_lua_ctx_t *ctx;
2425
ngx_http_cleanup_t *cln;
@@ -82,7 +83,24 @@ ngx_http_lua_content_by_chunk(lua_State *L, ngx_http_request_t *r)
8283

8384
ctx->context = NGX_HTTP_LUA_CONTEXT_CONTENT;
8485

85-
return ngx_http_lua_run_thread(L, r, ctx, 0);
86+
rc = ngx_http_lua_run_thread(L, r, ctx, 0);
87+
88+
if (rc == NGX_ERROR || rc >= NGX_OK) {
89+
return rc;
90+
}
91+
92+
if (rc == NGX_DONE) {
93+
return NGX_DONE;
94+
}
95+
96+
if (rc == NGX_AGAIN) {
97+
#if defined(nginx_version) && nginx_version >= 8011
98+
r->main->count++;
99+
#endif
100+
return NGX_DONE;
101+
}
102+
103+
return NGX_OK;
86104
}
87105

88106

@@ -236,24 +254,7 @@ ngx_http_lua_content_handler_file(ngx_http_request_t *r)
236254
/* make sure we have a valid code chunk */
237255
assert(lua_isfunction(L, -1));
238256

239-
rc = ngx_http_lua_content_by_chunk(L, r);
240-
241-
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
242-
return rc;
243-
}
244-
245-
if (rc == NGX_DONE) {
246-
return NGX_DONE;
247-
}
248-
249-
if (rc == NGX_AGAIN) {
250-
#if defined(nginx_version) && nginx_version >= 8011
251-
r->main->count++;
252-
#endif
253-
return NGX_DONE;
254-
}
255-
256-
return NGX_OK;
257+
return ngx_http_lua_content_by_chunk(L, r);
257258
}
258259

259260

@@ -287,23 +288,6 @@ ngx_http_lua_content_handler_inline(ngx_http_request_t *r)
287288
return NGX_HTTP_INTERNAL_SERVER_ERROR;
288289
}
289290

290-
rc = ngx_http_lua_content_by_chunk(L, r);
291-
292-
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
293-
return rc;
294-
}
295-
296-
if (rc == NGX_DONE) {
297-
return NGX_DONE;
298-
}
299-
300-
if (rc == NGX_AGAIN) {
301-
#if defined(nginx_version) && nginx_version >= 8011
302-
r->main->count++;
303-
#endif
304-
return NGX_DONE;
305-
}
306-
307-
return NGX_OK;
291+
return ngx_http_lua_content_by_chunk(L, r);
308292
}
309293

src/ngx_http_lua_rewriteby.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ ngx_http_lua_rewrite_by_chunk(lua_State *L, ngx_http_request_t *r)
280280

281281
rc = ngx_http_lua_run_thread(L, r, ctx, 0);
282282

283-
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
283+
if (rc == NGX_ERROR || rc > NGX_OK) {
284284
return rc;
285285
}
286286

@@ -293,10 +293,6 @@ ngx_http_lua_rewrite_by_chunk(lua_State *L, ngx_http_request_t *r)
293293
return NGX_DONE;
294294
}
295295

296-
if (rc >= NGX_HTTP_OK && rc < NGX_HTTP_SPECIAL_RESPONSE) {
297-
return rc;
298-
}
299-
300296
return NGX_DECLINED;
301297
}
302298

0 commit comments

Comments
 (0)