annotate src/http/modules/ngx_http_image_filter_module.c @ 2795:d82d08314f78

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