Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_image_filter_module.c @ 2875:6e1941b321b7
proxy_cache_methods and fastcgi_cache_methods
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 19 May 2009 13:27:27 +0000 |
parents | 0d8941f2b0ee |
children | 896db5a09bd2 |
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; | |
285 | |
286 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
|
287 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
|
288 |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
289 return ngx_http_image_send(r, ctx, in); |
2788 | 290 } |
291 | |
292 ctx->phase = NGX_HTTP_IMAGE_READ; | |
293 | |
294 /* fall through */ | |
295 | |
296 case NGX_HTTP_IMAGE_READ: | |
297 | |
298 rc = ngx_http_image_read(r, in); | |
299 | |
300 if (rc == NGX_AGAIN) { | |
301 return NGX_OK; | |
302 } | |
303 | |
304 if (rc == NGX_ERROR) { | |
305 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
|
306 &ngx_http_image_filter_module, |
2788 | 307 NGX_HTTP_UNSUPPORTED_MEDIA_TYPE); |
308 } | |
309 | |
310 /* fall through */ | |
311 | |
312 case NGX_HTTP_IMAGE_PROCESS: | |
313 | |
314 out.buf = ngx_http_image_process(r); | |
315 | |
316 if (out.buf == NULL) { | |
317 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
|
318 &ngx_http_image_filter_module, |
2788 | 319 NGX_HTTP_UNSUPPORTED_MEDIA_TYPE); |
320 } | |
321 | |
322 out.next = NULL; | |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
323 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
|
324 |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
325 return ngx_http_image_send(r, ctx, &out); |
2788 | 326 |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
327 case NGX_HTTP_IMAGE_PASS: |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
328 |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
329 return ngx_http_next_body_filter(r, in); |
2788 | 330 |
331 default: /* NGX_HTTP_IMAGE_DONE */ | |
332 | |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
333 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
|
334 |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
335 /* 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
|
336 return (rc == NGX_OK) ? NGX_ERROR : rc; |
2788 | 337 } |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
338 } |
2788 | 339 |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
340 |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
341 static ngx_int_t |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
342 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
|
343 ngx_chain_t *in) |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
344 { |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
345 ngx_int_t rc; |
2788 | 346 |
347 rc = ngx_http_next_header_filter(r); | |
348 | |
349 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
|
350 return NGX_ERROR; |
2788 | 351 } |
352 | |
2819
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
353 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
|
354 |
43fe53832da7
handle big responses for "size" and "test" image_filters
Igor Sysoev <igor@sysoev.ru>
parents:
2795
diff
changeset
|
355 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
|
356 /* 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
|
357 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
|
358 } |
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 return rc; |
2788 | 361 } |
362 | |
363 | |
364 static ngx_uint_t | |
365 ngx_http_image_test(ngx_http_request_t *r, ngx_chain_t *in) | |
366 { | |
367 u_char *p; | |
368 | |
369 p = in->buf->pos; | |
370 | |
371 if (in->buf->last - p < 16) { | |
372 return NGX_HTTP_IMAGE_NONE; | |
373 } | |
374 | |
375 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
376 "image filter: \"%c%c\"", p[0], p[1]); | |
377 | |
378 if (p[0] == 0xff && p[1] == 0xd8) { | |
379 | |
380 /* JPEG */ | |
381 | |
382 return NGX_HTTP_IMAGE_JPEG; | |
383 | |
384 } else if (p[0] == 'G' && p[1] == 'I' && p[2] == 'F' && p[3] == '8' | |
385 && p[4] == '9' && p[5] == 'a') | |
386 { | |
387 /* GIF */ | |
388 | |
389 return NGX_HTTP_IMAGE_GIF; | |
390 | |
391 } else if (p[0] == 0x89 && p[1] == 'P' && p[2] == 'N' && p[3] == 'G' | |
392 && p[4] == 0x0d && p[5] == 0x0a && p[6] == 0x1a && p[7] == 0x0a) | |
393 { | |
394 /* PNG */ | |
395 | |
396 return NGX_HTTP_IMAGE_PNG; | |
397 } | |
398 | |
399 return NGX_HTTP_IMAGE_NONE; | |
400 } | |
401 | |
402 | |
403 static ngx_int_t | |
404 ngx_http_image_read(ngx_http_request_t *r, ngx_chain_t *in) | |
405 { | |
406 u_char *p; | |
407 size_t size, rest; | |
408 ngx_buf_t *b; | |
409 ngx_chain_t *cl; | |
410 ngx_http_image_filter_ctx_t *ctx; | |
411 | |
412 ctx = ngx_http_get_module_ctx(r, ngx_http_image_filter_module); | |
413 | |
414 if (ctx->image == NULL) { | |
415 ctx->image = ngx_palloc(r->pool, ctx->length); | |
416 if (ctx->image == NULL) { | |
417 return NGX_ERROR; | |
418 } | |
419 | |
420 ctx->last = ctx->image; | |
421 } | |
422 | |
423 p = ctx->last; | |
424 | |
425 for (cl = in; cl; cl = cl->next) { | |
426 | |
427 b = cl->buf; | |
428 size = b->last - b->pos; | |
429 | |
430 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
431 "image buf: %uz", size); | |
432 | |
433 rest = ctx->image + ctx->length - p; | |
434 size = (rest < size) ? rest : size; | |
435 | |
436 p = ngx_cpymem(p, b->pos, size); | |
437 b->pos += size; | |
438 | |
439 if (b->last_buf) { | |
440 ctx->last = p; | |
441 return NGX_OK; | |
442 } | |
443 } | |
444 | |
445 ctx->last = p; | |
446 r->connection->buffered |= NGX_HTTP_IMAGE_BUFFERED; | |
447 | |
448 return NGX_AGAIN; | |
449 } | |
450 | |
451 | |
452 static ngx_buf_t * | |
453 ngx_http_image_process(ngx_http_request_t *r) | |
454 { | |
455 ngx_buf_t *b; | |
456 ngx_int_t rc; | |
457 ngx_http_image_filter_ctx_t *ctx; | |
458 ngx_http_image_filter_conf_t *conf; | |
459 | |
460 r->connection->buffered &= ~NGX_HTTP_IMAGE_BUFFERED; | |
461 | |
462 ctx = ngx_http_get_module_ctx(r, ngx_http_image_filter_module); | |
463 | |
464 rc = ngx_http_image_size(r, ctx); | |
465 | |
466 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module); | |
467 | |
468 if (conf->filter == NGX_HTTP_IMAGE_SIZE) { | |
469 | |
470 b = ngx_http_image_json(r, rc == NGX_OK ? ctx : NULL); | |
471 | |
472 } else if (rc == NGX_OK | |
473 && ctx->width <= conf->width | |
474 && ctx->height <= conf->height) | |
475 { | |
476 b = ngx_http_image_asis(r, ctx); | |
477 | |
478 } else { | |
479 b = ngx_http_image_resize(r, ctx); | |
480 } | |
481 | |
482 return b; | |
483 } | |
484 | |
485 | |
486 static ngx_buf_t * | |
487 ngx_http_image_json(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
488 { | |
489 size_t len; | |
490 ngx_buf_t *b; | |
491 | |
492 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); | |
493 if (b == NULL) { | |
494 return NULL; | |
495 } | |
496 | |
497 b->memory = 1; | |
498 b->last_buf = 1; | |
499 | |
500 ngx_http_clean_header(r); | |
501 | |
502 r->headers_out.status = NGX_HTTP_OK; | |
503 r->headers_out.content_type.len = sizeof("text/plain") - 1; | |
504 r->headers_out.content_type.data = (u_char *) "text/plain"; | |
505 | |
506 if (ctx == NULL) { | |
507 b->pos = (u_char *) "{}" CRLF; | |
508 b->last = b->pos + sizeof("{}" CRLF) - 1; | |
509 | |
510 ngx_http_image_length(r, b); | |
511 | |
512 return b; | |
513 } | |
514 | |
515 len = sizeof("{ \"img\" : " | |
516 "{ \"width\": , \"height\": , \"type\": \"jpeg\" } }" CRLF) - 1 | |
517 + 2 * NGX_SIZE_T_LEN; | |
518 | |
519 b->pos = ngx_pnalloc(r->pool, len); | |
520 if (b->pos == NULL) { | |
521 return NULL; | |
522 } | |
523 | |
524 b->last = ngx_sprintf(b->pos, | |
525 "{ \"img\" : " | |
526 "{ \"width\": %uz," | |
527 " \"height\": %uz," | |
528 " \"type\": \"%s\" } }" CRLF, | |
529 ctx->width, ctx->height, | |
530 ngx_http_image_types[ctx->type - 1].data + 6); | |
531 | |
532 ngx_http_image_length(r, b); | |
533 | |
534 return b; | |
535 } | |
536 | |
537 | |
538 static ngx_buf_t * | |
539 ngx_http_image_asis(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
540 { | |
541 ngx_buf_t *b; | |
542 | |
543 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); | |
544 if (b == NULL) { | |
545 return NULL; | |
546 } | |
547 | |
548 b->pos = ctx->image; | |
549 b->last = ctx->last; | |
550 b->memory = 1; | |
551 b->last_buf = 1; | |
552 | |
553 ngx_http_image_length(r, b); | |
554 | |
555 return b; | |
556 } | |
557 | |
558 | |
559 static void | |
560 ngx_http_image_length(ngx_http_request_t *r, ngx_buf_t *b) | |
561 { | |
562 r->headers_out.content_length_n = b->last - b->pos; | |
563 | |
564 if (r->headers_out.content_length) { | |
565 r->headers_out.content_length->hash = 0; | |
566 } | |
567 | |
568 r->headers_out.content_length = NULL; | |
569 } | |
570 | |
571 | |
572 static ngx_int_t | |
573 ngx_http_image_size(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
574 { | |
575 u_char *p, *last; | |
576 ngx_uint_t width, height; | |
577 | |
578 p = ctx->image; | |
579 | |
580 switch (ctx->type) { | |
581 | |
582 case NGX_HTTP_IMAGE_JPEG: | |
583 | |
584 p += 2; | |
585 last = ctx->image + ctx->length - 10; | |
586 | |
587 while (p < last) { | |
588 | |
589 if (p[0] == 0xff && p[1] != 0xff) { | |
590 | |
591 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
592 "JPEG: %02xd %02xd", *p, *(p + 1)); | |
593 | |
594 p++; | |
595 | |
596 if (*p == 0xc0 || *p == 0xc1 || *p == 0xc2 || *p == 0xc3 | |
597 || *p == 0xc9 || *p == 0xca || *p == 0xcb) | |
598 { | |
599 goto found; | |
600 } | |
601 | |
602 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
603 "JPEG: %02xd %02xd", p[1], p[2]); | |
604 | |
605 p += p[1] * 256 + p[2]; | |
606 | |
607 continue; | |
608 } | |
609 | |
610 p++; | |
611 } | |
612 | |
613 return NGX_DECLINED; | |
614 | |
615 found: | |
616 | |
617 width = p[6] * 256 + p[7]; | |
618 height = p[4] * 256 + p[5]; | |
619 | |
620 break; | |
621 | |
622 case NGX_HTTP_IMAGE_GIF: | |
623 | |
624 if (ctx->length < 10) { | |
625 return NGX_DECLINED; | |
626 } | |
627 | |
628 width = p[7] * 256 + p[6]; | |
629 height = p[9] * 256 + p[8]; | |
630 | |
631 break; | |
632 | |
633 case NGX_HTTP_IMAGE_PNG: | |
634 | |
635 if (ctx->length < 24) { | |
636 return NGX_DECLINED; | |
637 } | |
638 | |
639 width = p[18] * 256 + p[19]; | |
640 height = p[22] * 256 + p[23]; | |
641 | |
642 break; | |
643 | |
644 default: | |
645 | |
646 return NGX_DECLINED; | |
647 } | |
648 | |
649 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
650 "image size: %d x %d", width, height); | |
651 | |
652 ctx->width = width; | |
653 ctx->height = height; | |
654 | |
655 return NGX_OK; | |
656 } | |
657 | |
658 | |
659 static ngx_buf_t * | |
660 ngx_http_image_resize(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
661 { | |
662 int sx, sy, dx, dy, ox, oy, | |
663 colors, transparent, size; | |
664 u_char *out; | |
665 ngx_buf_t *b; | |
666 ngx_uint_t resize; | |
667 gdImagePtr src, dst; | |
668 ngx_pool_cleanup_t *cln; | |
669 ngx_http_image_filter_conf_t *conf; | |
670 | |
671 src = ngx_http_image_source(r, ctx); | |
672 | |
673 if (src == NULL) { | |
674 return NULL; | |
675 } | |
676 | |
677 sx = gdImageSX(src); | |
678 sy = gdImageSY(src); | |
679 | |
680 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module); | |
681 | |
682 if ((ngx_uint_t) sx <= conf->width && (ngx_uint_t) sy <= conf->height) { | |
683 gdImageDestroy(src); | |
684 return ngx_http_image_asis(r, ctx); | |
685 } | |
686 | |
687 colors = gdImageColorsTotal(src); | |
688 transparent = gdImageGetTransparent(src); | |
689 | |
690 dx = sx; | |
691 dy = sy; | |
692 | |
693 if (conf->filter == NGX_HTTP_IMAGE_RESIZE) { | |
694 | |
695 if ((ngx_uint_t) dx > conf->width) { | |
696 dy = dy * conf->width / dx; | |
697 dy = dy ? dy : 1; | |
698 dx = conf->width; | |
699 } | |
700 | |
701 if ((ngx_uint_t) dy > conf->height) { | |
702 dx = dx * conf->height / dy; | |
703 dx = dx ? dx : 1; | |
704 dy = conf->height; | |
705 } | |
706 | |
707 resize = 1; | |
708 | |
709 } else { /* NGX_HTTP_IMAGE_CROP */ | |
710 | |
711 resize = 0; | |
712 | |
713 if ((ngx_uint_t) (dx * 100 / dy) < conf->width * 100 / conf->height) { | |
714 | |
715 if ((ngx_uint_t) dx > conf->width) { | |
716 dy = dy * conf->width / dx; | |
717 dy = dy ? dy : 1; | |
718 dx = conf->width; | |
719 resize = 1; | |
720 } | |
721 | |
722 } else { | |
723 if ((ngx_uint_t) dy > conf->height) { | |
724 dx = dx * conf->height / dy; | |
725 dx = dx ? dx : 1; | |
726 dy = conf->height; | |
727 resize = 1; | |
728 } | |
729 } | |
730 } | |
731 | |
732 if (resize) { | |
733 dst = ngx_http_image_new(r, dx, dy, colors); | |
734 if (dst == NULL) { | |
735 gdImageDestroy(src); | |
736 return NULL; | |
737 } | |
738 | |
739 gdImageCopyResampled(dst, src, 0, 0, 0, 0, dx, dy, sx, sy); | |
740 | |
741 gdImageDestroy(src); | |
742 | |
743 } else { | |
744 dst = src; | |
745 } | |
746 | |
747 if (conf->filter == NGX_HTTP_IMAGE_CROP) { | |
748 | |
749 src = dst; | |
750 | |
751 if ((ngx_uint_t) dx > conf->width) { | |
752 ox = dx - conf->width; | |
753 | |
754 } else { | |
755 ox = 0; | |
756 } | |
757 | |
758 if ((ngx_uint_t) dy > conf->height) { | |
759 oy = dy - conf->height; | |
760 | |
761 } else { | |
762 oy = 0; | |
763 } | |
764 | |
765 if (ox || oy) { | |
766 | |
767 dst = ngx_http_image_new(r, dx - ox, dy - oy, colors); | |
768 | |
769 if (dst == NULL) { | |
770 gdImageDestroy(src); | |
771 return NULL; | |
772 } | |
773 | |
774 ox /= 2; | |
775 oy /= 2; | |
776 | |
777 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
778 "image crop: %d x %d @ %d x %d", | |
779 dx, dy, ox, oy); | |
780 | |
781 gdImageCopy(dst, src, 0, 0, ox, oy, dx - ox, dy - oy); | |
782 | |
783 gdImageDestroy(src); | |
784 } | |
785 } | |
786 | |
787 gdImageColorTransparent(dst, transparent); | |
788 | |
789 out = ngx_http_image_out(r, ctx->type, dst, &size); | |
790 | |
791 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
792 "image: %d x %d %d", sx, sy, colors); | |
793 | |
794 gdImageDestroy(dst); | |
795 ngx_pfree(r->pool, ctx->image); | |
796 | |
797 if (out == NULL) { | |
798 return NULL; | |
799 } | |
800 | |
801 cln = ngx_pool_cleanup_add(r->pool, 0); | |
802 if (cln == NULL) { | |
803 gdFree(out); | |
804 return NULL; | |
805 } | |
806 | |
807 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); | |
808 if (b == NULL) { | |
809 gdFree(out); | |
810 return NULL; | |
811 } | |
812 | |
813 cln->handler = ngx_http_image_cleanup; | |
814 cln->data = out; | |
815 | |
816 b->pos = out; | |
817 b->last = out + size; | |
818 b->memory = 1; | |
819 b->last_buf = 1; | |
820 | |
821 ngx_http_image_length(r, b); | |
822 | |
823 return b; | |
824 } | |
825 | |
826 | |
827 static gdImagePtr | |
828 ngx_http_image_source(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) | |
829 { | |
830 char *failed; | |
831 gdImagePtr img; | |
832 | |
833 img = NULL; | |
834 | |
835 switch (ctx->type) { | |
836 | |
837 case NGX_HTTP_IMAGE_JPEG: | |
838 img = gdImageCreateFromJpegPtr(ctx->length, ctx->image); | |
839 failed = "gdImageCreateFromJpegPtr() failed"; | |
840 break; | |
841 | |
842 case NGX_HTTP_IMAGE_GIF: | |
843 img = gdImageCreateFromGifPtr(ctx->length, ctx->image); | |
844 failed = "gdImageCreateFromGifPtr() failed"; | |
845 break; | |
846 | |
847 case NGX_HTTP_IMAGE_PNG: | |
848 img = gdImageCreateFromPngPtr(ctx->length, ctx->image); | |
849 failed = "gdImageCreateFromPngPtr() failed"; | |
850 break; | |
851 | |
852 default: | |
853 failed = "unknown image type"; | |
854 break; | |
855 } | |
856 | |
857 if (img == NULL) { | |
858 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, failed); | |
859 } | |
860 | |
861 return img; | |
862 } | |
863 | |
864 | |
865 static gdImagePtr | |
866 ngx_http_image_new(ngx_http_request_t *r, int w, int h, int colors) | |
867 { | |
868 gdImagePtr img; | |
869 | |
870 if (colors == 0) { | |
871 img = gdImageCreateTrueColor(w, h); | |
872 | |
873 if (img == NULL) { | |
874 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
875 "gdImageCreateTrueColor() failed"); | |
876 return NULL; | |
877 } | |
878 | |
879 } else { | |
880 img = gdImageCreate(w, h); | |
881 | |
882 if (img == NULL) { | |
883 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
884 "gdImageCreate() failed"); | |
885 return NULL; | |
886 } | |
887 } | |
888 | |
889 return img; | |
890 } | |
891 | |
892 | |
893 static u_char * | |
894 ngx_http_image_out(ngx_http_request_t *r, ngx_uint_t type, gdImagePtr img, | |
895 int *size) | |
896 { | |
2848 | 897 char *failed; |
898 u_char *out; | |
899 ngx_http_image_filter_conf_t *conf; | |
2788 | 900 |
901 out = NULL; | |
902 | |
903 switch (type) { | |
904 | |
905 case NGX_HTTP_IMAGE_JPEG: | |
2848 | 906 conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module); |
907 out = gdImageJpegPtr(img, size, conf->jpeg_quality); | |
2788 | 908 failed = "gdImageJpegPtr() failed"; |
909 break; | |
910 | |
911 case NGX_HTTP_IMAGE_GIF: | |
912 out = gdImageGifPtr(img, size); | |
913 failed = "gdImageGifPtr() failed"; | |
914 break; | |
915 | |
916 case NGX_HTTP_IMAGE_PNG: | |
917 out = gdImagePngPtr(img, size); | |
918 failed = "gdImagePngPtr() failed"; | |
919 break; | |
920 | |
921 default: | |
922 failed = "unknown image type"; | |
923 break; | |
924 } | |
925 | |
926 if (out == NULL) { | |
927 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, failed); | |
928 } | |
929 | |
930 return out; | |
931 } | |
932 | |
933 | |
934 static void | |
935 ngx_http_image_cleanup(void *data) | |
936 { | |
937 gdFree(data); | |
938 } | |
939 | |
940 | |
941 static void * | |
942 ngx_http_image_filter_create_conf(ngx_conf_t *cf) | |
943 { | |
944 ngx_http_image_filter_conf_t *conf; | |
945 | |
946 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_image_filter_conf_t)); | |
947 if (conf == NULL) { | |
948 return NGX_CONF_ERROR; | |
949 } | |
950 | |
951 conf->filter = NGX_CONF_UNSET_UINT; | |
2848 | 952 conf->jpeg_quality = NGX_CONF_UNSET; |
2788 | 953 conf->buffer_size = NGX_CONF_UNSET_SIZE; |
954 | |
955 return conf; | |
956 } | |
957 | |
958 | |
959 static char * | |
960 ngx_http_image_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
961 { | |
962 ngx_http_image_filter_conf_t *prev = parent; | |
963 ngx_http_image_filter_conf_t *conf = child; | |
964 | |
965 if (conf->filter == NGX_CONF_UNSET_UINT) { | |
966 | |
967 if (prev->filter == NGX_CONF_UNSET_UINT) { | |
968 conf->filter = NGX_HTTP_IMAGE_OFF; | |
969 | |
970 } else { | |
971 conf->filter = prev->filter; | |
972 conf->width = prev->width; | |
973 conf->height = prev->height; | |
974 } | |
975 } | |
976 | |
2848 | 977 /* 75 is libjpeg default quality */ |
978 ngx_conf_merge_value(conf->jpeg_quality, prev->jpeg_quality, 75); | |
979 | |
2788 | 980 ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size, |
981 1 * 1024 * 1024); | |
982 | |
983 return NGX_CONF_OK; | |
984 } | |
985 | |
986 | |
987 static char * | |
988 ngx_http_image_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
989 { | |
990 ngx_http_image_filter_conf_t *imcf = conf; | |
991 | |
992 ngx_str_t *value; | |
993 ngx_int_t n; | |
994 ngx_uint_t i; | |
995 | |
996 value = cf->args->elts; | |
997 | |
998 i = 1; | |
999 | |
1000 if (cf->args->nelts == 2) { | |
1001 if (ngx_strcmp(value[i].data, "off") == 0) { | |
1002 imcf->filter = NGX_HTTP_IMAGE_OFF; | |
1003 | |
1004 } else if (ngx_strcmp(value[i].data, "test") == 0) { | |
1005 imcf->filter = NGX_HTTP_IMAGE_TEST; | |
1006 | |
1007 } else if (ngx_strcmp(value[i].data, "size") == 0) { | |
1008 imcf->filter = NGX_HTTP_IMAGE_SIZE; | |
1009 | |
1010 } else { | |
1011 goto failed; | |
1012 } | |
1013 | |
1014 return NGX_CONF_OK; | |
1015 } | |
1016 | |
1017 if (ngx_strcmp(value[i].data, "resize") == 0) { | |
1018 imcf->filter = NGX_HTTP_IMAGE_RESIZE; | |
1019 | |
1020 } else if (ngx_strcmp(value[i].data, "crop") == 0) { | |
1021 imcf->filter = NGX_HTTP_IMAGE_CROP; | |
1022 | |
1023 } else { | |
1024 goto failed; | |
1025 } | |
1026 | |
1027 i++; | |
1028 | |
1029 if (value[i].len == 1 && value[i].data[0] == '-') { | |
1030 imcf->width = (ngx_uint_t) -1; | |
1031 | |
1032 } else { | |
1033 n = ngx_atoi(value[i].data, value[i].len); | |
1034 if (n == NGX_ERROR) { | |
1035 goto failed; | |
1036 } | |
1037 | |
1038 imcf->width = (ngx_uint_t) n; | |
1039 } | |
1040 | |
1041 i++; | |
1042 | |
1043 if (value[i].len == 1 && value[i].data[0] == '-') { | |
1044 imcf->height = (ngx_uint_t) -1; | |
1045 | |
1046 } else { | |
1047 n = ngx_atoi(value[i].data, value[i].len); | |
1048 if (n == NGX_ERROR) { | |
1049 goto failed; | |
1050 } | |
1051 | |
1052 imcf->height = (ngx_uint_t) n; | |
1053 } | |
1054 | |
1055 return NGX_CONF_OK; | |
1056 | |
1057 failed: | |
1058 | |
1059 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"", | |
1060 &value[i]); | |
1061 | |
1062 return NGX_CONF_ERROR; | |
1063 } | |
1064 | |
1065 | |
1066 static ngx_int_t | |
1067 ngx_http_image_filter_init(ngx_conf_t *cf) | |
1068 { | |
1069 ngx_http_next_header_filter = ngx_http_top_header_filter; | |
1070 ngx_http_top_header_filter = ngx_http_image_header_filter; | |
1071 | |
1072 ngx_http_next_body_filter = ngx_http_top_body_filter; | |
1073 ngx_http_top_body_filter = ngx_http_image_body_filter; | |
1074 | |
1075 return NGX_OK; | |
1076 } |