Skip to content

Commit f7006a1

Browse files
committed
Initial commit (hello world)
0 parents  commit f7006a1

File tree

6 files changed

+181
-0
lines changed

6 files changed

+181
-0
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cmake_install.cmake
2+
*.cbp
3+
CMakeFiles
4+
cmake-build-debug
5+
CMakeCache.txt
6+
*.so
7+
Makefile
8+
.idea/

CMakeLists.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required(VERSION 3.7)
2+
project(nginx_js C)
3+
4+
set(CMAKE_C_STANDARD 11)
5+
6+
7+
add_library(nginx_js SHARED library.c library.h)
8+
9+
target_include_directories(
10+
nginx_js
11+
PUBLIC
12+
/home/simon/Downloads/nginx-1.16.1/objs
13+
/home/simon/Downloads/nginx-1.16.1/src
14+
/home/simon/Downloads/nginx-1.16.1/src/core
15+
/home/simon/Downloads/nginx-1.16.1/src/event
16+
/home/simon/Downloads/nginx-1.16.1/src/http
17+
/home/simon/Downloads/nginx-1.16.1/src/http/modules
18+
/home/simon/Downloads/nginx-1.16.1/src/misc
19+
/home/simon/Downloads/nginx-1.16.1/src/os/unix
20+
/home/simon/Downloads/nginx-1.16.1/src/stream
21+
)

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
//todo
3+
4+
```bash
5+
apt install libperl-dev libgeoip-dev libgd-dev libxslt1-dev
6+
```

build.sh

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
NGINX_PATH=/home/simon/Downloads/nginx-1.16.1/
2+
3+
MODULE_PATH=$(pwd)/
4+
CONFIG_ARGS=$(nginx -V 2>&1 | tail -n 1 | cut -c 21- | sed 's/--add-dynamic-module=.*//g')
5+
6+
CONFIG_ARGS="${CONFIG_ARGS} --add-dynamic-module=${MODULE_PATH}"
7+
8+
echo $CONFIG_ARGS
9+
10+
(
11+
cd ${NGINX_PATH}
12+
bash -c "./configure ${CONFIG_ARGS}"
13+
make modules
14+
)

config

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ngx_addon_name=ngx_http_hello_world_module
2+
3+
if test -n "$ngx_module_link"; then
4+
ngx_module_type=HTTP
5+
ngx_module_name=ngx_http_hello_world_module
6+
ngx_module_srcs="$ngx_addon_dir/library.c"
7+
. auto/module
8+
else
9+
HTTP_MODULES="$HTTP_MODULES ngx_http_hello_world_module"
10+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/library.c"
11+
fi

library.c

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include <stdio.h>
2+
#include "ngx_http.c"
3+
4+
#define HELLO_WORLD "hello world\r\n"
5+
6+
static char *ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
7+
static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r);
8+
9+
/**
10+
* This module provided directive: hello world.
11+
*
12+
*/
13+
static ngx_command_t ngx_http_hello_world_commands[] = {
14+
15+
{ ngx_string("hello_world"), /* directive */
16+
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, /* location context and takes
17+
no arguments*/
18+
ngx_http_hello_world, /* configuration setup function */
19+
0, /* No offset. Only one context is supported. */
20+
0, /* No offset when storing the module configuration on struct. */
21+
NULL},
22+
23+
ngx_null_command /* command termination */
24+
};
25+
26+
/* The hello world string. */
27+
static u_char ngx_hello_world[] = HELLO_WORLD;
28+
29+
/* The module context. */
30+
static ngx_http_module_t ngx_http_hello_world_module_ctx = {
31+
NULL, /* preconfiguration */
32+
NULL, /* postconfiguration */
33+
34+
NULL, /* create main configuration */
35+
NULL, /* init main configuration */
36+
37+
NULL, /* create server configuration */
38+
NULL, /* merge server configuration */
39+
40+
NULL, /* create location configuration */
41+
NULL /* merge location configuration */
42+
};
43+
44+
/* Module definition. */
45+
ngx_module_t ngx_http_hello_world_module = {
46+
NGX_MODULE_V1,
47+
&ngx_http_hello_world_module_ctx, /* module context */
48+
ngx_http_hello_world_commands, /* module directives */
49+
NGX_HTTP_MODULE, /* module type */
50+
NULL, /* init master */
51+
NULL, /* init module */
52+
NULL, /* init process */
53+
NULL, /* init thread */
54+
NULL, /* exit thread */
55+
NULL, /* exit process */
56+
NULL, /* exit master */
57+
NGX_MODULE_V1_PADDING
58+
};
59+
60+
/**
61+
* Content handler.
62+
*
63+
* @param r
64+
* Pointer to the request structure. See http_request.h.
65+
* @return
66+
* The status of the response generation.
67+
*/
68+
static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r)
69+
{
70+
ngx_buf_t *b;
71+
ngx_chain_t out;
72+
73+
/* Set the Content-Type header. */
74+
r->headers_out.content_type.len = sizeof("text/plain") - 1;
75+
r->headers_out.content_type.data = (u_char *) "text/plain";
76+
77+
/* Allocate a new buffer for sending out the reply. */
78+
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
79+
80+
/* Insertion in the buffer chain. */
81+
out.buf = b;
82+
out.next = NULL; /* just one buffer */
83+
84+
b->pos = ngx_hello_world; /* first position in memory of the data */
85+
b->last = ngx_hello_world + sizeof(ngx_hello_world) - 1; /* last position in memory of the data */
86+
b->memory = 1; /* content is in read-only memory */
87+
b->last_buf = 1; /* there will be no more buffers in the request */
88+
89+
/* Sending the headers for the reply. */
90+
r->headers_out.status = NGX_HTTP_OK; /* 200 status code */
91+
/* Get the content length of the body. */
92+
r->headers_out.content_length_n = sizeof(ngx_hello_world) - 1;
93+
ngx_http_send_header(r); /* Send the headers */
94+
95+
/* Send the body, and return the status code of the output filter chain. */
96+
return ngx_http_output_filter(r, &out);
97+
} /* ngx_http_hello_world_handler */
98+
99+
/**
100+
* Configuration setup function that installs the content handler.
101+
*
102+
* @param cf
103+
* Module configuration structure pointer.
104+
* @param cmd
105+
* Module directives structure pointer.
106+
* @param conf
107+
* Module configuration structure pointer.
108+
* @return string
109+
* Status of the configuration setup.
110+
*/
111+
static char *ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
112+
{
113+
ngx_http_core_loc_conf_t *clcf; /* pointer to core location configuration */
114+
115+
/* Install the hello world handler. */
116+
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
117+
clcf->handler = ngx_http_hello_world_handler;
118+
119+
return NGX_CONF_OK;
120+
} /* ngx_http_hello_world */
121+

0 commit comments

Comments
 (0)