blob: 2a61c03b20086f6fc92f8e7aa37a57477c1fc325 [file] [log] [blame]
From 0459a285ca0159d45e73da8bd1164edb5c57cde3 Mon Sep 17 00:00:00 2001
From: Andrei Belov <defanator@gmail.com>
Date: Wed, 22 Mar 2017 07:50:57 +0300
Subject: [PATCH] feature: nginx 1.11.11+ can now build with this module.
Note: nginx 1.11.11+ are still not an officially supported target yet.
More work needed.
Closes openresty/lua-nginx-module#1016
See also:
http://hg.nginx.org/nginx/rev/e662cbf1b932
---
src/ngx_http_lua_common.h | 6 ++++
src/ngx_http_lua_headers.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++
src/ngx_http_lua_headers.h | 3 ++
src/ngx_http_lua_module.c | 13 ++++++++-
4 files changed, 89 insertions(+), 1 deletion(-)
diff --git a/src/ngx_http_lua_common.h b/src/ngx_http_lua_common.h
index 079a4dc..f37d776 100644
--- a/src/ngx_http_lua_common.h
+++ b/src/ngx_http_lua_common.h
@@ -199,6 +199,12 @@ struct ngx_http_lua_main_conf_s {
of reqeusts */
ngx_uint_t malloc_trim_req_count;
+#if nginx_version >= 1011011
+ /* the following 2 fields are only used by ngx.req.raw_headers() for now */
+ ngx_buf_t **busy_buf_ptrs;
+ ngx_int_t busy_buf_ptr_count;
+#endif
+
unsigned requires_header_filter:1;
unsigned requires_body_filter:1;
unsigned requires_capture_filter:1;
diff --git a/src/ngx_http_lua_headers.c b/src/ngx_http_lua_headers.c
index 2392598..6700ce8 100644
--- a/src/ngx_http_lua_headers.c
+++ b/src/ngx_http_lua_headers.c
@@ -26,6 +26,9 @@ static int ngx_http_lua_ngx_req_get_headers(lua_State *L);
static int ngx_http_lua_ngx_req_header_clear(lua_State *L);
static int ngx_http_lua_ngx_req_header_set(lua_State *L);
static int ngx_http_lua_ngx_resp_get_headers(lua_State *L);
+#if nginx_version >= 1011011
+void ngx_http_lua_ngx_raw_header_cleanup(void *data);
+#endif
static int
@@ -77,6 +80,11 @@ ngx_http_lua_ngx_req_raw_header(lua_State *L)
size_t size;
ngx_buf_t *b, *first = NULL;
ngx_int_t i, j;
+#if nginx_version >= 1011011
+ ngx_buf_t **bb;
+ ngx_chain_t *cl;
+ ngx_http_lua_main_conf_t *lmcf;
+#endif
ngx_connection_t *c;
ngx_http_request_t *r, *mr;
ngx_http_connection_t *hc;
@@ -93,6 +101,10 @@ ngx_http_lua_ngx_req_raw_header(lua_State *L)
return luaL_error(L, "no request object found");
}
+#if nginx_version >= 1011011
+ lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
+#endif
+
ngx_http_lua_check_fake_request(L, r);
mr = r->main;
@@ -109,8 +121,13 @@ ngx_http_lua_ngx_req_raw_header(lua_State *L)
dd("hc->nbusy: %d", (int) hc->nbusy);
if (hc->nbusy) {
+#if nginx_version >= 1011011
+ dd("hc->busy: %p %p %p %p", hc->busy->buf->start, hc->busy->buf->pos,
+ hc->busy->buf->last, hc->busy->buf->end);
+#else
dd("hc->busy: %p %p %p %p", hc->busy[0]->start, hc->busy[0]->pos,
hc->busy[0]->last, hc->busy[0]->end);
+#endif
}
dd("request line: %p %p", mr->request_line.data,
@@ -146,9 +163,37 @@ ngx_http_lua_ngx_req_raw_header(lua_State *L)
dd("size: %d", (int) size);
if (hc->nbusy) {
+#if nginx_version >= 1011011
+ if (hc->nbusy > lmcf->busy_buf_ptr_count) {
+ if (lmcf->busy_buf_ptrs) {
+ ngx_free(lmcf->busy_buf_ptrs);
+ }
+
+ lmcf->busy_buf_ptrs = ngx_alloc(hc->nbusy * sizeof(ngx_buf_t *),
+ r->connection->log);
+
+ if (lmcf->busy_buf_ptrs == NULL) {
+ return luaL_error(L, "no memory");
+ }
+
+ lmcf->busy_buf_ptr_count = hc->nbusy;
+ }
+
+ bb = lmcf->busy_buf_ptrs;
+ for (cl = hc->busy; cl; cl = cl->next) {
+ *bb++ = cl->buf;
+ }
+#endif
b = NULL;
+
+#if nginx_version >= 1011011
+ bb = lmcf->busy_buf_ptrs;
+ for (i = hc->nbusy; i > 0; i--) {
+ b = bb[i - 1];
+#else
for (i = 0; i < hc->nbusy; i++) {
b = hc->busy[i];
+#endif
dd("busy buf: %d: [%.*s]", (int) i, (int) (b->pos - b->start),
b->start);
@@ -223,8 +268,15 @@ ngx_http_lua_ngx_req_raw_header(lua_State *L)
}
if (hc->nbusy) {
+
+#if nginx_version >= 1011011
+ bb = lmcf->busy_buf_ptrs;
+ for (i = hc->nbusy - 1; i >= 0; i--) {
+ b = bb[i];
+#else
for (i = 0; i < hc->nbusy; i++) {
b = hc->busy[i];
+#endif
if (!found) {
if (b != first) {
@@ -1431,4 +1483,20 @@ ngx_http_lua_ffi_get_resp_header(ngx_http_request_t *r,
#endif /* NGX_LUA_NO_FFI_API */
+#if nginx_version >= 1011011
+void
+ngx_http_lua_ngx_raw_header_cleanup(void *data)
+{
+ ngx_http_lua_main_conf_t *lmcf;
+
+ lmcf = (ngx_http_lua_main_conf_t *) data;
+
+ if (lmcf->busy_buf_ptrs) {
+ ngx_free(lmcf->busy_buf_ptrs);
+ lmcf->busy_buf_ptrs = NULL;
+ }
+}
+#endif
+
+
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
diff --git a/src/ngx_http_lua_headers.h b/src/ngx_http_lua_headers.h
index 39f1114..ee4d21c 100644
--- a/src/ngx_http_lua_headers.h
+++ b/src/ngx_http_lua_headers.h
@@ -15,6 +15,9 @@
void ngx_http_lua_inject_resp_header_api(lua_State *L);
void ngx_http_lua_inject_req_header_api(lua_State *L);
void ngx_http_lua_create_headers_metatable(ngx_log_t *log, lua_State *L);
+#if nginx_version >= 1011011
+void ngx_http_lua_ngx_raw_header_cleanup(void *data);
+#endif
#endif /* _NGX_HTTP_LUA_HEADERS_H_INCLUDED_ */
diff --git a/src/ngx_http_lua_module.c b/src/ngx_http_lua_module.c
index 3dc2817..875f933 100644
--- a/src/ngx_http_lua_module.c
+++ b/src/ngx_http_lua_module.c
@@ -28,6 +28,7 @@
#include "ngx_http_lua_ssl_certby.h"
#include "ngx_http_lua_ssl_session_storeby.h"
#include "ngx_http_lua_ssl_session_fetchby.h"
+#include "ngx_http_lua_headers.h"
static void *ngx_http_lua_create_main_conf(ngx_conf_t *cf);
@@ -624,7 +625,7 @@ ngx_http_lua_init(ngx_conf_t *cf)
volatile ngx_cycle_t *saved_cycle;
ngx_http_core_main_conf_t *cmcf;
ngx_http_lua_main_conf_t *lmcf;
-#ifndef NGX_LUA_NO_FFI_API
+#if !defined(NGX_LUA_NO_FFI_API) || nginx_version >= 1011011
ngx_pool_cleanup_t *cln;
#endif
@@ -716,6 +717,16 @@ ngx_http_lua_init(ngx_conf_t *cf)
cln->handler = ngx_http_lua_sema_mm_cleanup;
#endif
+#if nginx_version >= 1011011
+ cln = ngx_pool_cleanup_add(cf->pool, 0);
+ if (cln == NULL) {
+ return NGX_ERROR;
+ }
+
+ cln->data = lmcf;
+ cln->handler = ngx_http_lua_ngx_raw_header_cleanup;
+#endif
+
if (lmcf->lua == NULL) {
dd("initializing lua vm");