untangle overly complex control flow logic
[lighttpd.git] / src / http_chunk.c
blob32d9eef1c85a8ad69f8b67d8d2dd9a5220409887
1 #include "first.h"
3 /**
4 * the HTTP chunk-API
7 */
9 #include "server.h"
10 #include "chunk.h"
11 #include "http_chunk.h"
12 #include "stat_cache.h"
13 #include "log.h"
15 #include <sys/types.h>
16 #include <sys/stat.h>
18 #include <stdlib.h>
19 #include <fcntl.h>
20 #include <unistd.h>
22 #include <stdio.h>
23 #include <errno.h>
24 #include <string.h>
26 static void http_chunk_append_len(server *srv, connection *con, uintmax_t len) {
27 buffer *b;
29 force_assert(NULL != srv);
31 b = srv->tmp_chunk_len;
33 buffer_string_set_length(b, 0);
34 buffer_append_uint_hex(b, len);
35 buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
37 chunkqueue_append_buffer(con->write_queue, b);
40 static int http_chunk_append_file_open_fstat(server *srv, connection *con, buffer *fn, struct stat *st) {
41 if (!con->conf.follow_symlink) {
42 /*(preserve existing stat_cache symlink checks)*/
43 stat_cache_entry *sce;
44 if (HANDLER_ERROR == stat_cache_get_entry(srv, con, fn, &sce)) return -1;
47 return stat_cache_open_rdonly_fstat(srv, con, fn, st);
50 static void http_chunk_append_file_fd_range(server *srv, connection *con, buffer *fn, int fd, off_t offset, off_t len) {
51 chunkqueue *cq = con->write_queue;
53 if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
54 http_chunk_append_len(srv, con, (uintmax_t)len);
57 chunkqueue_append_file_fd(cq, fn, fd, offset, len);
59 if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
60 chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
64 int http_chunk_append_file_range(server *srv, connection *con, buffer *fn, off_t offset, off_t len) {
65 struct stat st;
66 const int fd = http_chunk_append_file_open_fstat(srv, con, fn, &st);
67 if (fd < 0) return -1;
69 if (-1 == len) {
70 if (offset >= st.st_size) {
71 close(fd);
72 return (offset == st.st_size) ? 0 : -1;
74 len = st.st_size - offset;
75 } else if (st.st_size - offset < len) {
76 close(fd);
77 return -1;
80 http_chunk_append_file_fd_range(srv, con, fn, fd, offset, len);
81 return 0;
84 int http_chunk_append_file(server *srv, connection *con, buffer *fn) {
85 struct stat st;
86 const int fd = http_chunk_append_file_open_fstat(srv, con, fn, &st);
87 if (fd < 0) return -1;
89 if (0 != st.st_size) {
90 http_chunk_append_file_fd_range(srv, con, fn, fd, 0, st.st_size);
91 } else {
92 close(fd);
94 return 0;
97 void http_chunk_append_buffer(server *srv, connection *con, buffer *mem) {
98 chunkqueue *cq;
100 force_assert(NULL != con);
102 if (buffer_string_is_empty(mem)) return;
104 cq = con->write_queue;
106 if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
107 http_chunk_append_len(srv, con, buffer_string_length(mem));
110 chunkqueue_append_buffer(cq, mem);
112 if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
113 chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
117 void http_chunk_append_mem(server *srv, connection *con, const char * mem, size_t len) {
118 chunkqueue *cq;
120 force_assert(NULL != con);
121 force_assert(NULL != mem || 0 == len);
123 if (NULL == mem || 0 == len) return;
125 cq = con->write_queue;
127 if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
128 http_chunk_append_len(srv, con, len);
131 chunkqueue_append_mem(cq, mem, len);
133 if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
134 chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
138 void http_chunk_close(server *srv, connection *con) {
139 UNUSED(srv);
140 force_assert(NULL != con);
142 if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
143 chunkqueue_append_mem(con->write_queue, CONST_STR_LEN("0\r\n\r\n"));