lighttpd 1.4.x https://www.lighttpd.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
5.6 KiB

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
7 years ago
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
7 years ago
  1. #ifndef _CHUNK_H_
  2. #define _CHUNK_H_
  3. #include "first.h"
  4. #ifdef _AIX /*(AIX might #define mmap mmap64)*/
  5. #include "sys-mmap.h"
  6. #endif
  7. #include "buffer.h"
  8. #include "array.h"
  9. /* both should be way smaller than SSIZE_MAX :) */
  10. #define MAX_READ_LIMIT (256*1024)
  11. #define MAX_WRITE_LIMIT (256*1024)
  12. struct log_error_st; /*(declaration)*/
  13. typedef struct chunk {
  14. struct chunk *next;
  15. enum { MEM_CHUNK, FILE_CHUNK } type;
  16. buffer *mem; /* either the storage of the mem-chunk or the name of the file */
  17. /* the size of the chunk is either:
  18. * - mem-chunk: buffer_string_length(chunk::mem) - c->offset
  19. * - file-chunk: chunk::file.length - c->offset
  20. */
  21. off_t offset;
  22. struct {
  23. /* filechunk */
  24. off_t length; /* end pos + 1 in file (octets to send: file.length - c->offset) */
  25. int fd;
  26. int is_temp; /* file is temporary and will be deleted if on cleanup */
  27. struct {
  28. char *start; /* the start pointer of the mmap'ed area */
  29. size_t length; /* size of the mmap'ed area */
  30. off_t offset; /* start is <n> octet away from the start of the file */
  31. } mmap;
  32. void *ref;
  33. void(*refchg)(void *, int);
  34. } file;
  35. } chunk;
  36. typedef struct chunkqueue {
  37. chunk *first;
  38. chunk *last;
  39. off_t bytes_in, bytes_out;
  40. const array *tempdirs;
  41. off_t upload_temp_file_size;
  42. unsigned int tempdir_idx;
  43. } chunkqueue;
  44. __attribute_returns_nonnull__
  45. buffer * chunk_buffer_acquire(void);
  46. void chunk_buffer_release(buffer *b);
  47. size_t chunk_buffer_prepare_append (buffer *b, size_t sz);
  48. void chunkqueue_chunk_pool_clear(void);
  49. void chunkqueue_chunk_pool_free(void);
  50. __attribute_returns_nonnull__
  51. chunkqueue *chunkqueue_init(chunkqueue *cq);
  52. void chunkqueue_set_chunk_size (size_t sz);
  53. void chunkqueue_set_tempdirs_default_reset (void);
  54. void chunkqueue_set_tempdirs_default (const array *tempdirs, off_t upload_temp_file_size);
  55. void chunkqueue_set_tempdirs(chunkqueue * restrict cq, const array * restrict tempdirs, off_t upload_temp_file_size);
  56. void chunkqueue_append_file(chunkqueue * restrict cq, const buffer * restrict fn, off_t offset, off_t len); /* copies "fn" */
  57. void chunkqueue_append_file_fd(chunkqueue * restrict cq, const buffer * restrict fn, int fd, off_t offset, off_t len); /* copies "fn" */
  58. void chunkqueue_append_mem(chunkqueue * restrict cq, const char * restrict mem, size_t len); /* copies memory */
  59. void chunkqueue_append_mem_min(chunkqueue * restrict cq, const char * restrict mem, size_t len); /* copies memory */
  60. void chunkqueue_append_buffer(chunkqueue * restrict cq, buffer * restrict mem); /* may reset "mem" */
  61. void chunkqueue_append_chunkqueue(chunkqueue * restrict cq, chunkqueue * restrict src);
  62. __attribute_returns_nonnull__
  63. buffer * chunkqueue_prepend_buffer_open_sz(chunkqueue *cq, size_t sz);
  64. __attribute_returns_nonnull__
  65. buffer * chunkqueue_prepend_buffer_open(chunkqueue *cq);
  66. void chunkqueue_prepend_buffer_commit(chunkqueue *cq);
  67. __attribute_returns_nonnull__
  68. buffer * chunkqueue_append_buffer_open_sz(chunkqueue *cq, size_t sz);
  69. __attribute_returns_nonnull__
  70. buffer * chunkqueue_append_buffer_open(chunkqueue *cq);
  71. void chunkqueue_append_buffer_commit(chunkqueue *cq);
  72. int chunkqueue_append_mem_to_tempfile(chunkqueue * restrict cq, const char * restrict mem, size_t len, struct log_error_st * const restrict errh);
  73. /* functions to handle buffers to read into: */
  74. /* obtain/reserve memory in chunkqueue at least len (input) size,
  75. * return pointer to memory with len (output) available for use
  76. * modifying the chunkqueue invalidates the memory area.
  77. * should always be followed by chunkqueue_get_memory(),
  78. * even if nothing was read.
  79. * pass 0 in len for mem at least half of chunk_buf_sz
  80. */
  81. __attribute_returns_nonnull__
  82. char * chunkqueue_get_memory(chunkqueue * restrict cq, size_t * restrict len);
  83. /* commit len bytes of mem obtained from chunkqueue_get_memory() */
  84. void chunkqueue_use_memory(chunkqueue * restrict cq, chunk *ckpt, size_t len);
  85. void chunkqueue_update_file(chunkqueue * restrict cq, chunk *c, off_t len);
  86. /* mark first "len" bytes as written (incrementing chunk offsets)
  87. * and remove finished chunks
  88. */
  89. void chunkqueue_mark_written(chunkqueue *cq, off_t len);
  90. void chunkqueue_remove_finished_chunks(chunkqueue *cq);
  91. void chunkqueue_steal(chunkqueue * restrict dest, chunkqueue * restrict src, off_t len);
  92. int chunkqueue_steal_with_tempfiles(chunkqueue * restrict dest, chunkqueue * restrict src, off_t len, struct log_error_st * const restrict errh);
  93. int chunkqueue_open_file_chunk(chunkqueue * restrict cq, struct log_error_st * const restrict errh);
  94. void chunkqueue_compact_mem_offset(chunkqueue *cq);
  95. void chunkqueue_compact_mem(chunkqueue *cq, size_t clen);
  96. void chunkqueue_small_resp_optim (chunkqueue * restrict cq);
  97. ssize_t chunkqueue_write_chunk (int fd, chunkqueue * restrict cq, struct log_error_st * restrict errh);
  98. ssize_t chunkqueue_write_chunk_to_pipe (int fd, chunkqueue * restrict cq, struct log_error_st * restrict errh);
  99. int chunkqueue_peek_data (chunkqueue *cq, char **data, uint32_t *dlen, struct log_error_st * restrict errh);
  100. int chunkqueue_read_data (chunkqueue *cq, char *data, uint32_t dlen, struct log_error_st * restrict errh);
  101. buffer * chunkqueue_read_squash (chunkqueue * restrict cq, struct log_error_st * restrict errh);
  102. __attribute_pure__
  103. static inline off_t chunkqueue_length(const chunkqueue *cq);
  104. static inline off_t chunkqueue_length(const chunkqueue *cq) {
  105. return cq->bytes_in - cq->bytes_out;
  106. }
  107. __attribute_cold__
  108. void chunkqueue_free(chunkqueue *cq);
  109. void chunkqueue_reset(chunkqueue *cq);
  110. __attribute_pure__
  111. static inline int chunkqueue_is_empty(const chunkqueue *cq);
  112. static inline int chunkqueue_is_empty(const chunkqueue *cq) {
  113. return NULL == cq->first;
  114. }
  115. #endif