shiguredo_http11 2026.1.0

HTTP/1.1 Library
Documentation
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
use crate::compression::{CompressionError, CompressionStatus, Compressor, NoCompression};
use crate::error::EncodeError;
use crate::host::Host;
use crate::request::Request;
use crate::response::Response;
use crate::validate::{
    is_valid_field_value, is_valid_header_name, is_valid_method, is_valid_reason_phrase,
    is_valid_request_target, is_valid_status_code, is_valid_version_for_encode,
};

/// リクエストフィールドのバリデーション
fn validate_request_fields(request: &Request) -> Result<(), EncodeError> {
    // メソッドの検証
    if !is_valid_method(&request.method) {
        return Err(EncodeError::InvalidMethod {
            method: request.method.clone(),
        });
    }

    // リクエストターゲットの検証
    if !is_valid_request_target(&request.uri) {
        return Err(EncodeError::InvalidRequestTarget {
            uri: request.uri.clone(),
        });
    }

    // RFC 3986: URI は ASCII のみで構成される
    // obs-text (0x80-0xFF) は受信側では許容するが、送信側では拒否する
    if request.uri.bytes().any(|b| b > 0x7E) {
        return Err(EncodeError::InvalidRequestTarget {
            uri: request.uri.clone(),
        });
    }

    // RFC 9112 Section 3.2: メソッドと request-target 形式の整合性を検証
    validate_request_target_form(&request.method, &request.uri)?;

    // バージョンの検証
    if !is_valid_version_for_encode(&request.version) {
        return Err(EncodeError::InvalidVersion {
            version: request.version.clone(),
        });
    }

    // ヘッダーの検証
    validate_headers(&request.headers)?;

    Ok(())
}

/// request-target の形式を判定
///
/// RFC 9112 Section 3.2:
/// - origin-form: "/" で始まる (例: /path?query)
/// - absolute-form: absolute-URI (例: http://host/path, urn:isbn:0451450523)
/// - authority-form: uri-host ":" port (例: host:port)
/// - asterisk-form: "*" のみ
///
/// authority-form と "://" なし absolute-form は文法的に曖昧なため、
/// デコーダー (decoder/body.rs) と同じ順序で判定する:
/// 1. port が数値の host:port → authority-form
/// 2. 有効なスキームが検出 → absolute-form
fn detect_request_target_form(uri: &str) -> Result<RequestTargetForm, EncodeError> {
    if uri == "*" {
        Ok(RequestTargetForm::Asterisk)
    } else if uri.contains("://") {
        Ok(RequestTargetForm::Absolute)
    } else if uri.starts_with('/') {
        Ok(RequestTargetForm::Origin)
    } else if looks_like_authority_form(uri) {
        Ok(RequestTargetForm::Authority)
    } else if detect_scheme(uri).is_some() {
        // "://" を含まない absolute-URI (例: urn:isbn:0451450523)
        Ok(RequestTargetForm::Absolute)
    } else {
        Err(EncodeError::InvalidRequestTarget {
            uri: uri.to_string(),
        })
    }
}

/// authority-form かどうかの簡易判定
///
/// authority-form = uri-host ":" port (RFC 9112 Section 3.2.3)
/// uri-host = host (RFC 3986 Section 3.2.2)
///
/// ホスト部分に userinfo 区切りの "@" が含まれる場合は authority-form ではない。
/// RFC 9110 Section 9.3.6: "consisting of only the host and port number"
fn looks_like_authority_form(uri: &str) -> bool {
    // userinfo は authority-form に含まれない
    if uri.contains('@') {
        return false;
    }
    if let Some(colon_pos) = uri.rfind(':') {
        let port_str = &uri[colon_pos + 1..];
        let host = &uri[..colon_pos];
        !port_str.is_empty()
            && port_str.bytes().all(|b| b.is_ascii_digit())
            && port_str.parse::<u16>().is_ok()
            && !host.is_empty()
    } else {
        false
    }
}

/// authority-form の host を検証 (エンコーダー用)
///
/// RFC 9112 Section 3.2.3: authority-form = uri-host ":" port
/// デコーダー (decoder/body.rs) と同等の Host::parse による host 検証を行う
fn validate_encoder_authority_form(uri: &str) -> Result<(), EncodeError> {
    if let Some(colon_pos) = uri.rfind(':') {
        let host = &uri[..colon_pos];
        Host::parse(host).map_err(|_| EncodeError::InvalidRequestTarget {
            uri: uri.to_string(),
        })?;
    }
    Ok(())
}

/// スキームを検出する (RFC 3986 Section 3.1)
///
/// scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
/// 先頭が有効なスキーム + ":" であればスキームの長さを返す
fn detect_scheme(target: &str) -> Option<usize> {
    let bytes = target.as_bytes();
    if bytes.is_empty() || !bytes[0].is_ascii_alphabetic() {
        return None;
    }
    let colon_pos = bytes.iter().position(|&b| b == b':')?;
    if colon_pos == 0 {
        return None;
    }
    for &b in &bytes[1..colon_pos] {
        if !b.is_ascii_alphanumeric() && b != b'+' && b != b'-' && b != b'.' {
            return None;
        }
    }
    // 意図的な RFC 非準拠: path-empty (scheme ":" のみ) を拒否する。
    // RFC 3986 の ABNF では path-empty は合法だが、HTTP request-target として
    // path-empty が単独で出現する実用的なケースはないため、不正な入力として扱う。
    if colon_pos + 1 >= bytes.len() {
        return None;
    }
    Some(colon_pos)
}

/// request-target の形式
enum RequestTargetForm {
    /// origin-form: "/" path ["?" query]
    Origin,
    /// absolute-form: absolute-URI
    Absolute,
    /// authority-form: uri-host ":" port
    Authority,
    /// asterisk-form: "*"
    Asterisk,
}

/// RFC 9112 Section 3.2: メソッドと request-target 形式の整合性を検証
///
/// - CONNECT は authority-form のみ (Section 3.2.3)
/// - asterisk-form ("*") は OPTIONS のみ (Section 3.2.4)
/// - その他のメソッドは origin-form または absolute-form (Section 3.2.1, 3.2.2)
fn validate_request_target_form(method: &str, uri: &str) -> Result<(), EncodeError> {
    let form = detect_request_target_form(uri)?;

    // RFC 9110 Section 9.1: メソッドトークンは case-sensitive
    match (method, &form) {
        // CONNECT は authority-form のみ (RFC 9112 Section 3.2.3)
        ("CONNECT", RequestTargetForm::Authority) => {
            // RFC 3986: authority-form の host を検証
            validate_encoder_authority_form(uri)?;
            Ok(())
        }
        ("CONNECT", _) => Err(EncodeError::InvalidRequestTargetForm {
            method: method.to_string(),
            uri: uri.to_string(),
        }),
        // asterisk-form は OPTIONS のみ (RFC 9112 Section 3.2.4)
        (_, RequestTargetForm::Asterisk) => {
            if method == "OPTIONS" {
                Ok(())
            } else {
                Err(EncodeError::InvalidRequestTargetForm {
                    method: method.to_string(),
                    uri: uri.to_string(),
                })
            }
        }
        // CONNECT 以外が authority-form を使うのは不正
        (_, RequestTargetForm::Authority) => Err(EncodeError::InvalidRequestTargetForm {
            method: method.to_string(),
            uri: uri.to_string(),
        }),
        // origin-form: path と query の文字を検証
        (_, RequestTargetForm::Origin) => {
            // RFC 3986 Section 3.3/3.4: origin-form では "[" "]" は pchar に含まれない
            // (authority 内の IP-literal でのみ合法)
            if uri.bytes().any(|b| b == b'[' || b == b']') {
                return Err(EncodeError::InvalidRequestTarget {
                    uri: uri.to_string(),
                });
            }
            Ok(())
        }
        // absolute-form: http/https は "://" 必須 (RFC 9110 Section 4.2)
        (_, RequestTargetForm::Absolute) => {
            reject_http_without_authority_prefix(uri)?;
            Ok(())
        }
    }
}

/// レスポンスフィールドのバリデーション
fn validate_response_fields(response: &Response) -> Result<(), EncodeError> {
    // バージョンの検証
    if !is_valid_version_for_encode(&response.version) {
        return Err(EncodeError::InvalidVersion {
            version: response.version.clone(),
        });
    }

    // ステータスコードの検証
    if !is_valid_status_code(response.status_code) {
        return Err(EncodeError::InvalidStatusCode {
            code: response.status_code,
        });
    }

    // reason-phrase の検証
    if !is_valid_reason_phrase(&response.reason_phrase) {
        return Err(EncodeError::InvalidReasonPhrase {
            phrase: response.reason_phrase.clone(),
        });
    }

    // ヘッダーの検証
    validate_headers(&response.headers)?;

    Ok(())
}

/// ヘッダー名と値のバリデーション
fn validate_headers(headers: &[(String, String)]) -> Result<(), EncodeError> {
    for (name, value) in headers {
        if !is_valid_header_name(name) {
            return Err(EncodeError::InvalidHeaderName { name: name.clone() });
        }
        if !is_valid_field_value(value) {
            return Err(EncodeError::InvalidHeaderValue {
                name: name.clone(),
                value: value.clone(),
            });
        }
    }
    Ok(())
}

/// Host ヘッダーの詳細バリデーション (リクエスト用)
///
/// RFC 9112 Section 3.2:
/// - HTTP/1.1 リクエストには Host ヘッダーが必須
/// - Host ヘッダーは重複してはならない
/// - Host ヘッダーの値は有効な authority でなければならない
fn validate_host_header(request: &Request) -> Result<(), EncodeError> {
    if request.version != "HTTP/1.1" {
        return Ok(());
    }

    let host_headers: Vec<&str> = request
        .headers
        .iter()
        .filter(|(name, _)| name.eq_ignore_ascii_case("Host"))
        .map(|(_, value)| value.as_str())
        .collect();

    if host_headers.is_empty() {
        return Err(EncodeError::MissingHostHeader);
    }

    if host_headers.len() > 1 {
        return Err(EncodeError::DuplicateHostHeader);
    }

    let host_value = host_headers[0];
    // 空の Host ヘッダーは許可 (RFC 9112 Section 3.2: 空の field-value は許可)
    if !host_value.is_empty() && Host::parse(host_value).is_err() {
        return Err(EncodeError::InvalidHostHeader {
            value: host_value.to_string(),
        });
    }

    // RFC 9112 Section 3.2: absolute-form の場合、Host は authority と同一でなければならない (MUST)
    // authority が非空なら Host も非空かつ一致していなければならない
    if request.uri.contains("://")
        && let Some(authority) = extract_authority_from_uri(&request.uri)
        && !authority.is_empty()
        && !authority.eq_ignore_ascii_case(host_value)
    {
        return Err(EncodeError::HostAuthorityMismatch {
            host: host_value.to_string(),
            authority: authority.to_string(),
        });
    }

    // RFC 9112 Section 3.2: CONNECT (authority-form) の場合、
    // Host は request-target の authority と意味的に一致しなければならない (MUST)
    // RFC 9110 Section 7.2: Host = uri-host [ ":" port ] (port は省略可能)
    // RFC 9112 Section 3.2.3 / RFC 9110 Section 9.3.6 の例:
    //   CONNECT www.example.com:80 HTTP/1.1
    //   Host: www.example.com
    if request.method == "CONNECT"
        && let Some(colon_pos) = request.uri.rfind(':')
    {
        let target_host = &request.uri[..colon_pos];
        let target_port_str = &request.uri[colon_pos + 1..];

        // CONNECT の authority は常に存在するため、Host は非空でなければならない
        if host_value.is_empty() {
            return Err(EncodeError::HostAuthorityMismatch {
                host: host_value.to_string(),
                authority: request.uri.clone(),
            });
        }

        if let Ok(parsed_host) = Host::parse(host_value) {
            // host 部分の比較 (case-insensitive)
            if !parsed_host.host().eq_ignore_ascii_case(target_host) {
                return Err(EncodeError::HostAuthorityMismatch {
                    host: host_value.to_string(),
                    authority: request.uri.clone(),
                });
            }

            // Host にポートが指定されている場合、request-target のポートと一致するか確認
            if let Some(host_port) = parsed_host.port()
                && let Ok(target_port) = target_port_str.parse::<u16>()
                && host_port != target_port
            {
                return Err(EncodeError::HostAuthorityMismatch {
                    host: host_value.to_string(),
                    authority: request.uri.clone(),
                });
            }
        }
    }

    // RFC 9112 Section 3.2: authority がない target URI では Host を空にしなければならない (MUST)
    // "://" を含まない absolute-form (例: urn:isbn:xxx) が該当する
    if let Ok(RequestTargetForm::Absolute) = detect_request_target_form(&request.uri)
        && !request.uri.contains("://")
        && !host_value.is_empty()
    {
        return Err(EncodeError::NonEmptyHostWithoutAuthority {
            host: host_value.to_string(),
            uri: request.uri.clone(),
        });
    }

    Ok(())
}

/// RFC 9110 Section 4.2.4: http/https URI の userinfo を検出して拒否する
///
/// 送信者は http/https URI に userinfo を生成してはならない (MUST NOT)
/// 他のスキームには適用しない
fn reject_http_userinfo(uri: &str) -> Result<(), EncodeError> {
    let lower = uri.to_ascii_lowercase();
    if !lower.starts_with("http://") && !lower.starts_with("https://") {
        return Ok(());
    }
    let after_scheme = match uri.find("://") {
        Some(i) => &uri[i + 3..],
        None => return Ok(()),
    };
    let end = after_scheme.find(['/', '?']).unwrap_or(after_scheme.len());
    let authority = &after_scheme[..end];
    if authority.contains('@') {
        return Err(EncodeError::UserinfoInHttpUri {
            uri: uri.to_string(),
        });
    }
    Ok(())
}

/// RFC 9110 Section 4.2.1/4.2.2: http/https URI の空 host を検出して拒否する
///
/// 送信者は空 host 識別子を持つ http/https URI を生成してはならない (MUST NOT)
fn reject_http_empty_host(uri: &str) -> Result<(), EncodeError> {
    let lower = uri.to_ascii_lowercase();
    if !lower.starts_with("http://") && !lower.starts_with("https://") {
        return Ok(());
    }
    let after_scheme = match uri.find("://") {
        Some(i) => &uri[i + 3..],
        None => return Ok(()),
    };
    let end = after_scheme.find(['/', '?']).unwrap_or(after_scheme.len());
    let authority = &after_scheme[..end];
    // userinfo を除外して host 部分を取得
    let host_port = if let Some(at_pos) = authority.rfind('@') {
        &authority[at_pos + 1..]
    } else {
        authority
    };
    // host が空 (authority 自体が空、またはポートのみ)
    if host_port.is_empty() || host_port.starts_with(':') {
        return Err(EncodeError::EmptyHostInHttpUri {
            uri: uri.to_string(),
        });
    }
    Ok(())
}

/// RFC 9110 Section 4.2.1/4.2.2: http/https URI は "://" を含まなければならない
///
/// http-URI  = "http"  "://" authority path-abempty [ "?" query ]
/// https-URI = "https" "://" authority path-abempty [ "?" query ]
fn reject_http_without_authority_prefix(uri: &str) -> Result<(), EncodeError> {
    if let Some(colon_pos) = uri.find(':') {
        let scheme = &uri[..colon_pos];
        if (scheme.eq_ignore_ascii_case("http") || scheme.eq_ignore_ascii_case("https"))
            && !uri[colon_pos..].starts_with("://")
        {
            return Err(EncodeError::InvalidRequestTarget {
                uri: uri.to_string(),
            });
        }
    }
    Ok(())
}

/// Content-Length ヘッダーの ABNF 検証 (RFC 9110 Section 8.6)
///
/// 全 Content-Length ヘッダーを走査し:
/// 1. 各値が 1*DIGIT であることを検証 → 違反なら InvalidContentLengthValue
/// 2. 複数ヘッダーの値が一致することを検証 → 不一致なら DuplicateContentLength
/// 3. 検証済みの値を Option<u64> で返す
fn validate_content_length_headers(
    headers: &[(String, String)],
) -> Result<Option<u64>, EncodeError> {
    let mut result: Option<u64> = None;

    for (name, value) in headers {
        if !name.eq_ignore_ascii_case("Content-Length") {
            continue;
        }
        let trimmed = value.trim();
        // RFC 9110 Section 8.6: Content-Length = 1*DIGIT
        if trimmed.is_empty() || !trimmed.bytes().all(|b| b.is_ascii_digit()) {
            return Err(EncodeError::InvalidContentLengthValue {
                value: value.clone(),
            });
        }
        let parsed =
            trimmed
                .parse::<u64>()
                .map_err(|_| EncodeError::InvalidContentLengthValue {
                    value: value.clone(),
                })?;

        match result {
            None => result = Some(parsed),
            Some(prev) if prev != parsed => {
                return Err(EncodeError::DuplicateContentLength);
            }
            Some(_) => {} // 同じ値なので OK
        }
    }

    Ok(result)
}

/// URI から authority 部分を抽出 (userinfo を除外)
///
/// scheme "://" authority ["/" path]
/// RFC 9112 Section 3.2: Host ヘッダーとの比較では userinfo を除外する
fn extract_authority_from_uri(uri: &str) -> Option<String> {
    let after_scheme = uri.find("://").map(|i| &uri[i + 3..])?;
    // authority は次の "/" または "?" または末尾まで
    let end = after_scheme.find(['/', '?']).unwrap_or(after_scheme.len());
    let authority = &after_scheme[..end];
    // userinfo を除外して host:port のみを返す
    let host_port = if let Some(at_pos) = authority.rfind('@') {
        &authority[at_pos + 1..]
    } else {
        authority
    };
    Some(host_port.to_string())
}

/// リクエストをエンコード
///
/// RFC 9112 Section 3.2: HTTP/1.1 リクエストには Host ヘッダーが必須
/// RFC 9112 Section 6.2: Transfer-Encoding と Content-Length は同時に送信してはならない
pub fn encode_request(request: &Request) -> Result<Vec<u8>, EncodeError> {
    // フィールドバリデーション
    validate_request_fields(request)?;

    // RFC 9110 Section 4.2.4: http/https URI の userinfo を拒否する
    reject_http_userinfo(&request.uri)?;

    // RFC 9110 Section 4.2.1/4.2.2: http/https URI の空 host を拒否する
    reject_http_empty_host(&request.uri)?;

    // Host ヘッダーの詳細バリデーション
    validate_host_header(request)?;

    // RFC 9112 Section 6.2: Transfer-Encoding と Content-Length の同時送信は禁止
    if request.has_header("Transfer-Encoding") && request.has_header("Content-Length") {
        return Err(EncodeError::ConflictingTransferEncodingAndContentLength);
    }

    // RFC 9110 Section 9.3.6: CONNECT リクエストは content を持たない
    // RFC 9110 Section 9.1: メソッドトークンは case-sensitive
    if request.method == "CONNECT"
        && (!request.body.is_empty()
            || request.has_header("Content-Length")
            || request.has_header("Transfer-Encoding"))
    {
        return Err(EncodeError::ConnectRequestWithContent);
    }

    // Content-Length ヘッダーの ABNF 検証と body.len() との整合性を検証
    if !request.has_header("Transfer-Encoding")
        && let Some(header_value) = validate_content_length_headers(&request.headers)?
    {
        let body_length = request.body.len() as u64;
        if header_value != body_length {
            return Err(EncodeError::ContentLengthMismatch {
                header_value,
                body_length,
            });
        }
    }

    let mut buf = Vec::new();

    // Request line: METHOD SP URI SP VERSION CRLF
    buf.extend_from_slice(request.method.as_bytes());
    buf.push(b' ');
    buf.extend_from_slice(request.uri.as_bytes());
    buf.push(b' ');
    buf.extend_from_slice(request.version.as_bytes());
    buf.extend_from_slice(b"\r\n");

    // Headers
    for (name, value) in &request.headers {
        buf.extend_from_slice(name.as_bytes());
        buf.extend_from_slice(b": ");
        buf.extend_from_slice(value.as_bytes());
        buf.extend_from_slice(b"\r\n");
    }

    // Content-Length (if body is present, not already set, and not chunked)
    if !request.body.is_empty()
        && !request.has_header("Content-Length")
        && !request.has_header("Transfer-Encoding")
    {
        buf.extend_from_slice(b"Content-Length: ");
        buf.extend_from_slice(request.body.len().to_string().as_bytes());
        buf.extend_from_slice(b"\r\n");
    }

    // End of headers
    buf.extend_from_slice(b"\r\n");

    // Body
    buf.extend_from_slice(&request.body);

    Ok(buf)
}

/// レスポンスをエンコード
///
/// RFC 9112 Section 6.1: 1xx / 204 レスポンスに Transfer-Encoding を含めてはならない
/// RFC 9112 Section 6.2: Transfer-Encoding と Content-Length は同時に送信してはならない
///
/// # CONNECT 2xx レスポンスについて
///
/// RFC 9112 Section 6.1 / RFC 9110 Section 8.6 により、CONNECT リクエストへの
/// 2xx レスポンスには Transfer-Encoding / Content-Length を含めてはならない (MUST NOT)。
/// しかし、エンコーダーはリクエストメソッドの情報を持たないため、この制約は
/// 呼び出し側アプリケーションの責務とする。
pub fn encode_response(response: &Response) -> Result<Vec<u8>, EncodeError> {
    // フィールドバリデーション
    validate_response_fields(response)?;

    // RFC 9112 Section 6.2: Transfer-Encoding と Content-Length の同時送信は禁止
    if response.has_header("Transfer-Encoding") && response.has_header("Content-Length") {
        return Err(EncodeError::ConflictingTransferEncodingAndContentLength);
    }

    // RFC 9112 Section 6.1: 1xx / 204 レスポンスに Transfer-Encoding は禁止
    let is_1xx_or_204 = (100..200).contains(&response.status_code) || response.status_code == 204;
    if is_1xx_or_204 && response.has_header("Transfer-Encoding") {
        return Err(EncodeError::ForbiddenTransferEncoding {
            status_code: response.status_code,
        });
    }

    // RFC 9110 Section 8.6: 1xx / 204 レスポンスに Content-Length は禁止
    if is_1xx_or_204 && response.has_header("Content-Length") {
        return Err(EncodeError::ForbiddenContentLength {
            status_code: response.status_code,
        });
    }

    // RFC 9110 Section 15.3.6: 205 Reset Content はボディを生成してはならない
    if response.status_code == 205 {
        if !response.body.is_empty() {
            return Err(EncodeError::ForbiddenBodyFor205);
        }
        if response.has_header("Transfer-Encoding") {
            return Err(EncodeError::ForbiddenTransferEncoding { status_code: 205 });
        }
        // RFC 9110 Section 8.6: 205 の Content-Length は 0 のみ許可
        if let Some(cl) = response.get_header("Content-Length")
            && cl.trim() != "0"
        {
            return Err(EncodeError::ForbiddenContentLength { status_code: 205 });
        }
    }

    let status_has_body = !((100..200).contains(&response.status_code)
        || response.status_code == 204
        || response.status_code == 304);
    let body_will_be_encoded = status_has_body && !response.omit_body;

    // Content-Length ヘッダーの ABNF 検証と body.len() との整合性を検証
    // - 通常レスポンス: 常に一致必須
    // - omit_body: true の場合は、body が空のときのみ検証をスキップ
    //   (HEAD レスポンスで Content-Length が表現長を示すケース)
    // - 1xx/204/304 は message body がないため、ここでは検証しない
    if status_has_body
        && !response.has_header("Transfer-Encoding")
        && let Some(header_value) = validate_content_length_headers(&response.headers)?
    {
        let body_length = response.body.len() as u64;
        let should_validate = body_will_be_encoded || body_length != 0;
        if should_validate && header_value != body_length {
            return Err(EncodeError::ContentLengthMismatch {
                header_value,
                body_length,
            });
        }
    }

    let mut buf = Vec::new();

    // Status line: VERSION SP STATUS-CODE SP REASON-PHRASE CRLF
    buf.extend_from_slice(response.version.as_bytes());
    buf.push(b' ');
    buf.extend_from_slice(response.status_code.to_string().as_bytes());
    buf.push(b' ');
    buf.extend_from_slice(response.reason_phrase.as_bytes());
    buf.extend_from_slice(b"\r\n");

    // Headers
    for (name, value) in &response.headers {
        buf.extend_from_slice(name.as_bytes());
        buf.extend_from_slice(b": ");
        buf.extend_from_slice(value.as_bytes());
        buf.extend_from_slice(b"\r\n");
    }

    // Content-Length (if not already set and not chunked)
    // RFC 9112: keep-alive を維持するために Content-Length または Transfer-Encoding が必要
    // 1xx/204/304 はボディがないため Content-Length を追加しない
    // 205 は RFC 9110 Section 15.3.6 でボディ生成禁止だが、受信者のメッセージ長決定規則のため
    // Content-Length: 0 を付与する (close-delimited にならないようにする)
    // omit_body: true かつ body が空の場合は自動付与しない
    // (HEAD レスポンスで表現長が不明なケースに配慮)
    if status_has_body
        && (!response.omit_body || !response.body.is_empty())
        && !response.has_header("Content-Length")
        && !response.has_header("Transfer-Encoding")
    {
        buf.extend_from_slice(b"Content-Length: ");
        buf.extend_from_slice(response.body.len().to_string().as_bytes());
        buf.extend_from_slice(b"\r\n");
    }

    // End of headers
    buf.extend_from_slice(b"\r\n");

    // Body
    // RFC 9110 Section 6.4.1: 1xx/204/304 はボディを含めてはならない
    // HEAD レスポンスでは omit_body: true としてボディ送信を抑止する
    if body_will_be_encoded {
        buf.extend_from_slice(&response.body);
    }

    Ok(buf)
}

impl Request {
    /// リクエストをバイト列にエンコード
    ///
    /// HTTP/1.1 リクエストで Host ヘッダーがない場合はパニックする。
    /// エラーハンドリングが必要な場合は `try_encode()` を使用する。
    pub fn encode(&self) -> Vec<u8> {
        encode_request(self).expect("HTTP/1.1 request requires Host header")
    }

    /// リクエストをバイト列にエンコード (Result 版)
    ///
    /// RFC 9112 Section 3.2: HTTP/1.1 リクエストには Host ヘッダーが必須
    pub fn try_encode(&self) -> Result<Vec<u8>, EncodeError> {
        encode_request(self)
    }
}

impl Response {
    /// レスポンスをバイト列にエンコード
    ///
    /// RFC 違反のヘッダー組み合わせがある場合はパニックする。
    /// エラーハンドリングが必要な場合は `try_encode()` を使用する。
    pub fn encode(&self) -> Vec<u8> {
        encode_response(self).expect("invalid header combination")
    }

    /// レスポンスをバイト列にエンコード (Result 版)
    ///
    /// RFC 9112 Section 6.1: 1xx / 204 レスポンスに Transfer-Encoding を含めてはならない
    /// RFC 9112 Section 6.2: Transfer-Encoding と Content-Length は同時に送信してはならない
    pub fn try_encode(&self) -> Result<Vec<u8>, EncodeError> {
        encode_response(self)
    }
}

/// Chunked Transfer Encoding 用のチャンクをエンコード
///
/// データを HTTP chunked 形式にエンコードします。
/// 空のデータを渡すと終端チャンク (0\r\n\r\n) を生成します。
pub fn encode_chunk(data: &[u8]) -> Vec<u8> {
    let mut buf = Vec::new();

    if data.is_empty() {
        // 終端チャンク
        buf.extend_from_slice(b"0\r\n\r\n");
    } else {
        // チャンクサイズ (16進数)
        buf.extend_from_slice(format!("{:x}\r\n", data.len()).as_bytes());
        // チャンクデータ
        buf.extend_from_slice(data);
        // CRLF
        buf.extend_from_slice(b"\r\n");
    }

    buf
}

/// 複数のデータを chunked 形式でエンコード
///
/// すべてのチャンクを結合し、終端チャンクも追加します。
pub fn encode_chunks(chunks: &[&[u8]]) -> Vec<u8> {
    let mut buf = Vec::new();

    for chunk in chunks {
        buf.extend_from_slice(format!("{:x}\r\n", chunk.len()).as_bytes());
        buf.extend_from_slice(chunk);
        buf.extend_from_slice(b"\r\n");
    }

    // 終端チャンク
    buf.extend_from_slice(b"0\r\n\r\n");

    buf
}

/// リクエストヘッダーのみをエンコード (ボディなし)
///
/// Chunked Transfer Encoding を使う場合に便利です。
/// ヘッダー送信後に `encode_chunk` でボディを送信できます。
///
/// RFC 9112 Section 3.2: HTTP/1.1 リクエストには Host ヘッダーが必須
/// RFC 9112 Section 6.2: Transfer-Encoding と Content-Length は同時に送信してはならない
pub fn encode_request_headers(request: &Request) -> Result<Vec<u8>, EncodeError> {
    // フィールドバリデーション
    validate_request_fields(request)?;

    // RFC 9110 Section 4.2.4: http/https URI の userinfo を拒否する
    reject_http_userinfo(&request.uri)?;

    // RFC 9110 Section 4.2.1/4.2.2: http/https URI の空 host を拒否する
    reject_http_empty_host(&request.uri)?;

    // Host ヘッダーの詳細バリデーション
    validate_host_header(request)?;

    // RFC 9112 Section 6.2: Transfer-Encoding と Content-Length の同時送信は禁止
    if request.has_header("Transfer-Encoding") && request.has_header("Content-Length") {
        return Err(EncodeError::ConflictingTransferEncodingAndContentLength);
    }

    // RFC 9110 Section 9.3.6: CONNECT リクエストは content を持たない
    // RFC 9110 Section 9.1: メソッドトークンは case-sensitive
    if request.method == "CONNECT"
        && (request.has_header("Content-Length") || request.has_header("Transfer-Encoding"))
    {
        return Err(EncodeError::ConnectRequestWithContent);
    }

    let mut buf = Vec::new();

    // Request line: METHOD SP URI SP VERSION CRLF
    buf.extend_from_slice(request.method.as_bytes());
    buf.push(b' ');
    buf.extend_from_slice(request.uri.as_bytes());
    buf.push(b' ');
    buf.extend_from_slice(request.version.as_bytes());
    buf.extend_from_slice(b"\r\n");

    // Headers
    for (name, value) in &request.headers {
        buf.extend_from_slice(name.as_bytes());
        buf.extend_from_slice(b": ");
        buf.extend_from_slice(value.as_bytes());
        buf.extend_from_slice(b"\r\n");
    }

    // End of headers
    buf.extend_from_slice(b"\r\n");

    Ok(buf)
}

/// レスポンスヘッダーのみをエンコード (ボディなし)
///
/// Chunked Transfer Encoding を使う場合に便利です。
/// ヘッダー送信後に `encode_chunk` でボディを送信できます。
///
/// RFC 9112 Section 6.1: 1xx / 204 レスポンスに Transfer-Encoding を含めてはならない
/// RFC 9112 Section 6.2: Transfer-Encoding と Content-Length は同時に送信してはならない
///
/// # CONNECT 2xx レスポンスについて
///
/// RFC 9112 Section 6.1 / RFC 9110 Section 8.6 により、CONNECT リクエストへの
/// 2xx レスポンスには Transfer-Encoding / Content-Length を含めてはならない (MUST NOT)。
/// しかし、エンコーダーはリクエストメソッドの情報を持たないため、この制約は
/// 呼び出し側アプリケーションの責務とする。
pub fn encode_response_headers(response: &Response) -> Result<Vec<u8>, EncodeError> {
    // フィールドバリデーション
    validate_response_fields(response)?;

    // RFC 9112 Section 6.2: Transfer-Encoding と Content-Length の同時送信は禁止
    if response.has_header("Transfer-Encoding") && response.has_header("Content-Length") {
        return Err(EncodeError::ConflictingTransferEncodingAndContentLength);
    }

    // RFC 9112 Section 6.1: 1xx / 204 レスポンスに Transfer-Encoding は禁止
    let is_1xx_or_204 = (100..200).contains(&response.status_code) || response.status_code == 204;
    if is_1xx_or_204 && response.has_header("Transfer-Encoding") {
        return Err(EncodeError::ForbiddenTransferEncoding {
            status_code: response.status_code,
        });
    }

    // RFC 9110 Section 8.6: 1xx / 204 レスポンスに Content-Length は禁止
    if is_1xx_or_204 && response.has_header("Content-Length") {
        return Err(EncodeError::ForbiddenContentLength {
            status_code: response.status_code,
        });
    }

    // RFC 9110 Section 15.3.6: 205 Reset Content の Transfer-Encoding 禁止
    if response.status_code == 205 && response.has_header("Transfer-Encoding") {
        return Err(EncodeError::ForbiddenTransferEncoding { status_code: 205 });
    }

    // RFC 9110 Section 8.6: 205 の Content-Length は 0 のみ許可
    if response.status_code == 205
        && let Some(cl) = response.get_header("Content-Length")
        && cl.trim() != "0"
    {
        return Err(EncodeError::ForbiddenContentLength { status_code: 205 });
    }

    let mut buf = Vec::new();

    // Status line: VERSION SP STATUS-CODE SP REASON-PHRASE CRLF
    buf.extend_from_slice(response.version.as_bytes());
    buf.push(b' ');
    buf.extend_from_slice(response.status_code.to_string().as_bytes());
    buf.push(b' ');
    buf.extend_from_slice(response.reason_phrase.as_bytes());
    buf.extend_from_slice(b"\r\n");

    // Headers
    for (name, value) in &response.headers {
        buf.extend_from_slice(name.as_bytes());
        buf.extend_from_slice(b": ");
        buf.extend_from_slice(value.as_bytes());
        buf.extend_from_slice(b"\r\n");
    }

    // End of headers
    buf.extend_from_slice(b"\r\n");

    Ok(buf)
}

impl Request {
    /// ヘッダーのみをエンコード (Chunked Transfer Encoding 用)
    ///
    /// HTTP/1.1 リクエストで Host ヘッダーがない場合はパニックする。
    /// エラーハンドリングが必要な場合は `try_encode_headers()` を使用する。
    pub fn encode_headers(&self) -> Vec<u8> {
        encode_request_headers(self).expect("HTTP/1.1 request requires Host header")
    }

    /// ヘッダーのみをエンコード (Result 版)
    ///
    /// RFC 9112 Section 3.2: HTTP/1.1 リクエストには Host ヘッダーが必須
    pub fn try_encode_headers(&self) -> Result<Vec<u8>, EncodeError> {
        encode_request_headers(self)
    }
}

impl Response {
    /// ヘッダーのみをエンコード (Chunked Transfer Encoding 用)
    ///
    /// RFC 違反のヘッダー組み合わせがある場合はパニックする。
    /// エラーハンドリングが必要な場合は `try_encode_headers()` を使用する。
    pub fn encode_headers(&self) -> Vec<u8> {
        encode_response_headers(self).expect("invalid header combination")
    }

    /// ヘッダーのみをエンコード (Result 版)
    ///
    /// RFC 9112 Section 6.1: 1xx / 204 レスポンスに Transfer-Encoding を含めてはならない
    /// RFC 9112 Section 6.2: Transfer-Encoding と Content-Length は同時に送信してはならない
    pub fn try_encode_headers(&self) -> Result<Vec<u8>, EncodeError> {
        encode_response_headers(self)
    }
}

/// レスポンスエンコーダー (圧縮対応)
///
/// # 型パラメータ
///
/// - `C`: 圧縮器の型。デフォルトは `NoCompression`(圧縮なし)。
///
/// # 使い方
///
/// ## 圧縮なし(既存 API 互換)
///
/// ```rust
/// use shiguredo_http11::ResponseEncoder;
///
/// let encoder = ResponseEncoder::new();
/// ```
///
/// ## 圧縮あり
///
/// ```ignore
/// use shiguredo_http11::ResponseEncoder;
///
/// let mut encoder = ResponseEncoder::with_compressor(GzipCompressor::new());
/// let mut output = vec![0u8; 8192];
/// let status = encoder.compress_body(body_data, &mut output)?;
/// // output[..status.produced()] に圧縮データ
/// ```
#[derive(Debug)]
pub struct ResponseEncoder<C: Compressor = NoCompression> {
    compressor: C,
}

impl Default for ResponseEncoder<NoCompression> {
    fn default() -> Self {
        Self::new()
    }
}

impl ResponseEncoder<NoCompression> {
    /// 新しいエンコーダーを作成
    pub fn new() -> Self {
        Self {
            compressor: NoCompression::new(),
        }
    }
}

impl<C: Compressor> ResponseEncoder<C> {
    /// 圧縮器付きでエンコーダーを作成
    pub fn with_compressor(compressor: C) -> Self {
        Self { compressor }
    }

    /// ボディを圧縮(ストリーミング)
    ///
    /// # 引数
    /// - `input`: 圧縮する入力データ
    /// - `output`: 圧縮データを書き込む出力バッファ
    ///
    /// # 戻り値
    /// - `Continue`: 処理継続中、入力がすべて消費された
    /// - `OutputFull`: 出力バッファが満杯、再度呼び出す必要あり
    pub fn compress_body(
        &mut self,
        input: &[u8],
        output: &mut [u8],
    ) -> Result<CompressionStatus, CompressionError> {
        self.compressor.compress(input, output)
    }

    /// 圧縮を終了
    ///
    /// 残りの圧縮データをフラッシュする。
    ///
    /// # 引数
    /// - `output`: 残りの圧縮データを書き込む出力バッファ
    ///
    /// # 戻り値
    /// - `Complete`: 圧縮完了
    /// - `OutputFull`: 出力バッファが満杯、再度呼び出す必要あり
    pub fn finish(&mut self, output: &mut [u8]) -> Result<CompressionStatus, CompressionError> {
        self.compressor.finish(output)
    }

    /// 圧縮器をリセット
    pub fn reset(&mut self) {
        self.compressor.reset();
    }
}

/// リクエストエンコーダー (圧縮対応)
///
/// # 型パラメータ
///
/// - `C`: 圧縮器の型。デフォルトは `NoCompression`(圧縮なし)。
///
/// # 使い方
///
/// ## 圧縮なし(既存 API 互換)
///
/// ```rust
/// use shiguredo_http11::RequestEncoder;
///
/// let encoder = RequestEncoder::new();
/// ```
///
/// ## 圧縮あり
///
/// ```ignore
/// use shiguredo_http11::RequestEncoder;
///
/// let mut encoder = RequestEncoder::with_compressor(GzipCompressor::new());
/// let mut output = vec![0u8; 8192];
/// let status = encoder.compress_body(body_data, &mut output)?;
/// // output[..status.produced()] に圧縮データ
/// ```
#[derive(Debug)]
pub struct RequestEncoder<C: Compressor = NoCompression> {
    compressor: C,
}

impl Default for RequestEncoder<NoCompression> {
    fn default() -> Self {
        Self::new()
    }
}

impl RequestEncoder<NoCompression> {
    /// 新しいエンコーダーを作成
    pub fn new() -> Self {
        Self {
            compressor: NoCompression::new(),
        }
    }
}

impl<C: Compressor> RequestEncoder<C> {
    /// 圧縮器付きでエンコーダーを作成
    pub fn with_compressor(compressor: C) -> Self {
        Self { compressor }
    }

    /// ボディを圧縮(ストリーミング)
    ///
    /// # 引数
    /// - `input`: 圧縮する入力データ
    /// - `output`: 圧縮データを書き込む出力バッファ
    ///
    /// # 戻り値
    /// - `Continue`: 処理継続中、入力がすべて消費された
    /// - `OutputFull`: 出力バッファが満杯、再度呼び出す必要あり
    pub fn compress_body(
        &mut self,
        input: &[u8],
        output: &mut [u8],
    ) -> Result<CompressionStatus, CompressionError> {
        self.compressor.compress(input, output)
    }

    /// 圧縮を終了
    ///
    /// 残りの圧縮データをフラッシュする。
    ///
    /// # 引数
    /// - `output`: 残りの圧縮データを書き込む出力バッファ
    ///
    /// # 戻り値
    /// - `Complete`: 圧縮完了
    /// - `OutputFull`: 出力バッファが満杯、再度呼び出す必要あり
    pub fn finish(&mut self, output: &mut [u8]) -> Result<CompressionStatus, CompressionError> {
        self.compressor.finish(output)
    }

    /// 圧縮器をリセット
    pub fn reset(&mut self) {
        self.compressor.reset();
    }
}