1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
#include <ngx_config.h>
#include <ngx_string.h>
#include <ngx_file.h>
#include <ngx_hunk.h>
#include <ngx_http.h>
ngx_http_module_t ngx_http_static_module;
#if 0
/* STUB */
static ngx_http_static_ctx_t module_ctx;
void ngx_http_static_init()
{
module_ctx.out = NULL;
ngx_http_static_module.ctx = &module_ctx;
}
/* */
#endif
int ngx_http_static_handler(ngx_http_request_t *r)
{
int rc;
ngx_hunk_t *h;
ngx_http_log_ctx_t *ctx;
/*
ngx_http_event_static_handler_loc_conf_t *cf;
cf = (ngx_http_event_static_handler_loc_conf_t *)
ngx_get_module_loc_conf(r, &ngx_http_event_static_handler_module);
*/
ngx_http_discard_body(r);
ctx = r->connection->log->data;
ctx->action = "sending response";
if (r->fd != -1)
r->fd = ngx_open_file(r->filename.data, NGX_FILE_RDONLY);
if (r->fd == -1) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
"ngx_http_static_handler: "
ngx_open_file_n " %s failed", r->filename.data);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (ngx_stat_fd(r->fd, &r->fileinfo) == -1) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
"ngx_http_static_handler: "
ngx_stat_fd_n " %s failed", r->filename.data);
/* close fd */
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length = ngx_file_size(r->fileinfo);
/*
r->headers_out.last_modified = ngx_file_mtime(r->fileinfo);
*/
ngx_test_null(r->headers_out.content_type,
ngx_push_table(r->headers_out.headers),
NGX_HTTP_INTERNAL_SERVER_ERROR);
r->headers_out.content_type->key.len = 12;
r->headers_out.content_type->key.data = "Content-Type";
/* STUB */
if (r->exten.len) {
if (strcasecmp(r->exten.data, "html") == 0) {
r->headers_out.content_type->value.len = 25;
r->headers_out.content_type->value.data =
"text/html; charset=koi8-r";
} else if (strcasecmp(r->exten.data, "gif") == 0) {
r->headers_out.content_type->value.len = 9;
r->headers_out.content_type->value.data = "image/gif";
} else if (strcasecmp(r->exten.data, "jpg") == 0) {
r->headers_out.content_type->value.len = 10;
r->headers_out.content_type->value.data = "image/jpeg";
}
} else {
r->headers_out.content_type->value.len = 25;
r->headers_out.content_type->value.data = "text/html; charset=koi8-r";
}
/* STUB */
rc = ngx_http_header_filter(r);
/*
rc = ngx_send_http_header(r);
*/
if (r->header_only)
return rc;
/* TODO: NGX_HTTP_INTERNAL_SERVER_ERROR is too late */
/* STUB */
ngx_test_null(h, ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)),
NGX_HTTP_INTERNAL_SERVER_ERROR);
h->type = NGX_HUNK_FILE|NGX_HUNK_LAST;
h->pos.file = 0;
h->last.file = ngx_file_size(r->fileinfo);
/* STUB */
ngx_test_null(h->file, ngx_pcalloc(r->pool, sizeof(ngx_file_t)),
NGX_HTTP_INTERNAL_SERVER_ERROR);
h->file->fd = r->fd;
h->file->log = r->connection->log;
rc = ngx_http_output_filter(r, h);
ngx_log_debug(r->connection->log, "0 output_filter: %d" _ rc);
return rc;
}
#if 0
static void *ngx_create_index_config()
{
ngx_http_index_handler_loc_conf_t *cf;
ngx_check_null(cf, ngx_alloc(p, sizeof(ngx_http_index_handler_loc_conf)),
NULL);
cf->indices = ngx_create_array(p, sizeof(ngx_http_index_t), 5);
if (cf->indices == NULL)
return NULL;
cf->max_index_len = 0;
return cf;
}
static void *ngx_merge_index_config()
{
if (p->indices->nelts > 0) {
copy and check dups
if (c->max_index_len < c->max_index_len)
c->max_index_len < c->max_index_len);
}
}
static void *ngx_set_index()
{
if (*conf == NULL) {
cf = ngx_create_index_conf();
if (cf == NULL)
return "can not create config";
}
while (args) {
index = ngx_push_array(cf->indices);
index->name = arg;
index->len = ngx_strlen(arg) + 1;
if (cf->max_index_len < index->len)
cf->max_index_len = index->len;
}
*conf = cf;
}
#endif
|