comparison src/http/ngx_http_core_module.c @ 47:d81326c3b21b

nginx-0.0.1-2003-01-15-10:02:27 import
author Igor Sysoev <igor@sysoev.ru>
date Wed, 15 Jan 2003 07:02:27 +0000
parents f84a648211f4
children 27b628ef907e
comparison
equal deleted inserted replaced
46:f84a648211f4 47:d81326c3b21b
92 ngx_http_module_t *module; 92 ngx_http_module_t *module;
93 ngx_http_conf_ctx_t *ctx; 93 ngx_http_conf_ctx_t *ctx;
94 94
95 r->connection->unexpected_eof = 0; 95 r->connection->unexpected_eof = 0;
96 r->lingering_close = 1; 96 r->lingering_close = 1;
97 r->keepalive = 0; 97 r->keepalive = 1;
98 98
99 ctx = (ngx_http_conf_ctx_t *) r->connection->ctx; 99 ctx = (ngx_http_conf_ctx_t *) r->connection->ctx;
100 r->srv_conf = ctx->srv_conf; 100 r->srv_conf = ctx->srv_conf;
101 r->loc_conf = ctx->loc_conf; 101 r->loc_conf = ctx->loc_conf;
102 102
141 } 141 }
142 142
143 143
144 int ngx_http_core_translate_handler(ngx_http_request_t *r) 144 int ngx_http_core_translate_handler(ngx_http_request_t *r)
145 { 145 {
146 int i, rc; 146 int i, rc, len, f_offset, l_offset;
147 char *location, *last; 147 char *buf, *location, *last;
148 ngx_err_t err; 148 ngx_err_t err;
149 ngx_table_elt_t *h; 149 ngx_table_elt_t *h;
150 ngx_http_server_name_t *s_name;
150 ngx_http_core_srv_conf_t *scf; 151 ngx_http_core_srv_conf_t *scf;
151 ngx_http_core_loc_conf_t **lcf, *loc_conf; 152 ngx_http_core_loc_conf_t **lcf, *loc_conf;
152 153
153 scf = (ngx_http_core_srv_conf_t *) 154 scf = (ngx_http_core_srv_conf_t *)
154 ngx_http_get_module_srv_conf(r, ngx_http_core_module_ctx); 155 ngx_http_get_module_srv_conf(r, ngx_http_core_module_ctx);
178 } 179 }
179 180
180 loc_conf = (ngx_http_core_loc_conf_t *) 181 loc_conf = (ngx_http_core_loc_conf_t *)
181 ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx); 182 ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx);
182 183
183 r->file.name.len = loc_conf->doc_root.len + r->uri.len; 184 s_name = (ngx_http_server_name_t *) scf->server_names.elts;
184 185
185 ngx_test_null(r->file.name.data, 186 /* "+ 7" is "http://" */
186 ngx_palloc(r->pool, r->file.name.len + 1), 187 if (loc_conf->doc_root.len > s_name[0].name.len + 7) {
188 len = loc_conf->doc_root.len;
189 f_offset = 0;
190 l_offset = len - (s_name[0].name.len + 7);
191
192 } else {
193 len = s_name[0].name.len + 7;
194 f_offset = len - loc_conf->doc_root.len;
195 l_offset = 0;
196 }
197
198 /* "+ 2" is for trailing '/' in redirect and '\0' */
199 len += r->uri.len + 2;
200
201 ngx_test_null(buf, ngx_palloc(r->pool, len),
187 NGX_HTTP_INTERNAL_SERVER_ERROR); 202 NGX_HTTP_INTERNAL_SERVER_ERROR);
188 203
189 location = ngx_cpystrn(r->file.name.data, loc_conf->doc_root.data, 204 r->file.name.data = buf + f_offset;
190 loc_conf->doc_root.len + 1); 205 location = buf + l_offset;
191 last = ngx_cpystrn(location, r->uri.data, r->uri.len + 1); 206
192 207 last = ngx_cpystrn(ngx_cpystrn(r->file.name.data, loc_conf->doc_root.data,
193 ngx_log_debug(r->connection->log, "HTTP filename: '%s'" _ 208 loc_conf->doc_root.len + 1),
194 r->file.name.data); 209 r->uri.data, r->uri.len + 1);
210
211 r->file.name.len = last - r->file.name.data;
212
213 ngx_log_debug(r->connection->log, "HTTP filename: '%s'" _ r->file.name.data);
195 214
196 #if (WIN9X) 215 #if (WIN9X)
197 216
198 /* There is no way to open file or directory in Win9X with 217 /* There is no way to open file or directory in Win9X with
199 one syscall: Win9X has not FILE_FLAG_BACKUP_SEMANTICS flag. 218 one syscall: Win9X has not FILE_FLAG_BACKUP_SEMANTICS flag.
210 return NGX_HTTP_NOT_FOUND; 229 return NGX_HTTP_NOT_FOUND;
211 230
212 } else if (err == ERROR_PATH_NOT_FOUND) { 231 } else if (err == ERROR_PATH_NOT_FOUND) {
213 return NGX_HTTP_NOT_FOUND; 232 return NGX_HTTP_NOT_FOUND;
214 233
215 } else if (err == NGX_EACCESS) { 234 } else if (err == NGX_EACCES) {
216 return NGX_HTTP_FORBIDDEN; 235 return NGX_HTTP_FORBIDDEN;
217 236
218 } else { 237 } else {
219 return NGX_HTTP_INTERNAL_SERVER_ERROR; 238 return NGX_HTTP_INTERNAL_SERVER_ERROR;
220 } 239 }
239 return NGX_HTTP_NOT_FOUND; 258 return NGX_HTTP_NOT_FOUND;
240 #else 259 #else
241 } else if (err == NGX_ENOTDIR) { 260 } else if (err == NGX_ENOTDIR) {
242 return NGX_HTTP_NOT_FOUND; 261 return NGX_HTTP_NOT_FOUND;
243 #endif 262 #endif
244 } else if (err == NGX_EACCESS) { 263 } else if (err == NGX_EACCES) {
245 return NGX_HTTP_FORBIDDEN; 264 return NGX_HTTP_FORBIDDEN;
246 265
247 } else { 266 } else {
248 return NGX_HTTP_INTERNAL_SERVER_ERROR; 267 return NGX_HTTP_INTERNAL_SERVER_ERROR;
249 } 268 }
250 } 269 }
251 270
252 if (!r->file.info_valid) { 271 if (!r->file.info_valid) {
253 if (ngx_stat_fd(r->file.fd, &r->file.info) == NGX_FILE_ERROR) { 272 if (ngx_stat_fd(r->file.fd, &r->file.info) == NGX_FILE_ERROR) {
254 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno, 273 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
255 "ngx_http_core_handler: " 274 "ngx_http_core_handler: "
256 ngx_stat_fd_n " %s failed", r->file.name.data); 275 ngx_stat_fd_n " %s failed", r->file.name.data);
257 276
258 if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) { 277 if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) {
259 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno, 278 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
260 "ngx_http_core_handler: " 279 "ngx_http_core_handler: "
261 ngx_close_file_n " %s failed", r->file.name.data); 280 ngx_close_file_n " %s failed", r->file.name.data);
262 } 281 }
263 282
264 return NGX_HTTP_INTERNAL_SERVER_ERROR; 283 return NGX_HTTP_INTERNAL_SERVER_ERROR;
267 r->file.info_valid = 1; 286 r->file.info_valid = 1;
268 } 287 }
269 #endif 288 #endif
270 289
271 if (ngx_is_dir(r->file.info)) { 290 if (ngx_is_dir(r->file.info)) {
272 ngx_log_debug(r->connection->log, "HTTP DIR: '%s'" _ r->file.name.data); 291 ngx_log_debug(r->connection->log, "HTTP DIR: '%s'" _ r->file.name.data);
273 292
274 #if !(WIN9X) 293 #if !(WIN9X)
275 if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) { 294 if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) {
276 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno, 295 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
277 "ngx_http_core_handler: " 296 "ngx_http_core_handler: "
278 ngx_close_file_n " %s failed", r->file.name.data); 297 ngx_close_file_n " %s failed", r->file.name.data);
279 } 298 }
280 #endif 299 #endif
281 300
282 /* BROKEN: need to include server name */ 301 /* BROKEN: need to include server name */
283 302
284 ngx_test_null(h, ngx_push_table(r->headers_out.headers), 303 ngx_test_null(h, ngx_push_table(r->headers_out.headers),
285 NGX_HTTP_INTERNAL_SERVER_ERROR); 304 NGX_HTTP_INTERNAL_SERVER_ERROR);
305
306 ngx_memcpy(location, "http://", 7);
307 ngx_memcpy(location + 7, s_name[0].name.data, s_name[0].name.len);
286 308
287 *last++ = '/'; 309 *last++ = '/';
288 *last = '\0'; 310 *last = '\0';
289 h->key.len = 8; 311 h->key.len = 8;
290 h->key.data = "Location" ; 312 h->key.data = "Location" ;
310 h = (ngx_http_handler_pt *) ngx_http_index_handlers.elts; 332 h = (ngx_http_handler_pt *) ngx_http_index_handlers.elts;
311 for (i = 0; i < ngx_http_index_handlers.nelts; i++) { 333 for (i = 0; i < ngx_http_index_handlers.nelts; i++) {
312 rc = h[i](r); 334 rc = h[i](r);
313 335
314 if (rc != NGX_DECLINED) { 336 if (rc != NGX_DECLINED) {
337
338 if (rc == NGX_HTTP_NOT_FOUND) {
339 ngx_log_error(NGX_LOG_ERR, r->connection->log, r->path_err,
340 "%s is not found", r->path.data);
341 }
342
343 if (rc == NGX_HTTP_FORBIDDEN) {
344 ngx_log_error(NGX_LOG_ERR, r->connection->log, r->path_err,
345 "%s is forbidden", r->path.data);
346 }
347
315 return rc; 348 return rc;
316 } 349 }
317 } 350 }
351
352 r->path.data[r->path.len] = '\0';
353 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
354 "directory index of %s is forbidden", r->path.data);
318 355
319 return NGX_HTTP_FORBIDDEN; 356 return NGX_HTTP_FORBIDDEN;
320 } 357 }
321 358
322 359
357 ngx_assert((r->file.fd != NGX_INVALID_FILE), /* void */ ; , 394 ngx_assert((r->file.fd != NGX_INVALID_FILE), /* void */ ; ,
358 r->connection->log, "file already closed"); 395 r->connection->log, "file already closed");
359 396
360 if (r->file.fd != NGX_INVALID_FILE) { 397 if (r->file.fd != NGX_INVALID_FILE) {
361 if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) { 398 if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) {
362 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno, 399 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
363 ngx_close_file_n " failed"); 400 ngx_close_file_n " failed");
364 } 401 }
365 } 402 }
366 403
367 /* 404 /*
686 NGX_CONF_ERROR); 723 NGX_CONF_ERROR);
687 724
688 ngx_init_array(scf->locations, pool, 5, sizeof(void *), NGX_CONF_ERROR); 725 ngx_init_array(scf->locations, pool, 5, sizeof(void *), NGX_CONF_ERROR);
689 ngx_init_array(scf->listen, pool, 5, sizeof(ngx_http_listen_t), 726 ngx_init_array(scf->listen, pool, 5, sizeof(ngx_http_listen_t),
690 NGX_CONF_ERROR); 727 NGX_CONF_ERROR);
728 ngx_init_array(scf->server_names, pool, 5, sizeof(ngx_http_server_name_t),
729 NGX_CONF_ERROR);
691 730
692 ngx_test_null(cf, ngx_push_array(&ngx_http_servers), NGX_CONF_ERROR); 731 ngx_test_null(cf, ngx_push_array(&ngx_http_servers), NGX_CONF_ERROR);
693 *cf = scf; 732 *cf = scf;
694 733
695 return scf; 734 return scf;
699 static char *ngx_http_core_init_srv_conf(ngx_pool_t *pool, void *conf) 738 static char *ngx_http_core_init_srv_conf(ngx_pool_t *pool, void *conf)
700 { 739 {
701 ngx_http_core_srv_conf_t *scf = (ngx_http_core_srv_conf_t *) conf; 740 ngx_http_core_srv_conf_t *scf = (ngx_http_core_srv_conf_t *) conf;
702 741
703 ngx_http_listen_t *l; 742 ngx_http_listen_t *l;
743 ngx_http_server_name_t *n;
704 744
705 if (scf->listen.nelts == 0) { 745 if (scf->listen.nelts == 0) {
706 ngx_test_null(l, ngx_push_array(&scf->listen), NGX_CONF_ERROR); 746 ngx_test_null(l, ngx_push_array(&scf->listen), NGX_CONF_ERROR);
707 l->addr = INADDR_ANY; 747 l->addr = INADDR_ANY;
708 l->port = 8000; 748 l->port = 8000;
709 l->family = AF_INET; 749 l->family = AF_INET;
710 } 750 }
711 751
752 if (scf->server_names.nelts == 0) {
753 ngx_test_null(n, ngx_push_array(&scf->server_names), NGX_CONF_ERROR);
754 ngx_test_null(n->name.data, ngx_palloc(pool, NGX_MAXHOSTNAMELEN),
755 NGX_CONF_ERROR);
756 if (gethostname(n->name.data, NGX_MAXHOSTNAMELEN) == -1) {
757 /* STUB: no log here */
758 #if 0
759 ngx_log_error(NGX_LOG_EMERG, scf->log, ngx_errno,
760 "gethostname() failed");
761 #endif
762 return NGX_CONF_ERROR;
763 }
764 n->name.len = ngx_strlen(n->name.data);
765 n->core_srv_conf = conf;
766 }
767
712 return NGX_CONF_OK; 768 return NGX_CONF_OK;
713 } 769 }
714 770
715 771
716 static void *ngx_http_core_create_loc_conf(ngx_pool_t *pool) 772 static void *ngx_http_core_create_loc_conf(ngx_pool_t *pool)