Skip to content

Commit

Permalink
Implemented Nginx module
Browse files Browse the repository at this point in the history
  • Loading branch information
loentar committed Jul 22, 2016
1 parent 0e2e797 commit 6268b52
Show file tree
Hide file tree
Showing 10 changed files with 458 additions and 30 deletions.
7 changes: 7 additions & 0 deletions modules/CMakeLists.txt
Expand Up @@ -4,3 +4,10 @@ find_program (APXS_PATH apxs)
if (APXS_PATH)
add_subdirectory(apache2)
endif()

if ("$ENV{NGINX_PATH}" STREQUAL "")
message(Skipping Nginx module compilation: no Nginx source path provided)
else()
set(NGINX_SRC_PATH "$ENV{NGINX_PATH}")
add_subdirectory(nginx)
endif()
6 changes: 2 additions & 4 deletions modules/apache2/CMakeLists.txt
Expand Up @@ -14,12 +14,10 @@ STRING(REGEX REPLACE "\n" "" APXS_LDFLAGS ${APXS_LDFLAGS})
set(CMAKE_C_FLAGS "${APXS_CFLAGS} ${CMAKE_C_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${APXS_LDFLAGS} ${CMAKE_SHARED_LINKER_FLAGS}")

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

set (PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)

FILE(GLOB NGREST_APACHE2_MOD_SOURCES ${PROJECT_SOURCE_DIR}/*.c ${PROJECT_SOURCE_DIR}/*.cpp)
FILE(GLOB NGREST_APACHE2_MOD_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c ${CMAKE_CURRENT_SOURCE_DIR}/../share/*.cpp)

add_library(ngrest_apache2_mod SHARED ${NGREST_APACHE2_MOD_SOURCES})

Expand Down
32 changes: 22 additions & 10 deletions modules/apache2/src/mod_ngrest.c
Expand Up @@ -26,13 +26,13 @@
#include <http_protocol.h>
#include <http_log.h>

#include "ngrest_server.h"
#include "../../share/ngrest_server.h"


static const char* ngrestServicesPath = NULL;
static int initialized = 0;

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

#define INT_STR_LEN 16

const char* mod_ngrest_iterate_request_headers(void* req, void* data, iterate_request_headers_callback callback)
static void mod_ngrest_iterate_request_headers(void* req, void* data, iterate_request_headers_callback callback)
{
apr_table_do(callback, data, ((request_rec*)req)->headers_in, NULL);
}

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

void mod_ngrest_set_response_header(void* req, const char* header, const char* value)
static void mod_ngrest_set_response_header(void* req, const char* header, const char* value)
{
apr_table_set(((request_rec*)req)->headers_out, header, value);
}

void mod_ngrest_set_content_type(void* req, const char* contentType)
static void mod_ngrest_set_content_type(void* req, const char* contentType)
{
((request_rec*)req)->content_type = contentType;
}

int64_t mod_ngrest_write_client_callback(void* req, const void* buffer, int64_t size)
static void mod_ngrest_set_content_length(void* req, int64_t contentLength)
{
// apache2 ignores setting of Content-Length header
}

static int64_t mod_ngrest_write_client_callback(void* req, const void* buffer, int64_t size)
{
LOG1("response size: %ld", size);
return ap_rwrite(buffer, size, req);
}

static void mod_ngrest_finalize_request(void* req, int status)
{
((request_rec*)req)->status = status;
}

static struct ngrest_mod_callbacks mod_ngrest_server_callbacks = {
mod_ngrest_iterate_request_headers,
mod_ngrest_read_client_callback,
mod_ngrest_set_response_header,
mod_ngrest_set_content_type,
mod_ngrest_write_client_callback
mod_ngrest_set_content_length,
mod_ngrest_write_client_callback,
mod_ngrest_finalize_request
};

/* content handler */
Expand Down Expand Up @@ -137,7 +149,7 @@ static int mod_ngrest_handler(request_rec* req)
req->remaining
};

req->status = ngrest_server_dispatch(&request, mod_ngrest_server_callbacks);
ngrest_server_dispatch(&request, mod_ngrest_server_callbacks);
LOGLABEL;
}

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

static void ngrest_register_hooks(apr_pool_t* pool)
{
#ifdef NGREST_APACHE2_MOD_DEBUG
#ifdef NGREST_MOD_DEBUG
char logPath[PATH_MAX];
snprintf(logPath, PATH_MAX, "/tmp/ngrest_debug_%d.log", getpid());
logFile = fopen(logPath, "a");
Expand Down
24 changes: 24 additions & 0 deletions modules/nginx/CMakeLists.txt
@@ -0,0 +1,24 @@
project (ngrest_nginx_mod CXX)

#set(CMAKE_C_FLAGS "${APXS_CFLAGS} ${CMAKE_C_FLAGS}")
#set(CMAKE_SHARED_LINKER_FLAGS "${APXS_LDFLAGS} ${CMAKE_SHARED_LINKER_FLAGS}")

# remove -Wl,--no-undefined to allow ngx_* symbols be imported from nginx binary
STRING(REGEX REPLACE "-Wl,--no-undefined" "" CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS})

include_directories("${NGINX_SRC_PATH}/src/core" "${NGINX_SRC_PATH}/src/http" "${NGINX_SRC_PATH}/src/http/v2"
"${NGINX_SRC_PATH}/src/http/modules" "${NGINX_SRC_PATH}/src/event" "${NGINX_SRC_PATH}/src/os/unix"
"${NGINX_SRC_PATH}/objs")

FILE(GLOB NGREST_NGINX_MOD_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c ${CMAKE_CURRENT_SOURCE_DIR}/../share/*.cpp)

add_library(ngrest_nginx_mod SHARED ${NGREST_NGINX_MOD_SOURCES})

set_target_properties(ngrest_nginx_mod PROPERTIES PREFIX "")
set_target_properties(ngrest_nginx_mod PROPERTIES OUTPUT_NAME "mod_ngrest")
set_target_properties(ngrest_nginx_mod PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SHARE_DIR}/modules/nginx"
LIBRARY_OUTPUT_DIRECTORY "${PROJECT_SHARE_DIR}/modules/nginx"
)

target_link_libraries(ngrest_nginx_mod ngrestutils ngrestcommon ngrestjson ngrestengine)

0 comments on commit 6268b52

Please sign in to comment.