/
ngx_http_bo_module.c
141 lines (122 loc) · 3.96 KB
/
ngx_http_bo_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
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include "BOModuleInterface.h"
/* Module config */
static char *conf_file=NULL;
static char *ngx_http_bo_conf(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
/* Directives */
static ngx_command_t ngx_http_bo_commands[] = {
{ ngx_string("bo_conf"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_bo_conf,
0,
0,
NULL },
ngx_null_command
};
/* Http context of the module */
static ngx_http_module_t ngx_http_bo_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 configration */
NULL /* merge location configration */
};
static ngx_int_t ngx_http_bo_init(ngx_cycle_t *cycle)
{
return ( 0 == BOInit( conf_file ) ? NGX_OK : NGX_ERROR );
}
static void ngx_http_bo_destroy(ngx_cycle_t *cycle)
{
BODestroy();
if(conf_file)
{
free(conf_file);
conf_file=NULL;
}
}
/* Module */
ngx_module_t ngx_http_bo_module = {
NGX_MODULE_V1,
&ngx_http_bo_module_ctx, /* module context */
ngx_http_bo_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
ngx_http_bo_init, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
ngx_http_bo_destroy, /* exit master */
NGX_MODULE_V1_PADDING
};
/* Handler function */
static ngx_int_t
ngx_http_bo_handler(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_buf_t *b;
ngx_chain_t out;
char *content=NULL;
int content_len=0;
if(!(r->method & (NGX_HTTP_HEAD|NGX_HTTP_GET|NGX_HTTP_POST)))
{
return NGX_HTTP_NOT_ALLOWED;
}
r->headers_out.content_type.len = g_content_type.len;
r->headers_out.content_type.data = (u_char *) g_content_type.str;
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = 0;
if(r->method == NGX_HTTP_HEAD)
{
rc = ngx_http_send_header(r);
if(rc != NGX_OK)
{
return rc;
}
}
rc = BOProcess( (const char*)r->uri.data, r->uri.len, (const char*)r->args.data, r->args.len, &content, &content_len );
if( rc != 0 || content == NULL)
return NGX_HTTP_INTERNAL_SERVER_ERROR;
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
if(b == NULL)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate response buffer.");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
b->pos = (u_char *)ngx_palloc(r->pool, content_len);
if(b->pos == NULL)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate buffer for response content.");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
b->last = b->pos + content_len;
memcpy(b->pos, (void *)content, content_len);
BOFree(&content);
b->memory = 1;
b->last_buf = 1;
out.buf = b;
out.next = NULL;
r->headers_out.content_length_n = content_len;
rc = ngx_http_send_header(r);
if(rc != NGX_OK)
return rc;
return ngx_http_output_filter(r, &out);
}
static char *
ngx_http_bo_conf(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_bo_handler;
ngx_str_t *conf_args = cf->args->elts;
++conf_args;
conf_file = malloc(conf_args->len + 1);
memcpy(conf_file, conf_args->data, conf_args->len);
conf_file[conf_args->len] = 0;
return NGX_CONF_OK;
}