-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathngx_http_gif_magick_module.c
449 lines (343 loc) · 15.6 KB
/
ngx_http_gif_magick_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
/*
ngx_http_gif_magick - Dynamic (animated) GIF resizing with nginx
Michael Schenck [ [email protected], [email protected] ]
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <wand/MagickWand.h>
#define GIF_MAGICK_DISABLED 0
#define GIF_MAGICK_ENABLED 1
#define GIF_MAGICK_READ 2
#define GIF_MAGICK_RESIZE 3
#define GIF_MAGICK_SEND 4
#define GIF_MAGICK_DONE 5
#define GIF_MAGICK_DEFAULT_BUFFER_SIZE (size_t)20971520 /* 20 MB TODO:configurable */
typedef struct {
ngx_flag_t enabled; // (default: DISABLED) enable plugin on a given location
size_t buffer_size; // This determines the large image that will be processed
ngx_int_t width; // Target resize width
ngx_int_t height; // Target resize height (a sloppy safety net, retains aspect ratio)
} ngx_http_gif_magick_loc_conf_t;
typedef struct {
u_char *gif_data;
u_char *gif_last;
size_t gif_size;
size_t buffer_size;
ngx_int_t width; // Target resize width
ngx_int_t height; // Target resize height (a sloppy safety net, retains aspect ratio)
ngx_uint_t status; // Processing status
} ngx_http_gif_magick_ctx_t;
static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
static void * ngx_http_gif_magick_create_loc_conf( ngx_conf_t *cf );
static char * ngx_http_gif_magick_merge_loc_conf( ngx_conf_t *cf, void *parent, void *child );
static ngx_int_t ngx_http_gif_magick_read_image( ngx_http_request_t *request, ngx_chain_t *in_chain );
static ngx_int_t ngx_http_gif_magick_resize_image( ngx_http_request_t *request, ngx_chain_t *in_chain );
static ngx_int_t ngx_http_gif_magick_send_image( ngx_http_request_t *request, ngx_chain_t *in_chain );
static ngx_int_t ngx_http_gif_magick_header_filter( ngx_http_request_t *request );
static ngx_int_t ngx_http_gif_magick_body_filter( ngx_http_request_t *request, ngx_chain_t *in_chain );
static ngx_int_t ngx_http_gif_magick_init( ngx_conf_t *cf );
static ngx_command_t ngx_http_gif_magick_commands[] = {
{
ngx_string( "gif_magick" ), // 'gif_magick_resize' would be preferable, see if linked to conf_t
NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
{
ngx_string( "gif_magick_buffer" ), // 'gif_magick_resize' would be preferable, see if linked to conf_t
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof( ngx_http_gif_magick_loc_conf_t, buffer_size ),
NULL
},
{
ngx_string( "gif_magick_width" ), // 'gif_magick_resize' would be preferable, see if linked to conf_t
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof( ngx_http_gif_magick_loc_conf_t, width ),
NULL
},
{
ngx_string( "gif_magick_height" ), // 'gif_magick_resize' would be preferable, see if linked to conf_t
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof( ngx_http_gif_magick_loc_conf_t, height ),
NULL
},
ngx_null_command
};
static ngx_http_module_t ngx_http_gif_magick_module_ctx = {
NULL, // preconfiguration
ngx_http_gif_magick_init, // postconfiguration
NULL, // creating the main conf ( i.e., do a malloc and set defaults )
NULL, // initializing the main conf ( override the defaults with what's in nginx.conf )
NULL, // creating the server conf
NULL, // merging it with the main conf
ngx_http_gif_magick_create_loc_conf, // creating the location conf
ngx_http_gif_magick_merge_loc_conf, // merging it with the server conf
};
ngx_module_t ngx_http_gif_magick_module = {
NGX_MODULE_V1,
&ngx_http_gif_magick_module_ctx,
ngx_http_gif_magick_commands,
NGX_HTTP_MODULE,
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 void *
ngx_http_gif_magick_create_loc_conf( ngx_conf_t *cf )
{
ngx_http_gif_magick_loc_conf_t *conf;
conf = ngx_pcalloc( cf->pool, sizeof( ngx_http_gif_magick_loc_conf_t ) );
if ( conf == NULL ) {
return NGX_CONF_ERROR;
}
conf->enabled = NGX_CONF_UNSET;
conf->width = NGX_CONF_UNSET;
conf->height = NGX_CONF_UNSET;
conf->buffer_size = NGX_CONF_UNSET_SIZE;
return conf;
}
static char *
ngx_http_gif_magick_merge_loc_conf( ngx_conf_t *cf, void *parent, void *child )
{
ngx_http_gif_magick_loc_conf_t *prev = parent;
ngx_http_gif_magick_loc_conf_t *conf = child;
ngx_conf_merge_value( conf->enabled, prev->enabled, GIF_MAGICK_DISABLED );
ngx_conf_merge_size_value( conf->buffer_size, prev->buffer_size, GIF_MAGICK_DEFAULT_BUFFER_SIZE );
ngx_conf_merge_value( conf->width, prev->width, NGX_CONF_UNSET);
ngx_conf_merge_value( conf->height, prev->height, NGX_CONF_UNSET );
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_gif_magick_header_filter( ngx_http_request_t *request )
{
ngx_http_gif_magick_ctx_t *ctx;
ngx_http_gif_magick_loc_conf_t *gif_magick_conf;
ngx_log_debug0( NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "GIF_MAGICK header_filter.");
// Return quickly if not needed
if ( request->headers_out.status == NGX_HTTP_NOT_MODIFIED ) // Not Modified
return ngx_http_next_header_filter( request );
ctx = ngx_http_get_module_ctx( request, ngx_http_gif_magick_module );
if ( ctx == NULL ) {
ctx = ngx_pcalloc( request->pool, sizeof( ngx_http_gif_magick_ctx_t ) ); // Instantiate context
if ( ctx == NULL ) {
ngx_log_error( NGX_LOG_ERR, request->connection->log, 0, "Failed to allocate new ctx" );
return NGX_ERROR;
}
ngx_http_set_ctx( request, ctx, ngx_http_gif_magick_module );
}
gif_magick_conf = ngx_http_get_module_loc_conf( request, ngx_http_gif_magick_module );
if ( gif_magick_conf->enabled == GIF_MAGICK_DISABLED ) { // gif_magick disabled
ctx->status = GIF_MAGICK_DISABLED;
return ngx_http_next_header_filter( request );
}
// Prepare for resizing
ngx_log_debug0( NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "gif_magick preparing for resizing.");
ctx->status = GIF_MAGICK_ENABLED;
ctx->buffer_size = gif_magick_conf->buffer_size;
ctx->width = gif_magick_conf->width;
ctx->height = gif_magick_conf->height;
// TODO: add logic to verify the received body and headers
return NGX_OK;
}
// NOTE: The creation/clean-op of a MagickWand per run is intentional (and fairly cheap, all-in-all)
static ngx_int_t
ngx_http_gif_magick_resize_image( ngx_http_request_t *request, ngx_chain_t *in_chain )
{
ngx_http_gif_magick_ctx_t *ctx;
MagickWand *magick_wand;
MagickSizeType magick_length;
ngx_int_t target_width, target_height;
ctx = ngx_http_get_module_ctx( request, ngx_http_gif_magick_module );
if ( ctx == NULL ) {
ngx_log_error( NGX_LOG_ERR, request->connection->log, 0, "Failed to retrieve ctx for resizing" );
return NGX_ERROR;
}
// Initialize this Wand
ngx_log_debug0( NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "gif_magick initializing wand and loading image data.");
MagickWandGenesis();
magick_wand = NewMagickWand();
// Load original image into Wand
if ( MagickReadImageBlob( magick_wand, (void*)ctx->gif_data, ctx->buffer_size) == MagickFalse ) return NGX_ERROR;
// Determine target dimensions
if ( ( ctx->width != NGX_CONF_UNSET ) &&
( ctx->height != NGX_CONF_UNSET ) ) {
target_width = ctx->width;
target_height = ctx->height;
} else if ( ctx->width != NGX_CONF_UNSET ) {
target_width = ctx->width;
target_height = (ngx_int_t)( (float)ctx->width * (float)MagickGetImageHeight( magick_wand ) / (float)MagickGetImageWidth( magick_wand ) );
} else if ( ctx->height != NGX_CONF_UNSET ) {
target_height = ctx->height;
target_width = (ngx_int_t)( (float)ctx->height * (float)MagickGetImageWidth( magick_wand ) / (float)MagickGetImageHeight( magick_wand ) );
} else {
ngx_log_error( NGX_LOG_ERR, request->connection->log, 0, "gif_magick fed an invalid dimensions: NEITHER set!");
return NGX_ERROR;
}
ngx_log_debug2( NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "gif_magick target dimensions (%uz x %uz).", target_width, target_height );
// Resize all frames
MagickCoalesceImages( magick_wand );
MagickSetFirstIterator( magick_wand );
do {
MagickAdaptiveResizeImage( magick_wand, target_width, target_height );
} while ( MagickNextImage( magick_wand ) != MagickFalse );
MagickStripImage( magick_wand );
MagickEqualizeImage( magick_wand );
MagickOptimizeImageLayers( magick_wand ); // Fix any optimizations we broke in coalesce
// Update image content and size
MagickGetImageLength( magick_wand, &magick_length );
ctx->gif_size = (size_t)magick_length;
ctx->gif_data = MagickGetImagesBlob( magick_wand, &ctx->gif_size );
ngx_log_debug1( NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "gif_magic updated context image size to %uz B from wand.", ctx->gif_size);
// Magick time over ... cleaning up
magick_wand = DestroyMagickWand( magick_wand );
MagickWandTerminus();
return NGX_OK;
}
static ngx_int_t
ngx_http_gif_magick_read_image( ngx_http_request_t *request, ngx_chain_t *in_chain )
{
ngx_http_gif_magick_ctx_t *ctx;
u_char *gif_pos;
ngx_buf_t *buffer;
ngx_chain_t *chain_link;
size_t chunk_size, remaining;
request->connection->buffered &= ~0x08;
// Retrieve (or allocate if missing) context
ctx = ngx_http_get_module_ctx( request, ngx_http_gif_magick_module );
if ( ctx->gif_data == NULL ) {
ngx_log_debug1( NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "gif_magick allocating %uz B for image data" , ctx->buffer_size );
ctx->gif_data = ngx_palloc( request->pool, ctx->buffer_size );
if ( ctx->gif_data == NULL ) {
ngx_log_error( NGX_LOG_ERR, request->connection->log, 0, "Failed to allocate response buffer.");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ctx->gif_size = 0;
ctx->gif_last = ctx->gif_data;
}
gif_pos = ctx->gif_last;
for ( chain_link = in_chain; chain_link; chain_link = chain_link->next) {
buffer = chain_link->buf;
chunk_size = buffer->last - buffer->pos;
remaining = ctx->gif_data + ctx->buffer_size - gif_pos;
if ( remaining < chunk_size ) chunk_size = remaining;
ngx_log_debug2( NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "gif_magick context image buffer: Remaining: %uz B - chunk size: %uz B" , remaining, chunk_size );
gif_pos = ngx_cpymem( gif_pos, buffer->pos, chunk_size );
buffer->pos += chunk_size;
ctx->gif_size += chunk_size * sizeof( u_char );
if ( buffer->last_buf ) {
ctx->gif_last = gif_pos;
ngx_log_debug1( NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "gif_magick read image ( %uz B ) successful.", ctx->gif_size );
return NGX_OK;
}
}
ctx->gif_last = gif_pos;
request->connection->buffered |= 0x08;
return NGX_AGAIN;
}
static ngx_int_t
ngx_http_gif_magick_send_image( ngx_http_request_t *request, ngx_chain_t *in_chain )
{
ngx_http_gif_magick_ctx_t *ctx;
ngx_buf_t *buffer;
ngx_chain_t out_chain;
ngx_int_t result;
ngx_str_t content_type = ngx_string( "image/gif" );
ctx = ngx_http_get_module_ctx( request, ngx_http_gif_magick_module );
if ( ctx == NULL ) ngx_http_next_body_filter( request, in_chain );
// Set response body
buffer = ngx_pcalloc( request->pool, sizeof( ngx_buf_t ) );
if ( buffer == NULL ) return NGX_ERROR;
buffer->pos = ctx->gif_data;
buffer->last = ctx->gif_data + ctx->gif_size;
buffer->memory = 1;
buffer->last_buf = 1;
out_chain.buf = buffer;
out_chain.next = NULL;
// Set response headers
request->headers_out.content_length_n = ctx->gif_size;
ngx_log_debug1( NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "gif_magick response content-lenght: %d", ctx->gif_size );
if ( request->headers_out.content_length ) request->headers_out.content_length->hash = 0;
request->headers_out.content_length = NULL;
request->headers_out.content_type_len = content_type.len;
request->headers_out.content_type = content_type;
request->headers_out.content_type_lowcase = NULL;
request->headers_out.status = NGX_HTTP_OK;
result = ngx_http_next_header_filter( request );
if ( result == NGX_ERROR || request->header_only ) return NGX_ERROR;
ctx->status = GIF_MAGICK_DONE;
return ngx_http_next_body_filter( request, &out_chain );
}
static ngx_int_t
ngx_http_gif_magick_body_filter ( ngx_http_request_t *request, ngx_chain_t *in_chain )
{
ngx_http_gif_magick_ctx_t *ctx;
ngx_int_t result;
ngx_log_debug0( NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "GIF_MAGICK body_filter.");
// Return quickly if not needed
if ( in_chain == NULL ) return ngx_http_next_body_filter( request, in_chain );
ctx = ngx_http_get_module_ctx( request, ngx_http_gif_magick_module );
if ( ctx == NULL ) ngx_http_next_body_filter( request, in_chain );
switch( ctx->status ) {
case GIF_MAGICK_DISABLED:
return ngx_http_next_body_filter( request, in_chain );
case GIF_MAGICK_ENABLED:
ngx_log_debug0( NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "gif_magick starting to read image.");
ctx->status = GIF_MAGICK_READ;
case GIF_MAGICK_READ:
ngx_log_debug0( NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "gif_magick reading image.");
result = ngx_http_gif_magick_read_image( request, in_chain );
if ( result == NGX_ERROR ) {
ngx_log_error( NGX_LOG_ERR, request->connection->log, 0, "Failed to read image file.");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if ( result == NGX_AGAIN ) return NGX_OK;
ctx->status = GIF_MAGICK_RESIZE;
//return ngx_http_next_body_filter( request, in_chain );
case GIF_MAGICK_RESIZE:
ngx_log_debug0( NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "gif_magick resizing.");
result = ngx_http_gif_magick_resize_image( request, in_chain );
if ( result == NGX_ERROR ) {
ngx_log_error( NGX_LOG_ERR, request->connection->log, 0, "Failed to resize image file.");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ctx->status = GIF_MAGICK_SEND;
//return ngx_http_next_body_filter( request, in_chain );
case GIF_MAGICK_SEND:
ngx_log_debug0( NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "gif_magick sending response.");
return ngx_http_gif_magick_send_image( request, in_chain );
case GIF_MAGICK_DONE:
ngx_log_debug0( NGX_LOG_DEBUG_HTTP, request->connection->log, 0, "gif_magick complete.");
return ngx_http_next_body_filter( request, in_chain );
default:
//FIXME
ngx_log_error( NGX_LOG_ERR, request->connection->log, 0, "CONTEXT STATUS 'default' REACHED.");
result = ngx_http_next_body_filter( request, in_chain );
if ( result == NGX_OK ) result = NGX_ERROR;
return result;
}
return ngx_http_next_body_filter( request, in_chain );
}
static ngx_int_t
ngx_http_gif_magick_init( ngx_conf_t *cf )
{
ngx_http_next_header_filter = ngx_http_top_header_filter;
ngx_http_top_header_filter = ngx_http_gif_magick_header_filter;
ngx_http_next_body_filter = ngx_http_top_body_filter;
ngx_http_top_body_filter = ngx_http_gif_magick_body_filter;
return NGX_OK;
}