Klaus Demo nginx / a2b7ceb
r2202, r2408, r2425, r2454, r2459, r2482, r2504, r2542, r2565, r2579, r2580, r2585, r2586, r2587, r2591, r2626 merge: try_files Igor Sysoev 13 years ago
6 changed file(s) with 333 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
5151
5252
5353 #define ngx_strstr(s1, s2) strstr((const char *) s1, (const char *) s2)
54 #define ngx_strlen(s) strlen((const char *) s)
55
5456 #define ngx_strchr(s1, c) strchr((const char *) s1, (int) c)
55 #define ngx_strlen(s) strlen((const char *) s)
57
58 static ngx_inline u_char *
59 ngx_strlchr(u_char *p, u_char *last, u_char c)
60 {
61 while (p < last) {
62
63 if (*p == c) {
64 return p;
65 }
66
67 p++;
68 }
69
70 return NULL;
71 }
5672
5773
5874 /*
405405 use_rewrite = cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers.nelts ? 1 : 0;
406406 use_access = cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers.nelts ? 1 : 0;
407407
408 n = use_rewrite + use_access + 1; /* find config phase */
408 n = use_rewrite + use_access + cmcf->try_files + 1 /* find config phase */;
409409
410410 for (i = 0; i < NGX_HTTP_LOG_PHASE; i++) {
411411 n += cmcf->phases[i].handlers.nelts;
469469 if (use_access) {
470470 ph->checker = ngx_http_core_post_access_phase;
471471 ph->next = n;
472 ph++;
473 }
474
475 continue;
476
477 case NGX_HTTP_TRY_FILES_PHASE:
478 if (cmcf->try_files) {
479 ph->checker = ngx_http_core_try_files_phase;
480 n++;
472481 ph++;
473482 }
474483
7070 ngx_int_t ngx_http_parse_header_line(ngx_http_request_t *r, ngx_buf_t *b);
7171 ngx_int_t ngx_http_parse_multi_header_lines(ngx_array_t *headers,
7272 ngx_str_t *name, ngx_str_t *value);
73 void ngx_http_split_args(ngx_http_request_t *r, ngx_str_t *uri,
74 ngx_str_t *args);
7375
7476 ngx_int_t ngx_http_find_server_conf(ngx_http_request_t *r);
7577 void ngx_http_update_location_config(ngx_http_request_t *r);
6060 static char *ngx_http_core_limit_except(ngx_conf_t *cf, ngx_command_t *cmd,
6161 void *conf);
6262 static char *ngx_http_core_error_page(ngx_conf_t *cf, ngx_command_t *cmd,
63 void *conf);
64 static char *ngx_http_core_try_files(ngx_conf_t *cf, ngx_command_t *cmd,
6365 void *conf);
6466 static char *ngx_http_core_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd,
6567 void *conf);
529531 0,
530532 NULL },
531533
534 { ngx_string("try_files"),
535 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
536 ngx_http_core_try_files,
537 NGX_HTTP_LOC_CONF_OFFSET,
538 0,
539 NULL },
540
532541 { ngx_string("post_action"),
533542 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
534543 |NGX_CONF_TAKE1,
10111020
10121021
10131022 ngx_int_t
1023 ngx_http_core_try_files_phase(ngx_http_request_t *r,
1024 ngx_http_phase_handler_t *ph)
1025 {
1026 size_t len, root, alias, reserve, allocated;
1027 u_char *p, *name;
1028 ngx_str_t path, args;
1029 ngx_uint_t test_dir;
1030 ngx_http_try_file_t *tf;
1031 ngx_open_file_info_t of;
1032 ngx_http_script_code_pt code;
1033 ngx_http_script_engine_t e;
1034 ngx_http_core_loc_conf_t *clcf;
1035 ngx_http_script_len_code_pt lcode;
1036
1037 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1038 "try files phase: %ui", r->phase_handler);
1039
1040 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1041
1042 if (clcf->try_files == NULL) {
1043 r->phase_handler++;
1044 return NGX_AGAIN;
1045 }
1046
1047 allocated = 0;
1048 root = 0;
1049 name = NULL;
1050 /* suppress MSVC warning */
1051 path.data = NULL;
1052
1053 tf = clcf->try_files;
1054
1055 alias = clcf->alias ? clcf->name.len : 0;
1056
1057 for ( ;; ) {
1058
1059 if (tf->lengths) {
1060 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
1061
1062 e.ip = tf->lengths->elts;
1063 e.request = r;
1064
1065 /* 1 is for terminating '\0' as in static names */
1066 len = 1;
1067
1068 while (*(uintptr_t *) e.ip) {
1069 lcode = *(ngx_http_script_len_code_pt *) e.ip;
1070 len += lcode(&e);
1071 }
1072
1073 } else {
1074 len = tf->name.len;
1075 }
1076
1077 /* 16 bytes are preallocation */
1078 reserve = ngx_abs((ssize_t) (len - r->uri.len)) + alias + 16;
1079
1080 if (reserve > allocated) {
1081
1082 /* we just need to allocate path and to copy a root */
1083
1084 if (ngx_http_map_uri_to_path(r, &path, &root, reserve) == NULL) {
1085 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1086 return NGX_OK;
1087 }
1088
1089 name = path.data + root;
1090 allocated = path.len - root - (r->uri.len - alias);
1091 }
1092
1093 if (tf->values == NULL) {
1094
1095 /* tf->name.len includes the terminating '\0' */
1096
1097 ngx_memcpy(name, tf->name.data, tf->name.len);
1098
1099 path.len = (name + tf->name.len - 1) - path.data;
1100
1101 } else {
1102 e.ip = tf->values->elts;
1103 e.pos = name;
1104 e.flushed = 1;
1105
1106 while (*(uintptr_t *) e.ip) {
1107 code = *(ngx_http_script_code_pt *) e.ip;
1108 code((ngx_http_script_engine_t *) &e);
1109 }
1110
1111 path.len = e.pos - path.data;
1112
1113 *e.pos = '\0';
1114
1115 if (alias && ngx_strncmp(name, clcf->name.data, alias) == 0) {
1116 ngx_memcpy(name, name + alias, len - alias);
1117 path.len -= alias;
1118 }
1119 }
1120
1121 test_dir = tf->test_dir;
1122
1123 tf++;
1124
1125 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1126 "try to use file: \"%s\" \"%s\"", name, path.data);
1127
1128 if (tf->lengths == NULL && tf->name.len == 0) {
1129
1130 path.len -= root;
1131 path.data += root;
1132
1133 if (path.data[0] == '@') {
1134 (void) ngx_http_named_location(r, &path);
1135
1136 } else {
1137 ngx_http_split_args(r, &path, &args);
1138
1139 (void) ngx_http_internal_redirect(r, &path, &args);
1140 }
1141
1142 return NGX_OK;
1143 }
1144
1145 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
1146
1147 of.valid = clcf->open_file_cache_valid;
1148 of.min_uses = clcf->open_file_cache_min_uses;
1149 of.errors = clcf->open_file_cache_errors;
1150 of.events = clcf->open_file_cache_events;
1151
1152 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
1153 != NGX_OK)
1154 {
1155 if (of.err != NGX_ENOENT && of.err != NGX_ENOTDIR) {
1156 ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
1157 ngx_open_file_n " \"%s\" failed", path.data);
1158 }
1159
1160 continue;
1161 }
1162
1163 if (of.is_dir && !test_dir) {
1164 continue;
1165 }
1166
1167 path.len -= root;
1168 path.data += root;
1169
1170 if (!alias) {
1171 r->uri = path;
1172
1173 } else {
1174 r->uri.len = alias + path.len;
1175 r->uri.data = ngx_palloc(r->pool, r->uri.len);
1176 if (r->uri.data == NULL) {
1177 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1178 return NGX_OK;
1179 }
1180
1181 p = ngx_copy(r->uri.data, clcf->name.data, alias);
1182 ngx_memcpy(p, name, path.len);
1183 }
1184
1185 if (ngx_http_set_exten(r) != NGX_OK) {
1186 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1187 return NGX_OK;
1188 }
1189
1190 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1191 "try file uri: \"%V\"", &r->uri);
1192
1193 r->phase_handler++;
1194 return NGX_AGAIN;
1195 }
1196
1197 /* not reached */
1198 }
1199
1200
1201 ngx_int_t
10141202 ngx_http_core_content_phase(ngx_http_request_t *r,
10151203 ngx_http_phase_handler_t *ph)
10161204 {
26862874 * lcf->default_type = { 0, NULL };
26872875 * lcf->err_log = NULL;
26882876 * lcf->error_pages = NULL;
2877 * lcf->try_files = NULL;
26892878 * lcf->client_body_path = NULL;
26902879 * lcf->regex = NULL;
26912880 * lcf->exact_match = 0;
36053794
36063795
36073796 static char *
3797 ngx_http_core_try_files(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3798 {
3799 ngx_http_core_loc_conf_t *clcf = conf;
3800
3801 ngx_str_t *value;
3802 ngx_uint_t i, n;
3803 ngx_http_try_file_t *tf;
3804 ngx_http_script_compile_t sc;
3805 ngx_http_core_main_conf_t *cmcf;
3806
3807 if (clcf->try_files) {
3808 return "is duplicate";
3809 }
3810
3811 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
3812
3813 cmcf->try_files = 1;
3814
3815 tf = ngx_pcalloc(cf->pool, cf->args->nelts * sizeof(ngx_http_try_file_t));
3816 if (tf == NULL) {
3817 return NGX_CONF_ERROR;
3818 }
3819
3820 clcf->try_files = tf;
3821
3822 value = cf->args->elts;
3823
3824 for (i = 0; i < cf->args->nelts - 1; i++) {
3825
3826 tf[i].name = value[i + 1];
3827
3828 if (tf[i].name.data[tf[i].name.len - 1] == '/') {
3829 tf[i].test_dir = 1;
3830 tf[i].name.len--;
3831 tf[i].name.data[tf[i].name.len] = '\0';
3832 }
3833
3834 n = ngx_http_script_variables_count(&tf[i].name);
3835
3836 if (n) {
3837 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
3838
3839 sc.cf = cf;
3840 sc.source = &tf[i].name;
3841 sc.lengths = &tf[i].lengths;
3842 sc.values = &tf[i].values;
3843 sc.variables = n;
3844 sc.complete_lengths = 1;
3845 sc.complete_values = 1;
3846
3847 if (ngx_http_script_compile(&sc) != NGX_OK) {
3848 return NGX_CONF_ERROR;
3849 }
3850
3851 } else {
3852 /* add trailing '\0' to length */
3853 tf[i].name.len++;
3854 }
3855 }
3856
3857 return NGX_CONF_OK;
3858 }
3859
3860
3861 static char *
36083862 ngx_http_core_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
36093863 {
36103864 ngx_http_core_loc_conf_t *lcf = conf;
7878 NGX_HTTP_ACCESS_PHASE,
7979 NGX_HTTP_POST_ACCESS_PHASE,
8080
81 NGX_HTTP_TRY_FILES_PHASE,
8182 NGX_HTTP_CONTENT_PHASE,
8283
8384 NGX_HTTP_LOG_PHASE
125126 ngx_uint_t variables_hash_bucket_size;
126127
127128 ngx_hash_keys_arrays_t *variables_keys;
129
130 ngx_uint_t try_files; /* unsigned try_files:1 */
128131
129132 ngx_http_phase_t phases[NGX_HTTP_LOG_PHASE + 1];
130133 } ngx_http_core_main_conf_t;
233236 ngx_array_t *uri_lengths;
234237 ngx_array_t *uri_values;
235238 } ngx_http_err_page_t;
239
240
241 typedef struct {
242 ngx_array_t *lengths;
243 ngx_array_t *values;
244 ngx_str_t name;
245 ngx_uint_t test_dir; /* unsigned test_dir:1; */
246 } ngx_http_try_file_t;
236247
237248
238249 typedef struct ngx_http_core_loc_conf_s ngx_http_core_loc_conf_t;
324335 #endif
325336
326337 ngx_array_t *error_pages; /* error_page */
338 ngx_http_try_file_t *try_files; /* try_files */
327339
328340 ngx_path_t *client_body_temp_path; /* client_body_temp_path */
329341
354366 ngx_int_t ngx_http_core_access_phase(ngx_http_request_t *r,
355367 ngx_http_phase_handler_t *ph);
356368 ngx_int_t ngx_http_core_post_access_phase(ngx_http_request_t *r,
369 ngx_http_phase_handler_t *ph);
370 ngx_int_t ngx_http_core_try_files_phase(ngx_http_request_t *r,
357371 ngx_http_phase_handler_t *ph);
358372 ngx_int_t ngx_http_core_content_phase(ngx_http_request_t *r,
359373 ngx_http_phase_handler_t *ph);
14661466
14671467 return NGX_DECLINED;
14681468 }
1469
1470
1471 void
1472 ngx_http_split_args(ngx_http_request_t *r, ngx_str_t *uri, ngx_str_t *args)
1473 {
1474 u_char ch, *p, *last;
1475
1476 p = uri->data;
1477
1478 last = p + uri->len;
1479
1480 args->len = 0;
1481
1482 while (p < last) {
1483
1484 ch = *p++;
1485
1486 if (ch == '?') {
1487 args->len = last - p;
1488 args->data = p;
1489
1490 uri->len = p - 1 - uri->data;
1491
1492 if (ngx_strlchr(p, last, '\0') != NULL) {
1493 r->zero_in_uri = 1;
1494 }
1495
1496 return;
1497 }
1498
1499 if (ch == '\0') {
1500 r->zero_in_uri = 1;
1501 continue;
1502 }
1503 }
1504 }