1
2 /*
3 * Copyright 2007 Keith Rarick <kr@xph.us>
4 * Copyright (C) Igor Sysoev
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 */
28
29
30 #include <ngx_config.h>
31 #include <ngx_core.h>
32 #include <ngx_http.h>
33
34 #define NOTICE_BUF_SIZE 102400 /* 100KiB */
35
36 #define ALLOWED_METHODS (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_PUT|NGX_HTTP_POST)
37
38 static void *
39 ngx_http_notice_create_loc_conf(ngx_conf_t *cf);
40
41 static char *
42 ngx_http_notice_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
43
44 static char *
45 ngx_http_notice_post_handler(ngx_conf_t *cf, void *data, void *conf);
46
47 typedef struct {
48 ngx_str_t path;
49 ngx_str_t type;
50 } ngx_http_notice_conf_t;
51
52 static ngx_conf_post_t ngx_http_notice_post = {
53 ngx_http_notice_post_handler,
54 };
55
56 static ngx_command_t ngx_http_notice_commands[] = {
57
58 { ngx_string("notice"),
59 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
60 ngx_conf_set_str_slot,
61 NGX_HTTP_LOC_CONF_OFFSET,
62 offsetof(ngx_http_notice_conf_t, path),
63 &ngx_http_notice_post },
64
65 { ngx_string("notice_type"),
66 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
67 ngx_conf_set_str_slot,
68 NGX_HTTP_LOC_CONF_OFFSET,
69 offsetof(ngx_http_notice_conf_t, type),
70 NULL },
71
72 ngx_null_command
73 };
74
75
76
77 static ngx_http_module_t ngx_http_notice_module_ctx = {
78 NULL, /* preconfiguration */
79 NULL, /* postconfiguration */
80
81 NULL, /* create main configuration */
82 NULL, /* init main configuration */
83
84 NULL, /* create server configuration */
85 NULL, /* merge server configuration */
86
87 ngx_http_notice_create_loc_conf,/* create location configuration */
88 ngx_http_notice_merge_loc_conf, /* merge location configuration */
89 };
90
91
92 ngx_module_t ngx_http_notice_module = {
93 NGX_MODULE_V1,
94 &ngx_http_notice_module_ctx, /* module context */
95 ngx_http_notice_commands, /* module directives */
96 NGX_HTTP_MODULE, /* module type */
97 NULL, /* init master */
98 NULL, /* init module */
99 NULL, /* init process */
100 NULL, /* init thread */
101 NULL, /* exit thread */
102 NULL, /* exit process */
103 NULL, /* exit master */
104 NGX_MODULE_V1_PADDING
105 };
106
107
108 static void *
ngx_http_notice_create_loc_conf(ngx_conf_t * cf)109 ngx_http_notice_create_loc_conf(ngx_conf_t *cf)
110 {
111 ngx_http_notice_conf_t *conf;
112
113 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_notice_conf_t));
114 if (conf == NULL) return NGX_CONF_ERROR;
115
116 /*
117 * Set by ngx_pcalloc():
118 *
119 * conf->path.len = 0;
120 * conf->path.data = NULL;
121 * conf->type.len = 0;
122 * conf->type.data = NULL;
123 */
124
125 return conf;
126 }
127
128 static char *
ngx_http_notice_merge_loc_conf(ngx_conf_t * cf,void * parent,void * child)129 ngx_http_notice_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
130 {
131 ngx_http_notice_conf_t *prev = parent;
132 ngx_http_notice_conf_t *conf = child;
133
134 ngx_conf_merge_str_value(conf->path, prev->path, "");
135 ngx_conf_merge_str_value(conf->type, prev->type, "text/html");
136
137 return NGX_CONF_OK;
138 }
139
140 static ngx_int_t
ngx_http_notice_handler(ngx_http_request_t * r)141 ngx_http_notice_handler(ngx_http_request_t *r)
142 {
143 ngx_int_t rc, n;
144 ngx_fd_t fd;
145 ngx_buf_t *b;
146 ngx_chain_t out;
147 ngx_http_notice_conf_t *nlcf;
148 u_char notice[NOTICE_BUF_SIZE];
149
150 nlcf = ngx_http_get_module_loc_conf(r, ngx_http_notice_module);
151
152 if (!(r->method & ALLOWED_METHODS)) {
153 return NGX_HTTP_NOT_ALLOWED;
154 }
155
156 rc = ngx_http_discard_request_body(r);
157
158 if (rc != NGX_OK && rc != NGX_AGAIN) {
159 return rc;
160 }
161
162 r->headers_out.content_type = nlcf->type;
163
164 if (r->method == NGX_HTTP_HEAD) {
165 r->headers_out.status = NGX_HTTP_OK;
166
167 rc = ngx_http_send_header(r);
168
169 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
170 return rc;
171 }
172 }
173
174 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
175 if (b == NULL) {
176 return NGX_HTTP_INTERNAL_SERVER_ERROR;
177 }
178
179 out.buf = b;
180 out.next = NULL;
181
182 fd = ngx_open_file(nlcf->path.data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
183 if (fd == NGX_INVALID_FILE) {
184 /* TODO: log an error */
185 return NGX_HTTP_NOT_FOUND;
186 }
187
188 n = ngx_read_fd(fd, notice, NOTICE_BUF_SIZE);
189 if (n == NGX_FILE_ERROR) {
190 /* TODO: log an error */
191 return NGX_HTTP_NOT_FOUND;
192 }
193
194 rc = ngx_close_file(fd);
195 if (rc == NGX_FILE_ERROR) {
196 /* TODO: log an error */
197 /* but continue on with the request, because we have the data now */
198 }
199
200 b->pos = notice;
201 b->last = notice + n;
202 b->memory = 1;
203 b->last_buf = 1;
204
205 r->headers_out.status = NGX_HTTP_OK;
206 r->headers_out.content_length_n = n;
207 r->headers_out.last_modified_time = 23349600;
208
209 rc = ngx_http_send_header(r);
210
211 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
212 return rc;
213 }
214
215 return ngx_http_output_filter(r, &out);
216 }
217
218 static char *
ngx_http_notice_post_handler(ngx_conf_t * cf,void * data,void * conf)219 ngx_http_notice_post_handler(ngx_conf_t *cf, void *data, void *conf)
220 {
221 ngx_http_core_loc_conf_t *clcf;
222
223 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
224 clcf->handler = ngx_http_notice_handler;
225
226 return NGX_CONF_OK;
227 }
228
229