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