From f7006a18ea4332cd378f5a809a3e94a57dd23c8b Mon Sep 17 00:00:00 2001 From: simon987 Date: Sun, 1 Mar 2020 10:58:25 -0500 Subject: [PATCH] Initial commit (hello world) --- .gitignore | 8 ++++ CMakeLists.txt | 21 +++++++++ README.md | 6 +++ build.sh | 14 ++++++ config | 11 +++++ library.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 181 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 README.md create mode 100644 build.sh create mode 100644 config create mode 100644 library.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0db7952 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +cmake_install.cmake +*.cbp +CMakeFiles +cmake-build-debug +CMakeCache.txt +*.so +Makefile +.idea/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..832940d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.7) +project(nginx_js C) + +set(CMAKE_C_STANDARD 11) + + +add_library(nginx_js SHARED library.c library.h) + +target_include_directories( + nginx_js + PUBLIC + /home/simon/Downloads/nginx-1.16.1/objs + /home/simon/Downloads/nginx-1.16.1/src + /home/simon/Downloads/nginx-1.16.1/src/core + /home/simon/Downloads/nginx-1.16.1/src/event + /home/simon/Downloads/nginx-1.16.1/src/http + /home/simon/Downloads/nginx-1.16.1/src/http/modules + /home/simon/Downloads/nginx-1.16.1/src/misc + /home/simon/Downloads/nginx-1.16.1/src/os/unix + /home/simon/Downloads/nginx-1.16.1/src/stream +) diff --git a/README.md b/README.md new file mode 100644 index 0000000..ff84b55 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ + +//todo + +```bash +apt install libperl-dev libgeoip-dev libgd-dev libxslt1-dev +``` diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..0597360 --- /dev/null +++ b/build.sh @@ -0,0 +1,14 @@ +NGINX_PATH=/home/simon/Downloads/nginx-1.16.1/ + +MODULE_PATH=$(pwd)/ +CONFIG_ARGS=$(nginx -V 2>&1 | tail -n 1 | cut -c 21- | sed 's/--add-dynamic-module=.*//g') + +CONFIG_ARGS="${CONFIG_ARGS} --add-dynamic-module=${MODULE_PATH}" + +echo $CONFIG_ARGS + +( +cd ${NGINX_PATH} +bash -c "./configure ${CONFIG_ARGS}" +make modules +) diff --git a/config b/config new file mode 100644 index 0000000..37f5e68 --- /dev/null +++ b/config @@ -0,0 +1,11 @@ +ngx_addon_name=ngx_http_hello_world_module + +if test -n "$ngx_module_link"; then + ngx_module_type=HTTP + ngx_module_name=ngx_http_hello_world_module + ngx_module_srcs="$ngx_addon_dir/library.c" + . auto/module +else + HTTP_MODULES="$HTTP_MODULES ngx_http_hello_world_module" + NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/library.c" +fi \ No newline at end of file diff --git a/library.c b/library.c new file mode 100644 index 0000000..246ac00 --- /dev/null +++ b/library.c @@ -0,0 +1,121 @@ +#include +#include "ngx_http.c" + +#define HELLO_WORLD "hello world\r\n" + +static char *ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); +static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r); + +/** + * This module provided directive: hello world. + * + */ +static ngx_command_t ngx_http_hello_world_commands[] = { + + { ngx_string("hello_world"), /* directive */ + NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, /* location context and takes + no arguments*/ + ngx_http_hello_world, /* configuration setup function */ + 0, /* No offset. Only one context is supported. */ + 0, /* No offset when storing the module configuration on struct. */ + NULL}, + + ngx_null_command /* command termination */ +}; + +/* The hello world string. */ +static u_char ngx_hello_world[] = HELLO_WORLD; + +/* The module context. */ +static ngx_http_module_t ngx_http_hello_world_module_ctx = { + NULL, /* preconfiguration */ + NULL, /* postconfiguration */ + + NULL, /* create main configuration */ + NULL, /* init main configuration */ + + NULL, /* create server configuration */ + NULL, /* merge server configuration */ + + NULL, /* create location configuration */ + NULL /* merge location configuration */ +}; + +/* Module definition. */ +ngx_module_t ngx_http_hello_world_module = { + NGX_MODULE_V1, + &ngx_http_hello_world_module_ctx, /* module context */ + ngx_http_hello_world_commands, /* module directives */ + NGX_HTTP_MODULE, /* module type */ + NULL, /* init master */ + NULL, /* init module */ + NULL, /* init process */ + NULL, /* init thread */ + NULL, /* exit thread */ + NULL, /* exit process */ + NULL, /* exit master */ + NGX_MODULE_V1_PADDING +}; + +/** + * Content handler. + * + * @param r + * Pointer to the request structure. See http_request.h. + * @return + * The status of the response generation. + */ +static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r) +{ + ngx_buf_t *b; + ngx_chain_t out; + + /* Set the Content-Type header. */ + r->headers_out.content_type.len = sizeof("text/plain") - 1; + r->headers_out.content_type.data = (u_char *) "text/plain"; + + /* Allocate a new buffer for sending out the reply. */ + b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); + + /* Insertion in the buffer chain. */ + out.buf = b; + out.next = NULL; /* just one buffer */ + + b->pos = ngx_hello_world; /* first position in memory of the data */ + b->last = ngx_hello_world + sizeof(ngx_hello_world) - 1; /* last position in memory of the data */ + b->memory = 1; /* content is in read-only memory */ + b->last_buf = 1; /* there will be no more buffers in the request */ + + /* Sending the headers for the reply. */ + r->headers_out.status = NGX_HTTP_OK; /* 200 status code */ + /* Get the content length of the body. */ + r->headers_out.content_length_n = sizeof(ngx_hello_world) - 1; + ngx_http_send_header(r); /* Send the headers */ + + /* Send the body, and return the status code of the output filter chain. */ + return ngx_http_output_filter(r, &out); +} /* ngx_http_hello_world_handler */ + +/** + * Configuration setup function that installs the content handler. + * + * @param cf + * Module configuration structure pointer. + * @param cmd + * Module directives structure pointer. + * @param conf + * Module configuration structure pointer. + * @return string + * Status of the configuration setup. + */ +static char *ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +{ + ngx_http_core_loc_conf_t *clcf; /* pointer to core location configuration */ + + /* Install the hello world handler. */ + clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); + clcf->handler = ngx_http_hello_world_handler; + + return NGX_CONF_OK; +} /* ngx_http_hello_world */ +