forked from evanmiller/nginx_circle_gif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.c
364 lines (307 loc) · 11.8 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
/*
* --------------------------------------------------------------------------
* Copyright (C) Evan Miller
* --------------------------------------------------------------------------
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#ifdef MAGICK_IMAGE7
#include <wand/MagickWand.h>
#else
#include <wand/MagickWand.h>
#endif
/*
* --------------------------------------------------------------------------
* Public interface to ngx
* --------------------------------------------------------------------------
*/
static char* ngx_http_circle_gif(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
typedef struct {
ngx_uint_t max_radius;
ngx_uint_t min_radius;
ngx_uint_t step_radius;
unsigned char** circle_templates;
size_t* circle_sizes;
ngx_flag_t enable;
} loc_conf_t;
static ngx_command_t commands[] = {
{ ngx_string("circle_gif"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_circle_gif,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("circle_gif_min_radius"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(loc_conf_t, min_radius),
NULL },
{ ngx_string("circle_gif_max_radius"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(loc_conf_t, max_radius),
NULL },
{ ngx_string("circle_gif_step_radius"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(loc_conf_t, step_radius),
NULL },
ngx_null_command
};
/*
* --------------------------------------------------------------------------
* Private implementation
* --------------------------------------------------------------------------
*/
static ngx_int_t preconf(ngx_conf_t *cf);
static ngx_int_t postconf(ngx_conf_t *cf);
static ngx_int_t init(loc_conf_t *cf);
static void* create_loc_conf(ngx_conf_t *cf);
static char* merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
static ngx_int_t handler(ngx_http_request_t *r);
static ngx_http_module_t module_ctx = {
preconf, /* preconfiguration */
postconf, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
create_loc_conf, /* create location configuration */
merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_circle_gif_module = {
NGX_MODULE_V1,
&module_ctx, /* module context */
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 char *
ngx_http_circle_gif(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = handler;
loc_conf_t *cglcf = conf;
cglcf->enable = 1;
return NGX_CONF_OK;
}
/*
* --------------------------------------------------------------------------
* Private implementation
* --------------------------------------------------------------------------
*/
enum colors { RED, GREEN, BLUE };
static ngx_uint_t radius2index(int req_radius, const loc_conf_t *cglcf);
static unsigned char *template(int req_radius, size_t* image_length_ptr, MagickWand *wand, PixelWand *bg_wand, PixelWand *fg_wand, DrawingWand *dwand);
static void colorize(unsigned char *image, unsigned char *bg, unsigned char *fg);
static ngx_int_t
preconf(ngx_conf_t *cf)
{
MagickWandGenesis();
return 0;
}
static ngx_int_t
postconf(ngx_conf_t *cf)
{
MagickWandTerminus();
return 0;
}
static ngx_int_t
init(loc_conf_t *cglcf)
{
if ((cglcf->circle_templates = malloc((1+radius2index(cglcf->max_radius, cglcf))*sizeof(unsigned char*))) == NULL ||
(cglcf->circle_sizes = malloc((1+radius2index(cglcf->max_radius, cglcf))*sizeof(size_t))) == NULL) {
perror("malloc()");
return NGX_ERROR;
}
MagickWand *wand = NewMagickWand();
PixelWand *bg_wand = NewPixelWand();
PixelWand *fg_wand = NewPixelWand();
DrawingWand *dwand = NewDrawingWand();
u_int i;
for (i=0; i<=radius2index(cglcf->max_radius, cglcf); ++i) {
cglcf->circle_templates[i] =
template(cglcf->min_radius + i*cglcf->step_radius, &cglcf->circle_sizes[i], wand, bg_wand, fg_wand, dwand);
}
DestroyDrawingWand( dwand );
DestroyPixelWand( fg_wand );
DestroyPixelWand( bg_wand );
DestroyMagickWand( wand );
return i;
}
static void *
create_loc_conf(ngx_conf_t *cf)
{
loc_conf_t *conf = ngx_pcalloc(cf->pool, sizeof(loc_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->min_radius = NGX_CONF_UNSET_UINT;
conf->max_radius = NGX_CONF_UNSET_UINT;
conf->step_radius = NGX_CONF_UNSET_UINT;
conf->enable = NGX_CONF_UNSET;
return conf;
}
static char *
merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
loc_conf_t *prev = parent;
loc_conf_t *conf = child;
ngx_conf_merge_uint_value(conf->min_radius, prev->min_radius, 20);
ngx_conf_merge_uint_value(conf->max_radius, prev->max_radius, 80);
ngx_conf_merge_uint_value(conf->step_radius, prev->step_radius, 2);
ngx_conf_merge_value(conf->enable, prev->enable, 0);
if (conf->min_radius < 1) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "min_radius must be equal or more than 1");
return NGX_CONF_ERROR;
}
if (conf->max_radius < conf->min_radius) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "max_radius must be equal or more than min_radius");
return NGX_CONF_ERROR;
}
if (conf->enable)
init(conf);
return NGX_CONF_OK;
}
static ngx_int_t
handler(ngx_http_request_t *r)
{
// all right, let's figure out what they're asking for
loc_conf_t *cglcf = ngx_http_get_module_loc_conf(r, ngx_http_circle_gif_module);
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
return NGX_HTTP_NOT_ALLOWED;
}
ngx_int_t rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK && rc != NGX_AGAIN) {
return rc;
}
if (r->headers_in.if_modified_since) {
return NGX_HTTP_NOT_MODIFIED;
}
/*
* uri: circles/BGxxxx/FGxxxx/R.gif
* where:
* BG - 6 hex RGB digits
* FG - 6 hex RGB digits
* R - radius: range 10 - 300
*/
char *digit = (char *)r->uri.data + r->uri.len - 1;
if (!(*digit-- == 'f' && *digit-- == 'i' && *digit-- == 'g' && *digit-- == '.')) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Invalid extension with %s", digit);
return NGX_HTTP_NOT_FOUND;
}
u_int req_radius = 0;
size_t i;
ngx_int_t power;
for (i=0, power=1; '0' <= *digit && *digit <= '9' && i < (r->uri.len - 7 - 7 - 4); i++, power *= 10) {
req_radius += (*digit-- - '0')*power;
}
u_int fg_color = strtol(digit -= 6, NULL, 16); // "XXXXXX"
u_int bg_color = strtol(digit -= 7, NULL, 16); // "XXXXXX/"
unsigned char bg[3];
bg[RED] = bg_color >> 16;
bg[GREEN] = bg_color >> 8;
bg[BLUE] = bg_color;
unsigned char fg[3];
fg[RED] = fg_color >> 16;
fg[GREEN] = fg_color >> 8;
fg[BLUE] = fg_color;
static const char mime_type[] = "image/gif";
r->headers_out.content_type.len = sizeof(mime_type) - 1;
r->headers_out.content_type.data = (u_char *) mime_type;
if (req_radius < cglcf->min_radius || req_radius > cglcf->max_radius) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Invalid radius %ui", req_radius);
return NGX_HTTP_NOT_FOUND;
}
int radius_index = radius2index(req_radius, cglcf);
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = cglcf->circle_sizes[radius_index];
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;
}
unsigned char *image = ngx_palloc(r->pool, cglcf->circle_sizes[radius_index]);
if (image == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate memory for circle image.");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_memcpy(image, cglcf->circle_templates[radius_index], cglcf->circle_sizes[radius_index]);
colorize(image, bg, fg);
ngx_buf_t *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 = image;
b->last = image + cglcf->circle_sizes[radius_index];
b->memory = 1;
b->last_buf = 1;
ngx_chain_t out = { b, NULL };
return ngx_http_output_filter(r, &out);
}
/*
* --------------------------------------------------------------------------
* Private helpers
* --------------------------------------------------------------------------
*/
static ngx_uint_t
radius2index(int req_radius, const loc_conf_t *cglcf)
{
return (req_radius - cglcf->min_radius) / cglcf->step_radius;
}
// build a B&W circle GIF in memory and return a pointer to it.
static unsigned char *
template(int req_radius, size_t* image_length_ptr, MagickWand *wand, PixelWand *bg_wand, PixelWand *fg_wand, DrawingWand *dwand)
{
float radius = req_radius - 0.5;
ClearDrawingWand(dwand);
PixelSetColor(bg_wand, "#000000");
PixelSetColor(fg_wand, "#ffffff");
MagickNewImage(wand, req_radius*2, req_radius*2, bg_wand);
DrawSetFillColor(dwand, fg_wand);
DrawCircle(dwand, radius, radius, 0, radius);
MagickDrawImage(wand, dwand);
MagickSetImageFormat(wand, "gif");
unsigned char *image = MagickGetImageBlob(wand, image_length_ptr);
MagickRemoveImage(wand);
return image;
}
static void
colorize(unsigned char *image, unsigned char *bg, unsigned char *fg)
{
// A note about GIFs: the size of the color table is stored in
// the least significant 3 bits of the 11th byte. The color table itself
// begins on the 14th byte; it has, successively, a red byte,
// blue byte, and green byte, and repeats. This is the color palette.
// We replace the white-ness of the standard image with the foreground
// color, and the blackness with the background color.
// See also: http://www.martinreddy.net/gfx/2d/GIF89a.txt
unsigned char *color_table_ptr = &image[13];
unsigned char *end_color_table_ptr = color_table_ptr + 3*(1 << ((image[10] & 0x7) + 1));
// yikes, pointer arithmetic! Not really necessary, but kinda fun
while (color_table_ptr < end_color_table_ptr) {
unsigned char whiteness = *color_table_ptr; // actually the red byte; our B&W template has equal parts r, g, and b
unsigned char blackness = ~whiteness;
// now assign new values to the red, green, and blue bytes
*color_table_ptr++ = (whiteness*fg[RED] + blackness*bg[RED]) /0xff;
*color_table_ptr++ = (whiteness*fg[GREEN] + blackness*bg[GREEN])/0xff;
*color_table_ptr++ = (whiteness*fg[BLUE] + blackness*bg[BLUE]) /0xff;
}
}