2005-02-20 14:27:00 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "response.h"
|
|
|
|
#include "keyvalue.h"
|
|
|
|
#include "log.h"
|
2005-08-08 08:22:06 +00:00
|
|
|
#include "stat_cache.h"
|
2005-08-15 09:55:23 +00:00
|
|
|
#include "chunk.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2008-02-26 16:19:42 +00:00
|
|
|
#include "configfile.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "connections.h"
|
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
|
|
|
#include "sys-socket.h"
|
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
int http_response_write_header(server *srv, connection *con) {
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer *b;
|
|
|
|
size_t i;
|
2005-11-18 12:38:18 +00:00
|
|
|
int have_date = 0;
|
|
|
|
int have_server = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
b = chunkqueue_get_prepend_buffer(con->write_queue);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (con->request.http_version == HTTP_VERSION_1_1) {
|
|
|
|
BUFFER_COPY_STRING_CONST(b, "HTTP/1.1 ");
|
|
|
|
} else {
|
|
|
|
BUFFER_COPY_STRING_CONST(b, "HTTP/1.0 ");
|
|
|
|
}
|
|
|
|
buffer_append_long(b, con->http_status);
|
|
|
|
BUFFER_APPEND_STRING_CONST(b, " ");
|
|
|
|
buffer_append_string(b, get_http_status_name(con->http_status));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (con->request.http_version != HTTP_VERSION_1_1 || con->keep_alive == 0) {
|
|
|
|
BUFFER_APPEND_STRING_CONST(b, "\r\nConnection: ");
|
|
|
|
buffer_append_string(b, con->keep_alive ? "keep-alive" : "close");
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
|
|
|
|
BUFFER_APPEND_STRING_CONST(b, "\r\nTransfer-Encoding: chunked");
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* add all headers */
|
|
|
|
for (i = 0; i < con->response.headers->used; i++) {
|
|
|
|
data_string *ds;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
ds = (data_string *)con->response.headers->data[i];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (ds->value->used && ds->key->used &&
|
2008-01-16 00:19:54 +00:00
|
|
|
0 != strncmp(ds->key->ptr, "X-LIGHTTPD-", sizeof("X-LIGHTTPD-") - 1) &&
|
|
|
|
0 != strncmp(ds->key->ptr, "X-Sendfile", sizeof("X-Sendfile") - 1)) {
|
2005-11-18 12:38:18 +00:00
|
|
|
if (buffer_is_equal_string(ds->key, CONST_STR_LEN("Date"))) have_date = 1;
|
|
|
|
if (buffer_is_equal_string(ds->key, CONST_STR_LEN("Server"))) have_server = 1;
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
BUFFER_APPEND_STRING_CONST(b, "\r\n");
|
|
|
|
buffer_append_string_buffer(b, ds->key);
|
|
|
|
BUFFER_APPEND_STRING_CONST(b, ": ");
|
2007-04-13 20:47:40 +00:00
|
|
|
#if 0
|
2007-04-09 18:12:43 +00:00
|
|
|
/**
|
|
|
|
* the value might contain newlines, encode them with at least one white-space
|
|
|
|
*/
|
|
|
|
buffer_append_string_encoded(b, CONST_BUF_LEN(ds->value), ENCODING_HTTP_HEADER);
|
2007-04-13 20:47:40 +00:00
|
|
|
#else
|
|
|
|
buffer_append_string_buffer(b, ds->value);
|
2005-02-20 14:27:00 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-11-18 12:38:18 +00:00
|
|
|
if (!have_date) {
|
|
|
|
/* HTTP/1.1 requires a Date: header */
|
|
|
|
BUFFER_APPEND_STRING_CONST(b, "\r\nDate: ");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-11-18 12:38:18 +00:00
|
|
|
/* cache the generated timestamp */
|
|
|
|
if (srv->cur_ts != srv->last_generated_date_ts) {
|
|
|
|
buffer_prepare_copy(srv->ts_date_str, 255);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
strftime(srv->ts_date_str->ptr, srv->ts_date_str->size - 1,
|
2005-11-18 12:38:18 +00:00
|
|
|
"%a, %d %b %Y %H:%M:%S GMT", gmtime(&(srv->cur_ts)));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-11-18 12:38:18 +00:00
|
|
|
srv->ts_date_str->used = strlen(srv->ts_date_str->ptr) + 1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-11-18 12:38:18 +00:00
|
|
|
srv->last_generated_date_ts = srv->cur_ts;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-11-18 12:38:18 +00:00
|
|
|
buffer_append_string_buffer(b, srv->ts_date_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!have_server) {
|
|
|
|
if (buffer_is_empty(con->conf.server_tag)) {
|
|
|
|
BUFFER_APPEND_STRING_CONST(b, "\r\nServer: " PACKAGE_NAME "/" PACKAGE_VERSION);
|
2008-04-10 10:54:27 +00:00
|
|
|
} else if (con->conf.server_tag->used > 1) {
|
2005-11-18 12:38:18 +00:00
|
|
|
BUFFER_APPEND_STRING_CONST(b, "\r\nServer: ");
|
2007-04-09 18:12:43 +00:00
|
|
|
buffer_append_string_encoded(b, CONST_BUF_LEN(con->conf.server_tag), ENCODING_HTTP_HEADER);
|
2005-11-18 12:38:18 +00:00
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
BUFFER_APPEND_STRING_CONST(b, "\r\n\r\n");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->bytes_header = b->used - 1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (con->conf.log_response_header) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sSb", "Response-Header:", "\n", b);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-03-03 17:33:44 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
handler_t http_response_prepare(server *srv, connection *con) {
|
|
|
|
handler_t r;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* looks like someone has already done a decision */
|
2006-10-04 13:26:23 +00:00
|
|
|
if (con->mode == DIRECT &&
|
2005-02-20 14:27:00 +00:00
|
|
|
(con->http_status != 0 && con->http_status != 200)) {
|
|
|
|
/* remove a packets in the queue */
|
|
|
|
if (con->file_finished == 0) {
|
|
|
|
chunkqueue_reset(con->write_queue);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* no decision yet, build conf->filename */
|
|
|
|
if (con->mode == DIRECT && con->physical.path->used == 0) {
|
|
|
|
char *qstr;
|
2006-01-14 18:37:16 +00:00
|
|
|
|
|
|
|
/* we only come here when we have the parse the full request again
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
* a HANDLER_COMEBACK from mod_rewrite and mod_fastcgi might be a
|
2006-01-14 18:37:16 +00:00
|
|
|
* problem here as mod_setenv might get called multiple times
|
|
|
|
*
|
|
|
|
* fastcgi-auth might lead to a COMEBACK too
|
|
|
|
* fastcgi again dead server too
|
|
|
|
*
|
|
|
|
* mod_compress might add headers twice too
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2006-01-14 18:37:16 +00:00
|
|
|
* */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2008-02-26 16:19:42 +00:00
|
|
|
config_cond_cache_reset(srv, con);
|
|
|
|
config_setup_connection(srv, con); // Perhaps this could be removed at other places.
|
|
|
|
|
2005-08-09 06:42:33 +00:00
|
|
|
if (con->conf.log_condition_handling) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "run condition");
|
|
|
|
}
|
2005-08-08 17:25:55 +00:00
|
|
|
config_patch_connection(srv, con, COMP_SERVER_SOCKET); /* SERVERsocket */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/**
|
|
|
|
* prepare strings
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
* - uri.path_raw
|
2005-02-20 14:27:00 +00:00
|
|
|
* - uri.path (secure)
|
|
|
|
* - uri.query
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
/**
|
2005-02-20 14:27:00 +00:00
|
|
|
* Name according to RFC 2396
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
* - scheme
|
|
|
|
* - authority
|
|
|
|
* - path
|
|
|
|
* - query
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2006-10-04 07:20:14 +00:00
|
|
|
* (scheme)://(authority)(path)?(query)#fragment
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 09:04:44 +00:00
|
|
|
buffer_copy_string(con->uri.scheme, con->conf.is_ssl ? "https" : "http");
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer_copy_string_buffer(con->uri.authority, con->request.http_host);
|
2005-08-15 09:55:23 +00:00
|
|
|
buffer_to_lower(con->uri.authority);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2007-11-04 16:53:17 +00:00
|
|
|
config_patch_connection(srv, con, COMP_HTTP_SCHEME); /* Scheme: */
|
2005-08-08 17:25:55 +00:00
|
|
|
config_patch_connection(srv, con, COMP_HTTP_HOST); /* Host: */
|
2008-03-02 11:46:44 +00:00
|
|
|
config_patch_connection(srv, con, COMP_HTTP_REMOTE_IP); /* Client-IP */
|
2005-08-08 17:25:55 +00:00
|
|
|
config_patch_connection(srv, con, COMP_HTTP_REFERER); /* Referer: */
|
2008-03-02 11:46:44 +00:00
|
|
|
config_patch_connection(srv, con, COMP_HTTP_USER_AGENT);/* User-Agent: */
|
2005-08-08 17:25:55 +00:00
|
|
|
config_patch_connection(srv, con, COMP_HTTP_COOKIE); /* Cookie: */
|
2008-03-02 12:59:18 +00:00
|
|
|
config_patch_connection(srv, con, COMP_HTTP_REQUEST_METHOD); /* REQUEST_METHOD */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-10-04 07:20:14 +00:00
|
|
|
/** their might be a fragment which has to be cut away */
|
|
|
|
if (NULL != (qstr = strchr(con->request.uri->ptr, '#'))) {
|
|
|
|
con->request.uri->used = qstr - con->request.uri->ptr;
|
|
|
|
con->request.uri->ptr[con->request.uri->used++] = '\0';
|
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/** extract query string from request.uri */
|
|
|
|
if (NULL != (qstr = strchr(con->request.uri->ptr, '?'))) {
|
|
|
|
buffer_copy_string (con->uri.query, qstr + 1);
|
|
|
|
buffer_copy_string_len(con->uri.path_raw, con->request.uri->ptr, qstr - con->request.uri->ptr);
|
|
|
|
} else {
|
|
|
|
buffer_reset (con->uri.query);
|
|
|
|
buffer_copy_string_buffer(con->uri.path_raw, con->request.uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (con->conf.log_request_handling) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "-- splitting Request-URI");
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Request-URI : ", con->request.uri);
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "URI-scheme : ", con->uri.scheme);
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "URI-authority: ", con->uri.authority);
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "URI-path : ", con->uri.path_raw);
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "URI-query : ", con->uri.query);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* disable keep-alive if requested */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (con->request_count > con->conf.max_keep_alive_requests) {
|
|
|
|
con->keep_alive = 0;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/**
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
* call plugins
|
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
* - based on the raw URL
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
switch(r = plugins_call_handle_uri_raw(srv, con)) {
|
|
|
|
case HANDLER_GO_ON:
|
|
|
|
break;
|
|
|
|
case HANDLER_FINISHED:
|
|
|
|
case HANDLER_COMEBACK:
|
|
|
|
case HANDLER_WAIT_FOR_EVENT:
|
|
|
|
case HANDLER_ERROR:
|
|
|
|
return r;
|
|
|
|
default:
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sd", "handle_uri_raw: unknown return value", r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
/* build filename
|
2005-02-20 14:27:00 +00:00
|
|
|
*
|
|
|
|
* - decode url-encodings (e.g. %20 -> ' ')
|
|
|
|
* - remove path-modifiers (e.g. /../)
|
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2005-08-19 08:37:52 +00:00
|
|
|
if (con->request.http_method == HTTP_METHOD_OPTIONS &&
|
|
|
|
con->uri.path_raw->ptr[0] == '*' && con->uri.path_raw->ptr[1] == '\0') {
|
|
|
|
/* OPTIONS * ... */
|
|
|
|
buffer_copy_string_buffer(con->uri.path, con->uri.path_raw);
|
|
|
|
} else {
|
|
|
|
buffer_copy_string_buffer(srv->tmp_buf, con->uri.path_raw);
|
|
|
|
buffer_urldecode_path(srv->tmp_buf);
|
|
|
|
buffer_path_simplify(con->uri.path, srv->tmp_buf);
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
if (con->conf.log_request_handling) {
|
2005-08-15 09:55:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "-- sanatising URI");
|
2005-02-20 14:27:00 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "URI-path : ", con->uri.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
* call plugins
|
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
* - based on the clean URL
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-08 17:25:55 +00:00
|
|
|
config_patch_connection(srv, con, COMP_HTTP_URL); /* HTTPurl */
|
2008-03-02 12:59:18 +00:00
|
|
|
config_patch_connection(srv, con, COMP_HTTP_QUERY_STRING); /* HTTPqs */
|
2006-09-07 11:05:41 +00:00
|
|
|
|
|
|
|
/* do we have to downgrade to 1.0 ? */
|
|
|
|
if (!con->conf.allow_http11) {
|
|
|
|
con->request.http_version = HTTP_VERSION_1_0;
|
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
switch(r = plugins_call_handle_uri_clean(srv, con)) {
|
|
|
|
case HANDLER_GO_ON:
|
|
|
|
break;
|
|
|
|
case HANDLER_FINISHED:
|
|
|
|
case HANDLER_COMEBACK:
|
|
|
|
case HANDLER_WAIT_FOR_EVENT:
|
|
|
|
case HANDLER_ERROR:
|
|
|
|
return r;
|
|
|
|
default:
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "");
|
|
|
|
break;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-19 00:05:52 +00:00
|
|
|
if (con->request.http_method == HTTP_METHOD_OPTIONS &&
|
2005-08-19 08:37:52 +00:00
|
|
|
con->uri.path->ptr[0] == '*' && con->uri.path_raw->ptr[1] == '\0') {
|
2005-08-19 00:05:52 +00:00
|
|
|
/* option requests are handled directly without checking of the path */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-19 00:05:52 +00:00
|
|
|
response_header_insert(srv, con, CONST_STR_LEN("Allow"), CONST_STR_LEN("OPTIONS, GET, HEAD, POST"));
|
|
|
|
|
|
|
|
con->http_status = 200;
|
|
|
|
con->file_finished = 1;
|
|
|
|
|
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/***
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
* border
|
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
* logical filename (URI) becomes a physical filename here
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* 1. stat()
|
|
|
|
* ... ISREG() -> ok, go on
|
|
|
|
* ... ISDIR() -> index-file -> redirect
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
* 2. pathinfo()
|
2005-02-20 14:27:00 +00:00
|
|
|
* ... ISREG()
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
* 3. -> 404
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/*
|
|
|
|
* SEARCH DOCUMENT ROOT
|
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* set a default */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer_copy_string_buffer(con->physical.doc_root, con->conf.document_root);
|
|
|
|
buffer_copy_string_buffer(con->physical.rel_path, con->uri.path);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-03-04 14:53:29 +00:00
|
|
|
#if defined(__WIN32) || defined(__CYGWIN__)
|
2006-02-22 12:50:56 +00:00
|
|
|
/* strip dots from the end and spaces
|
|
|
|
*
|
|
|
|
* windows/dos handle those filenames as the same file
|
|
|
|
*
|
|
|
|
* foo == foo. == foo..... == "foo... " == "foo.. ./"
|
|
|
|
*
|
|
|
|
* This will affect in some cases PATHINFO
|
|
|
|
*
|
2006-03-04 14:53:29 +00:00
|
|
|
* on native windows we could prepend the filename with \\?\ to circumvent
|
|
|
|
* this behaviour. I have no idea how to push this through cygwin
|
|
|
|
*
|
2006-02-22 12:50:56 +00:00
|
|
|
* */
|
2006-03-04 14:53:29 +00:00
|
|
|
|
2006-02-22 12:50:56 +00:00
|
|
|
if (con->physical.rel_path->used > 1) {
|
|
|
|
buffer *b = con->physical.rel_path;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (b->used > 2 &&
|
|
|
|
b->ptr[b->used-2] == '/' &&
|
|
|
|
(b->ptr[b->used-3] == ' ' ||
|
|
|
|
b->ptr[b->used-3] == '.')) {
|
|
|
|
b->ptr[b->used--] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = b->used - 2; b->used > 1; i--) {
|
|
|
|
if (b->ptr[i] == ' ' ||
|
|
|
|
b->ptr[i] == '.') {
|
|
|
|
b->ptr[b->used--] = '\0';
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-03-04 14:53:29 +00:00
|
|
|
#endif
|
2006-02-22 12:50:56 +00:00
|
|
|
|
2005-08-08 09:42:27 +00:00
|
|
|
if (con->conf.log_request_handling) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "-- before doc_root");
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Doc-Root :", con->physical.doc_root);
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Rel-Path :", con->physical.rel_path);
|
2005-08-15 09:55:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Path :", con->physical.path);
|
2005-08-08 09:42:27 +00:00
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
/* the docroot plugin should set the doc_root and might also set the physical.path
|
2005-08-08 09:42:27 +00:00
|
|
|
* for us (all vhost-plugins are supposed to set the doc_root)
|
2005-02-20 14:27:00 +00:00
|
|
|
* */
|
|
|
|
switch(r = plugins_call_handle_docroot(srv, con)) {
|
|
|
|
case HANDLER_GO_ON:
|
|
|
|
break;
|
|
|
|
case HANDLER_FINISHED:
|
|
|
|
case HANDLER_COMEBACK:
|
|
|
|
case HANDLER_WAIT_FOR_EVENT:
|
|
|
|
case HANDLER_ERROR:
|
|
|
|
return r;
|
|
|
|
default:
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "");
|
|
|
|
break;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
/* MacOS X and Windows can't distiguish between upper and lower-case
|
|
|
|
*
|
2005-08-15 09:55:23 +00:00
|
|
|
* convert to lower-case
|
|
|
|
*/
|
2006-01-11 23:05:06 +00:00
|
|
|
if (con->conf.force_lowercase_filenames) {
|
2005-08-15 09:55:23 +00:00
|
|
|
buffer_to_lower(con->physical.rel_path);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2005-08-08 09:42:27 +00:00
|
|
|
|
2005-04-20 16:44:00 +00:00
|
|
|
/* the docroot plugins might set the servername, if they don't we take http-host */
|
|
|
|
if (buffer_is_empty(con->server_name)) {
|
|
|
|
buffer_copy_string_buffer(con->server_name, con->uri.authority);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* create physical filename
|
2005-08-08 09:42:27 +00:00
|
|
|
* -> physical.path = docroot + rel_path
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-08-08 09:42:27 +00:00
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-08 09:42:27 +00:00
|
|
|
buffer_copy_string_buffer(con->physical.path, con->physical.doc_root);
|
|
|
|
BUFFER_APPEND_SLASH(con->physical.path);
|
|
|
|
buffer_copy_string_buffer(con->physical.basedir, con->physical.path);
|
2005-09-08 09:57:55 +00:00
|
|
|
if (con->physical.rel_path->used &&
|
|
|
|
con->physical.rel_path->ptr[0] == '/') {
|
2005-08-08 09:42:27 +00:00
|
|
|
buffer_append_string_len(con->physical.path, con->physical.rel_path->ptr + 1, con->physical.rel_path->used - 2);
|
|
|
|
} else {
|
|
|
|
buffer_append_string_buffer(con->physical.path, con->physical.rel_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (con->conf.log_request_handling) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "-- after doc_root");
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Doc-Root :", con->physical.doc_root);
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Rel-Path :", con->physical.rel_path);
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Path :", con->physical.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(r = plugins_call_handle_physical(srv, con)) {
|
|
|
|
case HANDLER_GO_ON:
|
|
|
|
break;
|
|
|
|
case HANDLER_FINISHED:
|
|
|
|
case HANDLER_COMEBACK:
|
|
|
|
case HANDLER_WAIT_FOR_EVENT:
|
|
|
|
case HANDLER_ERROR:
|
|
|
|
return r;
|
|
|
|
default:
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "");
|
|
|
|
break;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (con->conf.log_request_handling) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "-- logical -> physical");
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Doc-Root :", con->physical.doc_root);
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Rel-Path :", con->physical.rel_path);
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Path :", con->physical.path);
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
/*
|
2005-08-15 09:55:23 +00:00
|
|
|
* Noone catched away the file from normal path of execution yet (like mod_access)
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-08-15 09:55:23 +00:00
|
|
|
* Go on and check of the file exists at all
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (con->mode == DIRECT) {
|
|
|
|
char *slash = NULL;
|
|
|
|
char *pathinfo = NULL;
|
|
|
|
int found = 0;
|
2005-08-08 08:22:06 +00:00
|
|
|
stat_cache_entry *sce = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (con->conf.log_request_handling) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "-- handling physical path");
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Path :", con->physical.path);
|
|
|
|
}
|
2006-09-07 11:00:02 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
if (HANDLER_ERROR != stat_cache_get_entry(srv, con, con->physical.path, &sce)) {
|
|
|
|
/* file exists */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
if (con->conf.log_request_handling) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "-- file found");
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Path :", con->physical.path);
|
|
|
|
}
|
2006-09-07 11:00:02 +00:00
|
|
|
#ifdef HAVE_LSTAT
|
|
|
|
if ((sce->is_symlink != 0) && !con->conf.follow_symlink) {
|
|
|
|
con->http_status = 403;
|
|
|
|
|
|
|
|
if (con->conf.log_request_handling) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "-- access denied due symlink restriction");
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Path :", con->physical.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_reset(con->physical.path);
|
|
|
|
return HANDLER_FINISHED;
|
|
|
|
};
|
|
|
|
#endif
|
2005-08-15 09:55:23 +00:00
|
|
|
if (S_ISDIR(sce->st.st_mode)) {
|
|
|
|
if (con->physical.path->ptr[con->physical.path->used - 2] != '/') {
|
|
|
|
/* redirect to .../ */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
http_response_redirect_to_directory(srv, con);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
2006-09-07 11:00:02 +00:00
|
|
|
#ifdef HAVE_LSTAT
|
|
|
|
} else if (!S_ISREG(sce->st.st_mode) && !sce->is_symlink) {
|
|
|
|
#else
|
2005-09-02 17:07:30 +00:00
|
|
|
} else if (!S_ISREG(sce->st.st_mode)) {
|
2006-09-07 11:00:02 +00:00
|
|
|
#endif
|
2005-09-02 17:07:30 +00:00
|
|
|
/* any special handling of non-reg files ?*/
|
|
|
|
|
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (errno) {
|
|
|
|
case EACCES:
|
2005-02-20 14:27:00 +00:00
|
|
|
con->http_status = 403;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-31 21:26:09 +00:00
|
|
|
if (con->conf.log_request_handling) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "-- access denied");
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Path :", con->physical.path);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer_reset(con->physical.path);
|
|
|
|
return HANDLER_FINISHED;
|
2005-08-15 09:55:23 +00:00
|
|
|
case ENOENT:
|
|
|
|
con->http_status = 404;
|
|
|
|
|
2005-08-31 21:26:09 +00:00
|
|
|
if (con->conf.log_request_handling) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "-- file not found");
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Path :", con->physical.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_reset(con->physical.path);
|
2005-08-15 09:55:23 +00:00
|
|
|
return HANDLER_FINISHED;
|
|
|
|
case ENOTDIR:
|
|
|
|
/* PATH_INFO ! :) */
|
|
|
|
break;
|
|
|
|
default:
|
2005-02-20 14:27:00 +00:00
|
|
|
/* we have no idea what happend. let's tell the user so. */
|
|
|
|
con->http_status = 500;
|
|
|
|
buffer_reset(con->physical.path);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ssbsb",
|
|
|
|
"file not found ... or so: ", strerror(errno),
|
|
|
|
con->uri.path,
|
|
|
|
"->", con->physical.path);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* not found, perhaps PATHINFO */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
buffer_copy_string_buffer(srv->tmp_buf, con->physical.path);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
do {
|
|
|
|
if (slash) {
|
2005-08-08 09:42:27 +00:00
|
|
|
buffer_copy_string_len(con->physical.path, srv->tmp_buf->ptr, slash - srv->tmp_buf->ptr);
|
2005-02-20 14:27:00 +00:00
|
|
|
} else {
|
2005-08-08 09:42:27 +00:00
|
|
|
buffer_copy_string_buffer(con->physical.path, srv->tmp_buf);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2008-02-26 16:22:08 +00:00
|
|
|
if (HANDLER_ERROR != stat_cache_get_entry(srv, con, con->physical.path, &sce)) {
|
|
|
|
found = S_ISREG(sce->st.st_mode);
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (pathinfo != NULL) {
|
|
|
|
*pathinfo = '\0';
|
|
|
|
}
|
|
|
|
slash = strrchr(srv->tmp_buf->ptr, '/');
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (pathinfo != NULL) {
|
|
|
|
/* restore '/' */
|
|
|
|
*pathinfo = '/';
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (slash) pathinfo = slash;
|
2006-10-03 22:19:50 +00:00
|
|
|
} while ((found == 0) && (slash != NULL) && ((size_t)(slash - srv->tmp_buf->ptr) > (con->physical.basedir->used - 2)));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (found == 0) {
|
|
|
|
/* no it really doesn't exists */
|
|
|
|
con->http_status = 404;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (con->conf.log_file_not_found) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sbsb",
|
2005-08-15 09:55:23 +00:00
|
|
|
"file not found:", con->uri.path,
|
|
|
|
"->", con->physical.path);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer_reset(con->physical.path);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2008-02-26 16:22:08 +00:00
|
|
|
#ifdef HAVE_LSTAT
|
|
|
|
if ((sce->is_symlink != 0) && !con->conf.follow_symlink) {
|
|
|
|
con->http_status = 403;
|
|
|
|
|
|
|
|
if (con->conf.log_request_handling) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "-- access denied due symlink restriction");
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Path :", con->physical.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_reset(con->physical.path);
|
|
|
|
return HANDLER_FINISHED;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* we have a PATHINFO */
|
|
|
|
if (pathinfo) {
|
|
|
|
buffer_copy_string(con->request.pathinfo, pathinfo);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/*
|
|
|
|
* shorten uri.path
|
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
con->uri.path->used -= strlen(pathinfo);
|
2005-02-20 14:27:00 +00:00
|
|
|
con->uri.path->ptr[con->uri.path->used - 1] = '\0';
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (con->conf.log_request_handling) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "-- after pathinfo check");
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Path :", con->physical.path);
|
2005-03-02 18:36:32 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "URI :", con->uri.path);
|
2005-02-20 14:27:00 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Pathinfo :", con->request.pathinfo);
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
if (con->conf.log_request_handling) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "-- handling subrequest");
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "Path :", con->physical.path);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* call the handlers */
|
|
|
|
switch(r = plugins_call_handle_subrequest_start(srv, con)) {
|
|
|
|
case HANDLER_GO_ON:
|
|
|
|
/* request was not handled */
|
|
|
|
break;
|
2005-08-15 09:55:23 +00:00
|
|
|
case HANDLER_FINISHED:
|
2005-02-20 14:27:00 +00:00
|
|
|
default:
|
2005-08-15 09:55:23 +00:00
|
|
|
if (con->conf.log_request_handling) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "-- subrequest finished");
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* something strange happend */
|
|
|
|
return r;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
/* if we are still here, no one wanted the file, status 403 is ok I think */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2008-02-26 19:14:14 +00:00
|
|
|
if (con->mode == DIRECT && con->http_status == 0) {
|
|
|
|
switch (con->request.http_method) {
|
|
|
|
case HTTP_METHOD_OPTIONS:
|
|
|
|
con->http_status = 200;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
con->http_status = 403;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
return HANDLER_FINISHED;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
switch(r = plugins_call_handle_subrequest(srv, con)) {
|
|
|
|
case HANDLER_GO_ON:
|
|
|
|
/* request was not handled, looks like we are done */
|
|
|
|
return HANDLER_FINISHED;
|
|
|
|
case HANDLER_FINISHED:
|
|
|
|
/* request is finished */
|
|
|
|
default:
|
|
|
|
/* something strange happend */
|
|
|
|
return r;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* can't happen */
|
|
|
|
return HANDLER_COMEBACK;
|
|
|
|
}
|
2005-08-31 13:39:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|