-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_magick_format.c
182 lines (146 loc) · 5.45 KB
/
mod_magick_format.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
/* 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_format module provides a filter that sets the
* output format of an image read by mod_magick.
*
* Author: Graham Leggett
*
* Basic configuration:
*
* <Location />
* <If "%{QUERY_STRING} =~ /./">
* SetOutputFilter MAGICK;MAGICK_FORMAT
* MagickFormat PNG
* </If>
* </Location>
*
* The MagickFormat directive sets the output format to be used. The list
* of supported formats can be found in the manual of the GraphicsMagick
* 'gm' command.
*/
#include <apr_strings.h>
#include "httpd.h"
#include "http_config.h"
#include "http_log.h"
#include "http_protocol.h"
#include "ap_expr.h"
#include "util_filter.h"
#include "mod_magick.h"
module AP_MODULE_DECLARE_DATA magick_format_module;
typedef struct magick_conf {
int format_set:1; /* has the format been set */
ap_expr_info_t *format; /* set to format */
} magick_conf;
static void *create_magick_dir_config(apr_pool_t *p, char *dummy)
{
magick_conf *new = (magick_conf *) apr_pcalloc(p, sizeof(magick_conf));
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->format = (add->format_set == 0) ? base->format : add->format;
new->format_set = add->format_set || base->format_set;
return new;
}
static const char *set_magick_format(cmd_parms *cmd, void *dconf, const char *arg)
{
magick_conf *conf = dconf;
const char *expr_err = NULL;
conf->format = ap_expr_parse_cmd(cmd, arg, AP_EXPR_FLAG_STRING_RESULT,
&expr_err, NULL);
if (expr_err) {
return apr_pstrcat(cmd->temp_pool,
"Cannot parse expression '", arg, "': ",
expr_err, NULL);
}
conf->format_set = 1;
return NULL;
}
static const command_rec magick_cmds[] = {
AP_INIT_TAKE1("MagickFormat", set_magick_format, NULL, ACCESS_CONF | OR_ALL,
"Set the format of the output image"), { NULL },
};
static apr_status_t magick_format_out_filter(ap_filter_t *f, apr_bucket_brigade *bb)
{
apr_bucket *e;
for (e = APR_BRIGADE_FIRST(bb);
e != APR_BRIGADE_SENTINEL(bb);
e = APR_BUCKET_NEXT(e))
{
/* EOS means we are done. */
if (APR_BUCKET_IS_EOS(e)) {
ap_remove_output_filter(f);
break;
}
/* Magick bucket? */
if (AP_BUCKET_IS_MAGICK(e)) {
magick_conf *conf = ap_get_module_config(f->r->per_dir_config,
&magick_format_module);
ap_bucket_magick *m = e->data;
const char *format;
char *mime;
if (conf->format) {
const char *err = NULL;
format = ap_expr_str_exec(f->r, conf->format, &err);
if (err) {
ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, f->r,
"Failure while evaluating the format expression for '%s', "
"format ignored: %s", f->r->uri, err);
continue;
}
}
else {
ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, f->r,
"No format expression for '%s', "
"format ignored", f->r->uri);
continue;
}
if (!MagickSetImageFormat(m->wand, format)) {
char *description;
ExceptionType severity;
description = MagickGetException(m->wand, &severity);
ap_log_rerror(APLOG_MARK, APLOG_ERR, APR_EGENERAL, f->r,
"MagickSetImageFormat: %s (severity %d)", description,
severity);
MagickRelinquishMemory(description);
return APR_EGENERAL;
}
mime = MagickToMime(format);
ap_set_content_type(f->r, apr_pstrdup(f->r->pool, mime));
MagickRelinquishMemory(mime);
}
}
return ap_pass_brigade(f->next, bb);
}
static void register_hooks(apr_pool_t *p)
{
ap_register_output_filter("MAGICK_FORMAT", magick_format_out_filter, NULL,
AP_FTYPE_CONTENT_SET);
}
AP_DECLARE_MODULE(magick_format) =
{
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 */
};