/*
- If the first index file is local (i.e. 'index.html', not '/index.html') then
- try to open it before the test of the directory existence because
- the valid requests should be many more then invalid ones. If open()
- is failed then stat() should be more quickly because some data
+ Try to open first index file before the test of the directory existence
+ because the valid requests should be many more then invalid ones.
+ If open() is failed then stat() should be more quickly because some data
is already cached in the kernel. Besides Win32 has ERROR_PATH_NOT_FOUND
and Unix has ENOTDIR error (although it less helpfull).
*/
file = ngx_cpystrn(loc.data, r->uri.data, r->uri.len + 1);
r->path.len = file - r->path.data;
- if (cf->test_dir) {
- rc = ngx_http_index_test_dir(r);
- if (rc != NGX_OK) {
- return rc;
- }
-
- test_dir = 0;
-
- } else {
- test_dir = 1;
- }
+ test_dir = 1;
index = (ngx_str_t *) cf->indices->elts;
for (i = 0; i < cf->indices->nelts; i++) {
if (fd == NGX_INVALID_FILE) {
err = ngx_errno;
- ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
- ngx_open_file_n " %s failed", name);
+ngx_log_error(NGX_LOG_DEBUG, r->connection->log, err,
+ "DEBUG: " ngx_open_file_n " %s failed", name);
#if (WIN32)
if (err == ERROR_PATH_NOT_FOUND) {
if (err == NGX_ENOTDIR) {
#endif
r->path_not_found = 1;
+
+ } else if (err == NGX_EACCES) {
+ r->path_err = err;
+ return NGX_HTTP_FORBIDDEN;
}
if (test_dir) {
if (r->path_not_found) {
+ r->path_err = err;
return NGX_HTTP_NOT_FOUND;
}
}
test_dir = 0;
-
- if (r->path_not_found) {
- continue;
- }
}
if (err == NGX_ENOENT) {
static int ngx_http_index_test_dir(ngx_http_request_t *r)
{
- ngx_err_t err;
-
r->path.data[r->path.len - 1] = '\0';
+ r->path.data[r->path.len] = '\0';
ngx_log_debug(r->connection->log, "IS_DIR: %s" _ r->path.data);
- if (ngx_file_type(r->path.data, &r->file.info) == -1) {
- err = ngx_errno;
- if (err == NGX_ENOENT) {
- return NGX_HTTP_NOT_FOUND;
+#if 0
+ if (r->path_err == NGX_EACCES) {
+ return NGX_HTTP_FORBIDDEN;
}
+#endif
- if (err == NGX_EACCESS) {
- return NGX_HTTP_FORBIDDEN;
+ if (ngx_file_type(r->path.data, &r->file.info) == -1) {
+
+ r->path_err = ngx_errno;
+
+ if (r->path_err == NGX_ENOENT) {
+ r->path.data[r->path.len - 1] = '/';
+ return NGX_HTTP_NOT_FOUND;
}
- ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
- "ngx_http_index_is_dir: "
- "stat() %s failed", r->path.data);
+ ngx_log_error(NGX_LOG_CRIT, r->connection->log, r->path_err,
+ "ngx_http_index_test_dir: "
+ ngx_file_type_n " %s failed", r->path.data);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
+ r->path.data[r->path.len - 1] = '/';
+
if (ngx_is_dir(r->file.info)) {
- r->path.data[r->path.len - 1] = '/';
return NGX_OK;
} else {
conf->max_index_len = sizeof(NGX_HTTP_INDEX);
}
- /* TODO: set conf->test_dir if first index is started with '/' */
+ /* FAIL: if first index is started with '/' */
return NULL;
}
r->connection->unexpected_eof = 0;
r->lingering_close = 1;
- r->keepalive = 0;
+ r->keepalive = 1;
ctx = (ngx_http_conf_ctx_t *) r->connection->ctx;
r->srv_conf = ctx->srv_conf;
int ngx_http_core_translate_handler(ngx_http_request_t *r)
{
- int i, rc;
- char *location, *last;
+ int i, rc, len, f_offset, l_offset;
+ char *buf, *location, *last;
ngx_err_t err;
ngx_table_elt_t *h;
+ ngx_http_server_name_t *s_name;
ngx_http_core_srv_conf_t *scf;
ngx_http_core_loc_conf_t **lcf, *loc_conf;
loc_conf = (ngx_http_core_loc_conf_t *)
ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx);
- r->file.name.len = loc_conf->doc_root.len + r->uri.len;
+ s_name = (ngx_http_server_name_t *) scf->server_names.elts;
- ngx_test_null(r->file.name.data,
- ngx_palloc(r->pool, r->file.name.len + 1),
+ /* "+ 7" is "http://" */
+ if (loc_conf->doc_root.len > s_name[0].name.len + 7) {
+ len = loc_conf->doc_root.len;
+ f_offset = 0;
+ l_offset = len - (s_name[0].name.len + 7);
+
+ } else {
+ len = s_name[0].name.len + 7;
+ f_offset = len - loc_conf->doc_root.len;
+ l_offset = 0;
+ }
+
+ /* "+ 2" is for trailing '/' in redirect and '\0' */
+ len += r->uri.len + 2;
+
+ ngx_test_null(buf, ngx_palloc(r->pool, len),
NGX_HTTP_INTERNAL_SERVER_ERROR);
- location = ngx_cpystrn(r->file.name.data, loc_conf->doc_root.data,
- loc_conf->doc_root.len + 1);
- last = ngx_cpystrn(location, r->uri.data, r->uri.len + 1);
+ r->file.name.data = buf + f_offset;
+ location = buf + l_offset;
+
+ last = ngx_cpystrn(ngx_cpystrn(r->file.name.data, loc_conf->doc_root.data,
+ loc_conf->doc_root.len + 1),
+ r->uri.data, r->uri.len + 1);
- ngx_log_debug(r->connection->log, "HTTP filename: '%s'" _
- r->file.name.data);
+ r->file.name.len = last - r->file.name.data;
+
+ngx_log_debug(r->connection->log, "HTTP filename: '%s'" _ r->file.name.data);
#if (WIN9X)
} else if (err == ERROR_PATH_NOT_FOUND) {
return NGX_HTTP_NOT_FOUND;
- } else if (err == NGX_EACCESS) {
+ } else if (err == NGX_EACCES) {
return NGX_HTTP_FORBIDDEN;
} else {
} else if (err == NGX_ENOTDIR) {
return NGX_HTTP_NOT_FOUND;
#endif
- } else if (err == NGX_EACCESS) {
+ } else if (err == NGX_EACCES) {
return NGX_HTTP_FORBIDDEN;
} else {
if (!r->file.info_valid) {
if (ngx_stat_fd(r->file.fd, &r->file.info) == NGX_FILE_ERROR) {
- ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
+ ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
"ngx_http_core_handler: "
ngx_stat_fd_n " %s failed", r->file.name.data);
if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) {
- ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
+ ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
"ngx_http_core_handler: "
ngx_close_file_n " %s failed", r->file.name.data);
}
#endif
if (ngx_is_dir(r->file.info)) {
- ngx_log_debug(r->connection->log, "HTTP DIR: '%s'" _ r->file.name.data);
+ngx_log_debug(r->connection->log, "HTTP DIR: '%s'" _ r->file.name.data);
#if !(WIN9X)
if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) {
- ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
+ ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
"ngx_http_core_handler: "
ngx_close_file_n " %s failed", r->file.name.data);
}
ngx_test_null(h, ngx_push_table(r->headers_out.headers),
NGX_HTTP_INTERNAL_SERVER_ERROR);
+ ngx_memcpy(location, "http://", 7);
+ ngx_memcpy(location + 7, s_name[0].name.data, s_name[0].name.len);
+
*last++ = '/';
*last = '\0';
h->key.len = 8;
rc = h[i](r);
if (rc != NGX_DECLINED) {
+
+ if (rc == NGX_HTTP_NOT_FOUND) {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, r->path_err,
+ "%s is not found", r->path.data);
+ }
+
+ if (rc == NGX_HTTP_FORBIDDEN) {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, r->path_err,
+ "%s is forbidden", r->path.data);
+ }
+
return rc;
}
}
+ r->path.data[r->path.len] = '\0';
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+ "directory index of %s is forbidden", r->path.data);
+
return NGX_HTTP_FORBIDDEN;
}
if (r->file.fd != NGX_INVALID_FILE) {
if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR) {
- ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
+ ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
ngx_close_file_n " failed");
}
}
ngx_init_array(scf->locations, pool, 5, sizeof(void *), NGX_CONF_ERROR);
ngx_init_array(scf->listen, pool, 5, sizeof(ngx_http_listen_t),
NGX_CONF_ERROR);
+ ngx_init_array(scf->server_names, pool, 5, sizeof(ngx_http_server_name_t),
+ NGX_CONF_ERROR);
ngx_test_null(cf, ngx_push_array(&ngx_http_servers), NGX_CONF_ERROR);
*cf = scf;
ngx_http_core_srv_conf_t *scf = (ngx_http_core_srv_conf_t *) conf;
ngx_http_listen_t *l;
+ ngx_http_server_name_t *n;
if (scf->listen.nelts == 0) {
ngx_test_null(l, ngx_push_array(&scf->listen), NGX_CONF_ERROR);
l->family = AF_INET;
}
+ if (scf->server_names.nelts == 0) {
+ ngx_test_null(n, ngx_push_array(&scf->server_names), NGX_CONF_ERROR);
+ ngx_test_null(n->name.data, ngx_palloc(pool, NGX_MAXHOSTNAMELEN),
+ NGX_CONF_ERROR);
+ if (gethostname(n->name.data, NGX_MAXHOSTNAMELEN) == -1) {
+/* STUB: no log here */
+#if 0
+ ngx_log_error(NGX_LOG_EMERG, scf->log, ngx_errno,
+ "gethostname() failed");
+#endif
+ return NGX_CONF_ERROR;
+ }
+ n->name.len = ngx_strlen(n->name.data);
+ n->core_srv_conf = conf;
+ }
+
return NGX_CONF_OK;
}