valyala/fasthttp · warning
missing the end position of byte range in %q
Error message
missing the end position of byte range in %q
What it means
A byte-range spec 'bytes=-N' (suffix form) is only valid when contentLength > 0; the '-' must appear and, when the spec starts with '-', fasthttp parses the suffix length and applies it to the content. This error fires when the range set contains no '-' character at all, so there is no end position to parse.
Source
Thrown at fs.go:1495
// ParseByteRange parses 'Range: bytes=...' header value.
//
// It follows https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35 .
func ParseByteRange(byteRange []byte, contentLength int) (startPos, endPos int, err error) {
b := byteRange
if !bytes.HasPrefix(b, strBytes) {
return 0, 0, fmt.Errorf("unsupported range units: %q: expecting %q", byteRange, strBytes)
}
b = b[len(strBytes):]
if len(b) == 0 || b[0] != '=' {
return 0, 0, fmt.Errorf("missing byte range in %q", byteRange)
}
b = b[1:]
n := bytes.IndexByte(b, '-')
if n < 0 {
return 0, 0, fmt.Errorf("missing the end position of byte range in %q", byteRange)
}
if n == 0 {
v, err := ParseUint(b[n+1:])
if err != nil {
return 0, 0, err
}
if contentLength <= 0 {
return 0, 0, fmt.Errorf("byte range %q is invalid for empty content", byteRange)
}
startPos := max(contentLength-v, 0)
return startPos, contentLength - 1, nil
}
if startPos, err = ParseUint(b[:n]); err != nil {
return 0, 0, err
}
if startPos >= contentLength {View on GitHub (pinned to c96f600972)
Solutions
- Send the correct form: bytes=100- (open-ended from offset 100) or bytes=100-199
- For the last N bytes send the suffix form: bytes=-500
- Pre-validate the header client-side: ensure it matches bytes=start-end form
- Strip malformed Range headers in a proxy layer so the server serves the full body with 200
Example fix
// before curl -H 'Range: bytes=100' https://host/bigfile // after curl -H 'Range: bytes=100-' https://host/bigfile
Defensive patterns
Strategy: validation
Validate before calling
spec := strings.TrimPrefix(string(hdr), "bytes=")
if !strings.Contains(spec, "-") {
return errors.New("byte range must contain '-' (e.g. bytes=0-99 or bytes=100-)")
} Type guard
func hasDash(spec []byte) bool { return bytes.IndexByte(spec, '-') >= 0 } Try / catch
start, end, err := fs.ParseByteRange(hdr, cl)
if err != nil && strings.Contains(err.Error(), "missing the end position") {
return 416 // or fall back to 200 full body
} Prevention
- Teach clients the suffix form: bytes=-N means last N bytes
- Open-ended ranges must keep the trailing dash: bytes=100-
- Reject non-conforming range specs before forwarding to origin
- Add integration tests asserting 416 on dashless ranges
When it happens
Trigger: Client sends Range: bytes=500 (no dash) or Range: bytes=abc — bytes.IndexByte(b, '-') returns -1; also any direct ParseByteRange call with a value lacking a '-'.
Common situations: Custom clients that implement ranges by sending only the start offset; misconfigured download managers; hand-written curl scripts missing the trailing dash (e.g. 'curl -H Range:bytes=100').
Related errors
- unsupported range units: %q: expecting %q
- missing byte range in %q
- the start position of byte range cannot exceed the end posit
- fasthttp: non-numeric chars found
- byte range %q is invalid for empty content
AI-assisted analysis of valyala/fasthttp@c96f600972 (2026-08-31).
Data as JSON: /api/errors/682eff57cb6ec454.
Report an issue: GitHub.