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