-
Notifications
You must be signed in to change notification settings - Fork 5
/
ngx_http_tfs_module.cpp
381 lines (312 loc) · 12.1 KB
/
ngx_http_tfs_module.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
*
*
* help on:
* http://www.jiajun.org/2010/10/06/nginx_module_development_part_2.html
* */
extern "C"{
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
}
#include "tfs_client_api.h"
#include "func.h"
using namespace std;
using namespace tfs::client;
using namespace tfs::common;
#define DEFAULT_TFS_READ_WRITE_SIZE (2 * 1024 * 1024)
static void* ngx_http_tfs_create_loc_conf(ngx_conf_t *cf);
static char* ngx_http_tfs_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
static char* ngx_http_tfs_put(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char* ngx_http_tfs_get(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
typedef struct {
ngx_str_t tfs_nsip; /* 字符串不要在_create_loc_conf中初始化,在_merge_loc_conf给默认值相当初始化 */
size_t tfs_rb_buffer_size;
} ngx_http_tfs_ns_loc_conf_t;
static ngx_command_t ngx_http_tfs_commands[] = {
{ ngx_string("tfs_put"),
NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS, /* 不带参数 */
ngx_http_tfs_put,
0,
0,
NULL },
{ ngx_string("tfs_get"),
NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS, /* 不带参数 */
ngx_http_tfs_get,
0,
0,
NULL },
{ ngx_string("tfs_nsip"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_str_slot , /* 直接调用内置的字符串解释函数解释参数*/
NGX_HTTP_LOC_CONF_OFFSET, /* 只在location中配置 */
offsetof(ngx_http_tfs_ns_loc_conf_t, tfs_nsip),
NULL },
{ ngx_string("tfs_rb_buffer_size"), /* 每次读写tfs文件buffer大小 */
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_tfs_ns_loc_conf_t, tfs_rb_buffer_size),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_tfs_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_tfs_create_loc_conf, /* create location configuration 创建location配置时调用 */
ngx_http_tfs_merge_loc_conf /* merge location configuration 与location配置合并时调用 */
};
ngx_module_t ngx_http_tfs_module = {
NGX_MODULE_V1,
&ngx_http_tfs_module_ctx, /* module context */
ngx_http_tfs_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
};
static ngx_int_t
ngx_http_tfs_get_args_tfsname(ngx_http_request_t *r, u_char *ret)
{
static const ngx_str_t TFSNAME_KEY = ngx_string("tfsname=");
if(!r->args.len) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_tfs_mods: --- > args not match! 1 %s, %zd", r->args.data, r->args.len);
return NGX_ERROR;
}
if(ngx_strncasecmp(r->args.data, TFSNAME_KEY.data, TFSNAME_KEY.len) == 0) {
ngx_cpystrn(ret, (r->args.data + TFSNAME_KEY.len), TFS_FILE_LEN);
ret[TFS_FILE_LEN] = '\0';
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_tfs_mods: --- > tfs_name: %s", ret);
return NGX_OK;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_tfs_mods: --- > args not match! 2 %s", ret + TFSNAME_KEY.len );
return NGX_ERROR;
}
static ngx_int_t
ngx_http_tfs_get_handler(ngx_http_request_t *r)
{
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "called:ngx_http_tfs_put_handler");
ngx_int_t rc;
ngx_buf_t *b;
ngx_chain_t out;
u_char tfsname[TFS_FILE_LEN + 1];
ngx_http_tfs_ns_loc_conf_t *cglcf;
cglcf = (ngx_http_tfs_ns_loc_conf_t*)ngx_http_get_module_loc_conf(r, ngx_http_tfs_module);
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
return NGX_HTTP_NOT_ALLOWED;
}
if (r->headers_in.if_modified_since) {
return NGX_HTTP_NOT_MODIFIED;
}
if( NGX_OK != ngx_http_tfs_get_args_tfsname(r, tfsname)) {
return NGX_DECLINED;
}
int ret = 0;
int fd = -1;
TfsClient* tfsclient = TfsClient::Instance();
tfsclient->initialize((const char*)cglcf->tfs_nsip.data);
// 打开待读写的文件
fd= tfsclient->open((const char*)tfsname, NULL, T_READ);
// 获得文件属性
TfsFileStat fstat;
ret = tfsclient->fstat(fd, &fstat);
if (ret != TFS_SUCCESS || fstat.size_ <= 0) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "get remote file info error");
return NGX_DECLINED;
}
b = (ngx_buf_t *)ngx_create_temp_buf(r->pool, fstat.size_);
if (b == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate response buffer.");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
out.buf = b;
out.next = NULL;
b->last = b->pos + fstat.size_ ;
b->memory = 1;
b->last_buf = 1;
int read = 0;
int read_size;
uint32_t crc = 0;
size_t left = fstat.size_;
// 读取文件
while (read < fstat.size_) {
read_size = left > cglcf->tfs_rb_buffer_size ? cglcf->tfs_rb_buffer_size : left;
ret = tfsclient->read(fd, (char*)b->pos + read, read_size);
if (ret < 0) {
break;
}
else {
crc = Func::crc(crc, (const char*)(b->pos + read), ret); // 对读取的文件计算crc值
read += ret;
left -= ret;
}
}
if (ret < 0 || crc != fstat.crc_) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "read remote file error!");
return NGX_DECLINED;
}
r->headers_out.content_type.len = sizeof("application/octet-stream") - 1;
r->headers_out.content_type.data = (u_char *) "application/octet-stream";
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = fstat.size_;
if (r->method == NGX_HTTP_HEAD) {
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
}
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);
}
void ngx_http_tfs_cb_handler (ngx_http_request_t *r)
{
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_tfs_mods: --- > reading quest body..!");
}
static ngx_int_t
ngx_http_tfs_put_handler(ngx_http_request_t *r)
{ // 读取post数据,上传到tfs
ngx_int_t rc;
ngx_buf_t *b;
ngx_chain_t out;
ngx_http_request_body_t *rb;
ngx_http_tfs_ns_loc_conf_t *cglcf;
int rb_size;
int ret = 0;
int fd = -1;
if (!(r->method & NGX_HTTP_POST)) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_tfs_mods: request method must be POST.");
return NGX_DECLINED;
}
// 必需先调用到个回调,读body数据
rc = ngx_http_read_client_request_body(r, ngx_http_tfs_cb_handler);
if(NULL == r->request_body || NULL == r->request_body->buf) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_tfs_mods: --- > request body is empty!");
return NGX_HTTP_BAD_REQUEST;
}
// 创建tfs客户端,并打开一个新的文件准备写入
TfsClient* tfsclient = TfsClient::Instance();
cglcf = (ngx_http_tfs_ns_loc_conf_t*)ngx_http_get_module_loc_conf(r, ngx_http_tfs_module);
//应该不必每次调用 init
tfsclient->initialize((const char*)cglcf->tfs_nsip.data);
fd = tfsclient->open((char*)NULL, NULL, NULL, T_WRITE);
rb = r->request_body;
rb_size = rb->buf->last - rb->buf->pos;
if(rb_size < 0) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_tfs_mods: invalid content data");
return NGX_HTTP_BAD_REQUEST;
}
int wrote = 0;
size_t left = rb_size;
int wrote_size;
char tfs_file_name[TFS_FILE_LEN];
while (wrote < rb_size) {
wrote_size = left > cglcf->tfs_rb_buffer_size ? cglcf->tfs_rb_buffer_size : left;
// 将buffer中的数据写入tfs
ret = tfsclient->write(fd, (char*)(rb->buf->pos + wrote), wrote_size);
if (ret < 0 || ret >= (int)left) {
// 读写失败或完成
break;
}
else {
// 若ret>0,则ret为实际写入的数据量
wrote += ret;
left -= ret;
}
}
// 读写失败
if (ret < 0) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "write data error!");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
// 提交写入
ret = tfsclient->close(fd, tfs_file_name, TFS_FILE_LEN);
if (ret != TFS_SUCCESS) {
// 提交失败
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_tfs_mods: upload file error! ret =%d ", ret);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
else {
b = ngx_create_temp_buf(r->pool, TFS_FILE_LEN);
if (b == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_tfs_mods: alloc memory fail (body_handler)");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_cpystrn(b->pos, (u_char*)tfs_file_name, TFS_FILE_LEN);
//b->temporary = 1;
b->memory = 1;
b->last_buf = 1;
b->last = b->pos + TFS_FILE_LEN;
out.buf = b;
out.next = NULL;
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "write remote file:%s", (u_char*)b->pos);
}
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 = FILE_NAME_LEN;
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
ngx_http_output_filter(r, &out);
ngx_http_finalize_request(r, rc);
return NGX_OK;
}
static char *
ngx_http_tfs_put(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
//printf("called:ngx_http_tfs_put.\n");
ngx_http_core_loc_conf_t *clcf;
clcf = reinterpret_cast<ngx_http_core_loc_conf_t*>(
ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module));
clcf->handler = ngx_http_tfs_put_handler;
return NGX_CONF_OK;
}
static char *
ngx_http_tfs_get(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
//printf("called:ngx_http_tfs_get.\n");
ngx_http_core_loc_conf_t *clcf;
clcf = reinterpret_cast<ngx_http_core_loc_conf_t*>(
ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module));
clcf->handler = ngx_http_tfs_get_handler;
return NGX_CONF_OK;
}
static void *
ngx_http_tfs_create_loc_conf(ngx_conf_t *cf)
{
//printf("called:ngx_http_tfs_create_loc_conf.\n");
ngx_http_tfs_ns_loc_conf_t *conf;
conf = (ngx_http_tfs_ns_loc_conf_t *)ngx_pcalloc(cf->pool, sizeof(ngx_http_tfs_ns_loc_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->tfs_rb_buffer_size = NGX_CONF_UNSET_SIZE;
return conf;
}
static char *
ngx_http_tfs_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
//printf("called:ngx_http_tfs_merge_loc_conf\n");
ngx_http_tfs_ns_loc_conf_t *prev = (ngx_http_tfs_ns_loc_conf_t *)parent;
ngx_http_tfs_ns_loc_conf_t *conf = (ngx_http_tfs_ns_loc_conf_t *)child;
ngx_conf_merge_str_value(conf->tfs_nsip, prev->tfs_nsip, "127.0.0.1:10000");
ngx_conf_merge_size_value(conf->tfs_rb_buffer_size, prev->tfs_rb_buffer_size, (size_t)DEFAULT_TFS_READ_WRITE_SIZE);
return NGX_CONF_OK;
}