-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_magick.c
418 lines (327 loc) · 11.6 KB
/
mod_magick.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
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* The Apache mod_magick module provides image filtering for the Apache
* httpd server.
*
* Author: Graham Leggett
*
* Basic configuration:
*
* <Location />
* <IfModule magick_module>
* <If "%{QUERY_STRING} =~ /./">
* SetOutputFilter MAGICK
* AddMagickOption jpeg:preserve-settings true
* </If>
* </IfModule>
* </Location>
*
* The MAGICK module converts a response into a magick bucket, which can be
* transformed by specific downstream magick filters to modify the image.
* The first filter that attempts to read the bucket will cause the output
* image to be rendered.
*
* The AddMagickOption allows the setting of options that affect the
* operation of GraphicsMagick. The options accepted are those as documented
* under the -define option in the gm tool.
*
* The MagickMaxSize option sets the largest size the source image is allowed to
* be. Beyond this size requests will be rejected to prevent the processing of
* huge images.
*/
#include <apr.h>
#include <apr_hash.h>
#include <apr_strings.h>
#include "httpd.h"
#include "http_config.h"
#include "http_log.h"
#include "util_filter.h"
#include "ap_expr.h"
#include "mod_magick.h"
module AP_MODULE_DECLARE_DATA magick_module;
#define DEFAULT_MAX_SIZE 10*1024*1024
typedef struct magick_conf {
int size_set:1; /* has the size been set */
apr_off_t size; /* maximum image size */
apr_hash_t *options; /* options */
} magick_conf;
typedef struct magick_option {
const char *format; /* set to format */
const char *key; /* set to key */
ap_expr_info_t *value; /* set to value */
} magick_option;
typedef struct magick_do {
request_rec *r;
MagickWand *wand;
} magick_do;
typedef struct magick_ctx {
apr_bucket_brigade *bb;
apr_bucket_brigade *mbb;
apr_size_t seen_bytes;
int seen_buckets;
int seen_eos;
} magick_ctx;
static void *create_magick_dir_config(apr_pool_t *p, char *dummy)
{
magick_conf *new = (magick_conf *) apr_pcalloc(p, sizeof(magick_conf));
new->size = DEFAULT_MAX_SIZE;
new->options = apr_hash_make(p);
return (void *) new;
}
static void *merge_magick_dir_config(apr_pool_t *p, void *basev, void *addv)
{
magick_conf *new = (magick_conf *) apr_pcalloc(p, sizeof(magick_conf));
magick_conf *add = (magick_conf *) addv;
magick_conf *base = (magick_conf *) basev;
new->size = (add->size_set == 0) ? base->size : add->size;
new->size_set = add->size_set || base->size_set;
new->options = apr_hash_overlay(p, add->options, base->options);
return new;
}
static const char *set_magick_size(cmd_parms *cmd, void *dconf, const char *arg)
{
magick_conf *conf = dconf;
if (APR_SUCCESS != apr_strtoff(&(conf->size), arg, NULL, 10) || conf->size
<= 0) {
return "MagickMaxSize must be a size in bytes, and greater than zero";
}
conf->size_set = 1;
return NULL;
}
static const char *add_magick_option(cmd_parms *cmd, void *dconf,
const char *key, const char *value)
{
magick_conf *conf = dconf;
const char *expr_err = NULL;
magick_option *option = apr_palloc(cmd->pool, sizeof(magick_option));
option->key = strchr(key, ':');
if (!option->key) {
return apr_psprintf(cmd->pool, "Key '%s' needs a colon character", key);
}
option->format = apr_pstrndup(cmd->pool, key, option->key - key);
option->key++;
option->value = ap_expr_parse_cmd(cmd, value, AP_EXPR_FLAG_STRING_RESULT,
&expr_err, NULL);
if (expr_err) {
return apr_pstrcat(cmd->temp_pool,
"Cannot parse expression '", value, "': ",
expr_err, NULL);
}
apr_hash_set(conf->options, key, APR_HASH_KEY_STRING, option);
return NULL;
}
static const command_rec magick_cmds[] = {
AP_INIT_TAKE1("MagickMaxSize", set_magick_size, NULL, ACCESS_CONF,
"Maximum size of the image processed by the magick filter"),
AP_INIT_TAKE2("AddMagickOption", add_magick_option, NULL, ACCESS_CONF,
"Add key/value option to be used by the filter."), { NULL },
};
static apr_status_t magick_bucket_read(apr_bucket *b, const char **str,
apr_size_t *len, apr_read_type_e block)
{
ap_bucket_magick *m = b->data;
if (m->wand) {
m->base = (char *)MagickWriteImageBlob(m->wand,
&b->length);
m->alloc_len = b->length;
DestroyMagickWand(m->wand);
m->wand = NULL;
/* morph into a magick heap bucket from now on */
b->type = &ap_bucket_type_magick_heap;
}
*str = m->base + b->start;
*len = b->length;
return APR_SUCCESS;
}
static void magick_bucket_destroy(void *data)
{
ap_bucket_magick *m = data;
if (apr_bucket_shared_destroy(m)) {
if (m->wand) {
DestroyMagickWand(m->wand);
m->wand = NULL;
}
if (m->base) {
MagickRelinquishMemory((void *)m->base);
m->base = NULL;
}
apr_bucket_free(m);
}
}
AP_DECLARE_DATA const apr_bucket_type_t ap_bucket_type_magick = {
"MAGICK", 5, APR_BUCKET_DATA,
magick_bucket_destroy,
magick_bucket_read,
apr_bucket_setaside_noop, /* don't need to setaside thanks to the cleanup*/
apr_bucket_shared_split,
apr_bucket_shared_copy
};
AP_DECLARE_DATA const apr_bucket_type_t ap_bucket_type_magick_heap = {
"MAGICK_HEAP", 5, APR_BUCKET_DATA,
magick_bucket_destroy,
magick_bucket_read,
apr_bucket_setaside_noop, /* don't need to setaside thanks to the cleanup*/
apr_bucket_shared_split,
apr_bucket_shared_copy
};
AP_DECLARE(apr_bucket *) ap_bucket_magick_make(apr_bucket *b)
{
ap_bucket_magick *m;
m = apr_bucket_alloc(sizeof(*m), b->list);
m->base = NULL;
b = apr_bucket_shared_make(b, m, 0, -1);
b->type = &ap_bucket_type_magick;
/* pre-initialize heap bucket member */
m->alloc_len = 0;
m->base = NULL;
m->wand = NewMagickWand();
return b;
}
AP_DECLARE(apr_bucket *) ap_bucket_magick_create(apr_bucket_alloc_t *list)
{
apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
APR_BUCKET_INIT(b);
b->free = apr_bucket_free;
b->list = list;
return ap_bucket_magick_make(b);
}
static int magick_set_option(void *ctx, const void *key, apr_ssize_t klen, const void *val)
{
magick_do *mdo = ctx;
const magick_option *option = val;
const char *err = NULL;
const char *str;
str = ap_expr_str_exec(mdo->r, option->value, &err);
if (err) {
ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, mdo->r,
"Failure while evaluating the option value expression for '%s', "
"option ignored: %s", mdo->r->uri, err);
}
else {
MagickSetImageOption(mdo->wand, option->format, option->key, str);
}
return 1;
}
static apr_status_t magick_out_filter(ap_filter_t *f, apr_bucket_brigade *bb)
{
request_rec *r = f->r;
magick_ctx *ctx = f->ctx;
magick_conf *conf = ap_get_module_config(f->r->per_dir_config, &magick_module);
apr_status_t rv = APR_SUCCESS;
apr_size_t size;
/* Do nothing if asked to filter nothing. */
if (APR_BRIGADE_EMPTY(bb)) {
return ap_pass_brigade(f->next, bb);
}
/* first time in? create a context */
if (!ctx) {
ctx = f->ctx = apr_pcalloc(r->pool, sizeof(*ctx));
ctx->bb = apr_brigade_create(r->pool, f->c->bucket_alloc);
ctx->mbb = apr_brigade_create(r->pool, f->c->bucket_alloc);
}
while (APR_SUCCESS == rv && !APR_BRIGADE_EMPTY(bb)) {
const char *data;
apr_bucket *e;
e = APR_BRIGADE_FIRST(bb);
/* EOS means we are done. */
if (APR_BUCKET_IS_EOS(e)) {
ctx->seen_eos = 1;
break;
}
/* A flush takes precedence over buffering */
if (APR_BUCKET_IS_FLUSH(e)) {
/* pass the bucket across */
APR_BUCKET_REMOVE(e);
APR_BRIGADE_INSERT_TAIL(ctx->mbb, e);
continue;
}
/* metadata buckets are preserved as is */
if (APR_BUCKET_IS_METADATA(e)) {
/* pass the bucket across */
APR_BUCKET_REMOVE(e);
APR_BRIGADE_INSERT_TAIL(ctx->mbb, e);
continue;
}
if (APR_SUCCESS == (rv = apr_bucket_read(e, &data, &size,
APR_BLOCK_READ))) {
ctx->seen_bytes += size;
if (ctx->seen_bytes > conf->size) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, APR_ENOSPC, r,
"Response is too large (>%" APR_OFF_T_FMT
"), aborting request.", conf->size);
return APR_ENOSPC;
}
/* pass the bucket across */
APR_BUCKET_REMOVE(e);
APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
}
}
if (ctx->seen_eos) {
/* keep the metadata and flush buckets */
APR_BRIGADE_PREPEND(bb, ctx->mbb);
if (ctx->seen_bytes) {
unsigned char *data;
apr_bucket *e;
ap_bucket_magick *m;
magick_do mdo;
/* insert wand bucket */
e = ap_bucket_magick_create(r->connection->bucket_alloc);
APR_BRIGADE_INSERT_HEAD(bb, e);
m = e->data;
data = MagickMalloc(ctx->seen_bytes);
apr_brigade_flatten(ctx->bb, (char *) data, &ctx->seen_bytes);
apr_brigade_cleanup(ctx->bb);
/* pass flags needed to pass through parameters from the
* original image.
*/
mdo.r = f->r;
mdo.wand = m->wand;
apr_hash_do(magick_set_option, &mdo, conf->options);
if (!MagickReadImageBlob(m->wand, data, ctx->seen_bytes)) {
char *description;
ExceptionType severity;
description = MagickGetException(m->wand, &severity);
ap_log_rerror(APLOG_MARK, APLOG_ERR, APR_EGENERAL, r,
"MagickReadImageBlob: %s (severity %d)", description,
severity);
MagickRelinquishMemory(description);
MagickFree(data);
return APR_EGENERAL;
}
MagickFree(data);
}
/* pass what we have left down the chain */
ap_remove_output_filter(f);
return ap_pass_brigade(f->next, bb);
}
return rv;
}
static void register_hooks(apr_pool_t *p)
{
ap_register_output_filter("MAGICK", magick_out_filter, NULL,
AP_FTYPE_CONTENT_SET);
}
AP_DECLARE_MODULE(magick) =
{
STANDARD20_MODULE_STUFF,
create_magick_dir_config, /* dir config creater */
merge_magick_dir_config, /* dir merger --- default is to override */
NULL, /* server config */
NULL, /* merge server config */
magick_cmds, /* command apr_table_t */
register_hooks /* register hooks */
};