2005-10-01 11:05:30 +00:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2005-08-18 09:26:29 +00:00
|
|
|
#include <time.h>
|
2005-02-28 08:42:47 +00:00
|
|
|
|
|
|
|
#include "base.h"
|
|
|
|
#include "array.h"
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "log.h"
|
2005-09-02 12:38:42 +00:00
|
|
|
#include "etag.h"
|
2005-02-28 08:42:47 +00:00
|
|
|
|
2005-02-28 09:04:44 +00:00
|
|
|
/*
|
|
|
|
* This was 'borrowed' from tcpdump.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This is fun.
|
|
|
|
*
|
|
|
|
* In older BSD systems, socket addresses were fixed-length, and
|
|
|
|
* "sizeof (struct sockaddr)" gave the size of the structure.
|
|
|
|
* All addresses fit within a "struct sockaddr".
|
|
|
|
*
|
|
|
|
* In newer BSD systems, the socket address is variable-length, and
|
|
|
|
* there's an "sa_len" field giving the length of the structure;
|
|
|
|
* this allows socket addresses to be longer than 2 bytes of family
|
|
|
|
* and 14 bytes of data.
|
|
|
|
*
|
|
|
|
* Some commercial UNIXes use the old BSD scheme, some use the RFC 2553
|
|
|
|
* variant of the old BSD scheme (with "struct sockaddr_storage" rather
|
|
|
|
* than "struct sockaddr"), and some use the new BSD scheme.
|
|
|
|
*
|
|
|
|
* Some versions of GNU libc use neither scheme, but has an "SA_LEN()"
|
|
|
|
* macro that determines the size based on the address family. Other
|
|
|
|
* versions don't have "SA_LEN()" (as it was in drafts of RFC 2553
|
|
|
|
* but not in the final version). On the latter systems, we explicitly
|
|
|
|
* check the AF_ type to determine the length; we assume that on
|
|
|
|
* all those systems we have "struct sockaddr_storage".
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
# ifndef SA_LEN
|
|
|
|
# ifdef HAVE_SOCKADDR_SA_LEN
|
|
|
|
# define SA_LEN(addr) ((addr)->sa_len)
|
|
|
|
# else /* HAVE_SOCKADDR_SA_LEN */
|
|
|
|
# ifdef HAVE_STRUCT_SOCKADDR_STORAGE
|
|
|
|
static size_t get_sa_len(const struct sockaddr *addr) {
|
|
|
|
switch (addr->sa_family) {
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 09:04:44 +00:00
|
|
|
# ifdef AF_INET
|
|
|
|
case AF_INET:
|
|
|
|
return (sizeof (struct sockaddr_in));
|
|
|
|
# endif
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 09:04:44 +00:00
|
|
|
# ifdef AF_INET6
|
|
|
|
case AF_INET6:
|
|
|
|
return (sizeof (struct sockaddr_in6));
|
|
|
|
# endif
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 09:04:44 +00:00
|
|
|
default:
|
|
|
|
return (sizeof (struct sockaddr));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 09:04:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
# define SA_LEN(addr) (get_sa_len(addr))
|
|
|
|
# else /* HAVE_SOCKADDR_STORAGE */
|
|
|
|
# define SA_LEN(addr) (sizeof (struct sockaddr))
|
|
|
|
# endif /* HAVE_SOCKADDR_STORAGE */
|
|
|
|
# endif /* HAVE_SOCKADDR_SA_LEN */
|
|
|
|
# endif /* SA_LEN */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
int response_header_insert(server *srv, connection *con, const char *key, size_t keylen, const char *value, size_t vallen) {
|
|
|
|
data_string *ds;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
UNUSED(srv);
|
|
|
|
|
|
|
|
if (NULL == (ds = (data_string *)array_get_unused_element(con->response.headers, TYPE_STRING))) {
|
|
|
|
ds = data_response_init();
|
|
|
|
}
|
|
|
|
buffer_copy_string_len(ds->key, key, keylen);
|
|
|
|
buffer_copy_string_len(ds->value, value, vallen);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
array_insert_unique(con->response.headers, (data_unset *)ds);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int response_header_overwrite(server *srv, connection *con, const char *key, size_t keylen, const char *value, size_t vallen) {
|
|
|
|
data_string *ds;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
UNUSED(srv);
|
|
|
|
|
|
|
|
/* if there already is a key by this name overwrite the value */
|
|
|
|
if (NULL != (ds = (data_string *)array_get_element(con->response.headers, key))) {
|
|
|
|
buffer_copy_string(ds->value, value);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
return response_header_insert(srv, con, key, keylen, value, vallen);
|
|
|
|
}
|
|
|
|
|
|
|
|
int http_response_redirect_to_directory(server *srv, connection *con) {
|
|
|
|
buffer *o;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
o = buffer_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
if (con->conf.is_ssl) {
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_copy_string_len(o, CONST_STR_LEN("https://"));
|
2005-02-28 08:42:47 +00:00
|
|
|
} else {
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_copy_string_len(o, CONST_STR_LEN("http://"));
|
2005-02-28 08:42:47 +00:00
|
|
|
}
|
|
|
|
if (con->uri.authority->used) {
|
|
|
|
buffer_append_string_buffer(o, con->uri.authority);
|
|
|
|
} else {
|
|
|
|
/* get the name of the currently connected socket */
|
|
|
|
struct hostent *he;
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
char hbuf[256];
|
|
|
|
#endif
|
|
|
|
sock_addr our_addr;
|
|
|
|
socklen_t our_addr_len;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
our_addr_len = sizeof(our_addr);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
if (-1 == getsockname(con->fd, &(our_addr.plain), &our_addr_len)) {
|
|
|
|
con->http_status = 500;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss",
|
|
|
|
"can't get sockname", strerror(errno));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
buffer_free(o);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
/* Lookup name: secondly try to get hostname for bind address */
|
|
|
|
switch(our_addr.plain.sa_family) {
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
case AF_INET6:
|
2006-10-04 13:26:23 +00:00
|
|
|
if (0 != getnameinfo((const struct sockaddr *)(&our_addr.ipv6),
|
|
|
|
SA_LEN((const struct sockaddr *)&our_addr.ipv6),
|
2005-02-28 08:42:47 +00:00
|
|
|
hbuf, sizeof(hbuf), NULL, 0, 0)) {
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
char dst[INET6_ADDRSTRLEN];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__,
|
2007-03-26 08:21:39 +00:00
|
|
|
"SSS", "NOTICE: getnameinfo failed: ",
|
2005-02-28 08:42:47 +00:00
|
|
|
strerror(errno), ", using ip-address instead");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
buffer_append_string(o,
|
|
|
|
inet_ntop(AF_INET6, (char *)&our_addr.ipv6.sin6_addr,
|
2005-02-28 08:42:47 +00:00
|
|
|
dst, sizeof(dst)));
|
|
|
|
} else {
|
|
|
|
buffer_append_string(o, hbuf);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case AF_INET:
|
|
|
|
if (NULL == (he = gethostbyaddr((char *)&our_addr.ipv4.sin_addr, sizeof(struct in_addr), AF_INET))) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__,
|
2007-03-26 08:21:39 +00:00
|
|
|
"SdS", "NOTICE: gethostbyaddr failed: ",
|
2005-08-08 16:32:17 +00:00
|
|
|
h_errno, ", using ip-address instead");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
buffer_append_string(o, inet_ntoa(our_addr.ipv4.sin_addr));
|
|
|
|
} else {
|
|
|
|
buffer_append_string(o, he->h_name);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log_error_write(srv, __FILE__, __LINE__,
|
|
|
|
"S", "ERROR: unsupported address-type");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
buffer_free(o);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
if (!((con->conf.is_ssl == 0 && srv->srvconf.port == 80) ||
|
2005-02-28 08:42:47 +00:00
|
|
|
(con->conf.is_ssl == 1 && srv->srvconf.port == 443))) {
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(o, CONST_STR_LEN(":"));
|
2005-02-28 08:42:47 +00:00
|
|
|
buffer_append_long(o, srv->srvconf.port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buffer_append_string_buffer(o, con->uri.path);
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(o, CONST_STR_LEN("/"));
|
2005-02-28 08:42:47 +00:00
|
|
|
if (!buffer_is_empty(con->uri.query)) {
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(o, CONST_STR_LEN("?"));
|
2005-02-28 08:42:47 +00:00
|
|
|
buffer_append_string_buffer(o, con->uri.query);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
response_header_insert(srv, con, CONST_STR_LEN("Location"), CONST_BUF_LEN(o));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
con->http_status = 301;
|
2006-02-08 13:40:25 +00:00
|
|
|
con->file_finished = 1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
buffer_free(o);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-28 08:42:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-18 09:26:29 +00:00
|
|
|
buffer * strftime_cache_get(server *srv, time_t last_mod) {
|
|
|
|
struct tm *tm;
|
|
|
|
size_t i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-18 09:26:29 +00:00
|
|
|
for (i = 0; i < FILE_CACHE_MAX; i++) {
|
|
|
|
/* found cache-entry */
|
|
|
|
if (srv->mtime_cache[i].mtime == last_mod) return srv->mtime_cache[i].str;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-18 09:26:29 +00:00
|
|
|
/* found empty slot */
|
|
|
|
if (srv->mtime_cache[i].mtime == 0) break;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-18 09:26:29 +00:00
|
|
|
if (i == FILE_CACHE_MAX) {
|
|
|
|
i = 0;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-18 09:26:29 +00:00
|
|
|
srv->mtime_cache[i].mtime = last_mod;
|
|
|
|
buffer_prepare_copy(srv->mtime_cache[i].str, 1024);
|
|
|
|
tm = gmtime(&(srv->mtime_cache[i].mtime));
|
2006-10-04 13:26:23 +00:00
|
|
|
srv->mtime_cache[i].str->used = strftime(srv->mtime_cache[i].str->ptr,
|
2005-08-18 09:26:29 +00:00
|
|
|
srv->mtime_cache[i].str->size - 1,
|
|
|
|
"%a, %d %b %Y %H:%M:%S GMT", tm);
|
|
|
|
srv->mtime_cache[i].str->used++;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-18 09:26:29 +00:00
|
|
|
return srv->mtime_cache[i].str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-02 10:45:45 +00:00
|
|
|
int http_response_handle_cachable(server *srv, connection *con, buffer *mtime) {
|
|
|
|
/*
|
|
|
|
* 14.26 If-None-Match
|
|
|
|
* [...]
|
|
|
|
* If none of the entity tags match, then the server MAY perform the
|
|
|
|
* requested method as if the If-None-Match header field did not exist,
|
|
|
|
* but MUST also ignore any If-Modified-Since header field(s) in the
|
|
|
|
* request. That is, if no entity tags match, then the server MUST NOT
|
|
|
|
* return a 304 (Not Modified) response.
|
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-02 10:45:45 +00:00
|
|
|
/* last-modified handling */
|
|
|
|
if (con->request.http_if_none_match) {
|
|
|
|
if (etag_is_equal(con->physical.etag, con->request.http_if_none_match)) {
|
2006-10-04 13:26:23 +00:00
|
|
|
if (con->request.http_method == HTTP_METHOD_GET ||
|
2005-09-02 10:45:45 +00:00
|
|
|
con->request.http_method == HTTP_METHOD_HEAD) {
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-02 10:45:45 +00:00
|
|
|
/* check if etag + last-modified */
|
|
|
|
if (con->request.http_if_modified_since) {
|
|
|
|
size_t used_len;
|
|
|
|
char *semicolon;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-02 10:45:45 +00:00
|
|
|
if (NULL == (semicolon = strchr(con->request.http_if_modified_since, ';'))) {
|
|
|
|
used_len = strlen(con->request.http_if_modified_since);
|
|
|
|
} else {
|
|
|
|
used_len = semicolon - con->request.http_if_modified_since;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-02 10:45:45 +00:00
|
|
|
if (0 == strncmp(con->request.http_if_modified_since, mtime->ptr, used_len)) {
|
|
|
|
con->http_status = 304;
|
|
|
|
return HANDLER_FINISHED;
|
|
|
|
} else {
|
|
|
|
char buf[sizeof("Sat, 23 Jul 2005 21:20:01 GMT")];
|
2006-09-07 10:19:29 +00:00
|
|
|
time_t t_header, t_file;
|
|
|
|
struct tm tm;
|
2005-09-02 10:45:45 +00:00
|
|
|
|
2006-09-07 10:19:29 +00:00
|
|
|
/* check if we can safely copy the string */
|
|
|
|
if (used_len >= sizeof(buf)) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ssdd",
|
|
|
|
"DEBUG: Last-Modified check failed as the received timestamp was too long:",
|
2005-11-10 12:13:48 +00:00
|
|
|
con->request.http_if_modified_since, used_len, sizeof(buf) - 1);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-02 10:45:45 +00:00
|
|
|
con->http_status = 412;
|
2008-08-01 16:13:34 +00:00
|
|
|
con->mode = DIRECT;
|
2005-09-02 10:45:45 +00:00
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
2006-09-07 10:19:29 +00:00
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-09-07 10:19:29 +00:00
|
|
|
strncpy(buf, con->request.http_if_modified_since, used_len);
|
|
|
|
buf[used_len] = '\0';
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2007-08-17 21:46:36 +00:00
|
|
|
if (NULL == strptime(buf, "%a, %d %b %Y %H:%M:%S GMT", &tm)) {
|
|
|
|
con->http_status = 412;
|
2008-08-01 16:13:34 +00:00
|
|
|
con->mode = DIRECT;
|
2007-08-17 21:46:36 +00:00
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
2006-09-07 10:19:29 +00:00
|
|
|
t_header = mktime(&tm);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-09-07 10:19:29 +00:00
|
|
|
strptime(mtime->ptr, "%a, %d %b %Y %H:%M:%S GMT", &tm);
|
|
|
|
t_file = mktime(&tm);
|
|
|
|
|
|
|
|
if (t_file > t_header) return HANDLER_GO_ON;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-09-07 10:19:29 +00:00
|
|
|
con->http_status = 304;
|
|
|
|
return HANDLER_FINISHED;
|
2005-09-02 10:45:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
con->http_status = 304;
|
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
con->http_status = 412;
|
2008-08-01 16:13:34 +00:00
|
|
|
con->mode = DIRECT;
|
2005-09-02 10:45:45 +00:00
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (con->request.http_if_modified_since) {
|
|
|
|
size_t used_len;
|
|
|
|
char *semicolon;
|
2006-09-07 10:19:29 +00:00
|
|
|
|
2005-09-02 10:45:45 +00:00
|
|
|
if (NULL == (semicolon = strchr(con->request.http_if_modified_since, ';'))) {
|
|
|
|
used_len = strlen(con->request.http_if_modified_since);
|
|
|
|
} else {
|
|
|
|
used_len = semicolon - con->request.http_if_modified_since;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-02 10:45:45 +00:00
|
|
|
if (0 == strncmp(con->request.http_if_modified_since, mtime->ptr, used_len)) {
|
2006-09-07 10:19:29 +00:00
|
|
|
con->http_status = 304;
|
|
|
|
return HANDLER_FINISHED;
|
|
|
|
} else {
|
|
|
|
char buf[sizeof("Sat, 23 Jul 2005 21:20:01 GMT")];
|
|
|
|
time_t t_header, t_file;
|
|
|
|
struct tm tm;
|
|
|
|
|
|
|
|
/* convert to timestamp */
|
|
|
|
if (used_len >= sizeof(buf)) return HANDLER_GO_ON;
|
|
|
|
|
|
|
|
strncpy(buf, con->request.http_if_modified_since, used_len);
|
|
|
|
buf[used_len] = '\0';
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2007-08-17 21:46:36 +00:00
|
|
|
if (NULL == strptime(buf, "%a, %d %b %Y %H:%M:%S GMT", &tm)) {
|
|
|
|
/**
|
|
|
|
* parsing failed, let's get out of here
|
|
|
|
*/
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss",
|
|
|
|
"strptime() failed on", buf);
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
2006-09-07 10:19:29 +00:00
|
|
|
t_header = mktime(&tm);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-09-07 10:19:29 +00:00
|
|
|
strptime(mtime->ptr, "%a, %d %b %Y %H:%M:%S GMT", &tm);
|
|
|
|
t_file = mktime(&tm);
|
|
|
|
|
|
|
|
if (t_file > t_header) return HANDLER_GO_ON;
|
|
|
|
|
2005-09-02 10:45:45 +00:00
|
|
|
con->http_status = 304;
|
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|