Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,18 @@ class StatusLine(
throw ProtocolException("Unexpected status line: $statusLine")
}

// Parse response code like "200". Always 3 digits.
// Parse response code like "200". Always 3 ASCII digits.
if (statusLine.length < codeStart + 3) {
throw ProtocolException("Unexpected status line: $statusLine")
}
val code =
statusLine.substring(codeStart, codeStart + 3).toIntOrNull()
?: throw ProtocolException(
"Unexpected status line: $statusLine",
)
var code = 0
for (i in codeStart until codeStart + 3) {
val digit = statusLine[i]
if (digit !in '0'..'9') {
throw ProtocolException("Unexpected status line: $statusLine")
}
code = code * 10 + (digit - '0')
}

// Parse an optional response message like "OK" or "Not Modified". If it
// exists, it is separated from the response code by a space.
Expand Down
10 changes: 10 additions & 0 deletions okhttp/src/jvmTest/kotlin/okhttp3/internal/http/StatusLineTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ class StatusLineTest {
assertInvalid("HTTP/1.1 two")
}

@Test
fun nonAsciiDigitCode() {
// toIntOrNull() honors a sign prefix and any Unicode decimal digit, so a status code such
// as "+99", "-12" or the Arabic-Indic "٢٠٠" would otherwise be accepted even though
// RFC 9112 defines status-code as three ASCII DIGIT.
assertInvalid("HTTP/1.1 +99 OK")
assertInvalid("HTTP/1.1 -12 OK")
assertInvalid("HTTP/1.1 ٢٠٠ OK")
}

@Test
fun truncated() {
assertInvalid("")
Expand Down
Loading