2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/**
|
|
|
|
* the HTTP chunk-API
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include "server.h"
|
|
|
|
#include "chunk.h"
|
|
|
|
#include "http_chunk.h"
|
2016-03-30 10:39:33 +00:00
|
|
|
#include "stat_cache.h"
|
2009-10-11 14:31:42 +00:00
|
|
|
#include "log.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-03-30 10:39:33 +00:00
|
|
|
static void http_chunk_append_len(server *srv, connection *con, uintmax_t len) {
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer *b;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
force_assert(NULL != srv);
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
b = srv->tmp_chunk_len;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 19:10:46 +00:00
|
|
|
buffer_string_set_length(b, 0);
|
|
|
|
buffer_append_uint_hex(b, len);
|
|
|
|
buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
chunkqueue_append_buffer(con->write_queue, b);
|
|
|
|
}
|
|
|
|
|
2016-03-30 10:39:33 +00:00
|
|
|
static int http_chunk_append_file_open_fstat(server *srv, connection *con, buffer *fn, struct stat *st) {
|
|
|
|
if (!con->conf.follow_symlink) {
|
|
|
|
/*(preserve existing stat_cache symlink checks)*/
|
|
|
|
stat_cache_entry *sce;
|
|
|
|
if (HANDLER_ERROR == stat_cache_get_entry(srv, con, fn, &sce)) return -1;
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2016-03-30 10:39:33 +00:00
|
|
|
return stat_cache_open_rdonly_fstat(srv, con, fn, st);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-03-30 10:39:33 +00:00
|
|
|
static void http_chunk_append_file_fd_range(server *srv, connection *con, buffer *fn, int fd, off_t offset, off_t len) {
|
|
|
|
chunkqueue *cq = con->write_queue;
|
2015-02-08 12:37:10 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
|
2016-03-30 10:39:33 +00:00
|
|
|
http_chunk_append_len(srv, con, (uintmax_t)len);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-03-30 10:39:33 +00:00
|
|
|
chunkqueue_append_file_fd(cq, fn, fd, offset, len);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
|
|
|
|
chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-30 10:39:33 +00:00
|
|
|
int http_chunk_append_file_range(server *srv, connection *con, buffer *fn, off_t offset, off_t len) {
|
|
|
|
struct stat st;
|
|
|
|
const int fd = http_chunk_append_file_open_fstat(srv, con, fn, &st);
|
|
|
|
if (fd < 0) return -1;
|
|
|
|
|
|
|
|
if (-1 == len) {
|
|
|
|
if (offset >= st.st_size) {
|
|
|
|
close(fd);
|
|
|
|
return (offset == st.st_size) ? 0 : -1;
|
|
|
|
}
|
|
|
|
len = st.st_size - offset;
|
|
|
|
} else if (st.st_size - offset < len) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_chunk_append_file_fd_range(srv, con, fn, fd, offset, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int http_chunk_append_file(server *srv, connection *con, buffer *fn) {
|
|
|
|
struct stat st;
|
|
|
|
const int fd = http_chunk_append_file_open_fstat(srv, con, fn, &st);
|
|
|
|
if (fd < 0) return -1;
|
|
|
|
|
|
|
|
if (0 != st.st_size) {
|
|
|
|
http_chunk_append_file_fd_range(srv, con, fn, fd, 0, st.st_size);
|
|
|
|
} else {
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-25 20:45:09 +00:00
|
|
|
static int http_chunk_append_to_tempfile(server *srv, connection *con, const char * mem, size_t len) {
|
|
|
|
chunkqueue * const cq = con->write_queue;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-05-25 20:45:09 +00:00
|
|
|
if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
|
|
|
|
/*http_chunk_append_len(srv, con, len);*/
|
|
|
|
buffer *b = srv->tmp_chunk_len;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-05-25 20:45:09 +00:00
|
|
|
buffer_string_set_length(b, 0);
|
|
|
|
buffer_append_uint_hex(b, len);
|
|
|
|
buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-05-25 20:45:09 +00:00
|
|
|
if (0 != chunkqueue_append_mem_to_tempfile(srv, cq, CONST_BUF_LEN(b))) {
|
|
|
|
return -1;
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-05-25 20:45:09 +00:00
|
|
|
if (0 != chunkqueue_append_mem_to_tempfile(srv, cq, mem, len)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
|
2016-05-25 20:45:09 +00:00
|
|
|
if (0 != chunkqueue_append_mem_to_tempfile(srv, cq, CONST_STR_LEN("\r\n"))) {
|
|
|
|
return -1;
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2016-05-25 20:45:09 +00:00
|
|
|
|
|
|
|
return 0;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 20:45:09 +00:00
|
|
|
static int http_chunk_append_data(server *srv, connection *con, buffer *b, const char * mem, size_t len) {
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-05-25 20:45:09 +00:00
|
|
|
chunkqueue * const cq = con->write_queue;
|
|
|
|
chunk *c = cq->last;
|
|
|
|
if (0 == len) return 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-05-25 20:45:09 +00:00
|
|
|
/* current usage does not append_mem or append_buffer after appending
|
|
|
|
* file, so not checking if users of this interface have appended large
|
|
|
|
* (references to) files to chunkqueue, which would not be in memory */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
[core] option to stream response body to client (fixes #949, #760, #1283, #1387)
Set server.stream-response-body = 1 or server.stream-response-body = 2
to have lighttpd stream response body to client as it arrives from the
backend (CGI, FastCGI, SCGI, proxy).
default: buffer entire response body before sending response to client.
(This preserves existing behavior for now, but may in the future be
changed to stream response to client, which is the behavior more
commonly expected.)
x-ref:
"fastcgi, cgi, flush, php5 problem."
https://redmine.lighttpd.net/issues/949
"Random crashing on FreeBSD 6.1"
https://redmine.lighttpd.net/issues/760
"Memory usage increases when proxy+ssl+large file"
https://redmine.lighttpd.net/issues/1283
"lighttpd+fastcgi memory problem"
https://redmine.lighttpd.net/issues/1387
2016-06-11 15:04:01 +00:00
|
|
|
/*(allow slightly larger mem use if FDEVENT_STREAM_RESPONSE_BUFMIN
|
|
|
|
* to reduce creation of temp files when backend producer will be
|
|
|
|
* blocked until more data is sent to network to client)*/
|
|
|
|
|
2016-05-25 20:45:09 +00:00
|
|
|
if ((c && c->type == FILE_CHUNK && c->file.is_temp)
|
[core] option to stream response body to client (fixes #949, #760, #1283, #1387)
Set server.stream-response-body = 1 or server.stream-response-body = 2
to have lighttpd stream response body to client as it arrives from the
backend (CGI, FastCGI, SCGI, proxy).
default: buffer entire response body before sending response to client.
(This preserves existing behavior for now, but may in the future be
changed to stream response to client, which is the behavior more
commonly expected.)
x-ref:
"fastcgi, cgi, flush, php5 problem."
https://redmine.lighttpd.net/issues/949
"Random crashing on FreeBSD 6.1"
https://redmine.lighttpd.net/issues/760
"Memory usage increases when proxy+ssl+large file"
https://redmine.lighttpd.net/issues/1283
"lighttpd+fastcgi memory problem"
https://redmine.lighttpd.net/issues/1387
2016-06-11 15:04:01 +00:00
|
|
|
|| cq->bytes_in - cq->bytes_out + len
|
|
|
|
> 1024 * ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN) ? 128 : 64)) {
|
2016-05-25 20:45:09 +00:00
|
|
|
return http_chunk_append_to_tempfile(srv, con, b ? b->ptr : mem, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not appending to prior mem chunk just in case using openssl
|
|
|
|
* and need to resubmit same args as prior call to openssl (required?)*/
|
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) {
|
2015-02-08 12:37:10 +00:00
|
|
|
http_chunk_append_len(srv, con, len);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-05-25 20:45:09 +00:00
|
|
|
/*(chunkqueue_append_buffer() might steal buffer contents)*/
|
|
|
|
b ? chunkqueue_append_buffer(cq, b) : chunkqueue_append_mem(cq, mem, len);
|
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) {
|
2015-02-08 12:37:10 +00:00
|
|
|
chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2016-05-25 20:45:09 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int http_chunk_append_buffer(server *srv, connection *con, buffer *mem) {
|
|
|
|
force_assert(NULL != con);
|
|
|
|
|
|
|
|
return http_chunk_append_data(srv, con, mem, NULL, buffer_string_length(mem));
|
|
|
|
}
|
|
|
|
|
|
|
|
int http_chunk_append_mem(server *srv, connection *con, const char * mem, size_t len) {
|
|
|
|
force_assert(NULL != con);
|
|
|
|
force_assert(NULL != mem || 0 == len);
|
|
|
|
|
|
|
|
return http_chunk_append_data(srv, con, NULL, mem, len);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void http_chunk_close(server *srv, connection *con) {
|
|
|
|
UNUSED(srv);
|
|
|
|
force_assert(NULL != con);
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
|
|
|
|
chunkqueue_append_mem(con->write_queue, CONST_STR_LEN("0\r\n\r\n"));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
}
|