shiguredo_http11 2026.4.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
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
use crate::compression::{CompressionError, CompressionStatus, Compressor, NoCompression};
use crate::decoder::HttpHead;
use crate::error::EncodeError;
use crate::host::Host;
use crate::request::Request;
use crate::request_target::RequestTargetForm;
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, trim_ows,
};
use alloc::string::{String, ToString};
use alloc::vec::Vec;

/// エンコード用のバージョン文字列バリデーション
///
/// VCHAR のみ (SP/CTL 禁止)。RTSP 等の非 HTTP プロトコルにも対応。
fn is_valid_version_for_encode(version: &str) -> bool {
    !version.is_empty() && version.bytes().all(|b| matches!(b, 0x21..=0x7E))
}

/// `usize` を 16 進数 ASCII (小文字) としてバッファに書き込む
///
/// `n == 0` を早期分岐で扱うのは、`leading_zeros` で桁数を計算する変種が
/// `n == 0` で誤桁数を返すのを避けるため。CRLF はヘルパー外で結合する。
fn write_hex_usize(buf: &mut Vec<u8>, n: usize) {
    if n == 0 {
        buf.push(b'0');
        return;
    }
    let mut tmp = [0u8; 16]; // 64bit usize の 16 進表記は最大 16 桁
    let mut i = tmp.len();
    let mut remaining = n;
    while remaining > 0 {
        i -= 1;
        let nibble = (remaining & 0xF) as u8;
        tmp[i] = if nibble < 10 {
            b'0' + nibble
        } else {
            b'a' + nibble - 10
        };
        remaining >>= 4;
    }
    buf.extend_from_slice(&tmp[i..]);
}

/// `usize` を 10 進数 ASCII としてバッファに書き込む
fn write_usize_decimal(buf: &mut Vec<u8>, n: usize) {
    if n == 0 {
        buf.push(b'0');
        return;
    }
    let mut tmp = [0u8; 20]; // 64bit usize の 10 進表記は最大 20 桁
    let mut i = tmp.len();
    let mut remaining = n;
    while remaining > 0 {
        i -= 1;
        tmp[i] = b'0' + (remaining % 10) as u8;
        remaining /= 10;
    }
    buf.extend_from_slice(&tmp[i..]);
}

/// `encode_request` / `encode_response` の事前確保サイズ上限 (64 MB)
///
/// 攻撃者制御のヘッダー値で見積もりが膨張した場合に、
/// `Vec::with_capacity` の OOM abort で DoS を引き起こさないための防御線。
/// 上限を超えた場合は `Vec::new()` にフォールバックして既存挙動と同等にする。
const ENCODE_CAPACITY_LIMIT: usize = 64 * 1024 * 1024;

/// 自動付与される Content-Length 行の容量見積もり (固定値)
///
/// `"Content-Length: " (16) + usize 最大 20 桁 + CRLF (2) = 38`
/// 桁数の厳密計算は二度走査回避のために行わず、最悪ケースで過剰確保する。
const AUTO_CONTENT_LENGTH_CAPACITY: usize = 38;

/// `encode_request` で Content-Length を自動付与するか判定
fn should_auto_emit_content_length_for_request(request: &Request) -> bool {
    request.body_bytes().is_some()
        && !request.has_header("Content-Length")
        && !request.has_header("Transfer-Encoding")
}

/// ステータスコードがボディを持ちうるか判定
///
/// RFC 9110 Section 6.4.1: 1xx / 204 / 304 はボディを含めてはならない
fn response_status_has_body(status_code: u16) -> bool {
    !((100..200).contains(&status_code) || status_code == 204 || status_code == 304)
}

/// `encode_response` で Content-Length を自動付与するか判定
fn should_auto_emit_content_length_for_response(response: &Response) -> bool {
    let status_has_body = response_status_has_body(response.status_code());
    let body_len = response.body_bytes().map(<[u8]>::len);
    status_has_body
        && !response.has_header("Content-Length")
        && !response.has_header("Transfer-Encoding")
        && match (response.is_body_omitted(), body_len) {
            (_, None) => false,
            (true, Some(0)) => false,
            (_, Some(_)) => true,
        }
}

/// `encode_request` の出力容量を `checked_add` で見積もる
///
/// オーバーフロー時は `None` を返し、呼び出し側は `Vec::new()` にフォールバックする
fn estimate_request_capacity(request: &Request) -> Option<usize> {
    let mut total: usize = 0;
    // Request line: METHOD SP URI SP VERSION CRLF (固定 4: SP + SP + CRLF)
    total = total.checked_add(request.method().len())?;
    total = total.checked_add(request.uri().len())?;
    total = total.checked_add(request.version().len())?;
    total = total.checked_add(4)?;
    // 各ヘッダー: name + ": " + value + CRLF (固定 4)
    for (name, value) in HttpHead::headers(request) {
        total = total.checked_add(name.len())?;
        total = total.checked_add(value.len())?;
        total = total.checked_add(4)?;
    }
    if should_auto_emit_content_length_for_request(request) {
        total = total.checked_add(AUTO_CONTENT_LENGTH_CAPACITY)?;
    }
    // End-of-headers CRLF
    total = total.checked_add(2)?;
    if let Some(body) = request.body_bytes() {
        total = total.checked_add(body.len())?;
    }
    Some(total)
}

/// `encode_response` の出力容量を `checked_add` で見積もる
///
/// オーバーフロー時は `None` を返し、呼び出し側は `Vec::new()` にフォールバックする
fn estimate_response_capacity(response: &Response) -> Option<usize> {
    let mut total: usize = 0;
    // ステータス行: VERSION SP STATUS-CODE SP REASON CRLF
    // (固定 4: SP + SP + CRLF, 加えて status code は 3 桁固定で見積もる)
    total = total.checked_add(HttpHead::version(response).len())?;
    total = total.checked_add(3)?; // status_code 最大桁数 (validate_response_fields で 100..=599 が保証)
    total = total.checked_add(response.reason_phrase().len())?;
    total = total.checked_add(4)?;
    for (name, value) in HttpHead::headers(response) {
        total = total.checked_add(name.len())?;
        total = total.checked_add(value.len())?;
        total = total.checked_add(4)?;
    }
    if should_auto_emit_content_length_for_response(response) {
        total = total.checked_add(AUTO_CONTENT_LENGTH_CAPACITY)?;
    }
    total = total.checked_add(2)?;
    let body_will_be_encoded =
        response_status_has_body(response.status_code()) && !response.is_body_omitted();
    if body_will_be_encoded && let Some(body) = response.body_bytes() {
        total = total.checked_add(body.len())?;
    }
    Some(total)
}

/// 容量見積もりを `ENCODE_CAPACITY_LIMIT` で頭打ちにし、`Vec` を確保する
///
/// オーバーフロー (`None`) または上限超過のときは `Vec::new()` を返す。
/// `Vec::with_capacity` の内部 `alloc` は fallible でないため、
/// 巨大値の見積もりが abort/panic につながらないように防御する。
fn allocate_encode_buffer(estimated: Option<usize>) -> Vec<u8> {
    match estimated {
        Some(c) if c <= ENCODE_CAPACITY_LIMIT => Vec::with_capacity(c),
        _ => Vec::new(),
    }
}

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

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

    // is_valid_request_target() は受信側の寛容な検証で obs-text を許容する。
    // 送信側では新規に obs-text を生成してはならないため、ここで拒否する。
    if request.uri().bytes().any(|b| b > 0x7E) {
        return Err(EncodeError::InvalidRequestTarget {
            uri: request.uri().to_string(),
        });
    }

    // 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().to_string(),
        });
    }

    // ヘッダーの検証
    validate_headers(HttpHead::headers(request))?;

    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)
}

/// 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(HttpHead::version(response)) {
        return Err(EncodeError::InvalidVersion {
            version: HttpHead::version(response).to_string(),
        });
    }

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

    // reason-phrase の検証
    // RFC 9112 Section 4: status-line ABNF では reason-phrase は OPTIONAL であり、
    // server は absent でも SP を送信しなければならない (MUST)。
    // decoder 経路で空文字列を持つ Response (reason-phrase absent の再送信) を
    // 受理するため、空文字列は文字集合検証をスキップする。
    if !response.reason_phrase().is_empty() && !is_valid_reason_phrase(response.reason_phrase()) {
        return Err(EncodeError::InvalidReasonPhrase {
            phrase: response.reason_phrase().to_string(),
        });
    }

    // ヘッダーの検証
    validate_headers(HttpHead::headers(response))?;

    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> = HttpHead::headers(request)
        .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().to_string(),
            });
        }

        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().to_string(),
                });
            }

            // 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().to_string(),
                });
            }
        }
    }

    // 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().to_string(),
        });
    }

    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;
        }
        // RFC 9110 Section 5.6.3 OWS = *( SP / HTAB ) のみ除去する。
        // str::trim() は Unicode 空白 (NBSP 等) も除去するため使用しない。
        let trimmed = trim_ows(value);
        // 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: "A CONNECT request message does not have content."
    // ただし RFC は CONNECT リクエスト側に MUST NOT の制約を課していない。
    // CONNECT の意味論的制約の判断はアプリケーション層の責務とする。

    // Content-Length ヘッダーの ABNF 検証と body.len() との整合性を検証
    // body == None の場合は body 長 0 として扱う
    if !request.has_header("Transfer-Encoding")
        && let Some(header_value) = validate_content_length_headers(HttpHead::headers(request))?
    {
        let body_length = request.body_bytes().map(<[u8]>::len).unwrap_or(0) as u64;
        if header_value != body_length {
            return Err(EncodeError::ContentLengthMismatch {
                header_value,
                body_length,
            });
        }
    }

    let mut buf = allocate_encode_buffer(estimate_request_capacity(request));

    // リクエスト行: 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");

    for (name, value) in HttpHead::headers(request) {
        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 (body == Some の場合、Content-Length / Transfer-Encoding 未指定なら自動付与)
    // RFC 9110 Section 8.6: メソッド意味論で content が想定されるかは呼び出し側の判断とする。
    // body == Some(vec![]) なら Content-Length: 0、body == None なら自動付与しない。
    if let Some(body) = request.body_bytes()
        && !request.has_header("Content-Length")
        && !request.has_header("Transfer-Encoding")
    {
        buf.extend_from_slice(b"Content-Length: ");
        write_usize_decimal(&mut buf, body.len());
        buf.extend_from_slice(b"\r\n");
    }

    // ヘッダー終端の空行
    buf.extend_from_slice(b"\r\n");

    if let Some(body) = request.body_bytes() {
        buf.extend_from_slice(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 はボディを生成してはならない
    // body == Some(non-empty) のときのみ違反。Some(vec![]) と None は許容する。
    if response.status_code() == 205 {
        if response.body_bytes().is_some_and(|b| !b.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 body_will_be_encoded =
        response_status_has_body(response.status_code()) && !response.is_body_omitted();

    // Content-Length ヘッダーの ABNF 検証と body.len() との整合性を検証
    // - 通常レスポンス: 常に一致必須
    // - omit_body: true の場合は、body が空 (None または Some(vec![])) のときのみ検証をスキップ
    //   (HEAD レスポンスで Content-Length が表現長を示すケース)
    // - 1xx/204/304 は message body がないため、ここでは検証しない
    // body == None は body 長 0 として扱う
    if response_status_has_body(response.status_code())
        && !response.has_header("Transfer-Encoding")
        && let Some(header_value) = validate_content_length_headers(HttpHead::headers(response))?
    {
        let body_length = response.body_bytes().map(<[u8]>::len).unwrap_or(0) 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 = allocate_encode_buffer(estimate_response_capacity(response));

    // ステータス行: VERSION SP STATUS-CODE SP REASON-PHRASE CRLF
    buf.extend_from_slice(HttpHead::version(response).as_bytes());
    buf.push(b' ');
    write_usize_decimal(&mut buf, response.status_code() as usize);
    buf.push(b' ');
    buf.extend_from_slice(response.reason_phrase().as_bytes());
    buf.extend_from_slice(b"\r\n");

    for (name, value) in HttpHead::headers(response) {
        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 自動付与
    // RFC 9112: keep-alive を維持するために Content-Length または Transfer-Encoding が必要
    // 1xx/204/304 はボディがないため Content-Length を追加しない
    // 205 は RFC 9110 Section 15.3.6 でボディ生成禁止だが、受信者のメッセージ長決定規則のため
    // body が Some(vec![]) なら Content-Length: 0 を付与する (close-delimited にならないようにする)
    // body == None ならボディ長を表現できないため自動付与しない
    // omit_body: true かつ body が空 (None または Some(vec![])) の場合も自動付与しない
    // (HEAD レスポンスで表現長が不明なケースに配慮)
    // 容量見積もりと判定ロジックを統一するため、`should_auto_emit_content_length_for_response`
    // を介して判定する (条件がずれると過小確保で再確保が発生する)
    if should_auto_emit_content_length_for_response(response) {
        let len = response.body_bytes().map(<[u8]>::len).unwrap_or(0);
        buf.extend_from_slice(b"Content-Length: ");
        write_usize_decimal(&mut buf, len);
        buf.extend_from_slice(b"\r\n");
    }

    // ヘッダー終端の空行
    buf.extend_from_slice(b"\r\n");

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

    Ok(buf)
}

impl Request {
    /// リクエストをバイト列にエンコード
    ///
    /// 構築時バリデーションで弾かれる構文上の不正値を含まない Request でも、
    /// 意味論的な RFC 違反 (Host 欠落、Content-Length 不一致、Transfer-Encoding と
    /// Content-Length の競合等) は encode 時に検出され `Err` を返す。
    ///
    /// RFC 9112 Section 3.2: HTTP/1.1 リクエストには Host ヘッダーが必須
    pub fn encode(&self) -> Result<Vec<u8>, EncodeError> {
        encode_request(self)
    }
}

impl Response {
    /// レスポンスをバイト列にエンコード
    ///
    /// 構築時バリデーションで弾かれる構文上の不正値を含まない Response でも、
    /// 意味論的な RFC 違反 (Content-Length 不一致、1xx/204 への Transfer-Encoding 等)
    /// は encode 時に検出され `Err` を返す。
    ///
    /// RFC 9112 Section 6.1: 1xx / 204 レスポンスに Transfer-Encoding を含めてはならない
    /// RFC 9112 Section 6.2: Transfer-Encoding と Content-Length は同時に送信してはならない
    pub fn 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> {
    if data.is_empty() {
        // 終端チャンク
        let mut buf = Vec::with_capacity(5);
        buf.extend_from_slice(b"0\r\n\r\n");
        return buf;
    }

    // 1 チャンクは `hex(最大 16) + CRLF(2) + data + CRLF(2) = data.len() + 20` バイト
    // 攻撃者制御の `data.len()` で wraparound を起こさないよう checked_add でフォールバック
    let cap = data.len().checked_add(20);
    let mut buf = match cap {
        Some(c) => Vec::with_capacity(c),
        None => Vec::new(),
    };

    // チャンクサイズ (16進数)
    write_hex_usize(&mut buf, data.len());
    buf.extend_from_slice(b"\r\n");
    // チャンクデータ
    buf.extend_from_slice(data);
    // CRLF
    buf.extend_from_slice(b"\r\n");

    buf
}

/// 複数のデータを chunked 形式でエンコード
///
/// すべてのチャンクを結合し、終端チャンクも追加します。
pub fn encode_chunks(chunks: &[&[u8]]) -> Vec<u8> {
    // 容量見積もり: 各チャンク `data.len() + 20` の総和 + 終端チャンク 5 バイト
    // 攻撃者制御のサイズで wraparound しないよう checked_add でフォールバックする
    let cap = encode_chunks_capacity(chunks);
    let mut buf = match cap {
        Some(c) => Vec::with_capacity(c),
        None => Vec::new(),
    };

    for chunk in chunks {
        write_hex_usize(&mut buf, chunk.len());
        buf.extend_from_slice(b"\r\n");
        buf.extend_from_slice(chunk);
        buf.extend_from_slice(b"\r\n");
    }

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

    buf
}

/// `encode_chunks` の出力容量を `checked_add` で見積もる
/// オーバーフロー時は `None` を返し、呼び出し側は `Vec::new()` にフォールバックする
fn encode_chunks_capacity(chunks: &[&[u8]]) -> Option<usize> {
    let mut total: usize = 0;
    for chunk in chunks {
        let per = chunk.len().checked_add(20)?;
        total = total.checked_add(per)?;
    }
    total.checked_add(5)
}

/// リクエストヘッダーのみをエンコード (ボディなし)
///
/// 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: "A CONNECT request message does not have content."
    // RFC は CONNECT リクエスト側に Content-Length / Transfer-Encoding を MUST NOT とはしていない。
    // encode_request_headers はボディを扱わないため、CONNECT 専用チェックは不要。
    // ヘッダーの有無による制約はアプリケーション層の責務とする。

    let mut buf = Vec::new();

    // リクエスト行: 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");

    for (name, value) in HttpHead::headers(request) {
        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");
    }

    // ヘッダー終端の空行
    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 });
    }

    // debug_assert!: encode_response 側で行っている Content-Length と実ボディ長の
    // 一致検証を headers-only 経路でも実行し、開発中の誤用を早期に検出する。
    // TE がない場合のみ検証 (TE がある場合は chunked 送信が前提のためスキップ)。
    debug_assert!(
        {
            if response.has_header("Transfer-Encoding") {
                true
            } else if let Ok(Some(cl)) =
                validate_content_length_headers(HttpHead::headers(response))
            {
                let body_len = response.body_bytes().map(|b| b.len() as u64).unwrap_or(0);
                cl == body_len
            } else {
                true
            }
        },
        "Content-Length header value does not match body length in encode_response_headers"
    );

    let mut buf = Vec::new();

    // ステータス行: VERSION SP STATUS-CODE SP REASON-PHRASE CRLF
    buf.extend_from_slice(HttpHead::version(response).as_bytes());
    buf.push(b' ');
    write_usize_decimal(&mut buf, response.status_code() as usize);
    buf.push(b' ');
    buf.extend_from_slice(response.reason_phrase().as_bytes());
    buf.extend_from_slice(b"\r\n");

    for (name, value) in HttpHead::headers(response) {
        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");
    }

    // ヘッダー終端の空行
    buf.extend_from_slice(b"\r\n");

    Ok(buf)
}

impl Request {
    /// ヘッダーのみをエンコード (Chunked Transfer Encoding 用)
    ///
    /// 構築時バリデーションで弾かれる構文上の不正値を含まない Request でも、
    /// 意味論的な RFC 違反 (Host 欠落、Transfer-Encoding と Content-Length の競合等) は
    /// encode 時に検出され `Err` を返す。
    ///
    /// RFC 9112 Section 3.2: HTTP/1.1 リクエストには Host ヘッダーが必須
    pub fn encode_headers(&self) -> Result<Vec<u8>, EncodeError> {
        encode_request_headers(self)
    }
}

impl Response {
    /// ヘッダーのみをエンコード (Chunked Transfer Encoding 用)
    ///
    /// RFC 違反のヘッダー組み合わせがある場合は encode 時に検出され `Err` を返す。
    ///
    /// RFC 9112 Section 6.1: 1xx / 204 レスポンスに Transfer-Encoding を含めてはならない
    /// RFC 9112 Section 6.2: Transfer-Encoding と Content-Length は同時に送信してはならない
    pub fn 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();
    }
}

/// 容量見積もりが実際の出力長以上であることを検証する内部テスト
///
/// `estimate_request_capacity` / `estimate_response_capacity` はプライベート関数なので
/// 外部テストから直接呼べない。このモジュールに置くことで容量見積もりの正しさを
/// 確実に担保する (過小確保で再確保が起きると最適化の意味を失うため)。
#[cfg(test)]
mod capacity_tests {
    use super::*;
    use crate::request::Request;
    use crate::response::Response;
    use crate::status_code::StatusCode;

    fn assert_request_capacity_sufficient(req: &Request) {
        let est = estimate_request_capacity(req).expect("estimate overflow");
        let out = encode_request(req).expect("encode failed");
        assert!(
            est >= out.len(),
            "estimate {} < output {}: req={req:?}",
            est,
            out.len(),
        );
    }

    fn assert_response_capacity_sufficient(res: &Response) {
        let est = estimate_response_capacity(res).expect("estimate overflow");
        let out = encode_response(res).expect("encode failed");
        assert!(
            est >= out.len(),
            "estimate {} < output {}: res={res:?}",
            est,
            out.len(),
        );
    }

    #[test]
    fn test_request_capacity_simple_get() {
        let req = Request::new("GET", "/")
            .unwrap()
            .header("Host", "example.com")
            .unwrap();
        assert_request_capacity_sufficient(&req);
    }

    #[test]
    fn test_request_capacity_post_with_body_auto_content_length() {
        let req = Request::new("POST", "/api")
            .unwrap()
            .header("Host", "example.com")
            .unwrap()
            .body(b"hello world".to_vec());
        assert_request_capacity_sufficient(&req);
    }

    #[test]
    fn test_request_capacity_post_with_explicit_content_length() {
        let req = Request::new("POST", "/api")
            .unwrap()
            .header("Host", "example.com")
            .unwrap()
            .header("Content-Length", "11")
            .unwrap()
            .body(b"hello world".to_vec());
        assert_request_capacity_sufficient(&req);
    }

    #[test]
    fn test_request_capacity_post_with_transfer_encoding_no_auto() {
        let req = Request::new("POST", "/api")
            .unwrap()
            .header("Host", "example.com")
            .unwrap()
            .header("Transfer-Encoding", "chunked")
            .unwrap()
            .body(b"hello".to_vec());
        assert_request_capacity_sufficient(&req);
    }

    #[test]
    fn test_request_capacity_many_headers() {
        let mut req = Request::new("GET", "/")
            .unwrap()
            .header("Host", "example.com")
            .unwrap();
        for i in 0..50 {
            req = req
                .header(
                    alloc::format!("X-Custom-{i}"),
                    alloc::format!("value-{i}-with-some-padding"),
                )
                .unwrap();
        }
        assert_request_capacity_sufficient(&req);
    }

    #[test]
    fn test_request_capacity_empty_body_auto_content_length_zero() {
        let req = Request::new("POST", "/")
            .unwrap()
            .header("Host", "example.com")
            .unwrap()
            .body(Vec::new());
        assert_request_capacity_sufficient(&req);
    }

    #[test]
    fn test_request_capacity_no_body() {
        let req = Request::new("GET", "/path/to/resource?q=1")
            .unwrap()
            .header("Host", "example.com")
            .unwrap();
        assert_request_capacity_sufficient(&req);
    }

    #[test]
    fn test_response_capacity_simple_ok() {
        let res = Response::with_status(StatusCode::OK).body(b"hello".to_vec());
        assert_response_capacity_sufficient(&res);
    }

    #[test]
    fn test_response_capacity_no_body_status() {
        // 1xx / 204 / 304 は body を含めない
        for &code in &[100u16, 204, 304] {
            let res = Response::new(code, "Reason").unwrap();
            assert_response_capacity_sufficient(&res);
        }
    }

    #[test]
    fn test_response_capacity_omit_body_with_content_length() {
        let res = Response::with_status(StatusCode::OK)
            .header("Content-Length", "100")
            .unwrap()
            .omit_body(true);
        assert_response_capacity_sufficient(&res);
    }

    #[test]
    fn test_response_capacity_with_transfer_encoding() {
        let res = Response::with_status(StatusCode::OK)
            .header("Transfer-Encoding", "chunked")
            .unwrap();
        assert_response_capacity_sufficient(&res);
    }

    #[test]
    fn test_response_capacity_many_headers() {
        let mut res = Response::with_status(StatusCode::OK).body(vec![b'X'; 1024]);
        for i in 0..50 {
            res = res
                .header(
                    alloc::format!("X-Custom-{i}"),
                    alloc::format!("value-{i}-with-some-padding"),
                )
                .unwrap();
        }
        assert_response_capacity_sufficient(&res);
    }

    #[test]
    fn test_response_capacity_status_code_3_digit_boundary() {
        // status_code は 100..=599、見積もりは 3 桁固定なので過小確保にならない
        for &code in &[100u16, 200, 599] {
            let res = Response::new(code, "Phrase")
                .unwrap()
                .body(b"body".to_vec());
            assert_response_capacity_sufficient(&res);
        }
    }

    #[test]
    fn test_request_capacity_normal_input_does_not_panic() {
        // 巨大なヘッダー長をシミュレートするのは現実不可能なので、
        // ここではオーバーフロー時のフォールバックパス (`Vec::new()`) が
        // パニックしないことを通常入力で確認する。
        // 実際のオーバーフロー検出は fuzz_encode_request で網羅する。
        let req = Request::new("GET", "/")
            .unwrap()
            .header("Host", "example.com")
            .unwrap();
        let _ = encode_request(&req).unwrap();
    }
}

/// `validate_response_fields` の reason-phrase absent 経路のカバレッジ補填テスト。
///
/// 構築時バリデーション (`Response::new` / `with_version`) では空 reason_phrase は
/// 拒否されるため、`Response::from_raw_parts` (`pub(crate)`) 経由で空 reason_phrase
/// を持つ Response を構築し、`encode_response` が空文字列を absent として受理する
/// ことを確認する (reverse proxy の transparent forward 経路の RFC 9112 Section 4 準拠)。
///
/// 他の検証分岐 (status_code, header name, header value 等) は `from_raw_parts` の
/// `debug_assert!` 契約と `validate_response_fields` の検査が同等のため、debug ビルド
/// (テスト時) では `from_raw_parts` 側の `debug_assert!` で先に弾かれる。これらの分岐
/// は release ビルドの最終防御線として機能するが、debug テスト経路では到達不能。
#[cfg(test)]
mod validate_response_fields_tests {
    use super::*;
    use crate::response::Response;
    use alloc::string::ToString;

    #[test]
    fn test_validate_response_fields_empty_reason_phrase_is_accepted() {
        let res =
            Response::from_raw_parts("HTTP/1.1".to_string(), 200, String::new(), Vec::new(), None);
        let encoded = encode_response(&res).unwrap();
        // status-line は "HTTP/1.1 200 \r\n" (status-code の後に SP 1 個 + CRLF)
        assert!(encoded.starts_with(b"HTTP/1.1 200 \r\n"));
    }
}