Skip to content

Commit e8df690

Browse files
author
bjrara
committed
add src
1 parent 5190c52 commit e8df690

File tree

16 files changed

+1819
-0
lines changed

16 files changed

+1819
-0
lines changed

chapter3/helloworld/config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ngx_addon_name=ngx_http_mytest_module
2+
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
3+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#include <ngx_config.h>
2+
#include <ngx_core.h>
3+
#include <ngx_http.h>
4+
5+
6+
static char *
7+
ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
8+
9+
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);
10+
11+
12+
13+
static ngx_command_t ngx_http_mytest_commands[] =
14+
{
15+
16+
{
17+
ngx_string("mytest"),
18+
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_NOARGS,
19+
ngx_http_mytest,
20+
NGX_HTTP_LOC_CONF_OFFSET,
21+
0,
22+
NULL
23+
},
24+
25+
ngx_null_command
26+
};
27+
28+
static ngx_http_module_t ngx_http_mytest_module_ctx =
29+
{
30+
NULL, /* preconfiguration */
31+
NULL, /* postconfiguration */
32+
33+
NULL, /* create main configuration */
34+
NULL, /* init main configuration */
35+
36+
NULL, /* create server configuration */
37+
NULL, /* merge server configuration */
38+
39+
NULL, /* create location configuration */
40+
NULL /* merge location configuration */
41+
};
42+
43+
ngx_module_t ngx_http_mytest_module =
44+
{
45+
NGX_MODULE_V1,
46+
&ngx_http_mytest_module_ctx, /* module context */
47+
ngx_http_mytest_commands, /* module directives */
48+
NGX_HTTP_MODULE, /* module type */
49+
NULL, /* init master */
50+
NULL, /* init module */
51+
NULL, /* init process */
52+
NULL, /* init thread */
53+
NULL, /* exit thread */
54+
NULL, /* exit process */
55+
NULL, /* exit master */
56+
NGX_MODULE_V1_PADDING
57+
};
58+
59+
60+
static char *
61+
ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
62+
{
63+
ngx_http_core_loc_conf_t *clcf;
64+
65+
//首先找到mytest配置项所属的配置块,clcf貌似是location块内的数据
66+
//结构,其实不然,它可以是main、srv或者loc级别配置项,也就是说在每个
67+
//http{}和server{}内也都有一个ngx_http_core_loc_conf_t结构体
68+
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
69+
70+
//http框架在处理用户请求进行到NGX_HTTP_CONTENT_PHASE阶段时,如果
71+
//请求的主机域名、URI与mytest配置项所在的配置块相匹配,就将调用我们
72+
//实现的ngx_http_mytest_handler方法处理这个请求
73+
clcf->handler = ngx_http_mytest_handler;
74+
75+
return NGX_CONF_OK;
76+
}
77+
78+
79+
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
80+
{
81+
//必须是GET或者HEAD方法,否则返回405 Not Allowed
82+
if (!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD)))
83+
{
84+
return NGX_HTTP_NOT_ALLOWED;
85+
}
86+
87+
//丢弃请求中的包体
88+
ngx_int_t rc = ngx_http_discard_request_body(r);
89+
if (rc != NGX_OK)
90+
{
91+
return rc;
92+
}
93+
94+
//设置返回的Content-Type。注意,ngx_str_t有一个很方便的初始化宏
95+
//ngx_string,它可以把ngx_str_t的data和len成员都设置好
96+
ngx_str_t type = ngx_string("text/plain");
97+
//返回的包体内容
98+
ngx_str_t response = ngx_string("Hello World!");
99+
//设置返回状态码
100+
r->headers_out.status = NGX_HTTP_OK;
101+
//响应包是有包体内容的,所以需要设置Content-Length长度
102+
r->headers_out.content_length_n = response.len;
103+
//设置Content-Type
104+
r->headers_out.content_type = type;
105+
106+
//发送http头部
107+
rc = ngx_http_send_header(r);
108+
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only)
109+
{
110+
return rc;
111+
}
112+
113+
//构造ngx_buf_t结构准备发送包体
114+
ngx_buf_t *b;
115+
b = ngx_create_temp_buf(r->pool, response.len);
116+
if (b == NULL)
117+
{
118+
return NGX_HTTP_INTERNAL_SERVER_ERROR;
119+
}
120+
//将Hello World拷贝到ngx_buf_t指向的内存中
121+
ngx_memcpy(b->pos, response.data, response.len);
122+
//注意,一定要设置好last指针
123+
b->last = b->pos + response.len;
124+
//声明这是最后一块缓冲区
125+
b->last_buf = 1;
126+
127+
//构造发送时的ngx_chain_t结构体
128+
ngx_chain_t out;
129+
//赋值ngx_buf_t
130+
out.buf = b;
131+
//设置next为NULL
132+
out.next = NULL;
133+
134+
//最后一步发送包体,http框架会调用ngx_http_finalize_request方法
135+
//结束请求
136+
return ngx_http_output_filter(r, &out);
137+
}
138+
139+

chapter3/sendfile/config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ngx_addon_name=ngx_http_mytest_module
2+
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
3+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
#include <ngx_config.h>
2+
#include <ngx_core.h>
3+
#include <ngx_http.h>
4+
5+
6+
static char *
7+
ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
8+
9+
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);
10+
11+
12+
13+
static ngx_command_t ngx_http_mytest_commands[] =
14+
{
15+
16+
{
17+
ngx_string("mytest"),
18+
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_NOARGS,
19+
ngx_http_mytest,
20+
NGX_HTTP_LOC_CONF_OFFSET,
21+
0,
22+
NULL
23+
},
24+
25+
ngx_null_command
26+
};
27+
28+
static ngx_http_module_t ngx_http_mytest_module_ctx =
29+
{
30+
NULL, /* preconfiguration */
31+
NULL, /* postconfiguration */
32+
33+
NULL, /* create main configuration */
34+
NULL, /* init main configuration */
35+
36+
NULL, /* create server configuration */
37+
NULL, /* merge server configuration */
38+
39+
NULL, /* create location configuration */
40+
NULL /* merge location configuration */
41+
};
42+
43+
ngx_module_t ngx_http_mytest_module =
44+
{
45+
NGX_MODULE_V1,
46+
&ngx_http_mytest_module_ctx, /* module context */
47+
ngx_http_mytest_commands, /* module directives */
48+
NGX_HTTP_MODULE, /* module type */
49+
NULL, /* init master */
50+
NULL, /* init module */
51+
NULL, /* init process */
52+
NULL, /* init thread */
53+
NULL, /* exit thread */
54+
NULL, /* exit process */
55+
NULL, /* exit master */
56+
NGX_MODULE_V1_PADDING
57+
};
58+
59+
60+
static char *
61+
ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
62+
{
63+
ngx_http_core_loc_conf_t *clcf;
64+
65+
//首先找到mytest配置项所属的配置块,clcf貌似是location块内的数据
66+
//结构,其实不然,它可以是main、srv或者loc级别配置项,也就是说在每个
67+
//http{}和server{}内也都有一个ngx_http_core_loc_conf_t结构体
68+
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
69+
70+
//http框架在处理用户请求进行到NGX_HTTP_CONTENT_PHASE阶段时,如果
71+
//请求的主机域名、URI与mytest配置项所在的配置块相匹配,就将调用我们
72+
//实现的ngx_http_mytest_handler方法处理这个请求
73+
clcf->handler = ngx_http_mytest_handler;
74+
75+
return NGX_CONF_OK;
76+
}
77+
78+
79+
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
80+
{
81+
//必须是GET或者HEAD方法,否则返回405 Not Allowed
82+
if (!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD)))
83+
{
84+
return NGX_HTTP_NOT_ALLOWED;
85+
}
86+
87+
//丢弃请求中的包体
88+
ngx_int_t rc = ngx_http_discard_request_body(r);
89+
if (rc != NGX_OK)
90+
{
91+
return rc;
92+
}
93+
94+
ngx_buf_t *b;
95+
b = ngx_palloc(r->pool, sizeof(ngx_buf_t));
96+
97+
//要打开的文件
98+
u_char* filename = (u_char*)"/tmp/test.txt";
99+
b->in_file = 1;
100+
b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
101+
b->file->fd = ngx_open_file(filename, NGX_FILE_RDONLY | NGX_FILE_NONBLOCK, NGX_FILE_OPEN, 0);
102+
b->file->log = r->connection->log;
103+
b->file->name.data = filename;
104+
b->file->name.len = sizeof(filename) - 1;
105+
if (b->file->fd <= 0)
106+
{
107+
return NGX_HTTP_NOT_FOUND;
108+
}
109+
110+
//支持断点续传
111+
r->allow_ranges = 1;
112+
113+
//获取文件长度
114+
if (ngx_file_info(filename, &b->file->info) == NGX_FILE_ERROR)
115+
{
116+
return NGX_HTTP_INTERNAL_SERVER_ERROR;
117+
}
118+
119+
//设置缓冲区指向的文件块
120+
b->file_pos = 0;
121+
b->file_last = b->file->info.st_size;
122+
123+
ngx_pool_cleanup_t* cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_pool_cleanup_file_t));
124+
if (cln == NULL)
125+
{
126+
return NGX_ERROR;
127+
}
128+
129+
cln->handler = ngx_pool_cleanup_file;
130+
ngx_pool_cleanup_file_t *clnf = cln->data;
131+
132+
clnf->fd = b->file->fd;
133+
clnf->name = b->file->name.data;
134+
clnf->log = r->pool->log;
135+
136+
137+
//设置返回的Content-Type。注意,ngx_str_t有一个很方便的初始化宏
138+
//ngx_string,它可以把ngx_str_t的data和len成员都设置好
139+
ngx_str_t type = ngx_string("text/plain");
140+
141+
//设置返回状态码
142+
r->headers_out.status = NGX_HTTP_OK;
143+
//响应包是有包体内容的,所以需要设置Content-Length长度
144+
r->headers_out.content_length_n = b->file->info.st_size;
145+
//设置Content-Type
146+
r->headers_out.content_type = type;
147+
148+
//发送http头部
149+
rc = ngx_http_send_header(r);
150+
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only)
151+
{
152+
return rc;
153+
}
154+
155+
//构造发送时的ngx_chain_t结构体
156+
ngx_chain_t out;
157+
//赋值ngx_buf_t
158+
out.buf = b;
159+
//设置next为NULL
160+
out.next = NULL;
161+
162+
//最后一步发送包体,http框架会调用ngx_http_finalize_request方法
163+
//结束请求
164+
return ngx_http_output_filter(r, &out);
165+
}
166+
167+

chapter4/config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ngx_addon_name=ngx_http_mytest_module
2+
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
3+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"

chapter4/nginx.conf

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
#user nobody;
3+
worker_processes 1;
4+
5+
error_log logs/error.log debug;
6+
7+
events {
8+
worker_connections 1024;
9+
}
10+
11+
12+
http {
13+
include mime.types;
14+
default_type application/octet-stream;
15+
16+
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
17+
# '$status $body_bytes_sent "$http_referer" '
18+
# '"$http_user_agent" "$http_x_forwarded_for"';
19+
20+
#access_log logs/access.log main;
21+
22+
keepalive_timeout 65;
23+
test_str SUPERMAN;
24+
25+
server {
26+
server_name HOST1;
27+
28+
location /HOST1_loc1 {
29+
test_str loc1_BANANA;
30+
}
31+
32+
location /HOST1_loc2 {
33+
}
34+
}
35+
36+
server {
37+
server_name HOST2;
38+
listen 8088;
39+
server_name localhost;
40+
test_str CAR;
41+
42+
location /index {
43+
root html;
44+
test_str FOCUS;
45+
index index.html index.htm;
46+
}
47+
48+
location /test {
49+
test_flag on;
50+
test_str PLANE;
51+
test_num 4;
52+
}
53+
54+
error_page 500 502 503 504 /50x.html;
55+
location = /50x.html {
56+
root html;
57+
}
58+
59+
}
60+
}
61+

0 commit comments

Comments
 (0)