summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Bühler <stbuehler@web.de>2009-06-11 12:35:00 +0000
committerStefan Bühler <stbuehler@web.de>2009-06-11 12:35:00 +0000
commit64fd36a977a890d1c24c7632a358ff6cab3161a4 (patch)
treeab5c223d88a3e2766e080c63c1301f6f8fd1bcbb
parent5aa3c296911d5824a00b2b68f89be28e69b180fd (diff)
downloadlighttpd-git-64fd36a977a890d1c24c7632a358ff6cab3161a4.tar.gz
Add X-Sendfile-Range feature (fixes #2005)
git-svn-id: svn+ssh://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@2531 152afb58-edef-0310-8abb-c4023f1b3aa9
-rw-r--r--NEWS1
-rw-r--r--src/mod_fastcgi.c39
-rw-r--r--src/response.c2
-rw-r--r--tests/docroot/www/sendfile.php7
-rw-r--r--tests/lighttpd.conf2
-rwxr-xr-xtests/mod-fastcgi.t18
6 files changed, 60 insertions, 9 deletions
diff --git a/NEWS b/NEWS
index 0c3d017c..add794f6 100644
--- a/NEWS
+++ b/NEWS
@@ -53,6 +53,7 @@ NEWS
* Add support for "real" entropy from /dev/[u]random (fixes #1977)
* Adding support for additional chars in LDAP usernames (fixes #1941)
* Ignore multiple "If-None-Match" headers (only use first one, fixes #753)
+ * Add X-Sendfile-Range feature (fixes #2005)
- 1.4.22 - 2009-03-07
* Fix wrong lua type for CACHE_MISS/CACHE_HIT in mod_cml (fixes #533)
diff --git a/src/mod_fastcgi.c b/src/mod_fastcgi.c
index 0fbfebf5..83cae360 100644
--- a/src/mod_fastcgi.c
+++ b/src/mod_fastcgi.c
@@ -2554,22 +2554,51 @@ static int fcgi_demux_response(server *srv, handler_ctx *hctx) {
stat_cache_entry *sce;
if (HANDLER_ERROR != stat_cache_get_entry(srv, con, ds->value, &sce)) {
- data_string *dcls;
+ data_string *dcls, *dsr;
+ off_t start = 0, end = sce->st.st_size, len;
if (NULL == (dcls = (data_string *)array_get_unused_element(con->response.headers, TYPE_STRING))) {
dcls = data_response_init();
}
/* found */
- http_chunk_append_file(srv, con, ds->value, 0, sce->st.st_size);
+ /* Assume we should serve from the beginning to the end of the file. */
+ /* If they gave us X-Sendfile-Range, adjust portion to serve. */
+ /* X-Sendfile-Range: header must include start offset, may include end offset, must end after the last offset */
+ if (NULL != (dsr = (data_string *) array_get_element(con->response.headers, "X-Sendfile-Range"))) {
+ char *end_part = NULL, *final_part = NULL;
+ start = strtoll(dsr->value->ptr, &end_part, 10);
+ if (start < 0 || end_part == ds->value->ptr) {
+ start = 0;
+ log_error_write(srv, __FILE__, __LINE__, "sb",
+ "Invalid X-Sendfile-Range header:",
+ dsr->value);
+ } else if (end_part && *end_part) {
+ end = strtoll(end_part, &final_part, 10);
+ if (end < 0 || end_part == final_part || !final_part || *final_part || start > end) {
+ start = 0;
+ end = sce->st.st_size;
+ log_error_write(srv, __FILE__, __LINE__, "sb",
+ "Invalid X-Sendfile-Range header:",
+ dsr->value);
+ } else {
+ if (end > sce->st.st_size) end = sce->st.st_size;
+ if (start > end) start = end;
+ }
+ } else {
+ if (start > end) start = end;
+ }
+ }
+ len = end - start;
+ if (len != 0) http_chunk_append_file(srv, con, ds->value, start, len);
hctx->send_content_body = 0; /* ignore the content */
joblist_append(srv, con);
- buffer_copy_string_len(dcls->key, "Content-Length", sizeof("Content-Length")-1);
- buffer_copy_off_t(dcls->value, sce->st.st_size);
+ buffer_copy_string_len(dcls->key, CONST_STR_LEN("Content-Length"));
+ buffer_copy_off_t(dcls->value, len);
dcls = (data_string*) array_replace(con->response.headers, (data_unset *)dcls);
if (dcls) dcls->free((data_unset*)dcls);
con->parsed_response |= HTTP_CONTENT_LENGTH;
- con->response.content_length = sce->st.st_size;
+ con->response.content_length = len;
} else {
log_error_write(srv, __FILE__, __LINE__, "sb",
"send-file error: couldn't get stat_cache entry for:",
diff --git a/src/response.c b/src/response.c
index 884a2e90..2b098063 100644
--- a/src/response.c
+++ b/src/response.c
@@ -65,7 +65,7 @@ int http_response_write_header(server *srv, connection *con) {
if (ds->value->used && ds->key->used &&
0 != strncasecmp(ds->key->ptr, CONST_STR_LEN("X-LIGHTTPD-")) &&
- 0 != strcasecmp(ds->key->ptr, "X-Sendfile")) {
+ 0 != strncasecmp(ds->key->ptr, CONST_STR_LEN("X-Sendfile"))) {
if (0 == strcasecmp(ds->key->ptr, "Date")) have_date = 1;
if (0 == strcasecmp(ds->key->ptr, "Server")) have_server = 1;
if (0 == strcasecmp(ds->key->ptr, "Content-Encoding") && 304 == con->http_status) continue;
diff --git a/tests/docroot/www/sendfile.php b/tests/docroot/www/sendfile.php
new file mode 100644
index 00000000..ecdd7125
--- /dev/null
+++ b/tests/docroot/www/sendfile.php
@@ -0,0 +1,7 @@
+<?php
+
+header("X-Sendfile: " . getcwd() . "/index.txt");
+
+if ($_GET["range"]) header("X-Sendfile-Range: " . $_GET["range"]);
+
+?> \ No newline at end of file
diff --git a/tests/lighttpd.conf b/tests/lighttpd.conf
index b4a90c4e..e2fd96a7 100644
--- a/tests/lighttpd.conf
+++ b/tests/lighttpd.conf
@@ -82,7 +82,7 @@ $HTTP["url"] =~ "\.pdf$" {
}
fastcgi.debug = 0
-fastcgi.server = ( ".php" => ( ( "host" => "127.0.0.1", "port" => 1026, "broken-scriptfilename" => "enable" ) ),
+fastcgi.server = ( ".php" => ( ( "host" => "127.0.0.1", "port" => 1026, "broken-scriptfilename" => "enable", "allow-x-send-file" => "enable" ) ),
"/prefix.fcgi" => ( ( "host" => "127.0.0.1", "port" => 1026, "check-local" => "disable", "broken-scriptfilename" => "enable" ) )
)
diff --git a/tests/mod-fastcgi.t b/tests/mod-fastcgi.t
index b5f74d64..8fc35e1d 100755
--- a/tests/mod-fastcgi.t
+++ b/tests/mod-fastcgi.t
@@ -7,7 +7,7 @@ BEGIN {
}
use strict;
-use Test::More tests => 53;
+use Test::More tests => 55;
use LightyTest;
my $tf = LightyTest->new();
@@ -25,7 +25,7 @@ SKIP: {
}
SKIP: {
- skip "no PHP running on port 1026", 30 unless $tf->listening_on(1026);
+ skip "no PHP running on port 1026", 32 unless $tf->listening_on(1026);
ok($tf->start_proc == 0, "Starting lighttpd") or goto cleanup;
@@ -174,6 +174,20 @@ EOF
$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/foo/bar' } ];
ok($tf->handle_http($t) == 0, 'PATH_INFO, check-local off');
+ $t->{REQUEST} = ( <<EOF
+GET /sendfile.php HTTP/1.0
+EOF
+ );
+ $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'Content-Length' => 4348 } ];
+ ok($tf->handle_http($t) == 0, 'X-Sendfile');
+
+ $t->{REQUEST} = ( <<EOF
+GET /sendfile.php?range=3+10 HTTP/1.0
+EOF
+ );
+ $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'OCTYPE ' } ];
+ ok($tf->handle_http($t) == 0, 'X-Sendfile-Range');
+
ok($tf->stop_proc == 0, "Stopping lighttpd");