Mercurial > hg > nginx
comparison src/http/modules/ngx_http_rewrite_handler.c @ 437:470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 23 Sep 2004 16:39:34 +0000 |
parents | 6f3b20c1ac50 |
children | da8c5707af39 |
comparison
equal
deleted
inserted
replaced
436:9549fc9508e5 | 437:470270fa84d2 |
---|---|
35 ngx_array_t rules; | 35 ngx_array_t rules; |
36 ngx_flag_t log; | 36 ngx_flag_t log; |
37 } ngx_http_rewrite_srv_conf_t; | 37 } ngx_http_rewrite_srv_conf_t; |
38 | 38 |
39 | 39 |
40 typedef struct { | |
41 ngx_str_t redirect; | |
42 } ngx_http_rewrite_loc_conf_t; | |
43 | |
44 | |
40 static void *ngx_http_rewrite_create_srv_conf(ngx_conf_t *cf); | 45 static void *ngx_http_rewrite_create_srv_conf(ngx_conf_t *cf); |
41 static char *ngx_http_rewrite_merge_srv_conf(ngx_conf_t *cf, | 46 static char *ngx_http_rewrite_merge_srv_conf(ngx_conf_t *cf, |
42 void *parent, void *child); | 47 void *parent, void *child); |
48 static void *ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf); | |
43 static char *ngx_http_rewrite_rule(ngx_conf_t *cf, ngx_command_t *cmd, | 49 static char *ngx_http_rewrite_rule(ngx_conf_t *cf, ngx_command_t *cmd, |
44 void *conf); | 50 void *conf); |
51 static char *ngx_http_redirect(ngx_conf_t *cf, void *post, void *data); | |
45 static ngx_int_t ngx_http_rewrite_init(ngx_cycle_t *cycle); | 52 static ngx_int_t ngx_http_rewrite_init(ngx_cycle_t *cycle); |
53 | |
54 | |
55 static ngx_conf_post_handler_pt ngx_http_redirect_p = ngx_http_redirect; | |
46 | 56 |
47 | 57 |
48 static ngx_command_t ngx_http_rewrite_commands[] = { | 58 static ngx_command_t ngx_http_rewrite_commands[] = { |
49 | 59 |
50 { ngx_string("rewrite"), | 60 { ngx_string("rewrite"), |
52 ngx_http_rewrite_rule, | 62 ngx_http_rewrite_rule, |
53 NGX_HTTP_SRV_CONF_OFFSET, | 63 NGX_HTTP_SRV_CONF_OFFSET, |
54 0, | 64 0, |
55 NULL }, | 65 NULL }, |
56 | 66 |
67 { ngx_string("redirect"), | |
68 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
69 ngx_conf_set_str_slot, | |
70 NGX_HTTP_LOC_CONF_OFFSET, | |
71 0, | |
72 &ngx_http_redirect_p }, | |
73 | |
57 { ngx_string("rewrite_log"), | 74 { ngx_string("rewrite_log"), |
58 NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1, | 75 NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1, |
59 ngx_conf_set_flag_slot, | 76 ngx_conf_set_flag_slot, |
60 NGX_HTTP_SRV_CONF_OFFSET, | 77 NGX_HTTP_SRV_CONF_OFFSET, |
61 offsetof(ngx_http_rewrite_srv_conf_t, log), | 78 offsetof(ngx_http_rewrite_srv_conf_t, log), |
72 NULL, /* init main configuration */ | 89 NULL, /* init main configuration */ |
73 | 90 |
74 ngx_http_rewrite_create_srv_conf, /* create server configuration */ | 91 ngx_http_rewrite_create_srv_conf, /* create server configuration */ |
75 ngx_http_rewrite_merge_srv_conf, /* merge server configuration */ | 92 ngx_http_rewrite_merge_srv_conf, /* merge server configuration */ |
76 | 93 |
77 NULL, /* create location configration */ | 94 ngx_http_rewrite_create_loc_conf, /* create location configration */ |
78 NULL, /* merge location configration */ | 95 NULL, /* merge location configration */ |
79 }; | 96 }; |
80 | 97 |
81 | 98 |
82 ngx_module_t ngx_http_rewrite_module = { | 99 ngx_module_t ngx_http_rewrite_module = { |
201 | 218 |
202 return NGX_DECLINED; | 219 return NGX_DECLINED; |
203 } | 220 } |
204 | 221 |
205 | 222 |
223 static ngx_int_t ngx_http_redirect_handler(ngx_http_request_t *r) | |
224 { | |
225 u_char *p; | |
226 ngx_http_rewrite_loc_conf_t *rlcf; | |
227 | |
228 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
229 "http redirect handler"); | |
230 | |
231 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_rewrite_module); | |
232 | |
233 r->headers_out.location = ngx_list_push(&r->headers_out.headers); | |
234 if (r->headers_out.location == NULL) { | |
235 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
236 } | |
237 | |
238 if (rlcf->redirect.data[0] != '/') { | |
239 r->headers_out.location->key.len = sizeof("Location") - 1; | |
240 r->headers_out.location->key.data = (u_char *) "Location"; | |
241 } | |
242 | |
243 r->headers_out.location->value.len = rlcf->redirect.len | |
244 + r->unparsed_uri.len; | |
245 r->headers_out.location->value.data = ngx_palloc(r->pool, | |
246 r->headers_out.location->value.len); | |
247 | |
248 if (r->headers_out.location->value.data == NULL) { | |
249 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
250 } | |
251 | |
252 p = ngx_cpymem(r->headers_out.location->value.data, rlcf->redirect.data, | |
253 rlcf->redirect.len); | |
254 p = ngx_cpystrn(p, r->unparsed_uri.data + 1, r->unparsed_uri.len); | |
255 | |
256 return NGX_HTTP_MOVED_TEMPORARILY; | |
257 } | |
258 | |
259 | |
206 static void *ngx_http_rewrite_create_srv_conf(ngx_conf_t *cf) | 260 static void *ngx_http_rewrite_create_srv_conf(ngx_conf_t *cf) |
207 { | 261 { |
208 ngx_http_rewrite_srv_conf_t *conf; | 262 ngx_http_rewrite_srv_conf_t *conf; |
209 | 263 |
210 if (!(conf = ngx_palloc(cf->pool, sizeof(ngx_http_rewrite_srv_conf_t)))) { | 264 if (!(conf = ngx_palloc(cf->pool, sizeof(ngx_http_rewrite_srv_conf_t)))) { |
227 ngx_http_rewrite_srv_conf_t *conf = child; | 281 ngx_http_rewrite_srv_conf_t *conf = child; |
228 | 282 |
229 ngx_conf_merge_value(conf->log, prev->log, 0); | 283 ngx_conf_merge_value(conf->log, prev->log, 0); |
230 | 284 |
231 return NGX_CONF_OK; | 285 return NGX_CONF_OK; |
286 } | |
287 | |
288 | |
289 static void *ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf) | |
290 { | |
291 ngx_http_rewrite_loc_conf_t *conf; | |
292 | |
293 if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_rewrite_loc_conf_t)))) { | |
294 return NGX_CONF_ERROR; | |
295 } | |
296 | |
297 return conf; | |
232 } | 298 } |
233 | 299 |
234 | 300 |
235 static char *ngx_http_rewrite_rule(ngx_conf_t *cf, ngx_command_t *cmd, | 301 static char *ngx_http_rewrite_rule(ngx_conf_t *cf, ngx_command_t *cmd, |
236 void *conf) | 302 void *conf) |
364 | 430 |
365 return NGX_CONF_OK; | 431 return NGX_CONF_OK; |
366 } | 432 } |
367 | 433 |
368 | 434 |
435 static char *ngx_http_redirect(ngx_conf_t *cf, void *post, void *data) | |
436 { | |
437 ngx_http_core_loc_conf_t *clcf; | |
438 | |
439 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | |
440 clcf->handler = ngx_http_redirect_handler; | |
441 | |
442 return NGX_CONF_OK; | |
443 } | |
444 | |
445 | |
369 static ngx_int_t ngx_http_rewrite_init(ngx_cycle_t *cycle) | 446 static ngx_int_t ngx_http_rewrite_init(ngx_cycle_t *cycle) |
370 { | 447 { |
371 ngx_http_handler_pt *h; | 448 ngx_http_handler_pt *h; |
372 ngx_http_core_main_conf_t *cmcf; | 449 ngx_http_core_main_conf_t *cmcf; |
373 | 450 |