aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-12-19 18:54:44 +0000
committerChristian Grothoff <christian@grothoff.org>2010-12-19 18:54:44 +0000
commit5022cec769294a488cbb65c7ad81fb78978631e1 (patch)
treefe9cb4031645204b28327d4c12ae4dae5714bfb6
parent0f9d6e9946afc5fce3f6cec62c43fec091f6ca4d (diff)
downloadlibmicrohttpd-5022cec769294a488cbb65c7ad81fb78978631e1.tar.gz
libmicrohttpd-5022cec769294a488cbb65c7ad81fb78978631e1.zip
support for sendfile with offset
-rw-r--r--ChangeLog3
-rw-r--r--doc/microhttpd.texi30
-rw-r--r--src/daemon/daemon.c2
-rw-r--r--src/daemon/internal.h5
-rw-r--r--src/daemon/response.c26
-rw-r--r--src/include/microhttpd.h15
6 files changed, 75 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index 81376826..96c787d1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,6 @@
1Sun Dec 19 19:54:15 CET 2010
2 Added 'MHD_create_response_from_fd_at_offset'. -CG
3
1Sun Dec 19 15:16:16 CET 2010 4Sun Dec 19 15:16:16 CET 2010
2 Fixing --enable and --disable configure options to behave properly. -CG 5 Fixing --enable and --disable configure options to behave properly. -CG
3 6
diff --git a/doc/microhttpd.texi b/doc/microhttpd.texi
index 883018f1..0298dfde 100644
--- a/doc/microhttpd.texi
+++ b/doc/microhttpd.texi
@@ -1287,11 +1287,37 @@ header information and then it can be used any number of times.
1287 1287
1288@table @var 1288@table @var
1289@item size 1289@item size
1290size of the data portion of the response, @code{-1} for unknown; 1290size of the data portion of the response (should be smaller or equal to the
1291size of the file)
1292
1293@item fd
1294file descriptor referring to a file on disk with the data; will be
1295closed when response is destroyed; note that 'fd' must be an actual
1296file descriptor (not a pipe or socket) since MHD might use 'sendfile'
1297or 'seek' on it
1298@end table
1299
1300Return @mynull{} on error (i.e. invalid arguments, out of memory).
1301@end deftypefun
1302
1303
1304@deftypefun {struct MHD_Response *} MHD_create_response_from_fd_at_offset (uint64_t size, int fd, off_t offset)
1305Create a response object. The response object can be extended with
1306header information and then it can be used any number of times.
1307
1308@table @var
1309@item size
1310size of the data portion of the response (number of bytes to transmit from the
1311file starting at offset).
1291 1312
1292@item fd 1313@item fd
1293file descriptor referring to a file on disk with the data; will be 1314file descriptor referring to a file on disk with the data; will be
1294closed when response is destroyed 1315closed when response is destroyed; note that 'fd' must be an actual
1316file descriptor (not a pipe or socket) since MHD might use 'sendfile'
1317or 'seek' on it
1318
1319@item offset
1320offset to start reading from in the file
1295@end table 1321@end table
1296 1322
1297Return @mynull{} on error (i.e. invalid arguments, out of memory). 1323Return @mynull{} on error (i.e. invalid arguments, out of memory).
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 */
306struct MHD_Response *MHD_create_response_from_fd (size_t size, 307struct 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 */
337struct 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,
1201struct MHD_Response *MHD_create_response_from_fd (size_t size, 1201struct 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 */
1214struct 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