1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
//! HTTP レスポンスデコーダー
//!
//! # RFC 非準拠
//!
//! - RFC 9112 Section 2.2: HTTP/1.1 メッセージはオクテット列として解析すべき (SHOULD) だが、
//! 本実装では UTF-8 として強制的に解析している。非 UTF-8 バイト列を含むレスポンスは
//! エラーとして拒否される。
use crate::compression::{CompressionStatus, Decompressor, NoCompression};
use crate::error::Error;
use crate::limits::DecoderLimits;
use crate::response::Response;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use crate::validate::{is_valid_protocol_version, is_valid_reason_phrase, is_valid_status_code};
use super::body::{
BodyDecoder, BodyKind, BodyProgress, TransferEncodingResult, find_line, parse_header_line,
resolve_body_headers_for_response,
};
use super::buffer;
use super::head::ResponseHead;
use super::phase::DecodePhase;
/// HTTP レスポンスデコーダー (Sans I/O)
///
/// クライアント側でサーバーからのレスポンスをパースする際に使用
///
/// # 型パラメータ
///
/// - `D`: 展開器の型。デフォルトは `NoCompression`(展開なし)。
///
/// # 使い方
///
/// ## 展開なし(既存 API 互換)
///
/// ```rust
/// use shiguredo_http11::ResponseDecoder;
///
/// let mut decoder = ResponseDecoder::new();
/// ```
///
/// ## 展開あり
///
/// ```ignore
/// use shiguredo_http11::ResponseDecoder;
///
/// let mut decoder = ResponseDecoder::with_decompressor(GzipDecompressor::new());
/// ```
#[derive(Debug)]
pub struct ResponseDecoder<D: Decompressor = NoCompression> {
buf: Vec<u8>,
phase: DecodePhase,
start_line: Option<String>,
headers: Vec<(String, String)>,
body_decoder: BodyDecoder,
limits: DecoderLimits,
/// HEAD リクエストへのレスポンスかどうか
expect_no_body: bool,
/// ステータスコード(ヘッダーデコード後に保持)
status_code: u16,
/// decode() 用: デコード済みヘッダー
decoded_head: Option<ResponseHead>,
/// decode() 用: ボディ種別
decoded_body_kind: Option<BodyKind>,
/// decode() 用: デコード済みボディ
decoded_body: Vec<u8>,
/// 展開器
decompressor: D,
/// リクエストメソッド (CONNECT トンネル判定用)
request_method: Option<String>,
/// `mut_buf` で確保した未確定領域のバイト数
pending: usize,
}
impl Default for ResponseDecoder<NoCompression> {
fn default() -> Self {
Self::new()
}
}
impl ResponseDecoder<NoCompression> {
/// 新しいデコーダーを作成
pub fn new() -> Self {
Self {
buf: Vec::new(),
phase: DecodePhase::StartLine,
start_line: None,
headers: Vec::new(),
body_decoder: BodyDecoder::new(),
limits: DecoderLimits::default(),
expect_no_body: false,
status_code: 0,
decoded_head: None,
decoded_body_kind: None,
decoded_body: Vec::new(),
decompressor: NoCompression::new(),
request_method: None,
pending: 0,
}
}
/// 制限付きでデコーダーを作成
pub fn with_limits(limits: DecoderLimits) -> Self {
Self {
buf: Vec::new(),
phase: DecodePhase::StartLine,
start_line: None,
headers: Vec::new(),
body_decoder: BodyDecoder::new(),
limits,
expect_no_body: false,
status_code: 0,
decoded_head: None,
decoded_body_kind: None,
decoded_body: Vec::new(),
decompressor: NoCompression::new(),
request_method: None,
pending: 0,
}
}
}
impl<D: Decompressor> ResponseDecoder<D> {
/// 展開器付きでデコーダーを作成
pub fn with_decompressor(decompressor: D) -> Self {
Self {
buf: Vec::new(),
phase: DecodePhase::StartLine,
start_line: None,
headers: Vec::new(),
body_decoder: BodyDecoder::new(),
limits: DecoderLimits::default(),
expect_no_body: false,
status_code: 0,
decoded_head: None,
decoded_body_kind: None,
decoded_body: Vec::new(),
decompressor,
request_method: None,
pending: 0,
}
}
/// 展開器と制限付きでデコーダーを作成
pub fn with_decompressor_and_limits(decompressor: D, limits: DecoderLimits) -> Self {
Self {
buf: Vec::new(),
phase: DecodePhase::StartLine,
start_line: None,
headers: Vec::new(),
body_decoder: BodyDecoder::new(),
limits,
expect_no_body: false,
status_code: 0,
decoded_head: None,
decoded_body_kind: None,
decoded_body: Vec::new(),
decompressor,
request_method: None,
pending: 0,
}
}
/// HEAD リクエストへのレスポンスとしてデコード (ボディなし)
pub fn set_expect_no_body(&mut self, expect_no_body: bool) {
self.expect_no_body = expect_no_body;
}
/// リクエストメソッドを設定 (CONNECT トンネル判定用)
///
/// CONNECT メソッドへの 2xx レスポンスはトンネルモードに切り替わる。
/// この場合、ボディは存在せず、バッファ残りデータはトンネルデータとなる。
pub fn set_request_method(&mut self, method: &str) {
self.request_method = Some(method.to_string());
}
/// バッファの残りデータを取り出す (トンネルモード用)
///
/// CONNECT 2xx レスポンス後にトンネルモードに切り替わった場合、
/// このメソッドでヘッダー後のデータを取り出してトンネルに転送する。
///
/// 呼び出し後、バッファは空になる。
pub fn take_remaining(&mut self) -> Vec<u8> {
debug_assert!(
self.pending == 0,
"take_remaining called with pending mut_buf"
);
// 不変条件 "buf 空 ↔ pending == 0" を維持する。
// release ビルドで debug_assert! が消えた状態で契約違反 (pending > 0 で
// 呼ばれた場合) でも、内部状態の整合性を保つために明示的にリセットする。
self.pending = 0;
core::mem::take(&mut self.buf)
}
/// トンネルモードかどうかを判定
///
/// CONNECT 2xx レスポンスの場合、トンネルモードになる。
pub fn is_tunnel(&self) -> bool {
matches!(self.phase, DecodePhase::Tunnel)
}
/// 制限設定を取得
pub fn limits(&self) -> &DecoderLimits {
&self.limits
}
/// 既にメモリ上にあるバイト列を内部バッファに投入する
///
/// `data` を `extend_from_slice` でコピーする (1 回の memcpy)。
///
/// # 使い分け
///
/// `feed` と [`mut_buf`](Self::mut_buf) / [`advance_buf`](Self::advance_buf)
/// は入力経路の違う別の最適解として共存しており、用途で使い分ける:
///
/// - **これから書き込む先のバッファが必要なケース** (OS の `read` でソケット
/// から受信する等): `mut_buf` / `advance_buf` を使う。OS が内部バッファに
/// 直接書き込めるので、スタックバッファ → 内部 `Vec<u8>` のコピーが発生
/// しない。
/// - **既にバイト列が `&[u8]` として手元にあるケース** (io_uring 等の完了通知
/// 型 I/O で渡されるバイト列、テスト用バイトリテラル、別経路から受け取った
/// バイト列の中継等): `feed` を使う。`mut_buf(len) + copy_from_slice +
/// advance_buf(len)` だと「ゼロ初期化 + memcpy」の二段になるが、`feed`
/// は素直に 1 memcpy で済む。
pub fn feed(&mut self, data: &[u8]) -> Result<(), Error> {
buffer::feed(
&mut self.buf,
self.pending,
self.limits.max_buffer_size,
data,
)
}
/// バッファにデータを追加 (制限チェックなし)
///
/// 用途は [`feed`](Self::feed) と同じ「既にメモリ上にあるバイト列を投入する」
/// で、`max_buffer_size` チェックをスキップする点だけが異なる。
///
/// # 警告
///
/// この関数は `DecoderLimits` による `max_buffer_size` チェックをスキップする。
/// 未信頼入力に対して使用すると、メモリを無制限に消費して OOM を引き起こす可能性がある。
/// 信頼済み入力またはテスト用途にのみ使用すること。
pub fn feed_unchecked(&mut self, data: &[u8]) {
buffer::feed_unchecked(&mut self.buf, self.pending, data);
}
/// 内部バッファ末尾に `len` バイトの書き込み枠を確保し、その可変スライスを返す
///
/// 返るスライスは `Vec::resize(_, 0)` によりゼロ初期化済みなので、
/// `std::io::Read::read` 等にそのまま渡せる。書き込み後は必ず
/// [`advance_buf`](Self::advance_buf) で実書き込みバイト数を通知すること。
///
/// 直前の `mut_buf` で確保された未確定領域は、関数の先頭で必ず破棄される
/// (`advance_buf` 呼び忘れの回復)。よってエラー時 (`BufferOverflow`) も
/// 「呼び出し前の状態に巻き戻る」のではなく、「pending 領域が破棄された
/// 上で新規枠が確保されない」状態になる。
///
/// pending 破棄後の `remaining().len() + len` が `max_buffer_size` を
/// 超える場合は `Err(Error::BufferOverflow)` を返す。
pub fn mut_buf(&mut self, len: usize) -> Result<&mut [u8], Error> {
buffer::mut_buf(
&mut self.buf,
&mut self.pending,
self.limits.max_buffer_size,
len,
)
}
/// 直前の [`mut_buf`](Self::mut_buf) で確保した枠のうち、
/// 実際に書き込まれた `len` バイトを確定する
///
/// 残り (`mut_buf` で確保した長さ - `len`) は破棄される。
/// `len = 0` で呼ぶと枠全体を破棄 (EOF や read 失敗時のリセット用)。
///
/// `len > pending` の場合、debug ビルドでは panic、release ビルドでは
/// `pending` で飽和する。
pub fn advance_buf(&mut self, len: usize) {
buffer::advance_buf(&mut self.buf, &mut self.pending, len);
}
/// 書き込み可能な残り容量を返す
///
/// `max_buffer_size` から現在のバッファ長 (確定済みデータ + 未確定 `pending`)
/// を引いた値。`mut_buf(decoder.available_buf().min(N))` のようにチャンクサイズ
/// を残容量に適応させる用途で使う。
pub fn available_buf(&self) -> usize {
buffer::available_buf(&self.buf, self.limits.max_buffer_size)
}
/// バッファの残りデータを取得
pub fn remaining(&self) -> &[u8] {
buffer::remaining(&self.buf, self.pending)
}
/// デコーダーをリセット
pub fn reset(&mut self) {
self.buf.clear();
self.phase = DecodePhase::StartLine;
self.start_line = None;
self.headers.clear();
self.body_decoder.reset();
self.expect_no_body = false;
self.status_code = 0;
self.decoded_head = None;
self.decoded_body_kind = None;
self.decoded_body.clear();
self.decompressor.reset();
self.request_method = None;
self.pending = 0;
}
/// 接続終了を通知 (close-delimited ボディ用)
///
/// close-delimited ボディを読み取り中に接続が閉じられた場合に呼び出す。
/// これにより、バッファ内の残りデータがボディとして確定し、Complete に遷移する。
///
/// close-delimited 以外の状態で呼び出した場合は何もしない。
pub fn mark_eof(&mut self) {
debug_assert!(self.pending == 0, "mark_eof called with pending mut_buf");
if matches!(self.phase, DecodePhase::BodyCloseDelimited) {
self.phase = DecodePhase::Complete;
}
}
/// close-delimited ボディを読み取り中かどうかを判定
pub fn is_close_delimited(&self) -> bool {
matches!(self.phase, DecodePhase::BodyCloseDelimited)
}
/// ステータスコードからボディがあるかどうかを判定
fn status_has_body(status_code: u16) -> bool {
// RFC 9112 Section 6.3: 1xx, 204, 304 はボディなし
!((100..200).contains(&status_code) || status_code == 204 || status_code == 304)
}
/// ボディモードを決定
///
/// RFC 9112 Section 6.3 の優先順位に従う:
/// 1. CONNECT 2xx はトンネルモード (Transfer-Encoding/Content-Length は無視)
/// 2. HEAD レスポンス、1xx/204/304 はボディなし (Transfer-Encoding/Content-Length を解析しない)
/// 注: 205 は送信者制約 (RFC 9110) だが、受信者はメッセージ長決定規則に従う
/// 3. Transfer-Encoding がある場合:
/// - chunked が最後 → chunked
/// - chunked がないか最後でない → close-delimited
/// 4. Content-Length がある場合は固定長
/// 5. それ以外は close-delimited (接続が閉じるまでがボディ)
fn determine_body_kind(&self, status_code: u16) -> Result<BodyKind, Error> {
// RFC 9112 Section 6.1: HTTP/1.0 + Transfer-Encoding は framing fault
let version = self
.start_line
.as_ref()
.and_then(|sl| sl.split(' ').next())
.unwrap_or("");
if version == "HTTP/1.0"
&& self
.headers
.iter()
.any(|(name, _)| name.eq_ignore_ascii_case("Transfer-Encoding"))
{
return Err(Error::InvalidData(
"Transfer-Encoding is not defined in HTTP/1.0".to_string(),
));
}
// RFC 9112 Section 6.3: CONNECT メソッドへの 2xx レスポンスは
// トンネルモードに切り替わる。Transfer-Encoding と Content-Length は無視される。
// RFC 9110 Section 9.1: メソッドトークンは case-sensitive
if let Some(ref method) = self.request_method
&& method == "CONNECT"
&& (200..300).contains(&status_code)
{
return Ok(BodyKind::Tunnel);
}
// RFC 9112 Section 6.3: HEAD/1xx/204/304 はボディなし
// 205 は送信者がボディを生成してはならない (RFC 9110 Section 15.3.6) が、
// 受信者はメッセージ長決定規則に従って処理する必要がある
// これらのステータスでは Transfer-Encoding/Content-Length を解析しない
// (不正な TE/CL があってもエラーにしない)
if self.expect_no_body || !Self::status_has_body(status_code) {
return Ok(BodyKind::None);
}
// ボディがある場合のみ TE/CL を解析
let (te_result, content_length) = resolve_body_headers_for_response(&self.headers)?;
match te_result {
TransferEncodingResult::Chunked => return Ok(BodyKind::Chunked),
TransferEncodingResult::Other => return Ok(BodyKind::CloseDelimited),
TransferEncodingResult::None => {}
}
if let Some(len) = content_length {
if len > self.limits.max_body_size as u64 {
return Err(Error::BodyTooLarge {
size: usize::try_from(len).unwrap_or(usize::MAX),
limit: self.limits.max_body_size,
});
}
return Ok(BodyKind::ContentLength(len));
}
// RFC 9112: TE も CL もない場合は close-delimited
// 接続が閉じられるまでをボディとして扱う
Ok(BodyKind::CloseDelimited)
}
/// ヘッダーをデコード
///
/// ヘッダーが完了したら `Some((ResponseHead, BodyKind))` を返す
/// データ不足の場合は `None` を返す
/// 既にヘッダーデコード済みの場合はエラー
pub fn decode_headers(&mut self) -> Result<Option<(ResponseHead, BodyKind)>, Error> {
debug_assert!(
self.pending == 0,
"decode_headers called with pending mut_buf"
);
loop {
match &self.phase {
DecodePhase::StartLine => {
if let Some(pos) = find_line(&self.buf) {
let line = String::from_utf8(self.buf[..pos].to_vec()).map_err(|e| {
Error::InvalidData(alloc::format!("invalid UTF-8: {e}"))
})?;
self.buf.drain(..pos + 2);
// CR/LF チェック (埋め込まれた改行を拒否)
if line.contains('\r') || line.contains('\n') {
return Err(Error::InvalidData(
"invalid status line: contains CR/LF".to_string(),
));
}
// Parse: VERSION SP STATUS-CODE SP REASON-PHRASE CRLF
let parts: Vec<&str> = line.splitn(3, ' ').collect();
if parts.len() < 2 {
return Err(Error::InvalidData(alloc::format!(
"invalid status line: {}",
line
)));
}
// プロトコルバージョンの検証
if !is_valid_protocol_version(parts[0]) {
return Err(Error::InvalidData(
"invalid status line: invalid protocol version".to_string(),
));
}
// ステータスコードの検証 (RFC 9110 Section 15)
let status_code: u16 = parts[1].parse().map_err(|_| {
Error::InvalidData(alloc::format!(
"invalid status line: invalid status code: {}",
parts[1]
))
})?;
if !is_valid_status_code(status_code) {
return Err(Error::InvalidData(alloc::format!(
"invalid status line: status code out of range: {}",
status_code
)));
}
// reason-phrase の検証 (RFC 9112 Section 4)
if let Some(reason) = parts.get(2)
&& !is_valid_reason_phrase(reason)
{
return Err(Error::InvalidData(
"invalid status line: invalid reason-phrase".to_string(),
));
}
self.start_line = Some(line);
self.phase = DecodePhase::Headers;
} else {
return Ok(None);
}
}
DecodePhase::Headers => {
if let Some(pos) = find_line(&self.buf) {
if pos == 0 {
// Empty line - end of headers
self.buf.drain(..2);
// ステータスコードを取得
let start_line = self.start_line.as_ref().ok_or_else(|| {
Error::InvalidData("missing status line".to_string())
})?;
let parts: Vec<&str> = start_line.splitn(3, ' ').collect();
let status_code: u16 = parts[1].parse().map_err(|_| {
Error::InvalidData(alloc::format!(
"invalid status code: {}",
parts[1]
))
})?;
self.status_code = status_code;
let body_kind = self.determine_body_kind(status_code)?;
// ヘッダー完了、ボディフェーズに遷移
match body_kind {
BodyKind::ContentLength(len) => {
if len > 0 {
self.phase =
DecodePhase::BodyContentLength { remaining: len };
} else {
self.phase = DecodePhase::Complete;
}
}
BodyKind::Chunked => {
self.phase = DecodePhase::BodyChunkedSize;
}
BodyKind::CloseDelimited => {
self.phase = DecodePhase::BodyCloseDelimited;
}
BodyKind::None => {
self.phase = DecodePhase::Complete;
}
BodyKind::Tunnel => {
self.phase = DecodePhase::Tunnel;
}
}
// ResponseHead を構築
let start_line = self.start_line.take().unwrap();
let parts: Vec<&str> = start_line.splitn(3, ' ').collect();
let head = ResponseHead {
version: parts[0].to_string(),
status_code,
reason_phrase: parts.get(2).unwrap_or(&"").to_string(),
headers: core::mem::take(&mut self.headers),
};
return Ok(Some((head, body_kind)));
} else {
// Check header line size limit
if pos > self.limits.max_header_line_size {
return Err(Error::HeaderLineTooLong {
size: pos,
limit: self.limits.max_header_line_size,
});
}
// Check header count limit
if self.headers.len() >= self.limits.max_headers_count {
return Err(Error::TooManyHeaders {
count: self.headers.len() + 1,
limit: self.limits.max_headers_count,
});
}
let line =
String::from_utf8(self.buf[..pos].to_vec()).map_err(|e| {
Error::InvalidData(alloc::format!("invalid UTF-8: {e}"))
})?;
self.buf.drain(..pos + 2);
let (name, value) = parse_header_line(&line)?;
self.headers.push((name, value));
}
} else {
return Ok(None);
}
}
DecodePhase::Complete => {
// 完了状態から次のメッセージへ遷移
self.phase = DecodePhase::StartLine;
self.start_line = None;
self.headers.clear();
self.body_decoder.reset();
self.expect_no_body = false;
self.status_code = 0;
continue;
}
DecodePhase::Tunnel => {
return Err(Error::InvalidData(
"decode_headers cannot be used in tunnel mode".to_string(),
));
}
_ => {
return Err(Error::InvalidData(
"decode_headers called during body decoding".to_string(),
));
}
}
}
}
/// 利用可能なボディデータを覗く(ゼロコピー)
///
/// `decode_headers()` 成功後に呼ぶ
/// データがある場合はスライスを返す
/// ボディがない場合や完了済みの場合は `None` を返す
pub fn peek_body(&self) -> Option<&[u8]> {
debug_assert!(self.pending == 0, "peek_body called with pending mut_buf");
self.body_decoder.peek_body(&self.buf, &self.phase)
}
/// ボディデータを展開して取得
///
/// `decode_headers()` 成功後に呼ぶ。
/// 利用可能なボディデータを展開して output に書き込む。
///
/// # 引数
/// - `output`: 展開データを書き込む出力バッファ
///
/// # 戻り値
/// - `Ok(Some(status))`: 展開成功。`status.produced()` バイトが output に書き込まれた。
/// `status.consumed()` バイトを `consume_body()` で消費する必要がある。
/// - `Ok(None)`: 利用可能なボディデータがない
/// - `Err(e)`: 展開エラー
///
/// # 使い方
///
/// ```ignore
/// let mut output = vec![0u8; 8192];
/// while let Some(status) = decoder.peek_body_decompressed(&mut output)? {
/// // output[..status.produced()] に展開済みデータ
/// process(&output[..status.produced()]);
/// decoder.consume_body(status.consumed())?;
/// }
/// ```
pub fn peek_body_decompressed(
&mut self,
output: &mut [u8],
) -> Result<Option<CompressionStatus>, Error> {
debug_assert!(
self.pending == 0,
"peek_body_decompressed called with pending mut_buf"
);
let input = match self.body_decoder.peek_body(&self.buf, &self.phase) {
Some(data) if !data.is_empty() => data,
_ => return Ok(None),
};
let status = self.decompressor.decompress(input, output)?;
Ok(Some(status))
}
/// ボディデータを消費
///
/// `peek_body()` で取得したデータを処理した後に呼ぶ
/// `len` は消費するバイト数 (1 以上)
pub fn consume_body(&mut self, len: usize) -> Result<BodyProgress, Error> {
debug_assert!(
self.pending == 0,
"consume_body called with pending mut_buf"
);
if len == 0 {
return Err(Error::InvalidData(
"consume_body(0) is not allowed, use progress() instead".to_string(),
));
}
self.body_decoder
.consume_body(&mut self.buf, &mut self.phase, len, &self.limits)
}
/// 状態機械を進める (ボディデータは消費しない)
///
/// Chunked エンコーディングの場合、チャンクサイズ行のパースや
/// 終端チャンクの処理を行う。
pub fn progress(&mut self) -> Result<BodyProgress, Error> {
debug_assert!(self.pending == 0, "progress called with pending mut_buf");
self.body_decoder
.consume_body(&mut self.buf, &mut self.phase, 0, &self.limits)
}
/// レスポンス全体を一括でデコード
///
/// ストリーミング API (`decode_headers()` / `peek_body()` / `consume_body()`) を
/// 内部で使用して、レスポンス全体をデコードする。
///
/// データ不足の場合は `None` を返す。
/// ストリーミング API と混在使用するとエラーを返す。
///
/// ## close-delimited ボディの場合
///
/// `BodyKind::CloseDelimited` の場合、接続が閉じられるまでがボディとなる。
/// `decode()` を使う場合は、接続終了後に `mark_eof()` を呼んでから
/// 再度 `decode()` を呼ぶ必要がある。
pub fn decode(&mut self) -> Result<Option<Response>, Error> {
debug_assert!(self.pending == 0, "decode called with pending mut_buf");
// ヘッダーがまだデコードされていない場合はデコード
if self.decoded_head.is_none() {
match self.phase {
DecodePhase::StartLine | DecodePhase::Headers => match self.decode_headers()? {
Some((head, body_kind)) => {
self.decoded_head = Some(head);
self.decoded_body_kind = Some(body_kind);
}
None => return Ok(None),
},
_ => {
return Err(Error::InvalidData(
"decode cannot be mixed with streaming API".to_string(),
));
}
}
}
// ボディを読む
let body_kind = *self.decoded_body_kind.as_ref().unwrap();
match body_kind {
BodyKind::Tunnel => {
return Err(Error::InvalidData(
"decode() cannot be used in tunnel mode, use take_remaining() instead"
.to_string(),
));
}
BodyKind::ContentLength(_) | BodyKind::Chunked => loop {
// バッファからボディデータを直接消費。
// body_decoder.peek_body() を直接呼ぶことで、返り値の lifetime が
// &self.buf に紐付き、self.decoded_body の可変借用と並立できる。
if let Some(data) = self.body_decoder.peek_body(&self.buf, &self.phase) {
let len = data.len();
self.decoded_body.extend_from_slice(data);
self.consume_body(len)?;
// consume_body の戻り値ではなく phase で完了判定する。
// BodyChunkedData → BodyChunkedDataCrlf → BodyChunkedSize の
// 多段遷移時は Advanced を返すため、phase を直接見るのが確実。
if matches!(self.phase, DecodePhase::Complete) {
break;
}
continue;
}
// ボディデータがない → 状態機械を進める
match self.progress()? {
BodyProgress::Complete { .. } => break,
BodyProgress::Advanced => continue,
BodyProgress::NeedData => return Ok(None),
}
},
BodyKind::CloseDelimited => {
// close-delimited: バッファにあるデータを読み込み、mark_eof() を待つ。
// max_body_size チェックは consume_body 内でも行うが、ここでは
// decoded_body に書き込む前にも事前チェックする (既存コードと同様)。
if let Some(data) = self.body_decoder.peek_body(&self.buf, &self.phase) {
let len = data.len();
let new_size =
self.decoded_body
.len()
.checked_add(len)
.ok_or(Error::BodyTooLarge {
size: usize::MAX,
limit: self.limits.max_body_size,
})?;
if new_size > self.limits.max_body_size {
return Err(Error::BodyTooLarge {
size: new_size,
limit: self.limits.max_body_size,
});
}
self.decoded_body.extend_from_slice(data);
self.consume_body(len)?;
}
// mark_eof() が呼ばれて Complete になったか確認
if !matches!(self.phase, DecodePhase::Complete) {
// まだ EOF でないのでデータ不足
return Ok(None);
}
}
BodyKind::None => {}
}
// Response を構築
// BodyKind::None / Tunnel は「フレーミングがない」ため body = None。
// それ以外 (ContentLength / Chunked / CloseDelimited) は明示的なボディなので body = Some。
let head = self.decoded_head.take().unwrap();
let body = match body_kind {
BodyKind::None | BodyKind::Tunnel => None,
BodyKind::ContentLength(_) | BodyKind::Chunked | BodyKind::CloseDelimited => {
Some(core::mem::take(&mut self.decoded_body))
}
};
// Keep-Alive 対応: 次のレスポンスのために状態をリセット
self.phase = DecodePhase::StartLine;
self.decoded_body_kind = None;
self.decoded_body.clear();
self.body_decoder.reset();
self.expect_no_body = false;
self.status_code = 0;
Ok(Some(Response {
version: head.version,
status_code: head.status_code,
reason_phrase: head.reason_phrase,
headers: head.headers,
body,
omit_body: false,
}))
}
}