Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_image_filter_module.c @ 2975:2c0f09aa8574
allow underscore in request method
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 13 Jul 2009 09:33:34 +0000 |
parents | 0070504324d8 |
children | fa8503fd3647 |
rev | line source |
---|---|
2788 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
10 |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
11 #include <gd.h> |
2788 | 12 |
13 | |
14 #define NGX_HTTP_IMAGE_OFF 0 | |
15 #define NGX_HTTP_IMAGE_TEST 1 | |
16 #define NGX_HTTP_IMAGE_SIZE 2 | |
17 #define NGX_HTTP_IMAGE_RESIZE 3 | |
18 #define NGX_HTTP_IMAGE_CROP 4 | |
19 | |
20 | |
21 #define NGX_HTTP_IMAGE_START 0 | |
22 #define NGX_HTTP_IMAGE_READ 1 | |
23 #define NGX_HTTP_IMAGE_PROCESS 2 | |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
24 #define NGX_HTTP_IMAGE_PASS 3 |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
25 #define NGX_HTTP_IMAGE_DONE 4 |
2788 | 26 |
27 | |
28 #define NGX_HTTP_IMAGE_NONE 0 | |
29 #define NGX_HTTP_IMAGE_JPEG 1 | |
30 #define NGX_HTTP_IMAGE_GIF 2 | |
31 #define NGX_HTTP_IMAGE_PNG 3 | |
32 | |
33 | |
34 #define NGX_HTTP_IMAGE_BUFFERED 0x08 | |
35 | |
36 | |
37 typedef struct { | |
38 ngx_uint_t filter; | |
39 ngx_uint_t width; | |
40 ngx_uint_t height; | |
2848 | 41 ngx_int_t jpeg_quality; |
2788 | 42 |
43 size_t buffer_size; | |
44 } ngx_http_image_filter_conf_t; | |
45 | |
46 | |
47 typedef struct { | |
48 u_char *image; | |
49 u_char *last; | |
50 | |
51 size_t length; | |
52 | |
53 ngx_uint_t width; | |
54 ngx_uint_t height; | |
55 | |
56 ngx_uint_t phase; | |
57 ngx_uint_t type; | |
58 } ngx_http_image_filter_ctx_t; | |
59 | |
60 | |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
61 static ngx_int_t ngx_http_image_send(ngx_http_request_t *r, |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
62 ngx_http_image_filter_ctx_t *ctx, ngx_chain_t *in); |
2788 | 63 static ngx_uint_t ngx_http_image_test(ngx_http_request_t *r, ngx_chain_t *in); |
64 static ngx_int_t ngx_http_image_read(ngx_http_request_t *r, ngx_chain_t *in); | |
65 static ngx_buf_t *ngx_http_image_process(ngx_http_request_t *r); | |
66 static ngx_buf_t *ngx_http_image_json(ngx_http_request_t *r, | |
67 ngx_http_image_filter_ctx_t *ctx); | |
68 static ngx_buf_t *ngx_http_image_asis(ngx_http_request_t *r, | |
69 ngx_http_image_filter_ctx_t *ctx); | |
70 static void ngx_http_image_length(ngx_http_request_t *r, ngx_buf_t *b); | |
71 static ngx_int_t ngx_http_image_size(ngx_http_request_t *r, | |
72 ngx_http_image_filter_ctx_t *ctx); | |
73 | |
74 static ngx_buf_t *ngx_http_image_resize(ngx_http_request_t *r, | |
75 ngx_http_image_filter_ctx_t *ctx); | |
76 static gdImagePtr ngx_http_image_source(ngx_http_request_t *r, | |
77 ngx_http_image_filter_ctx_t *ctx); | |
78 static gdImagePtr ngx_http_image_new(ngx_http_request_t *r, int w, int h, | |
79 int colors); | |
80 static u_char *ngx_http_image_out(ngx_http_request_t *r, ngx_uint_t type, | |
81 gdImagePtr img, int *size); | |
82 static void ngx_http_image_cleanup(void *data); | |
83 | |
84 | |
85 static void *ngx_http_image_filter_create_conf(ngx_conf_t *cf); | |
86 static char *ngx_http_image_filter_merge_conf(ngx_conf_t *cf, void *parent, | |
87 void *child); | |
88 static char *ngx_http_image_filter(ngx_conf_t *cf, ngx_command_t *cmd, | |
89 void *conf); | |
90 static ngx_int_t ngx_http_image_filter_init(ngx_conf_t *cf); | |
91 | |
92 | |
93 static ngx_command_t ngx_http_image_filter_commands[] = { | |
94 | |
95 { ngx_string("image_filter"), | |
96 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE13, | |
97 ngx_http_image_filter, | |
98 NGX_HTTP_LOC_CONF_OFFSET, | |
99 0, | |
100 NULL }, | |
101 | |
2848 | 102 { ngx_string("image_filter_jpeg_quality"), |
103 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
104 ngx_conf_set_num_slot, | |
105 NGX_HTTP_LOC_CONF_OFFSET, | |
106 offsetof(ngx_http_image_filter_conf_t, jpeg_quality), | |
107 NULL }, | |
108 | |
2788 | 109 { ngx_string("image_filter_buffer"), |
110 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
111 ngx_conf_set_size_slot, | |
112 NGX_HTTP_LOC_CONF_OFFSET, | |
113 offsetof(ngx_http_image_filter_conf_t, buffer_size), | |
114 NULL }, | |
115 | |
116 ngx_null_command | |
117 }; | |
118 | |
119 | |
120 static ngx_http_module_t ngx_http_image_filter_module_ctx = { | |
121 NULL, /* preconfiguration */ | |
122 ngx_http_image_filter_init, /* postconfiguration */ | |
123 | |
124 NULL, /* create main configuration */ | |
125 NULL, /* init main configuration */ | |
126 | |
127 NULL, /* create server configuration */ | |
128 NULL, /* merge server configuration */ | |
129 | |
130 ngx_http_image_filter_create_conf, /* create location configuration */ | |
131 ngx_http_image_filter_merge_conf /* merge location configuration */ | |
132 }; | |
133 | |
134 | |
135 ngx_module_t ngx_http_image_filter_module = { | |
136 NGX_MODULE_V1, | |
137 &ngx_http_image_filter_module_ctx, /* module context */ | |
138 ngx_http_image_filter_commands, /* module directives */ | |
139 NGX_HTTP_MODULE, /* module type */ | |
140 NULL, /* init master */ | |
141 NULL, /* init module */ | |
142 NULL, /* init process */ | |
143 NULL, /* init thread */ | |
144 NULL, /* exit thread */ | |
145 NULL, /* exit process */ | |
146 NULL, /* exit master */ | |
147 NGX_MODULE_V1_PADDING | |
148 }; | |
149 | |
150 | |
151 static ngx_http_output_header_filter_pt ngx_http_next_header_filter; | |
152 static ngx_http_output_body_filter_pt ngx_http_next_body_filter; | |
153 | |
154 | |
155 static ngx_str_t ngx_http_image_types[] = { | |
156 ngx_string("image/jpeg"), | |
157 ngx_string("image/gif"), | |
158 ngx_string("image/png") | |
159 }; | |
160 | |
161 | |
162 static ngx_int_t | |
163 ngx_http_image_header_filter(ngx_http_request_t *r) | |
164 { | |
165 off_t len; | |
166 ngx_http_image_filter_ctx_t *ctx; | |
167 ngx_http_image_filter_conf_t *conf; | |
168 | |
169 if (r->headers_out.status == NGX_HTTP_NOT_MODIFIED) { | |
170 return ngx_http_next_header_filter(r); | |
171 } | |
172 | |
2834
0449d289256c
test finalized image filter context before testing image_filter off
Igor Sysoev <igor@sysoev.ru>
parents:
2821
diff
changeset
|
173 ctx = ngx_http_get_module_ctx(r, ngx_http_image_filter_module); |
0449d289256c
test finalized image filter context before testing image_filter off
Igor Sysoev <igor@sysoev.ru>
parents:
2821
diff
changeset
|
174 |
0449d289256c
test finalized image filter context before testing image_filter off
Igor Sysoev <igor@sysoev.ru>
parents:
2821
diff
changeset
|
175 if (ctx) { |
0449d289256c
test finalized image filter context before testing image_filter off
Igor Sysoev <igor@sysoev.ru>
parents:
2821
diff
changeset
|
176 ngx_http_set_ctx(r, NULL, ngx_http_image_filter_module); |
0449d289256c
test finalized image filter context before testing image_filter off
Igor Sysoev <igor@sysoev.ru>
parents:
2821
diff
changeset
|
177 return ngx_http_next_header_filter(r); |
0449d289256c
test finalized image filter context before testing image_filter off
Igor Sysoev <igor@sysoev.ru>
parents:
2821
diff
changeset
|
178 } |
0449d289256c
test finalized image filter context before testing image_filter off
Igor Sysoev <igor@sysoev.ru>
parents:
2821
diff
changeset
|
179 |
2788 | 180 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module); |
181 | |
182 if (conf->filter == NGX_HTTP_IMAGE_OFF) { | |
183 return ngx_http_next_header_filter(r); | |
184 } | |
185 | |
186 if (r->headers_out.content_type.len | |
187 >= sizeof("multipart/x-mixed-replace") - 1 | |
188 && ngx_strncasecmp(r->headers_out.content_type.data, | |
189 (u_char *) "multipart/x-mixed-replace", | |
190 sizeof("multipart/x-mixed-replace") - 1) | |
191 == 0) | |
192 { | |
193 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
194 "image filter: multipart/x-mixed-replace response"); | |
195 | |
196 return NGX_ERROR; | |
197 } | |
198 | |
199 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_image_filter_ctx_t)); | |
200 if (ctx == NULL) { | |
201 return NGX_ERROR; | |
202 } | |
203 | |
204 ngx_http_set_ctx(r, ctx, ngx_http_image_filter_module); | |
205 | |
206 len = r->headers_out.content_length_n; | |
207 | |
2795
d82d08314f78
fix building ngx_http_image_filter_module on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
2788
diff
changeset
|
208 if (len != -1 && len > (off_t) conf->buffer_size) { |
2788 | 209 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
210 "image filter: too big response: %O", len); | |
211 | |
212 return NGX_ERROR; | |
213 } | |
214 | |
215 if (len == -1) { | |
216 ctx->length = conf->buffer_size; | |
217 | |
218 } else { | |
219 ctx->length = (size_t) len; | |
220 } | |
221 | |
222 if (r->headers_out.refresh) { | |
223 r->headers_out.refresh->hash = 0; | |
224 } | |
225 | |
226 r->main_filter_need_in_memory = 1; | |
227 r->allow_ranges = 0; | |
228 | |
229 return NGX_OK; | |
230 } | |
231 | |
232 | |
233 static ngx_int_t | |
234 ngx_http_image_body_filter(ngx_http_request_t *r, ngx_chain_t *in) | |
235 { | |
236 ngx_int_t rc; | |
237 ngx_str_t *ct; | |
238 ngx_chain_t out; | |
239 ngx_http_image_filter_ctx_t *ctx; | |
240 ngx_http_image_filter_conf_t *conf; | |
241 | |
242 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "image filter"); | |
243 | |
244 if (in == NULL) { | |
245 return ngx_http_next_body_filter(r, in); | |
246 } | |
247 | |
248 ctx = ngx_http_get_module_ctx(r, ngx_http_image_filter_module); | |
249 | |
250 if (ctx == NULL) { | |
251 return ngx_http_next_body_filter(r, in); | |
252 } | |
253 | |
254 switch (ctx->phase) { | |
255 | |
256 case NGX_HTTP_IMAGE_START: | |
257 | |
258 ctx->type = ngx_http_image_test(r, in); | |
259 | |
260 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module); | |
261 | |
262 if (ctx->type == NGX_HTTP_IMAGE_NONE) { | |
263 | |
264 if (conf->filter == NGX_HTTP_IMAGE_SIZE) { | |
265 out.buf = ngx_http_image_json(r, NULL); | |
266 | |
267 if (out.buf) { | |
268 out.next = NULL; | |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
269 ctx->phase = NGX_HTTP_IMAGE_DONE; |
2788 | 270 |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
271 return ngx_http_image_send(r, ctx, &out); |
2788 | 272 } |
273 } | |
274 | |
275 return ngx_http_filter_finalize_request(r, | |
2821
26e06e009ced
allow to pass image filter errors via the same location where the filter is set
Igor Sysoev <igor@sysoev.ru>
parents:
2819
diff
changeset
|
276 &ngx_http_image_filter_module, |
2788 | 277 NGX_HTTP_UNSUPPORTED_MEDIA_TYPE); |
278 } | |
279 | |
280 /* override content type */ | |
281 | |
282 ct = &ngx_http_image_types[ctx->type - 1]; | |
283 r->headers_out.content_type_len = ct->len; | |
284 r->headers_out.content_type = *ct; | |
2882
896db5a09bd2
reset content_type hash value, this fixes a bug when XSLT responses
Igor Sysoev <igor@sysoev.ru>
parents:
2848
diff
changeset
|
285 r->headers_out.content_type_lowcase = NULL; |
2788 | 286 |
287 if (conf->filter == NGX_HTTP_IMAGE_TEST) { | |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
288 ctx->phase = NGX_HTTP_IMAGE_PASS; |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
289 |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
290 return ngx_http_image_send(r, ctx, in); |
2788 | 291 } |
292 | |
293 ctx->phase = NGX_HTTP_IMAGE_READ; | |
294 | |
295 /* fall through */ | |
296 | |
297 case NGX_HTTP_IMAGE_READ: | |
298 | |
299 rc = ngx_http_image_read(r, in); | |
300 | |
301 if (rc == NGX_AGAIN) { | |
302 return NGX_OK; | |
303 } | |
304 | |
305 if (rc == NGX_ERROR) { | |
306 return ngx_http_filter_finalize_request(r, | |
2821
26e06e009ced
allow to pass image filter errors via the same location where the filter is set
Igor Sysoev <igor@sysoev.ru>
parents:
2819
diff
changeset
|
307 &ngx_http_image_filter_module, |
2788 | 308 NGX_HTTP_UNSUPPORTED_MEDIA_TYPE); |
309 } | |
310 | |
311 /* fall through */ | |
312 | |
313 case NGX_HTTP_IMAGE_PROCESS: | |
314 | |
315 out.buf = ngx_http_image_process(r); | |
316 | |
317 if (out.buf == NULL) { | |
318 return ngx_http_filter_finalize_request(r, | |
2821
26e06e009ced
allow to pass image filter errors via the same location where the filter is set
Igor Sysoev <igor@sysoev.ru>
parents:
2819
diff
changeset
|
319 &ngx_http_image_filter_module, |
2788 | 320 NGX_HTTP_UNSUPPORTED_MEDIA_TYPE); |
321 } | |
322 | |
323 out.next = NULL; | |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
324 ctx->phase = NGX_HTTP_IMAGE_PASS; |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
325 |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
326 return ngx_http_image_send(r, ctx, &out); |
2788 | 327 |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
328 case NGX_HTTP_IMAGE_PASS: |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
329 |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
330 return ngx_http_next_body_filter(r, in); |
2788 | 331 |
332 default: /* NGX_HTTP_IMAGE_DONE */ | |
333 | |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
334 rc = ngx_http_next_body_filter(r, NULL); |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
335 |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
336 /* NGX_ERROR resets any pending data */ |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
337 return (rc == NGX_OK) ? NGX_ERROR : rc; |
2788 | 338 } |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
339 } |
2788 | 340 |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
341 |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
342 static ngx_int_t |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
343 ngx_http_image_send(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx, |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
344 ngx_chain_t *in) |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
345 { |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
346 ngx_int_t rc; |
2788 | 347 |
348 rc = ngx_http_next_header_filter(r); | |
349 | |
350 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { | |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
351 return NGX_ERROR; |
2788 | 352 } |
353 | |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
354 rc = ngx_http_next_body_filter(r, in); |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
355 |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
356 if (ctx->phase == NGX_HTTP_IMAGE_DONE) { |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
357 /* NGX_ERROR resets any pending data */ |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
358 return (rc == NGX_OK) ? NGX_ERROR : rc; |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
359 } |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
360 |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
361 return rc; |
2788 | 362 } |
363 | |
364 | |
365 static ngx_uint_t | |
366 ngx_http_image_test(ngx_http_request_t *r, ngx_chain_t *in) | |
367 { | |
368 u_char *p; | |
369 | |
370 p = in->buf->pos; | |
371 | |
372 if (in->buf->last - p < 16) { | |
373 return NGX_HTTP_IMAGE_NONE; | |
374 } | |
375 | |
376 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
377 "image filter: \"%c%c\"", p[0], p[1]); | |
378 | |
379 if (p[0] == 0xff && p[1] == 0xd8) { | |
380 | |
381 /* JPEG */ | |
382 | |
383 return NGX_HTTP_IMAGE_JPEG; | |
384 | |
385 } else if (p[0] == 'G' && p[1] == 'I' && p[2] == 'F' && p[3] == '8' | |
2918 | 386 && p[5] == 'a') |
2788 | 387 { |
2918 | 388 if (p[4] == '9' || p[4] == '7') { |
389 /* GIF */ | |
390 return NGX_HTTP_IMAGE_GIF; | |
391 } | |
2788 | 392 |
393 } else if (p[0] == 0x89 && p[1] == 'P' && p[2] == 'N' && p[3] == 'G' | |
394 && p[4] == 0x0d && p[5] == 0x0a && p[6] == 0x1a && p[7] == 0x0a) | |
395 { | |
396 /* PNG */ | |
397 | |
398 return NGX_HTTP_IMAGE_PNG; | |
399 } | |
400 | |
401 return NGX_HTTP_IMAGE_NONE; | |
402 } | |
403 | |
404 | |
405 static ngx_int_t | |
406 ngx_http_image_read(ngx_http_request_t *r, ngx_chain_t *in) | |
407 { | |
408 u_char *p; | |
409 size_t size, rest; | |
410 ngx_buf_t *b; | |
411 ngx_chain_t *cl; | |
412 ngx_http_image_filter_ctx_t *ctx; | |
413 | |
414 ctx = ngx_http_get_module_ctx(r, ngx_http_image_filter_module); | |
415 | |
416 if (ctx->image == NULL) { | |
417 ctx->image = ngx_palloc(r->pool, ctx->length); | |
418 if (ctx->image == NULL) { | |
419 return NGX_ERROR; | |
420 } | |
421 | |
422 ctx->last = ctx->image; | |
423 } | |
424 | |
425 p = ctx->last; | |
426 | |
427 for (cl = in; cl; cl = cl->next) { | |
428 | |
429 b = cl->buf; | |
430 size = b->last - b->pos; | |
431 | |
432 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
433 "image buf: %uz", size); | |
434 | |
435 rest = ctx->image + ctx->length - p; | |
436 size = (rest < size) ? rest : size; | |
437 | |
438 p = ngx_cpymem(p, b->pos, size); | |
439 b->pos += size; | |
440 | |
441 if (b->last_buf) { | |
442 ctx->last = p; | |
443 return NGX_OK; | |
444 } | |
445 } | |
446 | |
447 ctx->last = p; | |
448 r->connection->buffered |= NGX_HTTP_IMAGE_BUFFERED; | |
449 | |
450 return NGX_AGAIN; | |
451 } | |
452 | |
453 | |
454 static ngx_buf_t * | |
455 ngx_http_image_process(ngx_http_request_t *r) | |
456 { | |
457 ngx_buf_t *b; | |
458 ngx_int_t rc; | |
459 ngx_http_image_filter_ctx_t *ctx; | |
460 ngx_http_image_filter_conf_t *conf; | |
461 | |
462 r->connection->buffered &= ~NGX_HTTP_IMAGE_BUFFERED; | |
463 | |
464 ctx = ngx_http_get_module_ctx(r, ngx_http_image_filter_module); | |
465 | |
466 rc = ngx_http_image_size(r, ctx); | |
467 | |
468 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module); | |
469 | |
470 if (conf->filter == NGX_HTTP_IMAGE_SIZE) { | |
471 | |
472 b = ngx_http_image_json(r, rc == NGX_OK ? ctx : NULL); | |
473 | |
474 } else if (rc == NGX_OK | |
475 && ctx->width <= conf->width | |
476 && ctx->height <= conf->height) | |
477 { | |
478 b = ngx_http_image_asis(r, ctx); | |
479 | |
480 } else { | |
481 b = ngx_http_image_resize(r, ctx); | |
482 } | |
483 | |
484 return b; | |
485 } | |
486 | |
487 | |
488 static ngx_buf_t * | |
489 ngx_http_image_json(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
490 { | |
491 size_t len; | |
492 ngx_buf_t *b; | |
493 | |
494 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); | |
495 if (b == NULL) { | |
496 return NULL; | |
497 } | |
498 | |
499 b->memory = 1; | |
500 b->last_buf = 1; | |
501 | |
502 ngx_http_clean_header(r); | |
503 | |
504 r->headers_out.status = NGX_HTTP_OK; | |
505 r->headers_out.content_type.len = sizeof("text/plain") - 1; | |
506 r->headers_out.content_type.data = (u_char *) "text/plain"; | |
2882
896db5a09bd2
reset content_type hash value, this fixes a bug when XSLT responses
Igor Sysoev <igor@sysoev.ru>
parents:
2848
diff
changeset
|
507 r->headers_out.content_type_lowcase = NULL; |
2788 | 508 |
509 if (ctx == NULL) { | |
510 b->pos = (u_char *) "{}" CRLF; | |
511 b->last = b->pos + sizeof("{}" CRLF) - 1; | |
512 | |
513 ngx_http_image_length(r, b); | |
514 | |
515 return b; | |
516 } | |
517 | |
518 len = sizeof("{ \"img\" : " | |
519 "{ \"width\": , \"height\": , \"type\": \"jpeg\" } }" CRLF) - 1 | |
520 + 2 * NGX_SIZE_T_LEN; | |
521 | |
522 b->pos = ngx_pnalloc(r->pool, len); | |
523 if (b->pos == NULL) { | |
524 return NULL; | |
525 } | |
526 | |
527 b->last = ngx_sprintf(b->pos, | |
528 "{ \"img\" : " | |
529 "{ \"width\": %uz," | |
530 " \"height\": %uz," | |
531 " \"type\": \"%s\" } }" CRLF, | |
532 ctx->width, ctx->height, | |
533 ngx_http_image_types[ctx->type - 1].data + 6); | |
534 | |
535 ngx_http_image_length(r, b); | |
536 | |
537 return b; | |
538 } | |
539 | |
540 | |
541 static ngx_buf_t * | |
542 ngx_http_image_asis(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
543 { | |
544 ngx_buf_t *b; | |
545 | |
546 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); | |
547 if (b == NULL) { | |
548 return NULL; | |
549 } | |
550 | |
551 b->pos = ctx->image; | |
552 b->last = ctx->last; | |
553 b->memory = 1; | |
554 b->last_buf = 1; | |
555 | |
556 ngx_http_image_length(r, b); | |
557 | |
558 return b; | |
559 } | |
560 | |
561 | |
562 static void | |
563 ngx_http_image_length(ngx_http_request_t *r, ngx_buf_t *b) | |
564 { | |
565 r->headers_out.content_length_n = b->last - b->pos; | |
566 | |
567 if (r->headers_out.content_length) { | |
568 r->headers_out.content_length->hash = 0; | |
569 } | |
570 | |
571 r->headers_out.content_length = NULL; | |
572 } | |
573 | |
574 | |
575 static ngx_int_t | |
576 ngx_http_image_size(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
577 { | |
578 u_char *p, *last; | |
579 ngx_uint_t width, height; | |
580 | |
581 p = ctx->image; | |
582 | |
583 switch (ctx->type) { | |
584 | |
585 case NGX_HTTP_IMAGE_JPEG: | |
586 | |
587 p += 2; | |
588 last = ctx->image + ctx->length - 10; | |
589 | |
590 while (p < last) { | |
591 | |
592 if (p[0] == 0xff && p[1] != 0xff) { | |
593 | |
594 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
595 "JPEG: %02xd %02xd", *p, *(p + 1)); | |
596 | |
597 p++; | |
598 | |
599 if (*p == 0xc0 || *p == 0xc1 || *p == 0xc2 || *p == 0xc3 | |
600 || *p == 0xc9 || *p == 0xca || *p == 0xcb) | |
601 { | |
602 goto found; | |
603 } | |
604 | |
605 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
606 "JPEG: %02xd %02xd", p[1], p[2]); | |
607 | |
608 p += p[1] * 256 + p[2]; | |
609 | |
610 continue; | |
611 } | |
612 | |
613 p++; | |
614 } | |
615 | |
616 return NGX_DECLINED; | |
617 | |
618 found: | |
619 | |
620 width = p[6] * 256 + p[7]; | |
621 height = p[4] * 256 + p[5]; | |
622 | |
623 break; | |
624 | |
625 case NGX_HTTP_IMAGE_GIF: | |
626 | |
627 if (ctx->length < 10) { | |
628 return NGX_DECLINED; | |
629 } | |
630 | |
631 width = p[7] * 256 + p[6]; | |
632 height = p[9] * 256 + p[8]; | |
633 | |
634 break; | |
635 | |
636 case NGX_HTTP_IMAGE_PNG: | |
637 | |
638 if (ctx->length < 24) { | |
639 return NGX_DECLINED; | |
640 } | |
641 | |
642 width = p[18] * 256 + p[19]; | |
643 height = p[22] * 256 + p[23]; | |
644 | |
645 break; | |
646 | |
647 default: | |
648 | |
649 return NGX_DECLINED; | |
650 } | |
651 | |
652 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
653 "image size: %d x %d", width, height); | |
654 | |
655 ctx->width = width; | |
656 ctx->height = height; | |
657 | |
658 return NGX_OK; | |
659 } | |
660 | |
661 | |
662 static ngx_buf_t * | |
663 ngx_http_image_resize(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
664 { | |
665 int sx, sy, dx, dy, ox, oy, | |
666 colors, transparent, size; | |
667 u_char *out; | |
668 ngx_buf_t *b; | |
669 ngx_uint_t resize; | |
670 gdImagePtr src, dst; | |
671 ngx_pool_cleanup_t *cln; | |
672 ngx_http_image_filter_conf_t *conf; | |
673 | |
674 src = ngx_http_image_source(r, ctx); | |
675 | |
676 if (src == NULL) { | |
677 return NULL; | |
678 } | |
679 | |
680 sx = gdImageSX(src); | |
681 sy = gdImageSY(src); | |
682 | |
683 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module); | |
684 | |
685 if ((ngx_uint_t) sx <= conf->width && (ngx_uint_t) sy <= conf->height) { | |
686 gdImageDestroy(src); | |
687 return ngx_http_image_asis(r, ctx); | |
688 } | |
689 | |
690 colors = gdImageColorsTotal(src); | |
691 transparent = gdImageGetTransparent(src); | |
692 | |
693 dx = sx; | |
694 dy = sy; | |
695 | |
696 if (conf->filter == NGX_HTTP_IMAGE_RESIZE) { | |
697 | |
698 if ((ngx_uint_t) dx > conf->width) { | |
699 dy = dy * conf->width / dx; | |
700 dy = dy ? dy : 1; | |
701 dx = conf->width; | |
702 } | |
703 | |
704 if ((ngx_uint_t) dy > conf->height) { | |
705 dx = dx * conf->height / dy; | |
706 dx = dx ? dx : 1; | |
707 dy = conf->height; | |
708 } | |
709 | |
710 resize = 1; | |
711 | |
712 } else { /* NGX_HTTP_IMAGE_CROP */ | |
713 | |
714 resize = 0; | |
715 | |
716 if ((ngx_uint_t) (dx * 100 / dy) < conf->width * 100 / conf->height) { | |
717 | |
718 if ((ngx_uint_t) dx > conf->width) { | |
719 dy = dy * conf->width / dx; | |
720 dy = dy ? dy : 1; | |
721 dx = conf->width; | |
722 resize = 1; | |
723 } | |
724 | |
725 } else { | |
726 if ((ngx_uint_t) dy > conf->height) { | |
727 dx = dx * conf->height / dy; | |
728 dx = dx ? dx : 1; | |
729 dy = conf->height; | |
730 resize = 1; | |
731 } | |
732 } | |
733 } | |
734 | |
735 if (resize) { | |
736 dst = ngx_http_image_new(r, dx, dy, colors); | |
737 if (dst == NULL) { | |
738 gdImageDestroy(src); | |
739 return NULL; | |
740 } | |
741 | |
742 gdImageCopyResampled(dst, src, 0, 0, 0, 0, dx, dy, sx, sy); | |
743 | |
744 gdImageDestroy(src); | |
745 | |
746 } else { | |
747 dst = src; | |
748 } | |
749 | |
750 if (conf->filter == NGX_HTTP_IMAGE_CROP) { | |
751 | |
752 src = dst; | |
753 | |
754 if ((ngx_uint_t) dx > conf->width) { | |
755 ox = dx - conf->width; | |
756 | |
757 } else { | |
758 ox = 0; | |
759 } | |
760 | |
761 if ((ngx_uint_t) dy > conf->height) { | |
762 oy = dy - conf->height; | |
763 | |
764 } else { | |
765 oy = 0; | |
766 } | |
767 | |
768 if (ox || oy) { | |
769 | |
770 dst = ngx_http_image_new(r, dx - ox, dy - oy, colors); | |
771 | |
772 if (dst == NULL) { | |
773 gdImageDestroy(src); | |
774 return NULL; | |
775 } | |
776 | |
777 ox /= 2; | |
778 oy /= 2; | |
779 | |
780 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
781 "image crop: %d x %d @ %d x %d", | |
782 dx, dy, ox, oy); | |
783 | |
784 gdImageCopy(dst, src, 0, 0, ox, oy, dx - ox, dy - oy); | |
785 | |
786 gdImageDestroy(src); | |
787 } | |
788 } | |
789 | |
790 gdImageColorTransparent(dst, transparent); | |
791 | |
792 out = ngx_http_image_out(r, ctx->type, dst, &size); | |
793 | |
794 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
795 "image: %d x %d %d", sx, sy, colors); | |
796 | |
797 gdImageDestroy(dst); | |
798 ngx_pfree(r->pool, ctx->image); | |
799 | |
800 if (out == NULL) { | |
801 return NULL; | |
802 } | |
803 | |
804 cln = ngx_pool_cleanup_add(r->pool, 0); | |
805 if (cln == NULL) { | |
806 gdFree(out); | |
807 return NULL; | |
808 } | |
809 | |
810 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); | |
811 if (b == NULL) { | |
812 gdFree(out); | |
813 return NULL; | |
814 } | |
815 | |
816 cln->handler = ngx_http_image_cleanup; | |
817 cln->data = out; | |
818 | |
819 b->pos = out; | |
820 b->last = out + size; | |
821 b->memory = 1; | |
822 b->last_buf = 1; | |
823 | |
824 ngx_http_image_length(r, b); | |
825 | |
826 return b; | |
827 } | |
828 | |
829 | |
830 static gdImagePtr | |
831 ngx_http_image_source(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
832 { | |
833 char *failed; | |
834 gdImagePtr img; | |
835 | |
836 img = NULL; | |
837 | |
838 switch (ctx->type) { | |
839 | |
840 case NGX_HTTP_IMAGE_JPEG: | |
841 img = gdImageCreateFromJpegPtr(ctx->length, ctx->image); | |
842 failed = "gdImageCreateFromJpegPtr() failed"; | |
843 break; | |
844 | |
845 case NGX_HTTP_IMAGE_GIF: | |
846 img = gdImageCreateFromGifPtr(ctx->length, ctx->image); | |
847 failed = "gdImageCreateFromGifPtr() failed"; | |
848 break; | |
849 | |
850 case NGX_HTTP_IMAGE_PNG: | |
851 img = gdImageCreateFromPngPtr(ctx->length, ctx->image); | |
852 failed = "gdImageCreateFromPngPtr() failed"; | |
853 break; | |
854 | |
855 default: | |
856 failed = "unknown image type"; | |
857 break; | |
858 } | |
859 | |
860 if (img == NULL) { | |
861 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, failed); | |
862 } | |
863 | |
864 return img; | |
865 } | |
866 | |
867 | |
868 static gdImagePtr | |
869 ngx_http_image_new(ngx_http_request_t *r, int w, int h, int colors) | |
870 { | |
871 gdImagePtr img; | |
872 | |
873 if (colors == 0) { | |
874 img = gdImageCreateTrueColor(w, h); | |
875 | |
876 if (img == NULL) { | |
877 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
878 "gdImageCreateTrueColor() failed"); | |
879 return NULL; | |
880 } | |
881 | |
882 } else { | |
883 img = gdImageCreate(w, h); | |
884 | |
885 if (img == NULL) { | |
886 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
887 "gdImageCreate() failed"); | |
888 return NULL; | |
889 } | |
890 } | |
891 | |
892 return img; | |
893 } | |
894 | |
895 | |
896 static u_char * | |
897 ngx_http_image_out(ngx_http_request_t *r, ngx_uint_t type, gdImagePtr img, | |
898 int *size) | |
899 { | |
2848 | 900 char *failed; |
901 u_char *out; | |
902 ngx_http_image_filter_conf_t *conf; | |
2788 | 903 |
904 out = NULL; | |
905 | |
906 switch (type) { | |
907 | |
908 case NGX_HTTP_IMAGE_JPEG: | |
2848 | 909 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module); |
910 out = gdImageJpegPtr(img, size, conf->jpeg_quality); | |
2788 | 911 failed = "gdImageJpegPtr() failed"; |
912 break; | |
913 | |
914 case NGX_HTTP_IMAGE_GIF: | |
915 out = gdImageGifPtr(img, size); | |
916 failed = "gdImageGifPtr() failed"; | |
917 break; | |
918 | |
919 case NGX_HTTP_IMAGE_PNG: | |
920 out = gdImagePngPtr(img, size); | |
921 failed = "gdImagePngPtr() failed"; | |
922 break; | |
923 | |
924 default: | |
925 failed = "unknown image type"; | |
926 break; | |
927 } | |
928 | |
929 if (out == NULL) { | |
930 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, failed); | |
931 } | |
932 | |
933 return out; | |
934 } | |
935 | |
936 | |
937 static void | |
938 ngx_http_image_cleanup(void *data) | |
939 { | |
940 gdFree(data); | |
941 } | |
942 | |
943 | |
944 static void * | |
945 ngx_http_image_filter_create_conf(ngx_conf_t *cf) | |
946 { | |
947 ngx_http_image_filter_conf_t *conf; | |
948 | |
949 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_image_filter_conf_t)); | |
950 if (conf == NULL) { | |
2912
c7d57b539248
return NULL instead of NGX_CONF_ERROR on a create conf failure
Igor Sysoev <igor@sysoev.ru>
parents:
2882
diff
changeset
|
951 return NULL; |
2788 | 952 } |
953 | |
954 conf->filter = NGX_CONF_UNSET_UINT; | |
2848 | 955 conf->jpeg_quality = NGX_CONF_UNSET; |
2788 | 956 conf->buffer_size = NGX_CONF_UNSET_SIZE; |
957 | |
958 return conf; | |
959 } | |
960 | |
961 | |
962 static char * | |
963 ngx_http_image_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
964 { | |
965 ngx_http_image_filter_conf_t *prev = parent; | |
966 ngx_http_image_filter_conf_t *conf = child; | |
967 | |
968 if (conf->filter == NGX_CONF_UNSET_UINT) { | |
969 | |
970 if (prev->filter == NGX_CONF_UNSET_UINT) { | |
971 conf->filter = NGX_HTTP_IMAGE_OFF; | |
972 | |
973 } else { | |
974 conf->filter = prev->filter; | |
975 conf->width = prev->width; | |
976 conf->height = prev->height; | |
977 } | |
978 } | |
979 | |
2848 | 980 /* 75 is libjpeg default quality */ |
981 ngx_conf_merge_value(conf->jpeg_quality, prev->jpeg_quality, 75); | |
982 | |
2788 | 983 ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size, |
984 1 * 1024 * 1024); | |
985 | |
986 return NGX_CONF_OK; | |
987 } | |
988 | |
989 | |
990 static char * | |
991 ngx_http_image_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
992 { | |
993 ngx_http_image_filter_conf_t *imcf = conf; | |
994 | |
995 ngx_str_t *value; | |
996 ngx_int_t n; | |
997 ngx_uint_t i; | |
998 | |
999 value = cf->args->elts; | |
1000 | |
1001 i = 1; | |
1002 | |
1003 if (cf->args->nelts == 2) { | |
1004 if (ngx_strcmp(value[i].data, "off") == 0) { | |
1005 imcf->filter = NGX_HTTP_IMAGE_OFF; | |
1006 | |
1007 } else if (ngx_strcmp(value[i].data, "test") == 0) { | |
1008 imcf->filter = NGX_HTTP_IMAGE_TEST; | |
1009 | |
1010 } else if (ngx_strcmp(value[i].data, "size") == 0) { | |
1011 imcf->filter = NGX_HTTP_IMAGE_SIZE; | |
1012 | |
1013 } else { | |
1014 goto failed; | |
1015 } | |
1016 | |
1017 return NGX_CONF_OK; | |
1018 } | |
1019 | |
1020 if (ngx_strcmp(value[i].data, "resize") == 0) { | |
1021 imcf->filter = NGX_HTTP_IMAGE_RESIZE; | |
1022 | |
1023 } else if (ngx_strcmp(value[i].data, "crop") == 0) { | |
1024 imcf->filter = NGX_HTTP_IMAGE_CROP; | |
1025 | |
1026 } else { | |
1027 goto failed; | |
1028 } | |
1029 | |
1030 i++; | |
1031 | |
1032 if (value[i].len == 1 && value[i].data[0] == '-') { | |
1033 imcf->width = (ngx_uint_t) -1; | |
1034 | |
1035 } else { | |
1036 n = ngx_atoi(value[i].data, value[i].len); | |
1037 if (n == NGX_ERROR) { | |
1038 goto failed; | |
1039 } | |
1040 | |
1041 imcf->width = (ngx_uint_t) n; | |
1042 } | |
1043 | |
1044 i++; | |
1045 | |
1046 if (value[i].len == 1 && value[i].data[0] == '-') { | |
1047 imcf->height = (ngx_uint_t) -1; | |
1048 | |
1049 } else { | |
1050 n = ngx_atoi(value[i].data, value[i].len); | |
1051 if (n == NGX_ERROR) { | |
1052 goto failed; | |
1053 } | |
1054 | |
1055 imcf->height = (ngx_uint_t) n; | |
1056 } | |
1057 | |
1058 return NGX_CONF_OK; | |
1059 | |
1060 failed: | |
1061 | |
1062 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"", | |
1063 &value[i]); | |
1064 | |
1065 return NGX_CONF_ERROR; | |
1066 } | |
1067 | |
1068 | |
1069 static ngx_int_t | |
1070 ngx_http_image_filter_init(ngx_conf_t *cf) | |
1071 { | |
1072 ngx_http_next_header_filter = ngx_http_top_header_filter; | |
1073 ngx_http_top_header_filter = ngx_http_image_header_filter; | |
1074 | |
1075 ngx_http_next_body_filter = ngx_http_top_body_filter; | |
1076 ngx_http_top_body_filter = ngx_http_image_body_filter; | |
1077 | |
1078 return NGX_OK; | |
1079 } |