comparison src/http/modules/ngx_http_static_handler.c @ 26:53cb81681040

nginx-0.0.1-2002-12-15-09:25:09 import
author Igor Sysoev <igor@sysoev.ru>
date Sun, 15 Dec 2002 06:25:09 +0000
parents 77c7629a2627
children 0e81ac0bb3e2
comparison
equal deleted inserted replaced
25:a8b156554dfe 26:53cb81681040
22 #endif 22 #endif
23 23
24 24
25 int ngx_http_static_handler(ngx_http_request_t *r) 25 int ngx_http_static_handler(ngx_http_request_t *r)
26 { 26 {
27 int rc; 27 int rc;
28 ngx_err_t err;
28 ngx_hunk_t *h; 29 ngx_hunk_t *h;
29 ngx_http_log_ctx_t *ctx; 30 ngx_http_log_ctx_t *ctx;
30 31
31 /* 32 #if 0
32 ngx_http_event_static_handler_loc_conf_t *cf; 33 ngx_http_event_static_handler_loc_conf_t *cf;
33 34
34 cf = (ngx_http_event_static_handler_loc_conf_t *) 35 cf = (ngx_http_event_static_handler_loc_conf_t *)
35 ngx_get_module_loc_conf(r, &ngx_http_event_static_handler_module); 36 ngx_get_module_loc_conf(r, &ngx_http_event_static_handler_module);
36 37
37 */ 38 #endif
38 39
39 ngx_http_discard_body(r); 40 ngx_http_discard_body(r);
40 ctx = r->connection->log->data; 41 ctx = r->connection->log->data;
41 ctx->action = "sending response"; 42 ctx->action = "sending response";
42 43
43 if (r->fd != -1) 44 if (r->file.fd == NGX_INVALID_FILE)
44 r->fd = ngx_open_file(r->filename.data, NGX_FILE_RDONLY); 45 r->file.fd = ngx_open_file(r->file.name.data, NGX_FILE_RDONLY);
45 46
46 if (r->fd == -1) { 47 if (r->file.fd == NGX_INVALID_FILE) {
48 err = ngx_errno;
47 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno, 49 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
48 "ngx_http_static_handler: " 50 "ngx_http_static_handler: "
49 ngx_open_file_n " %s failed", r->filename.data); 51 ngx_open_file_n " %s failed", r->file.name.data);
50 52
51 return NGX_HTTP_INTERNAL_SERVER_ERROR; 53 if (err == NGX_ENOENT)
54 return NGX_HTTP_NOT_FOUND;
55 #if (WIN32)
56 else if (err == ERROR_PATH_NOT_FOUND)
57 return NGX_HTTP_NOT_FOUND;
58 #endif
59 else
60 return NGX_HTTP_INTERNAL_SERVER_ERROR;
52 } 61 }
53 62
54 if (ngx_stat_fd(r->fd, &r->fileinfo) == -1) { 63 if (!r->file.info_valid) {
64 if (ngx_stat_fd(r->file.fd, &r->file.info) == NGX_FILE_ERROR) {
65 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
66 "ngx_http_static_handler: "
67 ngx_stat_fd_n " %s failed", r->file.name.data);
68
69 if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR)
70 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
71 "ngx_http_static_handler: "
72 ngx_close_file_n " %s failed", r->file.name.data);
73
74 return NGX_HTTP_INTERNAL_SERVER_ERROR;
75 }
76
77 r->file.info_valid = 1;
78 }
79
80 #if !(WIN32) /* it's probably Unix specific */
81
82 if (!ngx_is_file(r->file.info)) {
55 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno, 83 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
56 "ngx_http_static_handler: " 84 "ngx_http_static_handler: "
57 ngx_stat_fd_n " %s failed", r->filename.data); 85 "%s is not regular file", r->file.name.data);
58 86
59 /* close fd */ 87 if (ngx_close_file(r->file.fd) == NGX_FILE_ERROR)
60 return NGX_HTTP_INTERNAL_SERVER_ERROR; 88 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
89 "ngx_http_static_handler: "
90 ngx_close_file_n " %s failed", r->file.name.data);
91
92 return NGX_HTTP_NOT_FOUND;
61 } 93 }
62 94
95 #endif
96
63 r->headers_out.status = NGX_HTTP_OK; 97 r->headers_out.status = NGX_HTTP_OK;
64 r->headers_out.content_length = ngx_file_size(r->fileinfo); 98 r->headers_out.content_length = ngx_file_size(r->file.info);
65 /* 99 r->headers_out.last_modified_time = ngx_file_mtime(r->file.info);
66 r->headers_out.last_modified = ngx_file_mtime(r->fileinfo);
67 */
68 100
69 ngx_test_null(r->headers_out.content_type, 101 ngx_test_null(r->headers_out.content_type,
70 ngx_push_table(r->headers_out.headers), 102 ngx_push_table(r->headers_out.headers),
71 NGX_HTTP_INTERNAL_SERVER_ERROR); 103 NGX_HTTP_INTERNAL_SERVER_ERROR);
104
72 r->headers_out.content_type->key.len = 12; 105 r->headers_out.content_type->key.len = 12;
73 r->headers_out.content_type->key.data = "Content-Type"; 106 r->headers_out.content_type->key.data = "Content-Type";
74 107
75 /* STUB */ 108 /* STUB */
76 if (r->exten.len) { 109 if (r->exten.len) {
88 121
89 } else { 122 } else {
90 r->headers_out.content_type->value.len = 25; 123 r->headers_out.content_type->value.len = 25;
91 r->headers_out.content_type->value.data = "text/html; charset=koi8-r"; 124 r->headers_out.content_type->value.data = "text/html; charset=koi8-r";
92 } 125 }
126 /**/
93 127
94 /* STUB */ 128 /* we need to allocate them before header would be sent */
95 rc = ngx_http_header_filter(r); 129 ngx_test_null(h, ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)),
96 /* 130 NGX_HTTP_INTERNAL_SERVER_ERROR);
97 rc = ngx_send_http_header(r); 131
98 */ 132 ngx_test_null(h->file, ngx_pcalloc(r->pool, sizeof(ngx_file_t)),
133 NGX_HTTP_INTERNAL_SERVER_ERROR);
134
135 rc = ngx_http_send_header(r);
99 if (r->header_only) 136 if (r->header_only)
100 return rc; 137 return rc;
101 138
102 /* TODO: NGX_HTTP_INTERNAL_SERVER_ERROR is too late */
103
104 /* STUB */
105 ngx_test_null(h, ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)),
106 NGX_HTTP_INTERNAL_SERVER_ERROR);
107
108 h->type = NGX_HUNK_FILE|NGX_HUNK_LAST; 139 h->type = NGX_HUNK_FILE|NGX_HUNK_LAST;
109 h->pos.file = 0; 140 h->pos.file = 0;
110 h->last.file = ngx_file_size(r->fileinfo); 141 h->last.file = ngx_file_size(r->file.info);
111 142
112 /* STUB */ 143 h->file->fd = r->file.fd;
113 ngx_test_null(h->file, ngx_pcalloc(r->pool, sizeof(ngx_file_t)),
114 NGX_HTTP_INTERNAL_SERVER_ERROR);
115 h->file->fd = r->fd;
116 h->file->log = r->connection->log; 144 h->file->log = r->connection->log;
117 145
118 rc = ngx_http_output_filter(r, h); 146 rc = ngx_http_output_filter(r, h);
147
119 ngx_log_debug(r->connection->log, "0 output_filter: %d" _ rc); 148 ngx_log_debug(r->connection->log, "0 output_filter: %d" _ rc);
149
120 return rc; 150 return rc;
121 } 151 }
122
123 #if 0
124
125 static void *ngx_create_index_config()
126 {
127 ngx_http_index_handler_loc_conf_t *cf;
128
129 ngx_check_null(cf, ngx_alloc(p, sizeof(ngx_http_index_handler_loc_conf)),
130 NULL);
131
132 cf->indices = ngx_create_array(p, sizeof(ngx_http_index_t), 5);
133 if (cf->indices == NULL)
134 return NULL;
135
136 cf->max_index_len = 0;
137
138 return cf;
139 }
140
141 static void *ngx_merge_index_config()
142 {
143 if (p->indices->nelts > 0) {
144
145 copy and check dups
146
147 if (c->max_index_len < c->max_index_len)
148 c->max_index_len < c->max_index_len);
149 }
150 }
151
152 static void *ngx_set_index()
153 {
154 if (*conf == NULL) {
155 cf = ngx_create_index_conf();
156 if (cf == NULL)
157 return "can not create config";
158 }
159
160 while (args) {
161 index = ngx_push_array(cf->indices);
162 index->name = arg;
163 index->len = ngx_strlen(arg) + 1;
164
165 if (cf->max_index_len < index->len)
166 cf->max_index_len = index->len;
167 }
168
169 *conf = cf;
170 }
171
172 #endif