Skip to content

Commit 6268b52

Browse files
committed
Implemented Nginx module
1 parent 0e2e797 commit 6268b52

10 files changed

+458
-30
lines changed

modules/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@ find_program (APXS_PATH apxs)
44
if (APXS_PATH)
55
add_subdirectory(apache2)
66
endif()
7+
8+
if ("$ENV{NGINX_PATH}" STREQUAL "")
9+
message(Skipping Nginx module compilation: no Nginx source path provided)
10+
else()
11+
set(NGINX_SRC_PATH "$ENV{NGINX_PATH}")
12+
add_subdirectory(nginx)
13+
endif()

modules/apache2/CMakeLists.txt

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ STRING(REGEX REPLACE "\n" "" APXS_LDFLAGS ${APXS_LDFLAGS})
1414
set(CMAKE_C_FLAGS "${APXS_CFLAGS} ${CMAKE_C_FLAGS}")
1515
set(CMAKE_SHARED_LINKER_FLAGS "${APXS_LDFLAGS} ${CMAKE_SHARED_LINKER_FLAGS}")
1616

17-
# remove -Wl,--no-undefined to allow ap_* sumbols be undefined
17+
# remove -Wl,--no-undefined to allow ap_* symbols be imported from httpd binary
1818
STRING(REGEX REPLACE "-Wl,--no-undefined" "" CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS})
1919

20-
set (PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
21-
22-
FILE(GLOB NGREST_APACHE2_MOD_SOURCES ${PROJECT_SOURCE_DIR}/*.c ${PROJECT_SOURCE_DIR}/*.cpp)
20+
FILE(GLOB NGREST_APACHE2_MOD_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c ${CMAKE_CURRENT_SOURCE_DIR}/../share/*.cpp)
2321

2422
add_library(ngrest_apache2_mod SHARED ${NGREST_APACHE2_MOD_SOURCES})
2523

modules/apache2/src/mod_ngrest.c

+22-10
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
#include <http_protocol.h>
2727
#include <http_log.h>
2828

29-
#include "ngrest_server.h"
29+
#include "../../share/ngrest_server.h"
3030

3131

3232
static const char* ngrestServicesPath = NULL;
3333
static int initialized = 0;
3434

35-
#ifdef NGREST_APACHE2_MOD_DEBUG
35+
#ifdef NGREST_MOD_DEBUG
3636
FILE* logFile = NULL;
3737
#define LOGLABEL fprintf(logFile, "p%d t%d; %s[%d]: %s\n", \
3838
getpid(), (int)pthread_self(), __FILE__, __LINE__, __FUNCTION__); fflush(logFile);
@@ -52,12 +52,12 @@ FILE* logFile = NULL;
5252

5353
#define INT_STR_LEN 16
5454

55-
const char* mod_ngrest_iterate_request_headers(void* req, void* data, iterate_request_headers_callback callback)
55+
static void mod_ngrest_iterate_request_headers(void* req, void* data, iterate_request_headers_callback callback)
5656
{
5757
apr_table_do(callback, data, ((request_rec*)req)->headers_in, NULL);
5858
}
5959

60-
int64_t mod_ngrest_read_client_callback(void* req, char* buffer, int64_t size)
60+
static int64_t mod_ngrest_read_client_callback(void* req, char* buffer, int64_t size)
6161
{
6262
int64_t res = ap_get_client_block(req, buffer, size);
6363
LOG2("REMAINING: %ld / %ld", ((request_rec*)req)->remaining, ((request_rec*)req)->read_length);
@@ -66,28 +66,40 @@ int64_t mod_ngrest_read_client_callback(void* req, char* buffer, int64_t size)
6666
return res;
6767
}
6868

69-
void mod_ngrest_set_response_header(void* req, const char* header, const char* value)
69+
static void mod_ngrest_set_response_header(void* req, const char* header, const char* value)
7070
{
7171
apr_table_set(((request_rec*)req)->headers_out, header, value);
7272
}
7373

74-
void mod_ngrest_set_content_type(void* req, const char* contentType)
74+
static void mod_ngrest_set_content_type(void* req, const char* contentType)
7575
{
7676
((request_rec*)req)->content_type = contentType;
7777
}
7878

79-
int64_t mod_ngrest_write_client_callback(void* req, const void* buffer, int64_t size)
79+
static void mod_ngrest_set_content_length(void* req, int64_t contentLength)
80+
{
81+
// apache2 ignores setting of Content-Length header
82+
}
83+
84+
static int64_t mod_ngrest_write_client_callback(void* req, const void* buffer, int64_t size)
8085
{
8186
LOG1("response size: %ld", size);
8287
return ap_rwrite(buffer, size, req);
8388
}
8489

90+
static void mod_ngrest_finalize_request(void* req, int status)
91+
{
92+
((request_rec*)req)->status = status;
93+
}
94+
8595
static struct ngrest_mod_callbacks mod_ngrest_server_callbacks = {
8696
mod_ngrest_iterate_request_headers,
8797
mod_ngrest_read_client_callback,
8898
mod_ngrest_set_response_header,
8999
mod_ngrest_set_content_type,
90-
mod_ngrest_write_client_callback
100+
mod_ngrest_set_content_length,
101+
mod_ngrest_write_client_callback,
102+
mod_ngrest_finalize_request
91103
};
92104

93105
/* content handler */
@@ -137,7 +149,7 @@ static int mod_ngrest_handler(request_rec* req)
137149
req->remaining
138150
};
139151

140-
req->status = ngrest_server_dispatch(&request, mod_ngrest_server_callbacks);
152+
ngrest_server_dispatch(&request, mod_ngrest_server_callbacks);
141153
LOGLABEL;
142154
}
143155

@@ -158,7 +170,7 @@ static apr_status_t mod_ngrest_shutdown(void* data)
158170

159171
static void ngrest_register_hooks(apr_pool_t* pool)
160172
{
161-
#ifdef NGREST_APACHE2_MOD_DEBUG
173+
#ifdef NGREST_MOD_DEBUG
162174
char logPath[PATH_MAX];
163175
snprintf(logPath, PATH_MAX, "/tmp/ngrest_debug_%d.log", getpid());
164176
logFile = fopen(logPath, "a");

modules/nginx/CMakeLists.txt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
project (ngrest_nginx_mod CXX)
2+
3+
#set(CMAKE_C_FLAGS "${APXS_CFLAGS} ${CMAKE_C_FLAGS}")
4+
#set(CMAKE_SHARED_LINKER_FLAGS "${APXS_LDFLAGS} ${CMAKE_SHARED_LINKER_FLAGS}")
5+
6+
# remove -Wl,--no-undefined to allow ngx_* symbols be imported from nginx binary
7+
STRING(REGEX REPLACE "-Wl,--no-undefined" "" CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS})
8+
9+
include_directories("${NGINX_SRC_PATH}/src/core" "${NGINX_SRC_PATH}/src/http" "${NGINX_SRC_PATH}/src/http/v2"
10+
"${NGINX_SRC_PATH}/src/http/modules" "${NGINX_SRC_PATH}/src/event" "${NGINX_SRC_PATH}/src/os/unix"
11+
"${NGINX_SRC_PATH}/objs")
12+
13+
FILE(GLOB NGREST_NGINX_MOD_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c ${CMAKE_CURRENT_SOURCE_DIR}/../share/*.cpp)
14+
15+
add_library(ngrest_nginx_mod SHARED ${NGREST_NGINX_MOD_SOURCES})
16+
17+
set_target_properties(ngrest_nginx_mod PROPERTIES PREFIX "")
18+
set_target_properties(ngrest_nginx_mod PROPERTIES OUTPUT_NAME "mod_ngrest")
19+
set_target_properties(ngrest_nginx_mod PROPERTIES
20+
RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SHARE_DIR}/modules/nginx"
21+
LIBRARY_OUTPUT_DIRECTORY "${PROJECT_SHARE_DIR}/modules/nginx"
22+
)
23+
24+
target_link_libraries(ngrest_nginx_mod ngrestutils ngrestcommon ngrestjson ngrestengine)

0 commit comments

Comments
 (0)