lighttpd1.4/src/chunk.h

151 lines
5.3 KiB
C
Raw Normal View History

#ifndef _CHUNK_H_
#define _CHUNK_H_
#include "first.h"
#ifdef _AIX /*(AIX might #define mmap mmap64)*/
#include "sys-mmap.h"
#endif
#include "buffer.h"
#include "array.h"
/* both should be way smaller than SSIZE_MAX :) */
#define MAX_READ_LIMIT (256*1024)
#define MAX_WRITE_LIMIT (256*1024)
struct log_error_st; /*(declaration)*/
typedef struct chunk {
struct chunk *next;
fix buffer, chunk and http_chunk API * remove unused structs and functions (buffer_array, read_buffer) * change return type from int to void for many functions, as the return value (indicating error/success) was never checked, and the function would only fail on programming errors and not on invalid input; changed functions to use force_assert instead of returning an error. * all "len" parameters now are the real size of the memory to be read. the length of strings is given always without the terminating 0. * the "buffer" struct still counts the terminating 0 in ->used, provide buffer_string_length() to get the length of a string in a buffer. unset config "strings" have used == 0, which is used in some places to distinguish unset values from "" (empty string) values. * most buffer usages should now use it as string container. * optimise some buffer copying by "moving" data to other buffers * use (u)intmax_t for generic int-to-string functions * remove unused enum values: UNUSED_CHUNK, ENCODING_UNSET * converted BUFFER_APPEND_SLASH to inline function (no macro feature needed) * refactor: create chunkqueue_steal: moving (partial) chunks into another queue * http_chunk: added separate function to terminate chunked body instead of magic handling in http_chunk_append_mem(). http_chunk_append_* now handle empty chunks, and never terminate the chunked body. From: Stefan Bühler <stbuehler@web.de> git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@2975 152afb58-edef-0310-8abb-c4023f1b3aa9
2015-02-08 12:37:10 +00:00
enum { MEM_CHUNK, FILE_CHUNK } type;
buffer *mem; /* either the storage of the mem-chunk or the name of the file */
/* the size of the chunk is either:
* - mem-chunk: buffer_string_length(chunk::mem) - c->offset
* - file-chunk: chunk::file.length - c->offset
*/
off_t offset;
struct {
/* filechunk */
off_t length; /* end pos + 1 in file (octets to send: file.length - c->offset) */
int fd;
int is_temp; /* file is temporary and will be deleted if on cleanup */
struct {
char *start; /* the start pointer of the mmap'ed area */
size_t length; /* size of the mmap'ed area */
off_t offset; /* start is <n> octet away from the start of the file */
} mmap;
void *ref;
void(*refchg)(void *, int);
} file;
} chunk;
typedef struct chunkqueue {
chunk *first;
chunk *last;
off_t bytes_in, bytes_out;
2019-11-16 01:26:54 +00:00
const array *tempdirs;
2019-03-16 05:39:59 +00:00
off_t upload_temp_file_size;
unsigned int tempdir_idx;
} chunkqueue;
2019-10-09 03:31:34 +00:00
__attribute_returns_nonnull__
buffer * chunk_buffer_acquire(void);
2019-10-09 03:31:34 +00:00
void chunk_buffer_release(buffer *b);
void chunkqueue_chunk_pool_clear(void);
void chunkqueue_chunk_pool_free(void);
2019-10-09 03:31:34 +00:00
__attribute_returns_nonnull__
chunkqueue *chunkqueue_init(chunkqueue *cq);
2019-10-09 03:31:34 +00:00
void chunkqueue_set_chunk_size (size_t sz);
void chunkqueue_set_tempdirs_default_reset (void);
2019-11-16 01:26:54 +00:00
void chunkqueue_set_tempdirs_default (const array *tempdirs, off_t upload_temp_file_size);
void chunkqueue_set_tempdirs(chunkqueue * restrict cq, const array * restrict tempdirs, off_t upload_temp_file_size);
void chunkqueue_append_file(chunkqueue * restrict cq, const buffer * restrict fn, off_t offset, off_t len); /* copies "fn" */
void chunkqueue_append_file_fd(chunkqueue * restrict cq, const buffer * restrict fn, int fd, off_t offset, off_t len); /* copies "fn" */
void chunkqueue_append_mem(chunkqueue * restrict cq, const char * restrict mem, size_t len); /* copies memory */
void chunkqueue_append_mem_min(chunkqueue * restrict cq, const char * restrict mem, size_t len); /* copies memory */
void chunkqueue_append_buffer(chunkqueue * restrict cq, buffer * restrict mem); /* may reset "mem" */
void chunkqueue_append_chunkqueue(chunkqueue * restrict cq, chunkqueue * restrict src);
2019-10-09 03:31:34 +00:00
__attribute_returns_nonnull__
buffer * chunkqueue_prepend_buffer_open_sz(chunkqueue *cq, size_t sz);
2019-10-09 03:31:34 +00:00
__attribute_returns_nonnull__
buffer * chunkqueue_prepend_buffer_open(chunkqueue *cq);
2019-10-09 03:31:34 +00:00
void chunkqueue_prepend_buffer_commit(chunkqueue *cq);
2019-10-09 03:31:34 +00:00
__attribute_returns_nonnull__
buffer * chunkqueue_append_buffer_open_sz(chunkqueue *cq, size_t sz);
2019-10-09 03:31:34 +00:00
__attribute_returns_nonnull__
buffer * chunkqueue_append_buffer_open(chunkqueue *cq);
2019-10-09 03:31:34 +00:00
void chunkqueue_append_buffer_commit(chunkqueue *cq);
int chunkqueue_append_mem_to_tempfile(chunkqueue * restrict cq, const char * restrict mem, size_t len, struct log_error_st * const restrict errh);
/* functions to handle buffers to read into: */
/* obtain/reserve memory in chunkqueue at least len (input) size,
* return pointer to memory with len (output) available for use
* modifying the chunkqueue invalidates the memory area.
* should always be followed by chunkqueue_get_memory(),
* even if nothing was read.
* pass 0 in len for mem at least half of chunk_buf_sz
*/
2019-10-09 03:31:34 +00:00
__attribute_returns_nonnull__
char * chunkqueue_get_memory(chunkqueue * restrict cq, size_t * restrict len);
/* commit len bytes of mem obtained from chunkqueue_get_memory() */
void chunkqueue_use_memory(chunkqueue * restrict cq, chunk *ckpt, size_t len);
void chunkqueue_update_file(chunkqueue * restrict cq, chunk *c, off_t len);
/* mark first "len" bytes as written (incrementing chunk offsets)
* and remove finished chunks
*/
void chunkqueue_mark_written(chunkqueue *cq, off_t len);
fix buffer, chunk and http_chunk API * remove unused structs and functions (buffer_array, read_buffer) * change return type from int to void for many functions, as the return value (indicating error/success) was never checked, and the function would only fail on programming errors and not on invalid input; changed functions to use force_assert instead of returning an error. * all "len" parameters now are the real size of the memory to be read. the length of strings is given always without the terminating 0. * the "buffer" struct still counts the terminating 0 in ->used, provide buffer_string_length() to get the length of a string in a buffer. unset config "strings" have used == 0, which is used in some places to distinguish unset values from "" (empty string) values. * most buffer usages should now use it as string container. * optimise some buffer copying by "moving" data to other buffers * use (u)intmax_t for generic int-to-string functions * remove unused enum values: UNUSED_CHUNK, ENCODING_UNSET * converted BUFFER_APPEND_SLASH to inline function (no macro feature needed) * refactor: create chunkqueue_steal: moving (partial) chunks into another queue * http_chunk: added separate function to terminate chunked body instead of magic handling in http_chunk_append_mem(). http_chunk_append_* now handle empty chunks, and never terminate the chunked body. From: Stefan Bühler <stbuehler@web.de> git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@2975 152afb58-edef-0310-8abb-c4023f1b3aa9
2015-02-08 12:37:10 +00:00
void chunkqueue_remove_finished_chunks(chunkqueue *cq);
void chunkqueue_steal(chunkqueue * restrict dest, chunkqueue * restrict src, off_t len);
int chunkqueue_steal_with_tempfiles(chunkqueue * restrict dest, chunkqueue * restrict src, off_t len, struct log_error_st * const restrict errh);
int chunkqueue_open_file_chunk(chunkqueue * restrict cq, struct log_error_st * const restrict errh);
2020-10-11 17:56:54 +00:00
void chunkqueue_compact_mem_offset(chunkqueue *cq);
2019-07-14 07:37:06 +00:00
void chunkqueue_compact_mem(chunkqueue *cq, size_t clen);
void chunkqueue_small_resp_optim (chunkqueue * restrict cq);
int chunkqueue_peek_data (chunkqueue *cq, char **data, uint32_t *dlen, struct log_error_st * restrict errh);
int chunkqueue_read_data (chunkqueue *cq, char *data, uint32_t dlen, struct log_error_st * restrict errh);
buffer * chunkqueue_read_squash (chunkqueue * restrict cq, struct log_error_st * restrict errh);
__attribute_pure__
2020-09-08 05:15:25 +00:00
static inline off_t chunkqueue_length(const chunkqueue *cq);
static inline off_t chunkqueue_length(const chunkqueue *cq) {
return cq->bytes_in - cq->bytes_out;
}
__attribute_cold__
void chunkqueue_free(chunkqueue *cq);
void chunkqueue_reset(chunkqueue *cq);
__attribute_pure__
2017-10-09 04:41:06 +00:00
static inline int chunkqueue_is_empty(const chunkqueue *cq);
static inline int chunkqueue_is_empty(const chunkqueue *cq) {
return NULL == cq->first;
}
#endif