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