Browse Source

[core] abstraction layer for HTTP header manip

http_header.[ch]
convert existing calls to manip request/response headers
convert existing calls to manip environment array (often header-related)
personal/stbuehler/fix-fdevent
Glenn Strauss 3 years ago
parent
commit
3dd3cde902
  1. 4
      src/CMakeLists.txt
  2. 8
      src/Makefile.am
  3. 2
      src/SConscript
  4. 17
      src/base.h
  5. 12
      src/configfile-glue.c
  6. 9
      src/connections-glue.c
  7. 62
      src/connections.c
  8. 255
      src/http-header-glue.c
  9. 7
      src/http_auth.c
  10. 2
      src/http_auth.h
  11. 166
      src/http_header.c
  12. 57
      src/http_header.h
  13. 3
      src/meson.build
  14. 23
      src/mod_accesslog.c
  15. 39
      src/mod_auth.c
  16. 1
      src/mod_authn_file.c
  17. 37
      src/mod_authn_gssapi.c
  18. 14
      src/mod_cgi.c
  19. 1
      src/mod_cml.c
  20. 1
      src/mod_cml_funcs.c
  21. 29
      src/mod_cml_lua.c
  22. 37
      src/mod_compress.c
  23. 94
      src/mod_deflate.c
  24. 6
      src/mod_dirlisting.c
  25. 4
      src/mod_evasive.c
  26. 1
      src/mod_evhost.c
  27. 32
      src/mod_expire.c
  28. 54
      src/mod_extforward.c
  29. 4
      src/mod_flv_streaming.c
  30. 164
      src/mod_geoip.c
  31. 3
      src/mod_indexfile.c
  32. 49
      src/mod_magnet.c
  33. 38
      src/mod_openssl.c
  34. 217
      src/mod_proxy.c
  35. 7
      src/mod_redirect.c
  36. 20
      src/mod_setenv.c
  37. 23
      src/mod_ssi.c
  38. 13
      src/mod_status.c
  39. 14
      src/mod_trigger_b4_dl.c
  40. 16
      src/mod_uploadprogress.c
  41. 12
      src/mod_usertrack.c
  42. 70
      src/mod_webdav.c
  43. 127
      src/mod_wstunnel.c
  44. 302
      src/request.c
  45. 71
      src/response.c
  46. 4
      src/response.h
  47. 4
      src/t/test_request.c

4
src/CMakeLists.txt

@ -554,7 +554,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
set(COMMON_SRC
base64.c buffer.c burl.c log.c
http_kv.c keyvalue.c chunk.c
http_header.c http_kv.c keyvalue.c chunk.c
http_chunk.c stream.c fdevent.c gw_backend.c
stat_cache.c plugin.c joblist.c etag.c array.c
data_string.c data_array.c
@ -671,6 +671,7 @@ add_executable(test_configfile
array.c
data_config.c
data_string.c
http_header.c
http_kv.c
vector.c
log.c
@ -684,6 +685,7 @@ add_executable(test_request
buffer.c
array.c
data_string.c
http_header.c
http_kv.c
log.c
sock_addr.c

8
src/Makefile.am

@ -64,7 +64,7 @@ MAINTAINERCLEANFILES = configparser.c configparser.h mod_ssi_exprparser.c mod_ss
CLEANFILES = versionstamp.h versionstamp.h.tmp lemon$(BUILD_EXEEXT)
common_src=base64.c buffer.c burl.c log.c \
http_kv.c keyvalue.c chunk.c \
http_header.c http_kv.c keyvalue.c chunk.c \
http_chunk.c stream.c fdevent.c gw_backend.c \
stat_cache.c plugin.c joblist.c etag.c array.c \
data_string.c data_array.c \
@ -399,7 +399,7 @@ mod_wstunnel_la_LIBADD = $(common_libadd) $(CRYPTO_LIB)
hdr = server.h base64.h buffer.h burl.h network.h log.h http_kv.h keyvalue.h \
response.h request.h fastcgi.h chunk.h \
first.h settings.h http_chunk.h \
algo_sha1.h md5.h http_auth.h http_vhostdb.h stream.h \
algo_sha1.h md5.h http_auth.h http_header.h http_vhostdb.h stream.h \
fdevent.h gw_backend.h connections.h base.h base_decls.h stat_cache.h \
plugin.h \
etag.h joblist.h array.h vector.h crc32.h \
@ -532,10 +532,10 @@ t_test_base64_LDADD = $(LIBUNWIND_LIBS)
t_test_burl_SOURCES = t/test_burl.c burl.c buffer.c base64.c
t_test_burl_LDADD = $(LIBUNWIND_LIBS)
t_test_configfile_SOURCES = t/test_configfile.c buffer.c array.c data_config.c data_string.c http_kv.c vector.c log.c sock_addr.c
t_test_configfile_SOURCES = t/test_configfile.c buffer.c array.c data_config.c data_string.c http_header.c http_kv.c vector.c log.c sock_addr.c
t_test_configfile_LDADD = $(PCRE_LIB) $(LIBUNWIND_LIBS)
t_test_request_SOURCES = t/test_request.c request.c buffer.c array.c data_string.c http_kv.c log.c sock_addr.c
t_test_request_SOURCES = t/test_request.c request.c buffer.c array.c data_string.c http_header.c http_kv.c log.c sock_addr.c
t_test_request_LDADD = $(LIBUNWIND_LIBS)
noinst_HEADERS = $(hdr)

2
src/SConscript

@ -56,7 +56,7 @@ def GatherLibs(env, *libs):
return WorkaroundFreeBSDLibOrder(libs)
common_src = Split("base64.c buffer.c burl.c log.c \
http_kv.c keyvalue.c chunk.c \
http_header.c http_kv.c keyvalue.c chunk.c \
http_chunk.c stream.c fdevent.c gw_backend.c \
stat_cache.c plugin.c joblist.c etag.c array.c \
data_string.c data_array.c \

17
src/base.h

@ -21,17 +21,6 @@ struct stat_cache; /* declaration */
#define DIRECT 0 /* con->mode */
/* fcgi_response_header contains ... */
#define HTTP_STATUS BV(0)
#define HTTP_CONNECTION BV(1)
#define HTTP_CONTENT_LENGTH BV(2)
#define HTTP_DATE BV(3)
#define HTTP_LOCATION BV(4)
#define HTTP_TRANSFER_ENCODING BV(5)
#define HTTP_CONTENT_LOCATION BV(6)
#define HTTP_SET_COOKIE BV(7)
#define HTTP_UPGRADE BV(8)
typedef struct {
/** HEADER */
/* the request-line */
@ -47,10 +36,8 @@ typedef struct {
/* strings to the header */
buffer *http_host; /* not alloced */
const char *http_content_type;
const char *http_if_modified_since;
const char *http_if_none_match;
unsigned int htags; /* bitfield of flagged headers present in request */
array *headers;
/* CONTENT */
@ -65,6 +52,7 @@ typedef struct {
off_t content_length;
int keep_alive; /* used by the subrequests in proxy, cgi and fcgi to say the subrequest was keep-alive or not */
unsigned int htags; /* bitfield of flagged headers present in response */
array *headers;
enum {
@ -263,7 +251,6 @@ struct connection {
/* request */
buffer *parse_request;
unsigned int parsed_response; /* bitfield which contains the important header-fields of the parsed response header */
request request;
request_uri uri;

12
src/configfile-glue.c

@ -5,6 +5,7 @@
#include "array.h"
#include "log.h"
#include "fdevent.h"
#include "http_header.h"
#include "sock_addr.h"
#include "configfile.h"
@ -432,15 +433,10 @@ static cond_result_t config_check_cond_nocache(server *srv, connection *con, dat
l = srv_sock->srv_token;
break;
case COMP_HTTP_REQUEST_HEADER: {
data_string *ds;
if (NULL != (ds = (data_string *)array_get_element_klen(con->request.headers, CONST_BUF_LEN(dc->comp_tag)))) {
l = ds->value;
} else {
l = srv->empty_string;
}
case COMP_HTTP_REQUEST_HEADER:
l = http_header_request_get(con, HTTP_HEADER_UNSPECIFIED, CONST_BUF_LEN(dc->comp_tag));
if (NULL == l) l = srv->empty_string;
break;
}
case COMP_HTTP_REQUEST_METHOD: {
const char *method = get_http_method_name(con->request.http_method);

9
src/connections-glue.c

@ -4,6 +4,7 @@
#include "base.h"
#include "connections.h"
#include "fdevent.h"
#include "http_header.h"
#include "log.h"
#include <errno.h>
@ -435,9 +436,9 @@ handler_t connection_handle_read_post_state(server *srv, connection *con) {
if (chunkqueue_is_empty(cq) && 0 == dst_cq->bytes_in
&& con->request.http_version != HTTP_VERSION_1_0
&& chunkqueue_is_empty(con->write_queue) && con->is_writable) {
data_string *ds = (data_string *)array_get_element(con->request.headers, "Expect");
if (NULL != ds && 0 == buffer_caseless_compare(CONST_BUF_LEN(ds->value), CONST_STR_LEN("100-continue"))) {
buffer_reset(ds->value); /* unset value in request headers */
buffer *vb = http_header_request_get(con, HTTP_HEADER_EXPECT, CONST_STR_LEN("Expect"));
if (NULL != vb && 0 == buffer_caseless_compare(CONST_BUF_LEN(vb), CONST_STR_LEN("100-continue"))) {
buffer_reset(vb); /* unset value in request headers */
if (!connection_write_100_continue(srv, con)) {
return HANDLER_ERROR;
}
@ -490,7 +491,7 @@ void connection_response_reset(server *srv, connection *con) {
con->is_writable = 1;
con->file_finished = 0;
con->file_started = 0;
con->parsed_response = 0;
con->response.htags = 0;
con->response.keep_alive = 0;
con->response.content_length = -1;
con->response.transfer_encoding = 0;

62
src/connections.c

@ -5,6 +5,7 @@
#include "log.h"
#include "connections.h"
#include "fdevent.h"
#include "http_header.h"
#include "configfile.h"
#include "request.h"
@ -232,16 +233,14 @@ static void connection_handle_response_end_state(server *srv, connection *con) {
}
}
static void connection_handle_errdoc_init(server *srv, connection *con) {
static void connection_handle_errdoc_init(connection *con) {
/* modules that produce headers required with error response should
* typically also produce an error document. Make an exception for
* mod_auth WWW-Authenticate response header. */
buffer *www_auth = NULL;
if (401 == con->http_status) {
data_string *ds = (data_string *)array_get_element(con->response.headers, "WWW-Authenticate");
if (NULL != ds) {
www_auth = buffer_init_buffer(ds->value);
}
buffer *vb = http_header_response_get(con, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"));
if (NULL != vb) www_auth = buffer_init_buffer(vb);
}
con->response.transfer_encoding = 0;
@ -250,7 +249,7 @@ static void connection_handle_errdoc_init(server *srv, connection *con) {
chunkqueue_reset(con->write_queue);
if (NULL != www_auth) {
response_header_insert(srv, con, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(www_auth));
http_header_response_set(con, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(www_auth));
buffer_free(www_auth);
}
}
@ -271,10 +270,12 @@ static int connection_handle_write_prepare(server *srv, connection *con) {
* */
if ((!con->http_status || con->http_status == 200) && !buffer_string_is_empty(con->uri.path) &&
con->uri.path->ptr[0] != '*') {
response_header_insert(srv, con, CONST_STR_LEN("Allow"), CONST_STR_LEN("OPTIONS, GET, HEAD, POST"));
http_header_response_append(con, HTTP_HEADER_OTHER, CONST_STR_LEN("Allow"), CONST_STR_LEN("OPTIONS, GET, HEAD, POST"));
con->response.transfer_encoding &= ~HTTP_TRANSFER_ENCODING_CHUNKED;
con->parsed_response &= ~HTTP_CONTENT_LENGTH;
if (con->response.htags & HTTP_HEADER_CONTENT_LENGTH) {
http_header_response_set(con, HTTP_HEADER_CONTENT_LENGTH, CONST_STR_LEN("Content-Length"), CONST_STR_LEN(""));
}
con->http_status = 200;
con->file_finished = 1;
@ -300,7 +301,9 @@ static int connection_handle_write_prepare(server *srv, connection *con) {
case 304:
/* disable chunked encoding again as we have no body */
con->response.transfer_encoding &= ~HTTP_TRANSFER_ENCODING_CHUNKED;
con->parsed_response &= ~HTTP_CONTENT_LENGTH;
if (con->response.htags & HTTP_HEADER_CONTENT_LENGTH) {
http_header_response_set(con, HTTP_HEADER_CONTENT_LENGTH, CONST_STR_LEN("Content-Length"), CONST_STR_LEN(""));
}
chunkqueue_reset(con->write_queue);
con->file_finished = 1;
@ -313,7 +316,7 @@ static int connection_handle_write_prepare(server *srv, connection *con) {
con->file_finished = 0;
connection_handle_errdoc_init(srv, con);
connection_handle_errdoc_init(con);
/* try to send static errorfile */
if (!buffer_string_is_empty(con->conf.errorfile_prefix)) {
@ -327,7 +330,7 @@ static int connection_handle_write_prepare(server *srv, connection *con) {
con->file_finished = 1;
if (HANDLER_ERROR != stat_cache_get_entry(srv, con, con->physical.path, &sce)) {
stat_cache_content_type_get(srv, con, con->physical.path, sce);
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce->content_type));
http_header_response_set(con, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce->content_type));
}
}
}
@ -369,7 +372,7 @@ static int connection_handle_write_prepare(server *srv, connection *con) {
(void)http_chunk_append_buffer(srv, con, b);
buffer_free(b);
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html"));
http_header_response_set(con, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html"));
}
break;
}
@ -387,7 +390,7 @@ static int connection_handle_write_prepare(server *srv, connection *con) {
if (con->file_finished) {
/* we have all the content and chunked encoding is not used, set a content-length */
if (!(con->parsed_response & (HTTP_CONTENT_LENGTH|HTTP_TRANSFER_ENCODING))) {
if (!(con->response.htags & (HTTP_HEADER_CONTENT_LENGTH|HTTP_HEADER_TRANSFER_ENCODING))) {
off_t qlen = chunkqueue_length(con->write_queue);
/**
@ -401,18 +404,15 @@ static int connection_handle_write_prepare(server *srv, connection *con) {
if ((con->http_status >= 100 && con->http_status < 200) ||
con->http_status == 204 ||
con->http_status == 304) {
data_string *ds;
/* no Content-Body, no Content-Length */
if (NULL != (ds = (data_string*) array_get_element(con->response.headers, "Content-Length"))) {
buffer_reset(ds->value); /* Headers with empty values are ignored for output */
}
buffer *vb = http_header_response_get(con, HTTP_HEADER_CONTENT_LENGTH, CONST_STR_LEN("Content-Length"));
if (NULL != vb) buffer_reset(vb); /* Headers with empty values are ignored for output */
} else if (qlen > 0 || con->request.http_method != HTTP_METHOD_HEAD) {
/* qlen = 0 is important for Redirects (301, ...) as they MAY have
* a content. Browsers are waiting for a Content otherwise
*/
buffer_copy_int(srv->tmp_buf, qlen);
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Length"), CONST_BUF_LEN(srv->tmp_buf));
http_header_response_set(con, HTTP_HEADER_CONTENT_LENGTH, CONST_STR_LEN("Content-Length"), CONST_BUF_LEN(srv->tmp_buf));
}
}
} else {
@ -425,7 +425,7 @@ static int connection_handle_write_prepare(server *srv, connection *con) {
* - Upgrade: ... (lighttpd then acts as transparent proxy)
*/
if (!(con->parsed_response & (HTTP_CONTENT_LENGTH|HTTP_TRANSFER_ENCODING|HTTP_UPGRADE))) {
if (!(con->response.htags & (HTTP_HEADER_CONTENT_LENGTH|HTTP_HEADER_TRANSFER_ENCODING|HTTP_HEADER_UPGRADE))) {
if (con->request.http_method == HTTP_METHOD_CONNECT
&& con->http_status == 200) {
/*(no transfer-encoding if successful CONNECT)*/
@ -441,7 +441,7 @@ static int connection_handle_write_prepare(server *srv, connection *con) {
chunkqueue_prepend_buffer(con->write_queue, b);
chunkqueue_append_mem(con->write_queue, CONST_STR_LEN("\r\n"));
}
response_header_append(srv, con, CONST_STR_LEN("Transfer-Encoding"), CONST_STR_LEN("chunked"));
http_header_response_append(con, HTTP_HEADER_TRANSFER_ENCODING, CONST_STR_LEN("Transfer-Encoding"), CONST_STR_LEN("chunked"));
} else {
con->keep_alive = 0;
}
@ -456,7 +456,7 @@ static int connection_handle_write_prepare(server *srv, connection *con) {
*
* FIXME: to be nice we should remove the Connection: ...
*/
if (con->parsed_response & HTTP_CONNECTION) {
if (con->response.htags & HTTP_HEADER_CONNECTION) {
/* a subrequest disable keep-alive although the client wanted it */
if (con->keep_alive && !con->response.keep_alive) {
con->keep_alive = 0;
@ -473,11 +473,8 @@ static int connection_handle_write_prepare(server *srv, connection *con) {
chunkqueue_reset(con->write_queue);
con->response.transfer_encoding &= ~HTTP_TRANSFER_ENCODING_CHUNKED;
if (con->parsed_response & HTTP_TRANSFER_ENCODING) {
data_string *ds;
if (NULL != (ds = (data_string*) array_get_element(con->response.headers, "Transfer-Encoding"))) {
buffer_reset(ds->value); /* Headers with empty values are ignored for output */
}
if (con->response.htags & HTTP_HEADER_TRANSFER_ENCODING) {
http_header_response_set(con, HTTP_HEADER_TRANSFER_ENCODING, CONST_STR_LEN("Transfer-Encoding"), CONST_STR_LEN(""));
}
}
@ -651,9 +648,6 @@ int connection_reset(server *srv, connection *con) {
con->request.http_method = HTTP_METHOD_UNSET;
con->request.http_version = HTTP_VERSION_UNSET;
con->request.http_if_modified_since = NULL;
con->request.http_if_none_match = NULL;
#define CLEAN(x) \
if (con->x) buffer_reset(con->x);
@ -685,10 +679,10 @@ int connection_reset(server *srv, connection *con) {
con->request.x = NULL;
CLEAN(http_host);
CLEAN(http_content_type);
#undef CLEAN
con->request.content_length = 0;
con->request.te_chunked = 0;
con->request.htags = 0;
array_reset(con->request.headers);
array_reset(con->environment);
@ -1230,7 +1224,7 @@ int connection_state_machine(server *srv, connection *con) {
* for access by dynamic handlers
* https://redmine.lighttpd.net/issues/1828 */
buffer_copy_int(srv->tmp_buf, con->http_status);
array_insert_key_value(con->environment, CONST_STR_LEN("REDIRECT_STATUS"), CONST_BUF_LEN(srv->tmp_buf));
http_header_env_set(con, CONST_STR_LEN("REDIRECT_STATUS"), CONST_BUF_LEN(srv->tmp_buf));
if (error_handler == con->conf.error_handler) {
plugins_call_connection_reset(srv, con);
@ -1246,7 +1240,7 @@ int connection_state_machine(server *srv, connection *con) {
con->is_writable = 1;
con->file_finished = 0;
con->file_started = 0;
con->parsed_response = 0;
con->response.htags = 0;
con->response.keep_alive = 0;
con->response.content_length = -1;
con->response.transfer_encoding = 0;
@ -1260,7 +1254,7 @@ int connection_state_machine(server *srv, connection *con) {
}
buffer_copy_buffer(con->request.uri, error_handler);
connection_handle_errdoc_init(srv, con);
connection_handle_errdoc_init(con);
con->http_status = 0; /*(after connection_handle_errdoc_init())*/
done = -1;

255
src/http-header-glue.c

@ -7,6 +7,7 @@
#include "log.h"
#include "etag.h"
#include "http_chunk.h"
#include "http_header.h"
#include "response.h"
#include "sock_addr.h"
#include "stat_cache.h"
@ -22,56 +23,6 @@
#include <unistd.h>
int response_header_insert(server *srv, connection *con, const char *key, size_t keylen, const char *value, size_t vallen) {
data_string *ds;
UNUSED(srv);
if (0 == vallen) return 0;
/* if there already is a key by this name append the value */
if (NULL != (ds = (data_string *)array_get_element_klen(con->response.headers, key, keylen))) {
buffer_append_string_len(ds->value, CONST_STR_LEN("\r\n"));
buffer_append_string_len(ds->value, key, keylen);
buffer_append_string_len(ds->value, CONST_STR_LEN(": "));
buffer_append_string_len(ds->value, value, vallen);
return 0;
}
array_set_key_value(con->response.headers, key, keylen, value, vallen);
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;
UNUSED(srv);
/* if there already is a key by this name overwrite the value */
if (NULL != (ds = (data_string *)array_get_element_klen(con->response.headers, key, keylen))) {
buffer_copy_string_len(ds->value, value, vallen);
return 0;
}
return response_header_insert(srv, con, key, keylen, value, vallen);
}
int response_header_append(server *srv, connection *con, const char *key, size_t keylen, const char *value, size_t vallen) {
data_string *ds;
UNUSED(srv);
/* if there already is a key by this name append the value */
if (NULL != (ds = (data_string *)array_get_element_klen(con->response.headers, key, keylen))) {
buffer_append_string_len(ds->value, CONST_STR_LEN(", "));
buffer_append_string_len(ds->value, value, vallen);
return 0;
}
return response_header_insert(srv, con, key, keylen, value, vallen);
}
int http_response_redirect_to_directory(server *srv, connection *con) {
buffer *o;
@ -122,7 +73,7 @@ int http_response_redirect_to_directory(server *srv, connection *con) {
buffer_append_string_buffer(o, con->uri.query);
}
response_header_insert(srv, con, CONST_STR_LEN("Location"), CONST_BUF_LEN(o));
http_header_response_set(con, HTTP_HEADER_LOCATION, CONST_STR_LEN("Location"), CONST_BUF_LEN(o));
con->http_status = 301;
con->file_finished = 1;
@ -155,6 +106,7 @@ buffer * strftime_cache_get(server *srv, time_t last_mod) {
int http_response_handle_cachable(server *srv, connection *con, buffer *mtime) {
buffer *vb;
int head_or_get =
( HTTP_METHOD_GET == con->request.http_method
|| HTTP_METHOD_HEAD == con->request.http_method);
@ -170,11 +122,11 @@ int http_response_handle_cachable(server *srv, connection *con, buffer *mtime) {
* return a 304 (Not Modified) response.
*/
if (con->request.http_if_none_match) {
if ((vb = http_header_request_get(con, HTTP_HEADER_IF_NONE_MATCH, CONST_STR_LEN("If-None-Match")))) {
/* use strong etag checking for now: weak comparison must not be used
* for ranged requests
*/
if (etag_is_equal(con->physical.etag, con->request.http_if_none_match, 0)) {
if (etag_is_equal(con->physical.etag, vb->ptr, 0)) {
if (head_or_get) {
con->http_status = 304;
return HANDLER_FINISHED;
@ -184,18 +136,19 @@ int http_response_handle_cachable(server *srv, connection *con, buffer *mtime) {
return HANDLER_FINISHED;
}
}
} else if (con->request.http_if_modified_since && head_or_get) {
} else if (head_or_get
&& (vb = http_header_request_get(con, HTTP_HEADER_IF_MODIFIED_SINCE, CONST_STR_LEN("If-Modified-Since")))) {
/* last-modified handling */
size_t used_len;
char *semicolon;
if (NULL == (semicolon = strchr(con->request.http_if_modified_since, ';'))) {
used_len = strlen(con->request.http_if_modified_since);
if (NULL == (semicolon = strchr(vb->ptr, ';'))) {
used_len = buffer_string_length(vb);
} else {
used_len = semicolon - con->request.http_if_modified_since;
used_len = semicolon - vb->ptr;
}
if (0 == strncmp(con->request.http_if_modified_since, mtime->ptr, used_len)) {
if (buffer_is_equal_string(mtime, vb->ptr, used_len)) {
if ('\0' == mtime->ptr[used_len]) con->http_status = 304;
return HANDLER_FINISHED;
} else {
@ -206,7 +159,7 @@ int http_response_handle_cachable(server *srv, connection *con, buffer *mtime) {
/* convert to timestamp */
if (used_len >= sizeof(buf)) return HANDLER_GO_ON;
strncpy(buf, con->request.http_if_modified_since, used_len);
memcpy(buf, vb->ptr, used_len);
buf[used_len] = '\0';
if (NULL == strptime(buf, "%a, %d %b %Y %H:%M:%S GMT", &tm)) {
@ -239,18 +192,13 @@ static int http_response_parse_range(server *srv, connection *con, buffer *path,
off_t start, end;
const char *s, *minus;
char *boundary = "fkj49sn38dcn3";
data_string *ds;
buffer *content_type = NULL;
buffer *content_type = http_header_response_get(con, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"));
start = 0;
end = sce->st.st_size - 1;
con->response.content_length = 0;
if (NULL != (ds = (data_string *)array_get_element(con->response.headers, "Content-Type"))) {
content_type = ds->value;
}
for (s = range, error = 0;
!error && *s && NULL != (minus = strchr(s, '-')); ) {
char *err;
@ -375,8 +323,10 @@ static int http_response_parse_range(server *srv, connection *con, buffer *path,
buffer_append_string_len(b, CONST_STR_LEN("/"));
buffer_append_int(b, sce->st.st_size);
buffer_append_string_len(b, CONST_STR_LEN("\r\nContent-Type: "));
buffer_append_string_buffer(b, content_type);
if (content_type) {
buffer_append_string_len(b, CONST_STR_LEN("\r\nContent-Type: "));
buffer_append_string_buffer(b, content_type);
}
/* write END-OF-HEADER */
buffer_append_string_len(b, CONST_STR_LEN("\r\n\r\n"));
@ -412,7 +362,7 @@ static int http_response_parse_range(server *srv, connection *con, buffer *path,
buffer_append_string(srv->tmp_buf, boundary);
/* overwrite content-type */
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(srv->tmp_buf));
http_header_response_set(con, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(srv->tmp_buf));
} else {
/* add Content-Range-header */
@ -423,7 +373,7 @@ static int http_response_parse_range(server *srv, connection *con, buffer *path,
buffer_append_string_len(srv->tmp_buf, CONST_STR_LEN("/"));
buffer_append_int(srv->tmp_buf, sce->st.st_size);
response_header_insert(srv, con, CONST_STR_LEN("Content-Range"), CONST_BUF_LEN(srv->tmp_buf));
http_header_response_set(con, HTTP_HEADER_OTHER, CONST_STR_LEN("Content-Range"), CONST_BUF_LEN(srv->tmp_buf));
}
/* ok, the file is set-up */
@ -434,7 +384,7 @@ static int http_response_parse_range(server *srv, connection *con, buffer *path,
void http_response_send_file (server *srv, connection *con, buffer *path) {
stat_cache_entry *sce = NULL;
buffer *mtime = NULL;
data_string *ds;
buffer *vb;
int allow_caching = (0 == con->http_status || 200 == con->http_status);
if (HANDLER_ERROR == stat_cache_get_entry(srv, con, path, &sce)) {
@ -476,7 +426,7 @@ void http_response_send_file (server *srv, connection *con, buffer *path) {
/* set response content-type, if not set already */
if (NULL == array_get_element(con->response.headers, "Content-Type")) {
if (NULL == http_header_response_get(con, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"))) {
stat_cache_content_type_get(srv, con, path, sce);
if (buffer_string_is_empty(sce->content_type)) {
/* we are setting application/octet-stream, but also announce that
@ -485,34 +435,32 @@ void http_response_send_file (server *srv, connection *con, buffer *path) {
* This should fix the aggressive caching of FF and the script download
* seen by the first installations
*/
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("application/octet-stream"));
http_header_response_set(con, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("application/octet-stream"));
allow_caching = 0;
} else {
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce->content_type));
http_header_response_set(con, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce->content_type));
}
}
if (con->conf.range_requests) {
response_header_overwrite(srv, con, CONST_STR_LEN("Accept-Ranges"), CONST_STR_LEN("bytes"));
http_header_response_append(con, HTTP_HEADER_OTHER, CONST_STR_LEN("Accept-Ranges"), CONST_STR_LEN("bytes"));
}
if (allow_caching) {
if (con->etag_flags != 0 && !buffer_string_is_empty(stat_cache_etag_get(sce, con->etag_flags))) {
if (NULL == array_get_element(con->response.headers, "ETag")) {
if (NULL == http_header_response_get(con, HTTP_HEADER_ETAG, CONST_STR_LEN("ETag"))) {
/* generate e-tag */
etag_mutate(con->physical.etag, sce->etag);
response_header_overwrite(srv, con, CONST_STR_LEN("ETag"), CONST_BUF_LEN(con->physical.etag));
http_header_response_set(con, HTTP_HEADER_ETAG, CONST_STR_LEN("ETag"), CONST_BUF_LEN(con->physical.etag));
}
}
/* prepare header */
if (NULL == (ds = (data_string *)array_get_element(con->response.headers, "Last-Modified"))) {
if (NULL == (mtime = http_header_response_get(con, HTTP_HEADER_LAST_MODIFIED, CONST_STR_LEN("Last-Modified")))) {
mtime = strftime_cache_get(srv, sce->st.st_mtime);
response_header_overwrite(srv, con, CONST_STR_LEN("Last-Modified"), CONST_BUF_LEN(mtime));
} else {
mtime = ds->value;
http_header_response_set(con, HTTP_HEADER_LAST_MODIFIED, CONST_STR_LEN("Last-Modified"), CONST_BUF_LEN(mtime));
}
if (HANDLER_FINISHED == http_response_handle_cachable(srv, con, mtime)) {
@ -522,23 +470,23 @@ void http_response_send_file (server *srv, connection *con, buffer *path) {
if (con->conf.range_requests
&& (200 == con->http_status || 0 == con->http_status)
&& NULL != (ds = (data_string *)array_get_element(con->request.headers, "Range"))
&& NULL == array_get_element(con->response.headers, "Content-Encoding")) {
buffer *range = ds->value;
&& NULL != (vb = http_header_request_get(con, HTTP_HEADER_RANGE, CONST_STR_LEN("Range")))
&& NULL == http_header_response_get(con, HTTP_HEADER_CONTENT_ENCODING, CONST_STR_LEN("Content-Encoding"))) {
buffer *range = vb;
int do_range_request = 1;
/* check if we have a conditional GET */
if (NULL != (ds = (data_string *)array_get_element(con->request.headers, "If-Range"))) {
if (NULL != (vb = http_header_request_get(con, HTTP_HEADER_OTHER, CONST_STR_LEN("If-Range")))) {
/* if the value is the same as our ETag, we do a Range-request,
* otherwise a full 200 */
if (ds->value->ptr[0] == '"') {
if (vb->ptr[0] == '"') {
/**
* client wants a ETag
*/
if (!con->physical.etag) {
do_range_request = 0;
} else if (!buffer_is_equal(ds->value, con->physical.etag)) {
} else if (!buffer_is_equal(vb, con->physical.etag)) {
do_range_request = 0;
}
} else if (!mtime) {
@ -548,7 +496,7 @@ void http_response_send_file (server *srv, connection *con, buffer *path) {
* sending all
*/
do_range_request = 0;
} else if (!buffer_is_equal(ds->value, mtime)) {
} else if (!buffer_is_equal(vb, mtime)) {
do_range_request = 0;
}
}
@ -589,10 +537,8 @@ static void http_response_xsendfile (server *srv, connection *con, buffer *path,
* Content-Length might later be set to size of X-Sendfile static file,
* determined by open(), fstat() to reduces race conditions if the file
* is modified between stat() (stat_cache_get_entry()) and open(). */
if (con->parsed_response & HTTP_CONTENT_LENGTH) {
data_string *ds = (data_string *) array_get_element(con->response.headers, "Content-Length");
if (ds) buffer_reset(ds->value);
con->parsed_response &= ~HTTP_CONTENT_LENGTH;
if (con->response.htags & HTTP_HEADER_CONTENT_LENGTH) {
http_header_response_set(con, HTTP_HEADER_CONTENT_LENGTH, CONST_STR_LEN("Content-Length"), CONST_STR_LEN(""));
con->response.content_length = -1;
}
@ -643,11 +589,8 @@ static void http_response_xsendfile2(server *srv, connection *con, const buffer
const int status = con->http_status;
/* reset Content-Length, if set by backend */
if (con->parsed_response & HTTP_CONTENT_LENGTH) {
data_string *ds = (data_string *)
array_get_element(con->response.headers, "Content-Length");
if (ds) buffer_reset(ds->value);
con->parsed_response &= ~HTTP_CONTENT_LENGTH;
if (con->response.htags & HTTP_HEADER_CONTENT_LENGTH) {
http_header_response_set(con, HTTP_HEADER_CONTENT_LENGTH, CONST_STR_LEN("Content-Length"), CONST_STR_LEN(""));
con->response.content_length = -1;
}
@ -849,16 +792,15 @@ static handler_t http_response_process_local_redir(server *srv, connection *con,
/* con->http_status >= 300 && con->http_status < 400) */
size_t ulen = buffer_string_length(con->uri.path);
data_string *ds = (data_string *)
array_get_element(con->response.headers, "Location");
if (NULL != ds
&& ds->value->ptr[0] == '/'
&& (0 != strncmp(ds->value->ptr, con->uri.path->ptr, ulen)
|| (ds->value->ptr[ulen] != '\0'
&& ds->value->ptr[ulen] != '/'
&& ds->value->ptr[ulen] != '?'))
buffer *vb = http_header_response_get(con, HTTP_HEADER_LOCATION, CONST_STR_LEN("Location"));
if (NULL != vb
&& vb->ptr[0] == '/'
&& (0 != strncmp(vb->ptr, con->uri.path->ptr, ulen)
|| ( vb->ptr[ulen] != '\0'
&& vb->ptr[ulen] != '/'
&& vb->ptr[ulen] != '?'))
&& 0 == blen
&& !(con->parsed_response & HTTP_STATUS) /*no "Status" or NPH response*/
&& !(con->response.htags & HTTP_HEADER_STATUS) /*no "Status" or NPH response*/
&& 1 == con->response.headers->used) {
if (++con->loops_per_request > 5) {
log_error_write(srv, __FILE__, __LINE__, "sb",
@ -869,7 +811,7 @@ static handler_t http_response_process_local_redir(server *srv, connection *con,
return HANDLER_FINISHED;
}
buffer_copy_buffer(con->request.uri, ds->value);
buffer_copy_buffer(con->request.uri, vb);
if (con->request.content_length) {
if (con->request.content_length
@ -901,10 +843,12 @@ static int http_response_process_headers(server *srv, connection *con, http_resp
char *ns;
const char *s;
int line = 0;
int status_is_set = 0;
for (s = hdrs->ptr; NULL != (ns = strchr(s, '\n')); s = ns + 1, ++line) {
const char *key, *value;
int key_len;
enum http_header_e id;
/* strip the \n */
ns[0] = '\0';
@ -916,7 +860,8 @@ static int http_response_process_headers(server *srv, connection *con, http_resp
/* after the space should be a status code for us */
int status = strtol(s+9, NULL, 10);
if (status >= 100 && status < 1000) {
con->parsed_response |= HTTP_STATUS;
status_is_set = 1;
con->response.htags |= HTTP_HEADER_STATUS;
con->http_status = status;
} /* else we expected 3 digits and didn't get them */
}
@ -941,10 +886,11 @@ static int http_response_process_headers(server *srv, connection *con, http_resp
key_len = value - key;
do { ++value; } while (*value == ' ' || *value == '\t'); /* skip LWS */
id = http_header_hkey_get(key, key_len);
if (opts->authorizer) {
if (0 == con->http_status || 200 == con->http_status) {
if (key_len == 6 && 0 == strncasecmp(key, "Status", key_len)) {
if (id == HTTP_HEADER_STATUS) {
int status = strtol(value, NULL, 10);
if (status >= 100 && status < 1000) {
con->http_status = status;
@ -952,28 +898,22 @@ static int http_response_process_headers(server *srv, connection *con, http_resp
con->http_status = 502; /* Bad Gateway */
break;
}
} else if (key_len > 9
} else if (id == HTTP_HEADER_OTHER && key_len > 9
&& 0==strncasecmp(key, CONST_STR_LEN("Variable-"))) {
array_insert_key_value(con->environment, key + 9, key_len - 9, value, strlen(value));
http_header_env_append(con, key + 9, key_len - 9, value, strlen(value));
}
continue;
}
}
switch(key_len) {
case 4:
if (0 == strncasecmp(key, "Date", key_len)) {
con->parsed_response |= HTTP_DATE;
}
break;
case 6:
if (0 == strncasecmp(key, "Status", key_len)) {
switch (id) {
case HTTP_HEADER_STATUS:
{
int status;
if (opts->backend == BACKEND_PROXY) break; /*(pass w/o parse)*/
status = strtol(value, NULL, 10);
if (status >= 100 && status < 1000) {
con->http_status = status;
con->parsed_response |= HTTP_STATUS;
} else {
con->http_status = 502;
con->mode = DIRECT;
@ -981,60 +921,35 @@ static int http_response_process_headers(server *srv, connection *con, http_resp
continue; /* do not send Status to client */
}
break;
case 7:
if (0 == strncasecmp(key, "Upgrade", key_len)) {
/*(technically, should also verify Connection: upgrade)*/
/*(flag only for mod_proxy and mod_cgi (for now))*/
if (opts->backend == BACKEND_PROXY
|| opts->backend == BACKEND_CGI) {
con->parsed_response |= HTTP_UPGRADE;
}
case HTTP_HEADER_UPGRADE:
/*(technically, should also verify Connection: upgrade)*/
/*(flag only for mod_proxy and mod_cgi (for now))*/
if (opts->backend != BACKEND_PROXY
&& opts->backend != BACKEND_CGI) {
id = HTTP_HEADER_OTHER;
}
break;
case 8:
if (0 == strncasecmp(key, "Location", key_len)) {
con->parsed_response |= HTTP_LOCATION;
}
case HTTP_HEADER_CONNECTION:
if (opts->backend == BACKEND_PROXY) continue;
con->response.keep_alive =
(0 == strcasecmp(value, "Keep-Alive")) ? 1 : 0;
break;
case 10:
if (0 == strncasecmp(key, "Connection", key_len)) {
if (opts->backend == BACKEND_PROXY) continue;
con->response.keep_alive =
(0 == strcasecmp(value, "Keep-Alive")) ? 1 : 0;
con->parsed_response |= HTTP_CONNECTION;
}
else if (0 == strncasecmp(key, "Set-Cookie", key_len)) {
con->parsed_response |= HTTP_SET_COOKIE;
}
case HTTP_HEADER_CONTENT_LENGTH:
con->response.content_length = strtoul(value, NULL, 10);
break;
case 14:
if (0 == strncasecmp(key, "Content-Length", key_len)) {
con->response.content_length = strtoul(value, NULL, 10);
con->parsed_response |= HTTP_CONTENT_LENGTH;
}
break;
case 16:
if (0 == strncasecmp(key, "Content-Location", key_len)) {
con->parsed_response |= HTTP_CONTENT_LOCATION;
}
break;
case 17:
if (0 == strncasecmp(key, "Transfer-Encoding", key_len)) {
if (opts->backend == BACKEND_PROXY) continue;
con->parsed_response |= HTTP_TRANSFER_ENCODING;
}
case HTTP_HEADER_TRANSFER_ENCODING:
if (opts->backend == BACKEND_PROXY) continue;
break;
default:
default:
break;
}
response_header_insert(srv, con, key, key_len, value, strlen(value));
http_header_response_insert(con, id, key, key_len, value, strlen(value));
}
/* CGI/1.1 rev 03 - 7.2.1.2 */
/* (proxy requires Status-Line, so never true for proxy)*/
if ((con->parsed_response & HTTP_LOCATION) &&
!(con->parsed_response & HTTP_STATUS)) {
if (!status_is_set && (con->response.htags & HTTP_HEADER_LOCATION)) {
con->http_status = 302;
}
@ -1156,26 +1071,26 @@ handler_t http_response_parse_headers(server *srv, connection *con, http_respons
}
if (opts->local_redir && con->http_status >= 300 && con->http_status < 400){
/*(con->parsed_response & HTTP_LOCATION)*/
/*(con->response.htags & HTTP_HEADER_LOCATION)*/
handler_t rc = http_response_process_local_redir(srv, con, blen);
if (con->mode == DIRECT) con->file_started = 0;
if (rc != HANDLER_GO_ON) return rc;
}
if (opts->xsendfile_allow) {
data_string *ds;
buffer *vb;
/* X-Sendfile2 is deprecated; historical for fastcgi */
if (opts->backend == BACKEND_FASTCGI
&& NULL != (ds = (data_string *) array_get_element(con->response.headers, "X-Sendfile2"))) {
http_response_xsendfile2(srv, con, ds->value, opts->xsendfile_docroot);
buffer_reset(ds->value); /*(do not send to client)*/
&& NULL != (vb = http_header_response_get(con, HTTP_HEADER_OTHER, CONST_STR_LEN("X-Sendfile2")))) {
http_response_xsendfile2(srv, con, vb, opts->xsendfile_docroot);
buffer_reset(vb); /*(do not send to client)*/
if (con->mode == DIRECT) con->file_started = 0;
return HANDLER_FINISHED;
} else if (NULL != (ds = (data_string *) array_get_element(con->response.headers, "X-Sendfile"))
} else if (NULL != (vb = http_header_response_get(con, HTTP_HEADER_OTHER, CONST_STR_LEN("X-Sendfile")))
|| (opts->backend == BACKEND_FASTCGI /* X-LIGHTTPD-send-file is deprecated; historical for fastcgi */
&& NULL != (ds = (data_string *) array_get_element(con->response.headers, "X-LIGHTTPD-send-file")))) {
http_response_xsendfile(srv, con, ds->value, opts->xsendfile_docroot);
buffer_reset(ds->value); /*(do not send to client)*/
&& NULL != (vb = http_header_response_get(con, HTTP_HEADER_OTHER, CONST_STR_LEN("X-LIGHTTPD-send-file"))))) {
http_response_xsendfile(srv, con, vb, opts->xsendfile_docroot);
buffer_reset(vb); /*(do not send to client)*/
if (con->mode == DIRECT) con->file_started = 0;
return HANDLER_FINISHED;
}

7
src/http_auth.c

@ -1,6 +1,7 @@
#include "first.h"
#include "http_auth.h"
#include "http_header.h"
#include <stdlib.h>
#include <string.h>
@ -131,9 +132,9 @@ int http_auth_match_rules (const http_auth_require_t * const require, const char
return 0; /* no match */
}
void http_auth_setenv(array *env, const char *username, size_t ulen, const char *auth_type, size_t alen) {
array_set_key_value(env, CONST_STR_LEN("REMOTE_USER"), username, ulen);
array_set_key_value(env, CONST_STR_LEN("AUTH_TYPE"), auth_type, alen);
void http_auth_setenv(connection *con, const char *username, size_t ulen, const char *auth_type, size_t alen) {
http_header_env_set(con, CONST_STR_LEN("REMOTE_USER"), username, ulen);
http_header_env_set(con, CONST_STR_LEN("AUTH_TYPE"), auth_type, alen);
}
int http_auth_md5_hex2bin (const char *md5hex, size_t len, unsigned char md5bin[16])

2
src/http_auth.h

@ -45,7 +45,7 @@ const http_auth_backend_t * http_auth_backend_get (const buffer *name);
void http_auth_backend_set (const http_auth_backend_t *backend);
int http_auth_const_time_memeq (const char *a, size_t alen, const char *b, size_t blen);
void http_auth_setenv(array *env, const char *username, size_t ulen, const char *auth_type, size_t alen);
void http_auth_setenv(connection *con, const char *username, size_t ulen, const char *auth_type, size_t alen);
int http_auth_md5_hex2bin (const char *md5hex, size_t len, unsigned char md5bin[16]);

166
src/http_header.c

@ -0,0 +1,166 @@
#include "first.h"
#include "http_header.h"
#include "base.h"
#include "array.h"
#include "buffer.h"
typedef struct keyvlenvalue {
const int key;
const char * const value;
const size_t vlen;
} keyvlenvalue;
/* Note: must be sorted by length */
/* Note: must be kept in sync with http_header.h enum http_header_e */
static const keyvlenvalue http_headers[] = {
{ HTTP_HEADER_HOST, CONST_STR_LEN("Host") }
,{ HTTP_HEADER_DATE, CONST_STR_LEN("Date") }
,{ HTTP_HEADER_ETAG, CONST_STR_LEN("ETag") }
,{ HTTP_HEADER_VARY, CONST_STR_LEN("Vary") }
,{ HTTP_HEADER_RANGE, CONST_STR_LEN("Range") }
,{ HTTP_HEADER_COOKIE, CONST_STR_LEN("Cookie") }
,{ HTTP_HEADER_EXPECT, CONST_STR_LEN("Expect") }
,{ HTTP_HEADER_STATUS, CONST_STR_LEN("Status") }
,{ HTTP_HEADER_SERVER, CONST_STR_LEN("Server") }
,{ HTTP_HEADER_UPGRADE, CONST_STR_LEN("Upgrade") }
,{ HTTP_HEADER_LOCATION, CONST_STR_LEN("Location") }
,{ HTTP_HEADER_FORWARDED, CONST_STR_LEN("Forwarded") }
,{ HTTP_HEADER_CONNECTION, CONST_STR_LEN("Connection") }
,{ HTTP_HEADER_SET_COOKIE, CONST_STR_LEN("Set-Cookie") }
,{ HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type") }
,{ HTTP_HEADER_LAST_MODIFIED, CONST_STR_LEN("Last-Modified") }
,{ HTTP_HEADER_AUTHORIZATION, CONST_STR_LEN("Authorization") }
,{ HTTP_HEADER_IF_NONE_MATCH, CONST_STR_LEN("If-None-Match") }
,{ HTTP_HEADER_CACHE_CONTROL, CONST_STR_LEN("Cache-Control") }
,{ HTTP_HEADER_CONTENT_LENGTH, CONST_STR_LEN("Content-Length") }
,{ HTTP_HEADER_ACCEPT_ENCODING, CONST_STR_LEN("Accept-Encoding") }
,{ HTTP_HEADER_X_FORWARDED_FOR, CONST_STR_LEN("X-Forwarded-For") }
,{ HTTP_HEADER_CONTENT_ENCODING, CONST_STR_LEN("Content-Encoding") }
,{ HTTP_HEADER_CONTENT_LOCATION, CONST_STR_LEN("Content-Location") }
,{ HTTP_HEADER_IF_MODIFIED_SINCE, CONST_STR_LEN("If-Modified-Since") }
,{ HTTP_HEADER_TRANSFER_ENCODING, CONST_STR_LEN("Transfer-Encoding") }
,{ HTTP_HEADER_X_FORWARDED_PROTO, CONST_STR_LEN("X-Forwarded-Proto") }
,{ HTTP_HEADER_OTHER, NULL, 0 }
};
enum http_header_e http_header_hkey_get(const char *s, size_t slen) {
const struct keyvlenvalue * const kv = http_headers;
for (int i = 0; kv[i].vlen && slen >= kv[i].vlen; ++i) {
if (slen == kv[i].vlen
&& 0 == buffer_caseless_compare(s, slen, kv[i].value, kv[i].vlen))
return (enum http_header_e)kv[i].key;
}
return HTTP_HEADER_OTHER;
}
buffer * http_header_response_get(connection *con, enum http_header_e id, const char *k, size_t klen) {
data_string * const ds =
(id <= HTTP_HEADER_OTHER || (con->response.htags & id))
? (data_string *)array_get_element_klen(con->response.headers, k, klen)
: NULL;
return ds && !buffer_string_is_empty(ds->value) ? ds->value : NULL;
}
void http_header_response_set(connection *con, enum http_header_e id, const char *k, size_t klen, const char *v, size_t vlen) {
/* set value, including setting blank value if 0 == vlen
* (note: if 0 == vlen, header is still inserted with blank value,
* which is used to indicate a "removed" header)
*/
con->response.htags |= id;
array_set_key_value(con->response.headers, k, klen, v, vlen);
}
void http_header_response_append(connection *con, enum http_header_e id, const char *k, size_t klen, const char *v, size_t vlen) {
if (vlen) {
buffer *vb = (id <= HTTP_HEADER_OTHER || (con->response.htags & id))
? http_header_response_get(con, id, k, klen)
: NULL;
if (NULL == vb) {
array_insert_key_value(con->response.headers, k, klen, v, vlen);
con->response.htags |= id;
}
else { /* append value */
buffer_append_string_len(vb, CONST_STR_LEN(", "));
buffer_append_string_len(vb, v, vlen);
}
}
}
void http_header_response_insert(connection *con, enum http_header_e id, const char *k, size_t klen, const char *v, size_t vlen) {
if (vlen) {
buffer *vb = (id <= HTTP_HEADER_OTHER || (con->response.htags & id))
? http_header_response_get(con, id, k, klen)
: NULL;
if (NULL == vb) {
array_insert_key_value(con->response.headers, k, klen, v, vlen);
con->response.htags |= id;
}
else { /* append value */
buffer_append_string_len(vb, CONST_STR_LEN("\r\n"));
buffer_append_string_len(vb, k, klen);
buffer_append_string_len(vb, CONST_STR_LEN(": "));
buffer_append_string_len(vb, v, vlen);
}
}
}
buffer * http_header_request_get(connection *con, enum http_header_e id, const char *k, size_t klen) {
data_string * const ds =
(id <= HTTP_HEADER_OTHER || (con->request.htags & id))
? (data_string *)array_get_element_klen(con->request.headers, k, klen)
: NULL;
return ds && !buffer_string_is_empty(ds->value) ? ds->value : NULL;
}
void http_header_request_set(connection *con, enum http_header_e id, const char *k, size_t klen, const char *v, size_t vlen) {
/* set value, including setting blank value if 0 == vlen
* (note: if 0 == vlen, header is still inserted with blank value,
* which is used to indicate a "removed" header)
*/
con->request.htags |= id;
array_set_key_value(con->request.headers, k, klen, v, vlen);
}
void http_header_request_append(connection *con, enum http_header_e id, const char *k, size_t klen, const char *v, size_t vlen) {
if (vlen) {
buffer *vb = (id <= HTTP_HEADER_OTHER || (con->request.htags & id))
? http_header_request_get(con, id, k, klen)
: NULL;
if (NULL == vb) {
array_insert_key_value(con->request.headers, k, klen, v, vlen);
con->request.htags |= id;
}
else { /* append value */
buffer_append_string_len(vb, CONST_STR_LEN(", "));
buffer_append_string_len(vb, v, vlen);
}
}
}
buffer * http_header_env_get(connection *con, const char *k, size_t klen) {
data_string * const ds =
(data_string *)array_get_element_klen(con->environment, k, klen);
return ds && !buffer_string_is_empty(ds->value) ? ds->value : NULL;
}
void http_header_env_set(connection *con, const char *k, size_t klen, const char *v, size_t vlen) {
array_set_key_value(con->environment, k, klen, v, vlen);
}
void http_header_env_append(connection *con, const char *k, size_t klen, const char *v, size_t vlen) {
if (vlen) {
buffer * const vb = http_header_env_get(con, k, klen);
if (NULL == vb) {
array_insert_key_value(con->environment, k, klen, v, vlen);
}
else { /* append value */
buffer_append_string_len(vb, CONST_STR_LEN(", "));
buffer_append_string_len(vb, v, vlen);
}
}
}

57
src/http_header.h

@ -0,0 +1,57 @@
#ifndef INCLUDED_HTTP_HEADER_H
#define INCLUDED_HTTP_HEADER_H
#include "first.h"
#include "base_decls.h"
#include "buffer.h"
/* Note: must be kept in sync with http_header.c http_headers[] */
/* Note: when adding new items, must replace OTHER in existing code for item */
enum http_header_e {
HTTP_HEADER_UNSPECIFIED = -1
,HTTP_HEADER_OTHER = 0x00000000
,HTTP_HEADER_ACCEPT_ENCODING = 0x00000001
,HTTP_HEADER_AUTHORIZATION = 0x00000002
,HTTP_HEADER_CACHE_CONTROL = 0x00000004
,HTTP_HEADER_CONNECTION = 0x00000008
,HTTP_HEADER_CONTENT_ENCODING = 0x00000010
,HTTP_HEADER_CONTENT_LENGTH = 0x00000020
,HTTP_HEADER_CONTENT_LOCATION = 0x00000040
,HTTP_HEADER_CONTENT_TYPE = 0x00000080
,HTTP_HEADER_COOKIE = 0x00000100
,HTTP_HEADER_DATE = 0x00000200
,HTTP_HEADER_ETAG = 0x00000400
,HTTP_HEADER_EXPECT = 0x00000800
,HTTP_HEADER_FORWARDED = 0x00001000
,HTTP_HEADER_HOST = 0x00002000
,HTTP_HEADER_IF_MODIFIED_SINCE = 0x00004000
,HTTP_HEADER_IF_NONE_MATCH = 0x00008000
,HTTP_HEADER_LAST_MODIFIED = 0x00010000
,HTTP_HEADER_LOCATION = 0x00020000
,HTTP_HEADER_RANGE = 0x00040000
,HTTP_HEADER_SERVER = 0x00080000
,HTTP_HEADER_SET_COOKIE = 0x00100000
,HTTP_HEADER_STATUS = 0x00200000
,HTTP_HEADER_TRANSFER_ENCODING = 0x00400000
,HTTP_HEADER_UPGRADE = 0x00800000
,HTTP_HEADER_VARY = 0x01000000
,HTTP_HEADER_X_FORWARDED_FOR = 0x02000000
,HTTP_HEADER_X_FORWARDED_PROTO = 0x04000000
};
enum http_header_e http_header_hkey_get(const char *s, size_t slen);
buffer * http_header_response_get(connection *con, enum http_header_e id, const char *k, size_t klen);
void http_header_response_set(connection *con, enum http_header_e id, const char *k, size_t klen, const char *v, size_t vlen);
void http_header_response_append(connection *con, enum http_header_e id, const char *k, size_t klen, const char *v, size_t vlen);
void http_header_response_insert(connection *con, enum http_header_e id, const char *k, size_t klen, const char *v, size_t vlen);
buffer * http_header_request_get(connection *con, enum http_header_e id, const char *k, size_t klen);
void http_header_request_set(connection *con, enum http_header_e id, const char *k, size_t klen, const char *v, size_t vlen);
void http_header_request_append(connection *con, enum http_header_e id, const char *k, size_t klen, const char *v, size_t vlen);
buffer * http_header_env_get(connection *con, const char *k, size_t klen);
void http_header_env_set(connection *con, const char *k, size_t klen, const char *v, size_t vlen);
void http_header_env_append(connection *con, const char *k, size_t klen, const char *v, size_t vlen);
#endif

3
src/meson.build

@ -560,6 +560,7 @@ common_src = [
'gw_backend.c',
'http_auth.c',
'http_chunk.c',
'http_header.c',
'http_kv.c',
'http_vhostdb.c',
'http-header-glue.c',
@ -712,6 +713,7 @@ test('test_configfile', executable('test_configfile',
'array.c',
'data_config.c',
'data_string.c',
'http_header.c',
'http_kv.c',
'vector.c',
'log.c',
@ -728,6 +730,7 @@ test('test_request', executable('test_request',
'buffer.c',
'array.c',
'data_string.c',
'http_header.c',
'http_kv.c',
'log.c',
'sock_addr.c',

23
src/mod_accesslog.c

@ -4,6 +4,7 @@
#include "fdevent.h"
#include "log.h"
#include "buffer.h"
#include "http_header.h"
#include "sock_addr.h"
#include "plugin.h"
@ -766,7 +767,7 @@ REQUESTDONE_FUNC(log_access_write) {
size_t j;
int newts = 0;
data_string *ds;
buffer *vb;
struct timespec ts = { 0, 0 };
mod_accesslog_patch_connection(srv, con, p);
@ -942,8 +943,8 @@ REQUESTDONE_FUNC(log_access_write) {
buffer_append_string_len(b, CONST_STR_LEN("-"));
break;
case FORMAT_REMOTE_USER:
if (NULL != (ds = (data_string *)array_get_element(con->environment, "REMOTE_USER")) && !buffer_string_is_empty(ds->value)) {
accesslog_append_escaped(b, ds->value);
if (NULL != (vb = http_header_env_get(con, CONST_STR_LEN("REMOTE_USER")))) {
accesslog_append_escaped(b, vb);
} else {
buffer_append_string_len(b, CONST_STR_LEN("-"));
}
@ -966,23 +967,23 @@ REQUESTDONE_FUNC(log_access_write) {
}
break;
case FORMAT_HEADER: