cw-pause.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 2 #include files should be in this order */
  34. #include "curl_memory.h"
  35. #include "memdebug.h"
  36. /* body dynbuf sizes */
  37. #define CW_PAUSE_BUF_CHUNK (16 * 1024)
  38. /* when content decoding, write data in chunks */
  39. #define CW_PAUSE_DEC_WRITE_CHUNK (4096)
  40. struct cw_pause_buf {
  41. struct cw_pause_buf *next;
  42. struct bufq b;
  43. int type;
  44. };
  45. static struct cw_pause_buf *cw_pause_buf_create(int type, size_t buflen)
  46. {
  47. struct cw_pause_buf *cwbuf = calloc(1, sizeof(*cwbuf));
  48. if(cwbuf) {
  49. cwbuf->type = type;
  50. if(type & CLIENTWRITE_BODY)
  51. Curl_bufq_init2(&cwbuf->b, CW_PAUSE_BUF_CHUNK, 1,
  52. (BUFQ_OPT_SOFT_LIMIT|BUFQ_OPT_NO_SPARES));
  53. else
  54. Curl_bufq_init(&cwbuf->b, buflen, 1);
  55. }
  56. return cwbuf;
  57. }
  58. static void cw_pause_buf_free(struct cw_pause_buf *cwbuf)
  59. {
  60. if(cwbuf) {
  61. Curl_bufq_free(&cwbuf->b);
  62. free(cwbuf);
  63. }
  64. }
  65. struct cw_pause_ctx {
  66. struct Curl_cwriter super;
  67. struct cw_pause_buf *buf;
  68. size_t buf_total;
  69. };
  70. static CURLcode cw_pause_write(struct Curl_easy *data,
  71. struct Curl_cwriter *writer, int type,
  72. const char *buf, size_t nbytes);
  73. static void cw_pause_close(struct Curl_easy *data,
  74. struct Curl_cwriter *writer);
  75. static CURLcode cw_pause_init(struct Curl_easy *data,
  76. struct Curl_cwriter *writer);
  77. const struct Curl_cwtype Curl_cwt_pause = {
  78. "cw-pause",
  79. NULL,
  80. cw_pause_init,
  81. cw_pause_write,
  82. cw_pause_close,
  83. sizeof(struct cw_pause_ctx)
  84. };
  85. static CURLcode cw_pause_init(struct Curl_easy *data,
  86. struct Curl_cwriter *writer)
  87. {
  88. struct cw_pause_ctx *ctx = writer->ctx;
  89. (void)data;
  90. ctx->buf = NULL;
  91. return CURLE_OK;
  92. }
  93. static void cw_pause_bufs_free(struct cw_pause_ctx *ctx)
  94. {
  95. while(ctx->buf) {
  96. struct cw_pause_buf *next = ctx->buf->next;
  97. cw_pause_buf_free(ctx->buf);
  98. ctx->buf = next;
  99. }
  100. }
  101. static void cw_pause_close(struct Curl_easy *data, struct Curl_cwriter *writer)
  102. {
  103. struct cw_pause_ctx *ctx = writer->ctx;
  104. (void)data;
  105. cw_pause_bufs_free(ctx);
  106. }
  107. static CURLcode cw_pause_flush(struct Curl_easy *data,
  108. struct Curl_cwriter *cw_pause)
  109. {
  110. struct cw_pause_ctx *ctx = (struct cw_pause_ctx *)cw_pause;
  111. bool decoding = Curl_cwriter_is_content_decoding(data);
  112. CURLcode result = CURLE_OK;
  113. /* write the end of the chain until it blocks or gets empty */
  114. while(ctx->buf && !Curl_cwriter_is_paused(data)) {
  115. struct cw_pause_buf **plast = &ctx->buf;
  116. size_t blen, wlen = 0;
  117. const unsigned char *buf = NULL;
  118. while((*plast)->next) /* got to last in list */
  119. plast = &(*plast)->next;
  120. if(Curl_bufq_peek(&(*plast)->b, &buf, &blen)) {
  121. wlen = (decoding && ((*plast)->type & CLIENTWRITE_BODY)) ?
  122. CURLMIN(blen, CW_PAUSE_DEC_WRITE_CHUNK) : blen;
  123. result = Curl_cwriter_write(data, cw_pause->next, (*plast)->type,
  124. (const char *)buf, wlen);
  125. CURL_TRC_WRITE(data, "[PAUSE] flushed %zu/%zu bytes, type=%x -> %d",
  126. wlen, ctx->buf_total, (*plast)->type, result);
  127. Curl_bufq_skip(&(*plast)->b, wlen);
  128. DEBUGASSERT(ctx->buf_total >= wlen);
  129. ctx->buf_total -= wlen;
  130. if(result)
  131. return result;
  132. }
  133. else if((*plast)->type & CLIENTWRITE_EOS) {
  134. result = Curl_cwriter_write(data, cw_pause->next, (*plast)->type,
  135. (const char *)buf, 0);
  136. CURL_TRC_WRITE(data, "[PAUSE] flushed 0/%zu bytes, type=%x -> %d",
  137. ctx->buf_total, (*plast)->type, result);
  138. }
  139. if(Curl_bufq_is_empty(&(*plast)->b)) {
  140. cw_pause_buf_free(*plast);
  141. *plast = NULL;
  142. }
  143. }
  144. return result;
  145. }
  146. static CURLcode cw_pause_write(struct Curl_easy *data,
  147. struct Curl_cwriter *writer, int type,
  148. const char *buf, size_t blen)
  149. {
  150. struct cw_pause_ctx *ctx = writer->ctx;
  151. CURLcode result = CURLE_OK;
  152. size_t wlen = 0;
  153. bool decoding = Curl_cwriter_is_content_decoding(data);
  154. if(ctx->buf && !Curl_cwriter_is_paused(data)) {
  155. result = cw_pause_flush(data, writer);
  156. if(result)
  157. return result;
  158. }
  159. while(!ctx->buf && !Curl_cwriter_is_paused(data)) {
  160. int wtype = type;
  161. DEBUGASSERT(!ctx->buf);
  162. /* content decoding might blow up size considerably, write smaller
  163. * chunks to make pausing need buffer less. */
  164. wlen = (decoding && (type & CLIENTWRITE_BODY)) ?
  165. CURLMIN(blen, CW_PAUSE_DEC_WRITE_CHUNK) : blen;
  166. if(wlen < blen)
  167. wtype &= ~CLIENTWRITE_EOS;
  168. result = Curl_cwriter_write(data, writer->next, wtype, buf, wlen);
  169. CURL_TRC_WRITE(data, "[PAUSE] writing %zu/%zu bytes of type %x -> %d",
  170. wlen, blen, wtype, result);
  171. if(result)
  172. return result;
  173. buf += wlen;
  174. blen -= wlen;
  175. if(!blen)
  176. return result;
  177. }
  178. do {
  179. size_t nwritten = 0;
  180. if(ctx->buf && (ctx->buf->type == type) && (type & CLIENTWRITE_BODY)) {
  181. /* same type and body, append to current buffer which has a soft
  182. * limit and should take everything up to OOM. */
  183. result = Curl_bufq_cwrite(&ctx->buf->b, buf, blen, &nwritten);
  184. }
  185. else {
  186. /* Need a new buf, type changed */
  187. struct cw_pause_buf *cwbuf = cw_pause_buf_create(type, blen);
  188. if(!cwbuf)
  189. return CURLE_OUT_OF_MEMORY;
  190. cwbuf->next = ctx->buf;
  191. ctx->buf = cwbuf;
  192. result = Curl_bufq_cwrite(&ctx->buf->b, buf, blen, &nwritten);
  193. }
  194. CURL_TRC_WRITE(data, "[PAUSE] buffer %zu more bytes of type %x, "
  195. "total=%zu -> %d", nwritten, type, ctx->buf_total + wlen,
  196. result);
  197. if(result)
  198. return result;
  199. buf += nwritten;
  200. blen -= nwritten;
  201. ctx->buf_total += nwritten;
  202. } while(blen);
  203. return result;
  204. }
  205. CURLcode Curl_cw_pause_flush(struct Curl_easy *data)
  206. {
  207. struct Curl_cwriter *cw_pause;
  208. CURLcode result = CURLE_OK;
  209. cw_pause = Curl_cwriter_get_by_type(data, &Curl_cwt_pause);
  210. if(cw_pause)
  211. result = cw_pause_flush(data, cw_pause);
  212. return result;
  213. }