-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathngx_http_hello_world_module.c
161 lines (130 loc) · 4.2 KB
/
ngx_http_hello_world_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
typedef struct{
ngx_shm_zone_t *shm_zone;
} ngx_http_hello_world_loc_conf_t;
typedef struct{
int count;
} ngx_http_hello_world_shm_count_t;
static char* ngx_http_hello_world(ngx_conf_t* cf, ngx_command_t* cmd, void* conf);
static void* ngx_http_hello_world_create_loc_conf(ngx_conf_t* cf);
static char* ngx_http_hello_world_merge_loc_conf(ngx_conf_t* cf, void* parent, void* child);
static ngx_int_t ngx_http_hello_world_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data);
static ngx_command_t ngx_http_hello_world_commands[] = {
{
ngx_string("hello_world"), //The command name
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_http_hello_world, //The command handler
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_hello_world_loc_conf_t, shm_zone),
NULL
},
ngx_null_command
};
static ngx_http_module_t ngx_http_hello_world_module_ctx = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
ngx_http_hello_world_create_loc_conf,
ngx_http_hello_world_merge_loc_conf
};
ngx_module_t ngx_http_hello_world_module = {
NGX_MODULE_V1,
&ngx_http_hello_world_module_ctx,
ngx_http_hello_world_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
};
static ngx_int_t ngx_http_hello_world_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data){
ngx_slab_pool_t *shpool;
ngx_http_hello_world_shm_count_t *shm_count;
if(data){
shm_zone->data = data;
return NGX_OK;
}
shpool = (ngx_slab_pool_t *)shm_zone->shm.addr;
shm_count = ngx_slab_alloc(shpool, sizeof *shm_count);
shm_count->count = 0;
shm_zone->data = shm_count;
return NGX_OK;
}
static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t* r){
ngx_http_hello_world_loc_conf_t *lccf;
ngx_shm_zone_t *shm_zone;
int count;
ngx_int_t rc;
ngx_buf_t* b;
ngx_chain_t out;
lccf = ngx_http_get_module_loc_conf(r, ngx_http_hello_world_module);
if(lccf->shm_zone == NULL){
return NGX_DECLINED;
}
shm_zone = lccf->shm_zone;
count = ((ngx_http_hello_world_shm_count_t *)shm_zone->data)->count;
count = count+1;
((ngx_http_hello_world_shm_count_t *)shm_zone->data)->count = count;
r->headers_out.content_type.len = sizeof("text/plain") - 1;
r->headers_out.content_type.data = (u_char*)"text/plain";
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
out.buf = b;
out.next = NULL;
char string[10];
sprintf(string, "%d", count);
ngx_str_t count_str = ngx_string(string);
b->pos = count_str.data;
b->last = count_str.data + count_str.len;
b->memory = 1;
b->last_buf = 1;
r->headers_out.content_type.len = sizeof("text/html") - 1;
r->headers_out.content_type.data = (u_char *) "text/html";
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = count_str.len;
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
return ngx_http_output_filter(r, &out);
}
static char* ngx_http_hello_world(ngx_conf_t* cf, ngx_command_t* cmd, void* conf){
ngx_http_core_loc_conf_t *clcf;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_hello_world_handler;
ngx_conf_set_str_slot(cf, cmd, conf);
return NGX_CONF_OK;
}
static void* ngx_http_hello_world_create_loc_conf(ngx_conf_t* cf){
ngx_http_hello_world_loc_conf_t* conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_hello_world_loc_conf_t));
if(conf == NULL){
return NGX_CONF_ERROR;
}
return conf;
}
static char* ngx_http_hello_world_merge_loc_conf(ngx_conf_t* cf, void* parent, void* child){
ngx_shm_zone_t *shm_zone;
ngx_str_t *shm_name;
ngx_http_hello_world_loc_conf_t* prev = parent;
ngx_http_hello_world_loc_conf_t* conf = child;
shm_name = ngx_palloc(cf->pool, sizeof *shm_name);
shm_name->len = sizeof("shared_memory") - 1;
shm_name->data = (unsigned char *) "shared_memory";
shm_zone = ngx_shared_memory_add(cf, shm_name, 8 * ngx_pagesize, &ngx_http_hello_world_module);
if(shm_zone == NULL){
return NGX_CONF_ERROR;
}
shm_zone->init = ngx_http_hello_world_init_shm_zone;
conf->shm_zone = shm_zone;
ngx_conf_merge_ptr_value(conf->shm_zone, prev->shm_zone, NULL);
return NGX_CONF_OK;
}