summaryrefslogtreecommitdiff
path: root/data/lighttpd/lighttpd-1.4.53/src/http_chunk.c
blob: 1bdf2fa89dd70f4ba6b20b878ed1bce8cb5d01fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "first.h"

/**
 * the HTTP chunk-API
 *
 *
 */

#include "base.h"
#include "chunk.h"
#include "http_chunk.h"
#include "stat_cache.h"
#include "fdevent.h"
#include "log.h"

#include <sys/types.h>
#include <sys/stat.h>

#include <stdlib.h>
#include <unistd.h>

#include <errno.h>
#include <string.h>

static buffer * http_chunk_header(buffer *b, uintmax_t len) {
    buffer_clear(b);
    buffer_append_uint_hex(b, len);
    buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
    return b;
}

static void http_chunk_append_len(server *srv, connection *con, uintmax_t len) {
    buffer *b = http_chunk_header(srv->tmp_chunk_len, len);
    chunkqueue_append_mem(con->write_queue, CONST_BUF_LEN(b));
}

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;
	}

	return stat_cache_open_rdonly_fstat(srv, con, fn, st);
}

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;

	if (con->response.send_chunked) {
		http_chunk_append_len(srv, con, (uintmax_t)len);
	}

	chunkqueue_append_file_fd(cq, fn, fd, offset, len);

	if (con->response.send_chunked) {
		chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
	}
}

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;
}

static int http_chunk_append_to_tempfile(server *srv, connection *con, const char * mem, size_t len) {
	chunkqueue * const cq = con->write_queue;

	if (con->response.send_chunked) {
		buffer *b = http_chunk_header(srv->tmp_chunk_len, len);
		if (0 != chunkqueue_append_mem_to_tempfile(srv, cq, CONST_BUF_LEN(b))) {
			return -1;
		}
	}

	if (0 != chunkqueue_append_mem_to_tempfile(srv, cq, mem, len)) {
		return -1;
	}

	if (con->response.send_chunked) {
		if (0 != chunkqueue_append_mem_to_tempfile(srv, cq, CONST_STR_LEN("\r\n"))) {
			return -1;
		}
	}

	return 0;
}

static int http_chunk_append_cq_to_tempfile(server *srv, connection *con, chunkqueue *src, size_t len) {
    chunkqueue * const cq = con->write_queue;

    if (con->response.send_chunked) {
        buffer *b = http_chunk_header(srv->tmp_chunk_len, len);
        if (0 != chunkqueue_append_mem_to_tempfile(srv, cq, CONST_BUF_LEN(b))) {
            return -1;
        }
    }

    if (0 != chunkqueue_steal_with_tempfiles(srv, cq, src, len)) {
        return -1;
    }

    if (con->response.send_chunked) {
        if (0!=chunkqueue_append_mem_to_tempfile(srv,cq,CONST_STR_LEN("\r\n"))){
            return -1;
        }
    }

    return 0;
}

static int http_chunk_uses_tempfile(server *srv, connection *con, size_t len) {
	chunkqueue * const cq = con->write_queue;
	chunk *c = cq->last;
	UNUSED(srv);

	/* 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
	 * (but included in calculation for whether or not to use temp file) */

	/*(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)*/

	if ((c && c->type == FILE_CHUNK && c->file.is_temp)
	    || cq->bytes_in - cq->bytes_out + len
		> 1024 * ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN) ? 128 : 64)) {
		return 1;
	}

	return 0;
}

int http_chunk_append_buffer(server *srv, connection *con, buffer *mem) {
    chunkqueue * const cq = con->write_queue;
    size_t len = buffer_string_length(mem);
    if (0 == len) return 0;

    if (http_chunk_uses_tempfile(srv, con, len)) {
        return http_chunk_append_to_tempfile(srv, con, mem->ptr, len);
    }

    if (con->response.send_chunked) {
        http_chunk_append_len(srv, con, len);
    }

    /*(chunkqueue_append_buffer() might steal buffer contents)*/
    chunkqueue_append_buffer(cq, mem);

    if (con->response.send_chunked) {
        chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
    }

    return 0;
}

int http_chunk_append_mem(server *srv, connection *con, const char * mem, size_t len) {
    chunkqueue * const cq = con->write_queue;
    if (0 == len) return 0;
    force_assert(NULL != mem);

    if (http_chunk_uses_tempfile(srv, con, len)) {
        return http_chunk_append_to_tempfile(srv, con, mem, len);
    }

    if (con->response.send_chunked) {
        http_chunk_append_len(srv, con, len);
    }

    chunkqueue_append_mem(cq, mem, len);

    if (con->response.send_chunked) {
        chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
    }

    return 0;
}

int http_chunk_transfer_cqlen(server *srv, connection *con, chunkqueue *src, size_t len) {
    chunkqueue * const cq = con->write_queue;
    if (0 == len) return 0;

    if (http_chunk_uses_tempfile(srv, con, len)) {
        return http_chunk_append_cq_to_tempfile(srv, con, src, len);
    }

    if (con->response.send_chunked) {
        http_chunk_append_len(srv, con, len);
    }

    chunkqueue_steal(cq, src, len);

    if (con->response.send_chunked) {
        chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
    }

    return 0;
}

void http_chunk_close(server *srv, connection *con) {
	UNUSED(srv);
	force_assert(NULL != con);

	if (con->response.send_chunked) {
		chunkqueue_append_mem(con->write_queue, CONST_STR_LEN("0\r\n\r\n"));
	}
}