diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-12-19 18:54:44 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-12-19 18:54:44 +0000 |
commit | 5022cec769294a488cbb65c7ad81fb78978631e1 (patch) | |
tree | fe9cb4031645204b28327d4c12ae4dae5714bfb6 /src | |
parent | 0f9d6e9946afc5fce3f6cec62c43fec091f6ca4d (diff) | |
download | libmicrohttpd-5022cec769294a488cbb65c7ad81fb78978631e1.tar.gz libmicrohttpd-5022cec769294a488cbb65c7ad81fb78978631e1.zip |
support for sendfile with offset
Diffstat (limited to 'src')
-rw-r--r-- | src/daemon/daemon.c | 2 | ||||
-rw-r--r-- | src/daemon/internal.h | 5 | ||||
-rw-r--r-- | src/daemon/response.c | 26 | ||||
-rw-r--r-- | src/include/microhttpd.h | 15 |
4 files changed, 44 insertions, 4 deletions
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index 558954d6..31a87bf0 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c | |||
@@ -701,7 +701,7 @@ send_param_adapter (struct MHD_Connection *connection, | |||
701 | (-1 != (fd = connection->response->fd)) ) | 701 | (-1 != (fd = connection->response->fd)) ) |
702 | { | 702 | { |
703 | /* can use sendfile */ | 703 | /* can use sendfile */ |
704 | offset = (off_t) connection->response_write_position; | 704 | offset = (off_t) connection->response_write_position + connection->response->fd_off; |
705 | ret = sendfile (connection->socket_fd, | 705 | ret = sendfile (connection->socket_fd, |
706 | fd, | 706 | fd, |
707 | &offset, | 707 | &offset, |
diff --git a/src/daemon/internal.h b/src/daemon/internal.h index be476f4b..2ff69472 100644 --- a/src/daemon/internal.h +++ b/src/daemon/internal.h | |||
@@ -229,6 +229,11 @@ struct MHD_Response | |||
229 | uint64_t data_start; | 229 | uint64_t data_start; |
230 | 230 | ||
231 | /** | 231 | /** |
232 | * Offset to start reading from when using 'fd'. | ||
233 | */ | ||
234 | off_t fd_off; | ||
235 | |||
236 | /** | ||
232 | * Size of data. | 237 | * Size of data. |
233 | */ | 238 | */ |
234 | size_t data_size; | 239 | size_t data_size; |
diff --git a/src/daemon/response.c b/src/daemon/response.c index e630019c..7d9ae17c 100644 --- a/src/daemon/response.c +++ b/src/daemon/response.c | |||
@@ -273,7 +273,7 @@ file_reader (void *cls, uint64_t pos, char *buf, size_t max) | |||
273 | int ret; | 273 | int ret; |
274 | 274 | ||
275 | pthread_mutex_lock (&response->mutex); | 275 | pthread_mutex_lock (&response->mutex); |
276 | (void) lseek (response->fd, pos, SEEK_SET); | 276 | (void) lseek (response->fd, pos + response->fd_off, SEEK_SET); |
277 | ret = read (response->fd, buf, max); | 277 | ret = read (response->fd, buf, max); |
278 | pthread_mutex_unlock (&response->mutex); | 278 | pthread_mutex_unlock (&response->mutex); |
279 | return ret; | 279 | return ret; |
@@ -301,10 +301,12 @@ free_callback (void *cls) | |||
301 | * | 301 | * |
302 | * @param size size of the data portion of the response | 302 | * @param size size of the data portion of the response |
303 | * @param fd file descriptor referring to a file on disk with the data | 303 | * @param fd file descriptor referring to a file on disk with the data |
304 | * @param off offset to start reading from in the file | ||
304 | * @return NULL on error (i.e. invalid arguments, out of memory) | 305 | * @return NULL on error (i.e. invalid arguments, out of memory) |
305 | */ | 306 | */ |
306 | struct MHD_Response *MHD_create_response_from_fd (size_t size, | 307 | struct MHD_Response *MHD_create_response_from_fd_at_offset (size_t size, |
307 | int fd) | 308 | int fd, |
309 | off_t offset) | ||
308 | { | 310 | { |
309 | struct MHD_Response *ret; | 311 | struct MHD_Response *ret; |
310 | 312 | ||
@@ -316,11 +318,29 @@ struct MHD_Response *MHD_create_response_from_fd (size_t size, | |||
316 | if (ret == NULL) | 318 | if (ret == NULL) |
317 | return NULL; | 319 | return NULL; |
318 | ret->fd = fd; | 320 | ret->fd = fd; |
321 | ret->fd_off = offset; | ||
319 | ret->crc_cls = ret; | 322 | ret->crc_cls = ret; |
320 | return ret; | 323 | return ret; |
321 | } | 324 | } |
322 | 325 | ||
323 | 326 | ||
327 | |||
328 | |||
329 | /** | ||
330 | * Create a response object. The response object can be extended with | ||
331 | * header information and then be used any number of times. | ||
332 | * | ||
333 | * @param size size of the data portion of the response | ||
334 | * @param fd file descriptor referring to a file on disk with the data | ||
335 | * @return NULL on error (i.e. invalid arguments, out of memory) | ||
336 | */ | ||
337 | struct MHD_Response *MHD_create_response_from_fd (size_t size, | ||
338 | int fd) | ||
339 | { | ||
340 | return MHD_create_response_from_fd_at_offset (size, fd, 0); | ||
341 | } | ||
342 | |||
343 | |||
324 | /** | 344 | /** |
325 | * Create a response object. The response object can be extended with | 345 | * Create a response object. The response object can be extended with |
326 | * header information and then be used any number of times. | 346 | * header information and then be used any number of times. |
diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h index 4e133786..5edf8f98 100644 --- a/src/include/microhttpd.h +++ b/src/include/microhttpd.h | |||
@@ -1201,6 +1201,21 @@ struct MHD_Response *MHD_create_response_from_data (size_t size, | |||
1201 | struct MHD_Response *MHD_create_response_from_fd (size_t size, | 1201 | struct MHD_Response *MHD_create_response_from_fd (size_t size, |
1202 | int fd); | 1202 | int fd); |
1203 | 1203 | ||
1204 | |||
1205 | /** | ||
1206 | * Create a response object. The response object can be extended with | ||
1207 | * header information and then be used any number of times. | ||
1208 | * | ||
1209 | * @param size size of the data portion of the response | ||
1210 | * @param fd file descriptor referring to a file on disk with the data; will be closed when response is destroyed | ||
1211 | * @param off offset to start reading from in the file | ||
1212 | * @return NULL on error (i.e. invalid arguments, out of memory) | ||
1213 | */ | ||
1214 | struct MHD_Response *MHD_create_response_from_fd_at_offset (size_t size, | ||
1215 | int fd, | ||
1216 | off_t offset); | ||
1217 | |||
1218 | |||
1204 | /** | 1219 | /** |
1205 | * Destroy a response object and associated resources. Note that | 1220 | * Destroy a response object and associated resources. Note that |
1206 | * libmicrohttpd may keep some of the resources around if the response | 1221 | * libmicrohttpd may keep some of the resources around if the response |