-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodule.c
472 lines (416 loc) · 12.9 KB
/
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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include "required.h"
#include "helpers.h"
#include "bridge.h"
#include "module.h"
static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
// Module commands list
static ngx_command_t ngx_http_imgproc_commands[] = {
{
ngx_string("imgproc"),
NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(Config, Enable),
NULL
}, {
ngx_string("imgproc_watermark"),
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(Config, WatermarkPath),
NULL
}, {
ngx_string("imgproc_watermark_position"),
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE4,
OnParseWatermarkPosition,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
}, {
ngx_string("imgproc_watermark_opacity"),
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(Config, WatermarkOpacity),
NULL
}, {
ngx_string("imgproc_max_src_size"),
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(Config, MaxSrcSize),
NULL
}, {
ngx_string("imgproc_max_target_dimensions"),
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
OnParseMaxTargetDims,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
}, {
ngx_string("imgproc_max_filters_count"),
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(Config, MaxFiltersCount),
NULL
}, {
ngx_string("imgproc_allow_experiments"),
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(Config, AllowExperiments),
NULL
},
ngx_null_command
};
// Module events
static ngx_http_module_t ngx_http_imgproc_module_ctx = {
NULL,
OnModuleInit, // postconf
NULL,
NULL,
NULL,
NULL,
OnConfigCreate,
OnConfigMerge
};
// Module export
ngx_module_t ngx_http_imgproc_module = {
NGX_MODULE_V1,
&ngx_http_imgproc_module_ctx,
ngx_http_imgproc_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
OnProcessInit,
NULL,
NULL,
OnProcessDie,
NULL,
NGX_MODULE_V1_PADDING
};
// Callbacks
static ngx_int_t OnProcessInit(ngx_cycle_t* cycle) {
OnEnvStart();
return NGX_OK;
}
static void OnProcessDie(ngx_cycle_t* cycle) {
OnEnvDestroy();
}
static ngx_int_t OnModuleInit(ngx_conf_t* cfg) {
ngx_http_next_body_filter = ngx_http_top_body_filter;
ngx_http_top_body_filter = BodyFilter;
ngx_http_next_header_filter = ngx_http_top_header_filter;
ngx_http_top_header_filter = HeaderFilter;
return NGX_OK;
}
static void* OnConfigCreate(ngx_conf_t* cfg) {
Config* conf = ngx_pcalloc(cfg->pool, sizeof(Config));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->Enable = NGX_CONF_UNSET;
conf->WatermarkOpacity = NGX_CONF_UNSET;
conf->MaxSrcSize = NGX_CONF_UNSET;
conf->MaxFiltersCount = NGX_CONF_UNSET;
conf->AllowExperiments = NGX_CONF_UNSET;
return conf;
}
static char* OnConfigMerge(ngx_conf_t* cfg, void* parent, void* child) {
Config* prev = parent;
Config* conf = child;
ngx_conf_merge_value(conf->Enable, prev->Enable, 0);
if (conf->Enable == 0) {
return NGX_CONF_OK;
} else if (conf->Enable != 1) {
ngx_conf_log_error(NGX_LOG_EMERG, cfg, 0, "imp::[enable] should on/off value");
return NGX_CONF_ERROR;
}
ngx_conf_merge_str_value(conf->WatermarkPath, prev->WatermarkPath, "");
if (conf->WatermarkPath.len > 0) {
ngx_conf_merge_value(conf->WatermarkOpacity, prev->WatermarkOpacity, 100);
if (conf->WatermarkOpacity <= 0 || conf->WatermarkOpacity > 100) {
ngx_conf_log_error(NGX_LOG_EMERG, cfg, 0, "imp::[watermark_opacity] is out of valid range 1-100");
return NGX_CONF_ERROR;
}
if (!conf->WatermarkPosition) {
if (prev->WatermarkPosition) {
conf->WatermarkPosition = prev->WatermarkPosition;
} else {
ngx_conf_log_error(NGX_LOG_EMERG, cfg, 0, "imp::no any valid [watermark_position] found");
return NGX_CONF_ERROR;
}
}
int wmarkReadCode = PrepareWatermark(conf, cfg->pool);
if (wmarkReadCode != IMP_OK) {
ngx_conf_log_error(NGX_LOG_EMERG, cfg, 0, "imp::[watermark] path is not exists or not readable. Fix this or comment out [watermark] directive");
return NGX_CONF_ERROR;
}
} else {
conf->WatermarkInfo = NULL;
}
if (!conf->MaxTargetDimensions) {
if (prev->MaxTargetDimensions) {
conf->MaxTargetDimensions = prev->MaxTargetDimensions;
} else {
Dimensions* deflt = (Dimensions*)ngx_palloc(cfg->pool, sizeof(Dimensions));
deflt->W = 2000;
deflt->H = 2000;
conf->MaxTargetDimensions = deflt;
}
}
ngx_conf_merge_uint_value(conf->MaxSrcSize, prev->MaxSrcSize, 4 * 1024 * 1024);
ngx_conf_merge_value(conf->MaxFiltersCount, prev->MaxFiltersCount, 5);
ngx_conf_merge_value(conf->AllowExperiments, prev->AllowExperiments, 0);
if (conf->AllowExperiments != 0 && conf->AllowExperiments != 1) {
ngx_conf_log_error(NGX_LOG_EMERG, cfg, 0, "imp::[allow_experiments] should on/off value");
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
static ngx_int_t HeaderFilter(ngx_http_request_t* r) {
if (!r->args.len) {
return ngx_http_next_header_filter(r);
}
Config* conf = ngx_http_get_module_loc_conf(r, ngx_http_imgproc_module);
if (!conf->Enable || r->headers_out.status != NGX_HTTP_OK) {
return ngx_http_next_header_filter(r);
}
PeerContext* ctx = ngx_http_get_module_ctx(r, ngx_http_imgproc_module);
if (ctx) {
ngx_http_set_ctx(r, NULL, ngx_http_imgproc_module);
return ngx_http_next_header_filter(r);
}
ctx = ngx_pcalloc(r->pool, sizeof(PeerContext));
if (ctx == NULL) {
return NGX_ERROR;
}
ngx_http_set_ctx(r, ctx, ngx_http_imgproc_module);
int isMixed =
r->headers_out.content_type.len >= sizeof("multipart/x-mixed-replace") - 1
&& ngx_strncasecmp(r->headers_out.content_type.data, (u_char*)"multipart/x-mixed-replace", sizeof("multipart/x-mixed-replace") - 1) == 0;
if (isMixed) {
OutputErrorString("imp::multipart/x-mixed-replace response", r);
return NGX_ERROR;
}
long long int len = r->headers_out.content_length_n;
if (len != -1 && len > (off_t)conf->MaxSrcSize) {
char msg[64];
sprintf(msg, "imp::too big source image: %lld", len);
OutputErrorString(msg, r);
return NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;
}
if (len == -1) {
ctx->Length = conf->MaxSrcSize;
} else {
ctx->Length = (size_t)len;
}
ctx->ReadComplete = 0;
ctx->DataSent = 0;
if (r->headers_out.refresh) {
r->headers_out.refresh->hash = 0;
}
r->main_filter_need_in_memory = 1;
r->allow_ranges = 0;
return NGX_OK;
}
static ngx_int_t BodyFilter(ngx_http_request_t* r, ngx_chain_t* in) {
// Step 1: emergency check
if (in == NULL) {
OutputErrorString("imp::input chain is null", r);
return ngx_http_next_body_filter(r, in);
}
if (!r->args.len) {
#ifdef IMP_DEBUG
syslog(LOG_NOTICE, "imp::skipped because of empty args");
#endif
return ngx_http_next_body_filter(r, in);
}
Config* conf = ngx_http_get_module_loc_conf(r, ngx_http_imgproc_module);
if (conf == NULL) {
OutputErrorString("imp::module config is null", r);
return ngx_http_next_body_filter(r, in);
} else if (!conf->Enable) {
#ifdef IMP_DEBUG
syslog(LOG_NOTICE, "imp::disabled, skipped");
#endif
return ngx_http_next_body_filter(r, in);
} else if (r->headers_out.status != NGX_HTTP_OK) {
return ngx_http_next_body_filter(r, in);
}
PeerContext* ctx = ngx_http_get_module_ctx(r, ngx_http_imgproc_module);
if (ctx == NULL) {
OutputErrorString("imp::module context is null", r);
return ngx_http_next_body_filter(r, in);
}
// Step 2: already done check
if (ctx->DataSent) { // #todo: is this needed?
return ngx_http_next_body_filter(r, in);
}
// Step 3: complete buffer compilation progress check
if (!ctx->ReadComplete) {
ngx_int_t chunkState = OnChunkAvailable(r, in);
if (chunkState == NGX_AGAIN) {
return NGX_OK;
} else if (chunkState == NGX_ERROR) {
return ngx_http_filter_finalize_request(r, &ngx_http_imgproc_module, NGX_HTTP_UNSUPPORTED_MEDIA_TYPE);
}
}
ctx->ReadComplete = 1;
// Step 4: run processing
r->connection->buffered &= ~NGX_HTTP_IMAGE_BUFFERED;
JobResult* result = RunJob(ctx->Image, ctx->Length, r, conf);
// Step 5: check processing result
#ifdef IMP_DEBUG
syslog(LOG_NOTICE, "imp::done; code:%d", result->Code);
#endif
if (result->Code) {
ngx_int_t httpCode = NGX_HTTP_INTERNAL_SERVER_ERROR;
switch (result->Code) {
case IMP_ERROR_UNSUPPORTED:
OutputErrorString("imp::imgproc can't read data", r);
httpCode = NGX_HTTP_UNSUPPORTED_MEDIA_TYPE;
break;
case IMP_ERROR_INVALID_ARGS:
httpCode = NGX_HTTP_BAD_REQUEST;
break;
case IMP_ERROR_UPSCALE:
case IMP_ERROR_NO_SUCH_FILTER:
case IMP_ERROR_TOO_MUCH_FILTERS:
httpCode = NGX_HTTP_NOT_ALLOWED;
break;
case IMP_ERROR_TOO_BIG_TARGET:
httpCode = NGX_HTTP_REQUEST_ENTITY_TOO_LARGE;
break;
case IMP_ERROR_FEATURE_DISABLED:
httpCode = 424; // Failed Dependency
break;
}
char msg[44];
sprintf(msg, "imp::Job failed at step %d with code %d", result->Step, result->Code);
OutputErrorString(msg, r);
return ngx_http_filter_finalize_request(r, &ngx_http_imgproc_module, httpCode);
}
// Step 6: initiate destructor
// Not used anymore, but left here in case of
// ngx_pool_cleanup_t* cln = ngx_pool_cleanup_add(r->pool, sizeof(PeerContext));
// cln->handler = OnPeerClose;
// cln->data = ctx;
// Step 7: return response
ngx_buf_t* imgdata = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
imgdata->pos = result->EncodedBytes;
imgdata->last = result->EncodedBytes + result->Length;
imgdata->memory = 1;
imgdata->last_buf = 1;
r->headers_out.content_length_n = result->Length;
if (result->MIME != IMP_MIME_INTACT) {
char* content_type = NULL;
switch (result->MIME) {
case IMP_MIME_JPG:
content_type = "image/jpeg";
break;
case IMP_MIME_PNG:
content_type = "image/png";
break;
case IMP_MIME_JSON:
content_type = "application/json";
break;
case IMP_MIME_TEXT:
content_type = "text/plain";
break;
default:
#ifdef IMP_FEATURE_ADVANCED_IO
content_type = (char*)FreeImage_GetFIFMimeType(result->MIME);
#else
content_type = "image/png";
#endif
break;
}
r->headers_out.content_type.data = (u_char*)content_type;
r->headers_out.content_type.len = strlen(content_type);
}
ngx_chain_t out;
out.buf = imgdata;
out.next = NULL;
ctx->DataSent = 1;
ngx_int_t sendState = ngx_http_next_header_filter(r);
if (sendState == NGX_ERROR || sendState > NGX_OK || r->header_only) {
return NGX_ERROR;
} else {
return ngx_http_next_body_filter(r, &out);
}
}
static ngx_int_t OnChunkAvailable(ngx_http_request_t* r, ngx_chain_t* in) {
PeerContext* ctx = ngx_http_get_module_ctx(r, ngx_http_imgproc_module);
if (ctx->Image == NULL) {
ctx->Image = ngx_palloc(r->pool, ctx->Length);
if (ctx->Image == NULL) {
return NGX_ERROR;
}
ctx->Last = ctx->Image;
}
u_char *p = ctx->Last;
ngx_buf_t* b;
ngx_chain_t* cl;
for (cl = in; cl; cl = cl->next) {
b = cl->buf;
size_t size = b->last - b->pos;
size_t rest = ctx->Image + ctx->Length - p;
if (size > rest) {
OutputErrorString("imp::too big source", r);
return NGX_ERROR;
}
p = ngx_cpymem(p, b->pos, size);
b->pos += size;
if (b->last_buf) {
ctx->Last = p;
return NGX_OK;
}
}
ctx->Last = p;
r->connection->buffered |= NGX_HTTP_IMAGE_BUFFERED;
return NGX_AGAIN;
}
static char* OnParseWatermarkPosition(ngx_conf_t* input, ngx_command_t* cmd, void* _cfg) {
Position* result = (Position*)ngx_palloc(input->pool, sizeof(JobResult));
ngx_str_t* value = input->args->elts;
u_char* token = value[1].data;
if (token == NULL || (ngx_strcmp(token, "l") != 0 && ngx_strcmp(token, "c") != 0 && ngx_strcmp(token, "r") != 0)) {
goto error;
} else {
result->GravityX = token[0];
}
token = value[2].data;
if (token == NULL || (ngx_strcmp(token, "t") != 0 && ngx_strcmp(token, "c") != 0 && ngx_strcmp(token, "b") != 0)) {
goto error;
} else {
result->GravityY = token[0];
}
if (!value[3].len) {
goto error;
} else {
result->OffsetX = ngx_atoi(value[3].data, value[3].len);
}
if (!value[4].len) {
goto error;
} else {
result->OffsetY = ngx_atoi(value[4].data, value[4].len);
}
Config* config = _cfg;
config->WatermarkPosition = result;
return NGX_CONF_OK;
error:
ngx_conf_log_error(NGX_LOG_EMERG, input, 0, "imp::[watermark_position] parse error");
return NGX_CONF_OK;
}
static char* OnParseMaxTargetDims(ngx_conf_t* input, ngx_command_t* cmd, void* _cfg) {
Dimensions* result = (Dimensions*)ngx_palloc(input->pool, sizeof(Dimensions));
ngx_str_t* value = input->args->elts;
result->W = fmax(0, ngx_atoi(value[1].data, value[1].len));
result->H = fmax(0, ngx_atoi(value[2].data, value[2].len));
Config* config = _cfg;
config->MaxTargetDimensions = result;
return NGX_CONF_OK;
}