/src/CMake/Utilities/cmcurl/lib/cw-pause.c
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 | | size_t buf_total; |
71 | | }; |
72 | | |
73 | | static CURLcode cw_pause_write(struct Curl_easy *data, |
74 | | struct Curl_cwriter *writer, int type, |
75 | | const char *buf, size_t nbytes); |
76 | | static void cw_pause_close(struct Curl_easy *data, |
77 | | struct Curl_cwriter *writer); |
78 | | static CURLcode cw_pause_init(struct Curl_easy *data, |
79 | | struct Curl_cwriter *writer); |
80 | | |
81 | | const struct Curl_cwtype Curl_cwt_pause = { |
82 | | "cw-pause", |
83 | | NULL, |
84 | | cw_pause_init, |
85 | | cw_pause_write, |
86 | | cw_pause_close, |
87 | | sizeof(struct cw_pause_ctx) |
88 | | }; |
89 | | |
90 | | static CURLcode cw_pause_init(struct Curl_easy *data, |
91 | | struct Curl_cwriter *writer) |
92 | 0 | { |
93 | 0 | struct cw_pause_ctx *ctx = writer->ctx; |
94 | 0 | (void)data; |
95 | 0 | ctx->buf = NULL; |
96 | 0 | return CURLE_OK; |
97 | 0 | } |
98 | | |
99 | | static void cw_pause_bufs_free(struct cw_pause_ctx *ctx) |
100 | 0 | { |
101 | 0 | while(ctx->buf) { |
102 | 0 | struct cw_pause_buf *next = ctx->buf->next; |
103 | 0 | cw_pause_buf_free(ctx->buf); |
104 | 0 | ctx->buf = next; |
105 | 0 | } |
106 | 0 | } |
107 | | |
108 | | static void cw_pause_close(struct Curl_easy *data, struct Curl_cwriter *writer) |
109 | 0 | { |
110 | 0 | struct cw_pause_ctx *ctx = writer->ctx; |
111 | |
|
112 | 0 | (void)data; |
113 | 0 | cw_pause_bufs_free(ctx); |
114 | 0 | } |
115 | | |
116 | | static CURLcode cw_pause_flush(struct Curl_easy *data, |
117 | | struct Curl_cwriter *cw_pause) |
118 | 0 | { |
119 | 0 | struct cw_pause_ctx *ctx = (struct cw_pause_ctx *)cw_pause; |
120 | 0 | bool decoding = Curl_cwriter_is_content_decoding(data); |
121 | 0 | CURLcode result = CURLE_OK; |
122 | | |
123 | | /* write the end of the chain until it blocks or gets empty */ |
124 | 0 | while(ctx->buf && !Curl_cwriter_is_paused(data)) { |
125 | 0 | struct cw_pause_buf **plast = &ctx->buf; |
126 | 0 | size_t blen, wlen = 0; |
127 | 0 | const unsigned char *buf = NULL; |
128 | |
|
129 | 0 | while((*plast)->next) /* got to last in list */ |
130 | 0 | plast = &(*plast)->next; |
131 | 0 | if(Curl_bufq_peek(&(*plast)->b, &buf, &blen)) { |
132 | 0 | wlen = (decoding && ((*plast)->type & CLIENTWRITE_BODY)) ? |
133 | 0 | CURLMIN(blen, CW_PAUSE_DEC_WRITE_CHUNK) : blen; |
134 | 0 | result = Curl_cwriter_write(data, cw_pause->next, (*plast)->type, |
135 | 0 | (const char *)buf, wlen); |
136 | 0 | CURL_TRC_WRITE(data, "[PAUSE] flushed %zu/%zu bytes, type=%x -> %d", |
137 | 0 | wlen, ctx->buf_total, (*plast)->type, result); |
138 | 0 | Curl_bufq_skip(&(*plast)->b, wlen); |
139 | 0 | DEBUGASSERT(ctx->buf_total >= wlen); |
140 | 0 | ctx->buf_total -= wlen; |
141 | 0 | if(result) |
142 | 0 | return result; |
143 | 0 | } |
144 | 0 | else if((*plast)->type & CLIENTWRITE_EOS) { |
145 | 0 | result = Curl_cwriter_write(data, cw_pause->next, (*plast)->type, |
146 | 0 | (const char *)buf, 0); |
147 | 0 | CURL_TRC_WRITE(data, "[PAUSE] flushed 0/%zu bytes, type=%x -> %d", |
148 | 0 | ctx->buf_total, (*plast)->type, result); |
149 | 0 | } |
150 | | |
151 | 0 | if(Curl_bufq_is_empty(&(*plast)->b)) { |
152 | 0 | cw_pause_buf_free(*plast); |
153 | 0 | *plast = NULL; |
154 | 0 | } |
155 | 0 | } |
156 | 0 | return result; |
157 | 0 | } |
158 | | |
159 | | static CURLcode cw_pause_write(struct Curl_easy *data, |
160 | | struct Curl_cwriter *writer, int type, |
161 | | const char *buf, size_t blen) |
162 | 0 | { |
163 | 0 | struct cw_pause_ctx *ctx = writer->ctx; |
164 | 0 | CURLcode result = CURLE_OK; |
165 | 0 | size_t wlen = 0; |
166 | 0 | bool decoding = Curl_cwriter_is_content_decoding(data); |
167 | |
|
168 | 0 | if(ctx->buf && !Curl_cwriter_is_paused(data)) { |
169 | 0 | result = cw_pause_flush(data, writer); |
170 | 0 | if(result) |
171 | 0 | return result; |
172 | 0 | } |
173 | | |
174 | 0 | while(!ctx->buf && !Curl_cwriter_is_paused(data)) { |
175 | 0 | int wtype = type; |
176 | 0 | DEBUGASSERT(!ctx->buf); |
177 | | /* content decoding might blow up size considerably, write smaller |
178 | | * chunks to make pausing need buffer less. */ |
179 | 0 | wlen = (decoding && (type & CLIENTWRITE_BODY)) ? |
180 | 0 | CURLMIN(blen, CW_PAUSE_DEC_WRITE_CHUNK) : blen; |
181 | 0 | if(wlen < blen) |
182 | 0 | wtype &= ~CLIENTWRITE_EOS; |
183 | 0 | result = Curl_cwriter_write(data, writer->next, wtype, buf, wlen); |
184 | 0 | CURL_TRC_WRITE(data, "[PAUSE] writing %zu/%zu bytes of type %x -> %d", |
185 | 0 | wlen, blen, wtype, result); |
186 | 0 | if(result) |
187 | 0 | return result; |
188 | 0 | buf += wlen; |
189 | 0 | blen -= wlen; |
190 | 0 | if(!blen) |
191 | 0 | return result; |
192 | 0 | } |
193 | | |
194 | 0 | do { |
195 | 0 | size_t nwritten = 0; |
196 | 0 | if(ctx->buf && (ctx->buf->type == type) && (type & CLIENTWRITE_BODY)) { |
197 | | /* same type and body, append to current buffer which has a soft |
198 | | * limit and should take everything up to OOM. */ |
199 | 0 | result = Curl_bufq_cwrite(&ctx->buf->b, buf, blen, &nwritten); |
200 | 0 | } |
201 | 0 | else { |
202 | | /* Need a new buf, type changed */ |
203 | 0 | struct cw_pause_buf *cwbuf = cw_pause_buf_create(type, blen); |
204 | 0 | if(!cwbuf) |
205 | 0 | return CURLE_OUT_OF_MEMORY; |
206 | 0 | cwbuf->next = ctx->buf; |
207 | 0 | ctx->buf = cwbuf; |
208 | 0 | result = Curl_bufq_cwrite(&ctx->buf->b, buf, blen, &nwritten); |
209 | 0 | } |
210 | 0 | CURL_TRC_WRITE(data, "[PAUSE] buffer %zu more bytes of type %x, " |
211 | 0 | "total=%zu -> %d", nwritten, type, ctx->buf_total + wlen, |
212 | 0 | result); |
213 | 0 | if(result) |
214 | 0 | return result; |
215 | 0 | buf += nwritten; |
216 | 0 | blen -= nwritten; |
217 | 0 | ctx->buf_total += nwritten; |
218 | 0 | } while(blen); |
219 | | |
220 | 0 | return result; |
221 | 0 | } |
222 | | |
223 | | CURLcode Curl_cw_pause_flush(struct Curl_easy *data) |
224 | 0 | { |
225 | 0 | struct Curl_cwriter *cw_pause; |
226 | 0 | CURLcode result = CURLE_OK; |
227 | |
|
228 | 0 | cw_pause = Curl_cwriter_get_by_type(data, &Curl_cwt_pause); |
229 | 0 | if(cw_pause) |
230 | 0 | result = cw_pause_flush(data, cw_pause); |
231 | |
|
232 | 0 | return result; |
233 | 0 | } |