cw-pause.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #include "urldata.h"
  27. #include "bufq.h"
  28. #include "cfilters.h"
  29. #include "headers.h"
  30. #include "multiif.h"
  31. #include "sendf.h"
  32. #include "cw-pause.h"
  33. /* The last 3 #include files should be in this order */
  34. #include "curl_printf.h"
  35. #include "curl_memory.h"
  36. #include "memdebug.h"
  37. /* body dynbuf sizes */
  38. #define CW_PAUSE_BUF_CHUNK (16 * 1024)
  39. /* when content decoding, write data in chunks */
  40. #define CW_PAUSE_DEC_WRITE_CHUNK (4096)
  41. struct cw_pause_buf {
  42. struct cw_pause_buf *next;
  43. struct bufq b;
  44. int type;
  45. };
  46. static struct cw_pause_buf *cw_pause_buf_create(int type, size_t buflen)
  47. {
  48. struct cw_pause_buf *cwbuf = calloc(1, sizeof(*cwbuf));
  49. if(cwbuf) {
  50. cwbuf->type = type;
  51. if(type & CLIENTWRITE_BODY)
  52. Curl_bufq_init2(&cwbuf->b, CW_PAUSE_BUF_CHUNK, 1,
  53. (BUFQ_OPT_SOFT_LIMIT|BUFQ_OPT_NO_SPARES));
  54. else
  55. Curl_bufq_init(&cwbuf->b, buflen, 1);
  56. }
  57. return cwbuf;
  58. }
  59. static void cw_pause_buf_free(struct cw_pause_buf *cwbuf)
  60. {
  61. if(cwbuf) {
  62. Curl_bufq_free(&cwbuf->b);
  63. free(cwbuf);
  64. }
  65. }
  66. struct cw_pause_ctx {
  67. struct Curl_cwriter super;
  68. struct cw_pause_buf *buf;
  69. size_t buf_total;
  70. };
  71. static CURLcode cw_pause_write(struct Curl_easy *data,
  72. struct Curl_cwriter *writer, int type,
  73. const char *buf, size_t nbytes);
  74. static void cw_pause_close(struct Curl_easy *data,
  75. struct Curl_cwriter *writer);
  76. static CURLcode cw_pause_init(struct Curl_easy *data,
  77. struct Curl_cwriter *writer);
  78. const struct Curl_cwtype Curl_cwt_pause = {
  79. "cw-pause",
  80. NULL,
  81. cw_pause_init,
  82. cw_pause_write,
  83. cw_pause_close,
  84. sizeof(struct cw_pause_ctx)
  85. };
  86. static CURLcode cw_pause_init(struct Curl_easy *data,
  87. struct Curl_cwriter *writer)
  88. {
  89. struct cw_pause_ctx *ctx = writer->ctx;
  90. (void)data;
  91. ctx->buf = NULL;
  92. return CURLE_OK;
  93. }
  94. static void cw_pause_bufs_free(struct cw_pause_ctx *ctx)
  95. {
  96. while(ctx->buf) {
  97. struct cw_pause_buf *next = ctx->buf->next;
  98. cw_pause_buf_free(ctx->buf);
  99. ctx->buf = next;
  100. }
  101. }
  102. static void cw_pause_close(struct Curl_easy *data, struct Curl_cwriter *writer)
  103. {
  104. struct cw_pause_ctx *ctx = writer->ctx;
  105. (void)data;
  106. cw_pause_bufs_free(ctx);
  107. }
  108. static CURLcode cw_pause_flush(struct Curl_easy *data,
  109. struct Curl_cwriter *cw_pause)
  110. {
  111. struct cw_pause_ctx *ctx = (struct cw_pause_ctx *)cw_pause;
  112. bool decoding = Curl_cwriter_is_content_decoding(data);
  113. CURLcode result = CURLE_OK;
  114. /* write the end of the chain until it blocks or gets empty */
  115. while(ctx->buf && !Curl_cwriter_is_paused(data)) {
  116. struct cw_pause_buf **plast = &ctx->buf;
  117. size_t blen, wlen = 0;
  118. const unsigned char *buf = NULL;
  119. while((*plast)->next) /* got to last in list */
  120. plast = &(*plast)->next;
  121. if(Curl_bufq_peek(&(*plast)->b, &buf, &blen)) {
  122. wlen = (decoding && ((*plast)->type & CLIENTWRITE_BODY)) ?
  123. CURLMIN(blen, CW_PAUSE_DEC_WRITE_CHUNK) : blen;
  124. result = Curl_cwriter_write(data, cw_pause->next, (*plast)->type,
  125. (const char *)buf, wlen);
  126. CURL_TRC_WRITE(data, "[PAUSE] flushed %zu/%zu bytes, type=%x -> %d",
  127. wlen, ctx->buf_total, (*plast)->type, result);
  128. Curl_bufq_skip(&(*plast)->b, wlen);
  129. DEBUGASSERT(ctx->buf_total >= wlen);
  130. ctx->buf_total -= wlen;
  131. if(result)
  132. return result;
  133. }
  134. else if((*plast)->type & CLIENTWRITE_EOS) {
  135. result = Curl_cwriter_write(data, cw_pause->next, (*plast)->type,
  136. (const char *)buf, 0);
  137. CURL_TRC_WRITE(data, "[PAUSE] flushed 0/%zu bytes, type=%x -> %d",
  138. ctx->buf_total, (*plast)->type, result);
  139. }
  140. if(Curl_bufq_is_empty(&(*plast)->b)) {
  141. cw_pause_buf_free(*plast);
  142. *plast = NULL;
  143. }
  144. }
  145. return result;
  146. }
  147. static CURLcode cw_pause_write(struct Curl_easy *data,
  148. struct Curl_cwriter *writer, int type,
  149. const char *buf, size_t blen)
  150. {
  151. struct cw_pause_ctx *ctx = writer->ctx;
  152. CURLcode result = CURLE_OK;
  153. size_t wlen = 0;
  154. bool decoding = Curl_cwriter_is_content_decoding(data);
  155. if(ctx->buf && !Curl_cwriter_is_paused(data)) {
  156. result = cw_pause_flush(data, writer);
  157. if(result)
  158. return result;
  159. }
  160. while(!ctx->buf && !Curl_cwriter_is_paused(data)) {
  161. int wtype = type;
  162. DEBUGASSERT(!ctx->buf);
  163. /* content decoding might blow up size considerably, write smaller
  164. * chunks to make pausing need buffer less. */
  165. wlen = (decoding && (type & CLIENTWRITE_BODY)) ?
  166. CURLMIN(blen, CW_PAUSE_DEC_WRITE_CHUNK) : blen;
  167. if(wlen < blen)
  168. wtype &= ~CLIENTWRITE_EOS;
  169. result = Curl_cwriter_write(data, writer->next, wtype, buf, wlen);
  170. CURL_TRC_WRITE(data, "[PAUSE] writing %zu/%zu bytes of type %x -> %d",
  171. wlen, blen, wtype, result);
  172. if(result)
  173. return result;
  174. buf += wlen;
  175. blen -= wlen;
  176. if(!blen)
  177. return result;
  178. }
  179. do {
  180. size_t nwritten = 0;
  181. if(ctx->buf && (ctx->buf->type == type) && (type & CLIENTWRITE_BODY)) {
  182. /* same type and body, append to current buffer which has a soft
  183. * limit and should take everything up to OOM. */
  184. result = Curl_bufq_cwrite(&ctx->buf->b, buf, blen, &nwritten);
  185. }
  186. else {
  187. /* Need a new buf, type changed */
  188. struct cw_pause_buf *cwbuf = cw_pause_buf_create(type, blen);
  189. if(!cwbuf)
  190. return CURLE_OUT_OF_MEMORY;
  191. cwbuf->next = ctx->buf;
  192. ctx->buf = cwbuf;
  193. result = Curl_bufq_cwrite(&ctx->buf->b, buf, blen, &nwritten);
  194. }
  195. CURL_TRC_WRITE(data, "[PAUSE] buffer %zu more bytes of type %x, "
  196. "total=%zu -> %d", nwritten, type, ctx->buf_total + wlen,
  197. result);
  198. if(result)
  199. return result;
  200. buf += nwritten;
  201. blen -= nwritten;
  202. ctx->buf_total += nwritten;
  203. } while(blen);
  204. return result;
  205. }
  206. CURLcode Curl_cw_pause_flush(struct Curl_easy *data)
  207. {
  208. struct Curl_cwriter *cw_pause;
  209. CURLcode result = CURLE_OK;
  210. cw_pause = Curl_cwriter_get_by_type(data, &Curl_cwt_pause);
  211. if(cw_pause)
  212. result = cw_pause_flush(data, cw_pause);
  213. return result;
  214. }