shiguredo_http11 2026.1.1

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
//! Range リクエストヘッダー (RFC 9110)
//!
//! ## 概要
//!
//! RFC 9110 Section 14 に基づいた Range リクエストヘッダーのパースを提供します。
//!
//! ## 使い方
//!
//! ```rust
//! use shiguredo_http11::range::{Range, ContentRange, AcceptRanges};
//!
//! // Range ヘッダーパース
//! let range = Range::parse("bytes=0-499").unwrap();
//! assert_eq!(range.unit(), "bytes");
//! let specs = range.ranges();
//! assert_eq!(specs.len(), 1);
//!
//! // Content-Range ヘッダー生成
//! let cr = ContentRange::new_bytes(0, 499, Some(1000));
//! assert_eq!(cr.to_string(), "bytes 0-499/1000");
//!
//! // Accept-Ranges ヘッダーパース
//! let ar = AcceptRanges::parse("bytes").unwrap();
//! assert!(ar.accepts_bytes());
//! ```

use core::fmt;

/// Range パースエラー
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RangeError {
    /// 空の入力
    Empty,
    /// 不正な形式
    InvalidFormat,
    /// 不正な単位
    InvalidUnit,
    /// 不正な範囲
    InvalidRange,
    /// 範囲が不正 (開始 > 終了)
    InvalidBounds,
}

impl fmt::Display for RangeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RangeError::Empty => write!(f, "empty range header"),
            RangeError::InvalidFormat => write!(f, "invalid range header format"),
            RangeError::InvalidUnit => write!(f, "invalid range unit"),
            RangeError::InvalidRange => write!(f, "invalid range specification"),
            RangeError::InvalidBounds => write!(f, "invalid range bounds (start > end)"),
        }
    }
}

impl std::error::Error for RangeError {}

/// 範囲指定
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RangeSpec {
    /// 開始位置から終了位置まで (両端含む)
    /// bytes=0-499 → Range { start: 0, end: 499 }
    Range { start: u64, end: u64 },
    /// 開始位置から末尾まで
    /// bytes=500- → FromStart { start: 500 }
    FromStart { start: u64 },
    /// 末尾から n バイト
    /// bytes=-500 → Suffix { length: 500 }
    Suffix { length: u64 },
}

impl RangeSpec {
    /// 実際のバイト範囲を計算
    ///
    /// total_length はリソースの総バイト数
    /// 戻り値は (start, end) で両端含む
    pub fn to_bounds(&self, total_length: u64) -> Option<(u64, u64)> {
        if total_length == 0 {
            return None;
        }
        match *self {
            RangeSpec::Range { start, end } => {
                if start > end || start >= total_length {
                    return None;
                }
                let end = end.min(total_length - 1);
                Some((start, end))
            }
            RangeSpec::FromStart { start } => {
                if start >= total_length {
                    return None;
                }
                Some((start, total_length - 1))
            }
            RangeSpec::Suffix { length } => {
                if length == 0 {
                    return None;
                }
                let start = total_length.saturating_sub(length);
                Some((start, total_length - 1))
            }
        }
    }
}

impl fmt::Display for RangeSpec {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RangeSpec::Range { start, end } => write!(f, "{}-{}", start, end),
            RangeSpec::FromStart { start } => write!(f, "{}-", start),
            RangeSpec::Suffix { length } => write!(f, "-{}", length),
        }
    }
}

/// Range ヘッダー (RFC 9110 Section 14.2)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Range {
    /// 範囲単位 (通常は "bytes")
    unit: String,
    /// 範囲指定のリスト
    ranges: Vec<RangeSpec>,
}

impl Range {
    /// Range ヘッダーをパース
    ///
    /// # 例
    ///
    /// ```rust
    /// use shiguredo_http11::range::Range;
    ///
    /// // 単一範囲
    /// let range = Range::parse("bytes=0-499").unwrap();
    /// assert_eq!(range.unit(), "bytes");
    ///
    /// // 複数範囲
    /// let range = Range::parse("bytes=0-499, 1000-1499").unwrap();
    /// assert_eq!(range.ranges().len(), 2);
    ///
    /// // 末尾から
    /// let range = Range::parse("bytes=-500").unwrap();
    /// ```
    pub fn parse(input: &str) -> Result<Self, RangeError> {
        let input = input.trim();
        if input.is_empty() {
            return Err(RangeError::Empty);
        }

        // unit=ranges の形式
        let eq_pos = input.find('=').ok_or(RangeError::InvalidFormat)?;
        let unit = input[..eq_pos].trim();
        let ranges_str = input[eq_pos + 1..].trim();

        // RFC 9110 Section 14.1: range-unit = token
        if !is_valid_token(unit) {
            return Err(RangeError::InvalidUnit);
        }

        let mut ranges = Vec::new();
        for part in ranges_str.split(',') {
            let part = part.trim();
            if part.is_empty() {
                continue;
            }
            ranges.push(parse_range_spec(part)?);
        }

        if ranges.is_empty() {
            return Err(RangeError::Empty);
        }

        Ok(Range {
            unit: unit.to_string(),
            ranges,
        })
    }

    /// 単位を取得
    pub fn unit(&self) -> &str {
        &self.unit
    }

    /// バイト範囲かどうか
    pub fn is_bytes(&self) -> bool {
        self.unit.eq_ignore_ascii_case("bytes")
    }

    /// 範囲指定のリストを取得
    pub fn ranges(&self) -> &[RangeSpec] {
        &self.ranges
    }

    /// 最初の範囲を取得
    pub fn first(&self) -> Option<&RangeSpec> {
        self.ranges.first()
    }
}

impl fmt::Display for Range {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}=", self.unit)?;
        let specs: Vec<String> = self.ranges.iter().map(|r| r.to_string()).collect();
        write!(f, "{}", specs.join(", "))
    }
}

/// 範囲指定をパース
fn parse_range_spec(s: &str) -> Result<RangeSpec, RangeError> {
    let dash_pos = s.find('-').ok_or(RangeError::InvalidRange)?;

    let start_str = s[..dash_pos].trim();
    let end_str = s[dash_pos + 1..].trim();

    if start_str.is_empty() && end_str.is_empty() {
        return Err(RangeError::InvalidRange);
    }

    if start_str.is_empty() {
        // Suffix: -500
        let length = end_str
            .parse::<u64>()
            .map_err(|_| RangeError::InvalidRange)?;
        return Ok(RangeSpec::Suffix { length });
    }

    let start = start_str
        .parse::<u64>()
        .map_err(|_| RangeError::InvalidRange)?;

    if end_str.is_empty() {
        // FromStart: 500-
        return Ok(RangeSpec::FromStart { start });
    }

    // Range: 0-499
    let end = end_str
        .parse::<u64>()
        .map_err(|_| RangeError::InvalidRange)?;

    if start > end {
        return Err(RangeError::InvalidBounds);
    }

    Ok(RangeSpec::Range { start, end })
}

/// Content-Range ヘッダー (RFC 9110 Section 14.4)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContentRange {
    /// 範囲単位
    unit: String,
    /// 開始位置
    start: Option<u64>,
    /// 終了位置
    end: Option<u64>,
    /// 完全な長さ (不明な場合は None)
    complete_length: Option<u64>,
}

impl ContentRange {
    /// Content-Range ヘッダーをパース
    pub fn parse(input: &str) -> Result<Self, RangeError> {
        let input = input.trim();
        if input.is_empty() {
            return Err(RangeError::Empty);
        }

        // unit range/length の形式
        let space_pos = input.find(' ').ok_or(RangeError::InvalidFormat)?;
        let unit = input[..space_pos].trim();
        let rest = input[space_pos + 1..].trim();

        // RFC 9110 Section 14.1: range-unit = token
        if !is_valid_token(unit) {
            return Err(RangeError::InvalidUnit);
        }

        // range/length
        let slash_pos = rest.find('/').ok_or(RangeError::InvalidFormat)?;
        let range_str = rest[..slash_pos].trim();
        let length_str = rest[slash_pos + 1..].trim();

        let complete_length = if length_str == "*" {
            None
        } else {
            Some(
                length_str
                    .parse::<u64>()
                    .map_err(|_| RangeError::InvalidFormat)?,
            )
        };

        if range_str == "*" {
            // RFC 9110 Section 14.4: unsatisfied-range = "*/" complete-length
            // complete-length = 1*DIGIT (必須)
            if complete_length.is_none() {
                return Err(RangeError::InvalidFormat);
            }
            return Ok(ContentRange {
                unit: unit.to_string(),
                start: None,
                end: None,
                complete_length,
            });
        }

        // start-end
        let dash_pos = range_str.find('-').ok_or(RangeError::InvalidFormat)?;
        let start = range_str[..dash_pos]
            .parse::<u64>()
            .map_err(|_| RangeError::InvalidFormat)?;
        let end = range_str[dash_pos + 1..]
            .parse::<u64>()
            .map_err(|_| RangeError::InvalidFormat)?;

        if start > end {
            return Err(RangeError::InvalidBounds);
        }

        // RFC 9110 Section 14.4: complete-length が last-pos 以下なら invalid
        if let Some(len) = complete_length
            && len <= end
        {
            return Err(RangeError::InvalidBounds);
        }

        Ok(ContentRange {
            unit: unit.to_string(),
            start: Some(start),
            end: Some(end),
            complete_length,
        })
    }

    /// 新しい Content-Range を作成 (bytes)
    pub fn new_bytes(start: u64, end: u64, complete_length: Option<u64>) -> Self {
        ContentRange {
            unit: "bytes".to_string(),
            start: Some(start),
            end: Some(end),
            complete_length,
        }
    }

    /// 範囲が満たせない場合の Content-Range (bytes */total)
    pub fn unsatisfied(unit: &str, complete_length: u64) -> Self {
        ContentRange {
            unit: unit.to_string(),
            start: None,
            end: None,
            complete_length: Some(complete_length),
        }
    }

    /// 単位を取得
    pub fn unit(&self) -> &str {
        &self.unit
    }

    /// 開始位置を取得
    pub fn start(&self) -> Option<u64> {
        self.start
    }

    /// 終了位置を取得
    pub fn end(&self) -> Option<u64> {
        self.end
    }

    /// 完全な長さを取得
    pub fn complete_length(&self) -> Option<u64> {
        self.complete_length
    }

    /// 範囲の長さを取得
    pub fn length(&self) -> Option<u64> {
        match (self.start, self.end) {
            (Some(s), Some(e)) => Some(e - s + 1),
            _ => None,
        }
    }

    /// 範囲が満たせないかどうか
    pub fn is_unsatisfied(&self) -> bool {
        self.start.is_none() && self.end.is_none()
    }
}

impl fmt::Display for ContentRange {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match (self.start, self.end) {
            (Some(s), Some(e)) => {
                write!(f, "{} {}-{}/", self.unit, s, e)?;
            }
            _ => {
                write!(f, "{} */", self.unit)?;
            }
        }
        match self.complete_length {
            Some(len) => write!(f, "{}", len),
            None => write!(f, "*"),
        }
    }
}

/// Accept-Ranges ヘッダー (RFC 9110 Section 14.3)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AcceptRanges {
    /// 受け入れる範囲単位のリスト
    units: Vec<String>,
}

impl AcceptRanges {
    /// Accept-Ranges ヘッダーをパース
    pub fn parse(input: &str) -> Result<Self, RangeError> {
        let input = input.trim();
        if input.is_empty() {
            return Err(RangeError::Empty);
        }

        let units: Vec<String> = input
            .split(',')
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
            .collect();

        if units.is_empty() {
            return Err(RangeError::Empty);
        }

        // RFC 9110 Section 14.3: acceptable-ranges = 1#range-unit
        // range-unit = token
        for unit in &units {
            if !is_valid_token(unit) {
                return Err(RangeError::InvalidUnit);
            }
        }

        // RFC 9110 Section 14.3: "none" は範囲リクエスト非対応を示す予約語であり、
        // 他の単位と混在させることはできない
        if units.len() > 1 && units.iter().any(|u| u.eq_ignore_ascii_case("none")) {
            return Err(RangeError::InvalidUnit);
        }

        Ok(AcceptRanges { units })
    }

    /// bytes を作成
    pub fn bytes() -> Self {
        AcceptRanges {
            units: vec!["bytes".to_string()],
        }
    }

    /// none を作成
    pub fn none() -> Self {
        AcceptRanges {
            units: vec!["none".to_string()],
        }
    }

    /// 単位のリストを取得
    pub fn units(&self) -> &[String] {
        &self.units
    }

    /// bytes を受け入れるかどうか
    pub fn accepts_bytes(&self) -> bool {
        self.units.iter().any(|u| u.eq_ignore_ascii_case("bytes"))
    }

    /// 何も受け入れないかどうか
    pub fn is_none(&self) -> bool {
        self.units.len() == 1 && self.units[0].eq_ignore_ascii_case("none")
    }
}

impl fmt::Display for AcceptRanges {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.units.join(", "))
    }
}

/// 有効なトークン文字列かどうか (RFC 9110 Section 5.6.2)
fn is_valid_token(s: &str) -> bool {
    !s.is_empty() && s.bytes().all(is_token_char)
}

/// トークン文字 (RFC 9110 Section 5.6.2)
///
/// token = 1*tchar
/// tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
///         "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
fn is_token_char(b: u8) -> bool {
    matches!(
        b,
        b'!' | b'#' | b'$' | b'%' | b'&' | b'\'' | b'*' | b'+' | b'-' | b'.' |
        b'0'..=b'9' | b'A'..=b'Z' | b'^' | b'_' | b'`' | b'a'..=b'z' | b'|' | b'~'
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_range_single() {
        let range = Range::parse("bytes=0-499").unwrap();
        assert_eq!(range.unit(), "bytes");
        assert!(range.is_bytes());
        let specs = range.ranges();
        assert_eq!(specs.len(), 1);
        match specs[0] {
            RangeSpec::Range { start, end } => {
                assert_eq!(start, 0);
                assert_eq!(end, 499);
            }
            _ => panic!("expected Range"),
        }
    }

    #[test]
    fn test_parse_range_multiple() {
        let range = Range::parse("bytes=0-499, 1000-1499").unwrap();
        assert_eq!(range.ranges().len(), 2);
    }

    #[test]
    fn test_parse_range_suffix() {
        let range = Range::parse("bytes=-500").unwrap();
        match range.first().unwrap() {
            RangeSpec::Suffix { length } => assert_eq!(*length, 500),
            _ => panic!("expected Suffix"),
        }
    }

    #[test]
    fn test_parse_range_from_start() {
        let range = Range::parse("bytes=500-").unwrap();
        match range.first().unwrap() {
            RangeSpec::FromStart { start } => assert_eq!(*start, 500),
            _ => panic!("expected FromStart"),
        }
    }

    #[test]
    fn test_parse_range_invalid_bounds() {
        assert!(Range::parse("bytes=500-100").is_err());
    }

    #[test]
    fn test_parse_range_invalid_unit() {
        // RFC 9110 Section 14.1: range-unit = token
        // 不正な token 文字を含む unit は拒否されるべき
        assert!(Range::parse("byt es=0-499").is_err()); // スペースは token に含まれない
        assert!(Range::parse("bytes/foo=0-499").is_err()); // '/' は token に含まれない
        assert!(Range::parse("=0-499").is_err()); // 空の unit
        assert!(Range::parse("by\tes=0-499").is_err()); // タブは token に含まれない (unit の中)
        assert!(Range::parse("byt(es=0-499").is_err()); // '(' は token に含まれない
    }

    #[test]
    fn test_range_spec_to_bounds() {
        let total = 1000;

        // 0-499
        let spec = RangeSpec::Range { start: 0, end: 499 };
        assert_eq!(spec.to_bounds(total), Some((0, 499)));

        // 500-
        let spec = RangeSpec::FromStart { start: 500 };
        assert_eq!(spec.to_bounds(total), Some((500, 999)));

        // -200
        let spec = RangeSpec::Suffix { length: 200 };
        assert_eq!(spec.to_bounds(total), Some((800, 999)));

        // 範囲外
        let spec = RangeSpec::Range {
            start: 1000,
            end: 1500,
        };
        assert_eq!(spec.to_bounds(total), None);
    }

    #[test]
    fn test_range_display() {
        let range = Range::parse("bytes=0-499, 1000-1499").unwrap();
        assert_eq!(range.to_string(), "bytes=0-499, 1000-1499");
    }

    #[test]
    fn test_content_range_parse() {
        let cr = ContentRange::parse("bytes 0-499/1000").unwrap();
        assert_eq!(cr.unit(), "bytes");
        assert_eq!(cr.start(), Some(0));
        assert_eq!(cr.end(), Some(499));
        assert_eq!(cr.complete_length(), Some(1000));
        assert_eq!(cr.length(), Some(500));
    }

    #[test]
    fn test_content_range_unknown_length() {
        let cr = ContentRange::parse("bytes 0-499/*").unwrap();
        assert_eq!(cr.complete_length(), None);
    }

    #[test]
    fn test_content_range_unsatisfied() {
        let cr = ContentRange::parse("bytes */1000").unwrap();
        assert!(cr.is_unsatisfied());
        assert_eq!(cr.complete_length(), Some(1000));
    }

    #[test]
    fn test_content_range_unsatisfied_requires_length() {
        // unsatisfied-range = "*/" complete-length (complete-length 必須)
        assert!(ContentRange::parse("bytes */*").is_err());
    }

    #[test]
    fn test_content_range_complete_length_must_exceed_last_pos() {
        // complete-length <= last-pos は invalid
        assert!(ContentRange::parse("bytes 0-999/500").is_err());
        assert!(ContentRange::parse("bytes 0-999/999").is_err());
        // complete-length > last-pos は OK
        assert!(ContentRange::parse("bytes 0-999/1000").is_ok());
    }

    #[test]
    fn test_content_range_display() {
        let cr = ContentRange::new_bytes(0, 499, Some(1000));
        assert_eq!(cr.to_string(), "bytes 0-499/1000");

        let cr = ContentRange::unsatisfied("bytes", 1000);
        assert_eq!(cr.to_string(), "bytes */1000");
    }

    #[test]
    fn test_accept_ranges_bytes() {
        let ar = AcceptRanges::parse("bytes").unwrap();
        assert!(ar.accepts_bytes());
        assert!(!ar.is_none());
    }

    #[test]
    fn test_accept_ranges_none() {
        let ar = AcceptRanges::parse("none").unwrap();
        assert!(ar.is_none());
        assert!(!ar.accepts_bytes());
    }

    #[test]
    fn test_accept_ranges_display() {
        let ar = AcceptRanges::bytes();
        assert_eq!(ar.to_string(), "bytes");
    }

    #[test]
    fn test_accept_ranges_invalid_token() {
        assert!(AcceptRanges::parse("inv@lid").is_err());
        assert!(AcceptRanges::parse("bytes, inv@lid").is_err());
    }
}