nginx-0.0.1-2003-01-10-09:09:20 import

This commit is contained in:
Igor Sysoev 2003-01-10 06:09:20 +00:00
parent 4e9393a054
commit b2620634c5
15 changed files with 165 additions and 37 deletions

View File

@ -3,6 +3,7 @@
#include <ngx_config.h> #include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_string.h> #include <ngx_string.h>
#include <ngx_errno.h> #include <ngx_errno.h>
#include <ngx_time.h> #include <ngx_time.h>
@ -41,6 +42,7 @@ ngx_array_t ngx_listening_sockets;
int main(int argc, char *const *argv) int main(int argc, char *const *argv)
{ {
int i;
ngx_str_t conf_file; ngx_str_t conf_file;
ngx_conf_t conf; ngx_conf_t conf;
@ -70,9 +72,18 @@ int main(int argc, char *const *argv)
conf_file.data = "nginx.conf"; conf_file.data = "nginx.conf";
if (ngx_conf_parse(&conf, &conf_file) != NGX_CONF_OK) { if (ngx_conf_parse(&conf, &conf_file) != NGX_CONF_OK) {
exit(1); return 1;
} }
for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->init_module) {
if (ngx_modules[i]->init_module(ngx_pool) == NGX_ERROR) {
return 1;
}
}
}
#if 0 #if 0
/* STUB */ /* STUB */
/* TODO: init chain of global modules (like ngx_http.c), /* TODO: init chain of global modules (like ngx_http.c),

View File

@ -83,7 +83,7 @@ ngx_log_debug(cf->log, "token %d" _ rc);
found = 0; found = 0;
for (i = 0; !found && ngx_modules[i]; i++) { for (i = 0; !found && ngx_modules[i]; i++) {
if (ngx_modules[i]->type != NULL if (ngx_modules[i]->type != NGX_CONF_MODULE_TYPE
&& ngx_modules[i]->type != cf->type) && ngx_modules[i]->type != cf->type)
{ {
continue; continue;

View File

@ -31,6 +31,7 @@
#define NGX_CORE_MODULE_TYPE 0x45524f43 /* "CORE" */ #define NGX_CORE_MODULE_TYPE 0x45524f43 /* "CORE" */
#define NGX_CONF_MODULE_TYPE 0x464E4f43 /* "CONF" */
typedef struct ngx_conf_s ngx_conf_t; typedef struct ngx_conf_s ngx_conf_t;

View File

@ -29,6 +29,7 @@ typedef struct {
#define ngx_memzero bzero #define ngx_memzero bzero
#define ngx_strcasecmp strcasecmp
#define ngx_strncmp strncmp #define ngx_strncmp strncmp
#define ngx_strcmp strcmp #define ngx_strcmp strcmp

View File

@ -13,6 +13,7 @@
#include <ngx_http_index_handler.h> #include <ngx_http_index_handler.h>
static int ngx_http_index_init(ngx_pool_t *pool);
static void *ngx_http_index_create_conf(ngx_pool_t *pool); static void *ngx_http_index_create_conf(ngx_pool_t *pool);
static char *ngx_http_index_merge_conf(ngx_pool_t *p, static char *ngx_http_index_merge_conf(ngx_pool_t *p,
void *parent, void *child); void *parent, void *child);
@ -55,13 +56,13 @@ ngx_module_t ngx_http_index_module = {
&ngx_http_index_module_ctx, /* module context */ &ngx_http_index_module_ctx, /* module context */
ngx_http_index_commands, /* module directives */ ngx_http_index_commands, /* module directives */
NGX_HTTP_MODULE_TYPE, /* module type */ NGX_HTTP_MODULE_TYPE, /* module type */
NULL /* init module */ ngx_http_index_init /* init module */
}; };
int ngx_http_index_handler(ngx_http_request_t *r) int ngx_http_index_handler(ngx_http_request_t *r)
{ {
int i; int i, len;
char *name, *file; char *name, *file;
ngx_str_t loc, *index; ngx_str_t loc, *index;
ngx_err_t err; ngx_err_t err;
@ -76,19 +77,31 @@ int ngx_http_index_handler(ngx_http_request_t *r)
core_cf = (ngx_http_core_loc_conf_t *) core_cf = (ngx_http_core_loc_conf_t *)
ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx); ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx);
ngx_test_null(name, ngx_test_null(r->path.data,
ngx_palloc(r->pool, ngx_palloc(r->pool,
core_cf->doc_root.len + r->uri.len core_cf->doc_root.len + r->uri.len
+ cf->max_index_len), + cf->max_index_len),
NGX_HTTP_INTERNAL_SERVER_ERROR); NGX_HTTP_INTERNAL_SERVER_ERROR);
loc.data = ngx_cpystrn(name, core_cf->doc_root.data, loc.data = ngx_cpystrn(r->path.data, core_cf->doc_root.data,
core_cf->doc_root.len + 1); core_cf->doc_root.len + 1);
file = ngx_cpystrn(loc.data, r->uri.data, r->uri.len + 1); file = ngx_cpystrn(loc.data, r->uri.data, r->uri.len + 1);
r->path.len = file - r->path.data;
index = (ngx_str_t *) cf->indices->elts; index = (ngx_str_t *) cf->indices->elts;
for (i = 0; i < cf->indices->nelts; i++) { for (i = 0; i < cf->indices->nelts; i++) {
ngx_memcpy(file, index[i].data, index[i].len + 1);
if (index[i].data[0] != '/') {
if (!r->path_not_found) {
continue;
}
ngx_memcpy(file, index[i].data, index[i].len + 1);
name = r->path.data;
} else {
name = index[i].data;
}
fd = ngx_open_file(name, NGX_FILE_RDONLY); fd = ngx_open_file(name, NGX_FILE_RDONLY);
if (fd == NGX_INVALID_FILE) { if (fd == NGX_INVALID_FILE) {
@ -98,6 +111,12 @@ int ngx_http_index_handler(ngx_http_request_t *r)
} }
#if (WIN32) #if (WIN32)
if (err == ERROR_PATH_NOT_FOUND) { if (err == ERROR_PATH_NOT_FOUND) {
r->path_not_found = 1;
continue;
}
#else
if (err == NGX_ENOTDIR) {
r->path_not_found = 1;
continue; continue;
} }
#endif #endif
@ -108,11 +127,20 @@ int ngx_http_index_handler(ngx_http_request_t *r)
return NGX_HTTP_INTERNAL_SERVER_ERROR; return NGX_HTTP_INTERNAL_SERVER_ERROR;
} }
r->file.name.len = core_cf->doc_root.len + r->uri.len + index[i].len;
r->file.name.data = name; r->file.name.data = name;
r->file.fd = fd; r->file.fd = fd;
loc.len = r->uri.len + index[i].len; if (index[i].data[0] == '/') {
r->file.name.len = index[i].len;
loc.len = index[i].len;
loc.data = index[i].data;
} else {
loc.len = r->uri.len + index[i].len;
r->file.name.len = core_cf->doc_root.len + r->uri.len
+ index[i].len;
}
return ngx_http_internal_redirect(r, loc); return ngx_http_internal_redirect(r, loc);
} }
@ -120,6 +148,18 @@ int ngx_http_index_handler(ngx_http_request_t *r)
} }
static int ngx_http_index_init(ngx_pool_t *pool)
{
ngx_http_handler_pt *h;
ngx_test_null(h, ngx_push_array(&ngx_http_index_handlers), NGX_ERROR);
*h = ngx_http_index_handler;
return NGX_OK;
}
static void *ngx_http_index_create_conf(ngx_pool_t *pool) static void *ngx_http_index_create_conf(ngx_pool_t *pool)
{ {
ngx_http_index_conf_t *conf; ngx_http_index_conf_t *conf;

View File

@ -107,17 +107,17 @@ int ngx_http_static_handler(ngx_http_request_t *r)
/* STUB */ /* STUB */
if (r->exten.len) { if (r->exten.len) {
if (strcasecmp(r->exten.data, "html") == 0) { if (ngx_strcasecmp(r->exten.data, "html") == 0) {
r->headers_out.content_type->value.len = 25; r->headers_out.content_type->value.len = 25;
r->headers_out.content_type->value.data = r->headers_out.content_type->value.data =
"text/html; charset=koi8-r"; "text/html; charset=koi8-r";
} else if (strcasecmp(r->exten.data, "gif") == 0) { } else if (ngx_strcasecmp(r->exten.data, "gif") == 0) {
r->headers_out.content_type->value.len = 9; r->headers_out.content_type->value.len = 9;
r->headers_out.content_type->value.data = "image/gif"; r->headers_out.content_type->value.data = "image/gif";
} else if (strcasecmp(r->exten.data, "jpg") == 0) { } else if (ngx_strcasecmp(r->exten.data, "jpg") == 0) {
r->headers_out.content_type->value.len = 10; r->headers_out.content_type->value.len = 10;
r->headers_out.content_type->value.data = "image/jpeg"; r->headers_out.content_type->value.data = "image/jpeg";
} else if (strcasecmp(r->exten.data, "pdf") == 0) { } else if (ngx_strcasecmp(r->exten.data, "pdf") == 0) {
r->headers_out.content_type->value.len = 15; r->headers_out.content_type->value.len = 15;
r->headers_out.content_type->value.data = "application/pdf"; r->headers_out.content_type->value.data = "application/pdf";
} }

View File

@ -26,6 +26,10 @@ int ngx_http_lingering_timeout = 5000;
int ngx_http_lingering_time = 30; int ngx_http_lingering_time = 30;
/**/ /**/
ngx_array_t ngx_http_index_handlers;
int (*ngx_http_top_header_filter) (ngx_http_request_t *r); int (*ngx_http_top_header_filter) (ngx_http_request_t *r);
@ -117,6 +121,9 @@ static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy)
if (rv != NGX_CONF_OK) if (rv != NGX_CONF_OK)
return rv; return rv;
ngx_init_array(ngx_http_index_handlers,
cf->pool, 3, sizeof(ngx_http_handler_pt), NGX_CONF_ERROR);
ngx_http_init_filters(cf->pool, ngx_modules); ngx_http_init_filters(cf->pool, ngx_modules);
#if 1 #if 1

View File

@ -37,6 +37,7 @@
#define NGX_HTTP_MOVED_TEMPORARILY 302 #define NGX_HTTP_MOVED_TEMPORARILY 302
#define NGX_HTTP_NOT_MODIFIED 304 #define NGX_HTTP_NOT_MODIFIED 304
#define NGX_HTTP_BAD_REQUEST 400 #define NGX_HTTP_BAD_REQUEST 400
#define NGX_HTTP_FORBIDDEN 403
#define NGX_HTTP_NOT_FOUND 404 #define NGX_HTTP_NOT_FOUND 404
#define NGX_HTTP_REQUEST_URI_TOO_LARGE 414 #define NGX_HTTP_REQUEST_URI_TOO_LARGE 414
#define NGX_HTTP_INTERNAL_SERVER_ERROR 500 #define NGX_HTTP_INTERNAL_SERVER_ERROR 500
@ -106,13 +107,6 @@ typedef struct ngx_http_request_s ngx_http_request_t;
struct ngx_http_request_s { struct ngx_http_request_s {
ngx_file_t file; ngx_file_t file;
#if 0
ngx_str_t filename;
ngx_file_info_t fileinfo;
ngx_fd_t fd;
int filename_len;
#endif
void **ctx; void **ctx;
void **srv_conf; void **srv_conf;
void **loc_conf; void **loc_conf;
@ -145,6 +139,8 @@ struct ngx_http_request_s {
ssize_t client_content_length; ssize_t client_content_length;
char *discarded_buffer; char *discarded_buffer;
ngx_str_t path;
unsigned keepalive:1; unsigned keepalive:1;
unsigned lingering_close:1; unsigned lingering_close:1;
@ -156,6 +152,7 @@ struct ngx_http_request_s {
unsigned header_only:1; unsigned header_only:1;
unsigned unusual_uri:1; /* URI is not started with '/' - "GET http://" */ unsigned unusual_uri:1; /* URI is not started with '/' - "GET http://" */
unsigned complex_uri:1; /* URI with "/." or with "//" (WIN32) */ unsigned complex_uri:1; /* URI with "/." or with "//" (WIN32) */
unsigned path_not_found:1;
int state; int state;
char *uri_start; char *uri_start;
@ -180,6 +177,8 @@ typedef struct {
} ngx_http_log_ctx_t; } ngx_http_log_ctx_t;
typedef int (*ngx_http_handler_pt)(ngx_http_request_t *r);
typedef int (*ngx_http_output_header_filter_p)(ngx_http_request_t *r); typedef int (*ngx_http_output_header_filter_p)(ngx_http_request_t *r);
typedef int (*ngx_http_output_body_filter_p) typedef int (*ngx_http_output_body_filter_p)
@ -251,6 +250,9 @@ extern int ngx_http_lingering_timeout;
extern int ngx_http_lingering_time; extern int ngx_http_lingering_time;
extern ngx_array_t ngx_http_index_handlers;
extern ngx_http_module_t *ngx_http_modules[]; extern ngx_http_module_t *ngx_http_modules[];

View File

@ -11,16 +11,16 @@
#if 0 #if 0
#include <ngx_http_write_filter.h> #include <ngx_http_write_filter.h>
#include <ngx_http_output_filter.h> #include <ngx_http_output_filter.h>
#include <ngx_http_index_handler.h>
#endif #endif
/* STUB */ /* STUB */
#include <ngx_http_output_filter.h> #include <ngx_http_output_filter.h>
int ngx_http_static_handler(ngx_http_request_t *r); int ngx_http_static_handler(ngx_http_request_t *r);
int ngx_http_index_handler(ngx_http_request_t *r);
int ngx_http_proxy_handler(ngx_http_request_t *r); int ngx_http_proxy_handler(ngx_http_request_t *r);
/**/ /**/
static int ngx_http_core_index_handler(ngx_http_request_t *r);
static char *ngx_server_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy); static char *ngx_server_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy);
static char *ngx_location_block(ngx_conf_t *cf, ngx_command_t *cmd, static char *ngx_location_block(ngx_conf_t *cf, ngx_command_t *cmd,
@ -173,9 +173,7 @@ ngx_log_debug(r->connection->log, "trans: %s" _ lcf[i]->name.data);
} }
if (r->uri.data[r->uri.len - 1] == '/') { if (r->uri.data[r->uri.len - 1] == '/') {
/* TODO: find index handler */ r->handler = ngx_http_core_index_handler;
/* STUB */ r->handler = ngx_http_index_handler;
return NGX_OK; return NGX_OK;
} }
@ -234,6 +232,9 @@ ngx_log_debug(r->connection->log, "trans: %s" _ lcf[i]->name.data);
#if (WIN32) #if (WIN32)
} else if (err == ERROR_PATH_NOT_FOUND) { } else if (err == ERROR_PATH_NOT_FOUND) {
return NGX_HTTP_NOT_FOUND; return NGX_HTTP_NOT_FOUND;
#else
} else if (err == NGX_ENOTDIR) {
return NGX_HTTP_NOT_FOUND;
#endif #endif
} else { } else {
return NGX_HTTP_INTERNAL_SERVER_ERROR; return NGX_HTTP_INTERNAL_SERVER_ERROR;
@ -293,6 +294,62 @@ ngx_log_debug(r->connection->log, "trans: %s" _ lcf[i]->name.data);
} }
static int ngx_http_core_index_handler(ngx_http_request_t *r)
{
int i, rc;
ngx_err_t err;
ngx_http_handler_pt *h;
h = (ngx_http_handler_pt *) ngx_http_index_handlers.elts;
for (i = 0; i < ngx_http_index_handlers.nelts; i++) {
rc = h[i](r);
if (rc != NGX_DECLINED) {
return rc;
}
}
#if (WIN32)
if (r->path_not_found) {
return NGX_HTTP_NOT_FOUND;
} else {
return NGX_HTTP_FORBIDDEN;
}
#else
if (r->path_not_found) {
return NGX_HTTP_NOT_FOUND;
}
r->path.data[r->path.len] = '\0';
if (stat(r->path.data, &r->file.info) == -1) {
err = ngx_errno;
if (err == NGX_ENOENT) {
return NGX_HTTP_NOT_FOUND;
}
ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
"ngx_http_core_index_handler: "
"stat() %s failed", r->path.data);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (ngx_is_dir(r->file.info)) {
return NGX_HTTP_FORBIDDEN;
} else {
return NGX_HTTP_NOT_FOUND;
}
#endif
}
int ngx_http_send_header(ngx_http_request_t *r) int ngx_http_send_header(ngx_http_request_t *r)
{ {
return (*ngx_http_top_header_filter)(r); return (*ngx_http_top_header_filter)(r);

View File

@ -400,7 +400,7 @@ static int ngx_http_process_request_header_line(ngx_http_request_t *r)
for (i = 0; headers_in[i].len != 0; i++) { for (i = 0; headers_in[i].len != 0; i++) {
if (headers_in[i].len == h->key.len) { if (headers_in[i].len == h->key.len) {
if (strcasecmp(headers_in[i].data, h->key.data) == 0) { if (ngx_strcasecmp(headers_in[i].data, h->key.data) == 0) {
*((ngx_table_elt_t **) *((ngx_table_elt_t **)
((char *) &r->headers_in + headers_in[i].offset)) = h; ((char *) &r->headers_in + headers_in[i].offset)) = h;
} }

View File

@ -68,11 +68,10 @@ int ngx_http_output_filter(ngx_http_request_t *r, ngx_hunk_t *hunk)
ctx = (ngx_http_output_filter_ctx_t *) ctx = (ngx_http_output_filter_ctx_t *)
ngx_http_get_module_ctx(r->main ? r->main : r, ngx_http_get_module_ctx(r->main ? r->main : r,
ngx_http_output_filter_module_ctx); ngx_http_output_filter_module);
if (ctx == NULL) { if (ctx == NULL) {
ngx_http_create_ctx(r, ctx, ngx_http_create_ctx(r, ctx, ngx_http_output_filter_module,
ngx_http_output_filter_module_ctx,
sizeof(ngx_http_output_filter_ctx_t)); sizeof(ngx_http_output_filter_ctx_t));
} }
@ -183,9 +182,9 @@ int ngx_http_output_filter(ngx_http_request_t *r, ngx_hunk_t *hunk)
if (hunk->type & NGX_HUNK_LAST) { if (hunk->type & NGX_HUNK_LAST) {
conf = (ngx_http_output_filter_conf_t *) conf = (ngx_http_output_filter_conf_t *)
ngx_http_get_module_loc_conf( ngx_http_get_module_loc_conf(
r->main ? r->main : r, r->main ? r->main : r,
ngx_http_output_filter_module_ctx); ngx_http_output_filter_module);
size = hunk->last.mem - hunk->pos.mem; size = hunk->last.mem - hunk->pos.mem;
if (size > conf->hunk_size) { if (size > conf->hunk_size) {

View File

@ -19,6 +19,13 @@ static char error_400_page[] =
"<center><h1>400 Bad Request</h1></center>" CRLF "<center><h1>400 Bad Request</h1></center>" CRLF
; ;
static char error_403_page[] =
"<html>" CRLF
"<head><title>403 Forbidden</title></head>" CRLF
"<body bgcolor=\"white\">" CRLF
"<center><h1>403 Forbidden</h1></center>" CRLF
;
static char error_404_page[] = static char error_404_page[] =
"<html>" CRLF "<html>" CRLF
"<head><title>404 Not Found</title></head>" CRLF "<head><title>404 Not Found</title></head>" CRLF
@ -36,7 +43,7 @@ static ngx_str_t error_pages[] = {
{ sizeof(error_400_page) - 1, error_400_page }, { sizeof(error_400_page) - 1, error_400_page },
{ 0, NULL}, /* 401 */ { 0, NULL}, /* 401 */
{ 0, NULL}, /* 402 */ { 0, NULL}, /* 402 */
{ 0, NULL}, /* 403 */ { sizeof(error_403_page) - 1, error_403_page },
{ sizeof(error_404_page) - 1, error_404_page }, { sizeof(error_404_page) - 1, error_404_page },
{ 0, NULL} /* 500 */ { 0, NULL} /* 500 */

View File

@ -66,10 +66,9 @@ int ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
ctx = (ngx_http_write_filter_ctx_t *) ctx = (ngx_http_write_filter_ctx_t *)
ngx_http_get_module_ctx(r->main ? r->main : r, ngx_http_get_module_ctx(r->main ? r->main : r,
ngx_http_write_filter_module_ctx); ngx_http_write_filter_module);
if (ctx == NULL) { if (ctx == NULL) {
ngx_http_create_ctx(r, ctx, ngx_http_create_ctx(r, ctx, ngx_http_write_filter_module,
ngx_http_write_filter_module_ctx,
sizeof(ngx_http_write_filter_ctx_t)); sizeof(ngx_http_write_filter_ctx_t));
} }
@ -126,7 +125,7 @@ int ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
conf = (ngx_http_write_filter_conf_t *) conf = (ngx_http_write_filter_conf_t *)
ngx_http_get_module_loc_conf(r->main ? r->main : r, ngx_http_get_module_loc_conf(r->main ? r->main : r,
ngx_http_write_filter_module_ctx); ngx_http_write_filter_module);
#if (NGX_DEBUG_WRITE_FILTER) #if (NGX_DEBUG_WRITE_FILTER)
ngx_log_debug(r->connection->log, "write filter: last:%d flush:%d" _ ngx_log_debug(r->connection->log, "write filter: last:%d flush:%d" _

View File

@ -9,6 +9,7 @@ typedef int ngx_err_t;
#define NGX_ENOENT ENOENT #define NGX_ENOENT ENOENT
#define NGX_EINTR EINTR #define NGX_EINTR EINTR
#define NGX_ENOTDIR ENOTDIR
#define NGX_EAGAIN EWOULDBLOCK #define NGX_EAGAIN EWOULDBLOCK
#define NGX_EINPROGRESS EINPROGRESS #define NGX_EINPROGRESS EINPROGRESS
#define NGX_EADDRINUSE EADDRINUSE #define NGX_EADDRINUSE EADDRINUSE

View File

@ -8,6 +8,9 @@
typedef int ssize_t; typedef int ssize_t;
typedef long time_t; typedef long time_t;
typedef unsigned __int32 u_int32_t;
#define QD_FMT "%I64d" #define QD_FMT "%I64d"
#define QX_FMT "%I64x" #define QX_FMT "%I64x"