Merge pull request #4895 from roc-lang/digits-parser-crash

Parse numbers in Http parser
This commit is contained in:
Folkert de Vries 2023-01-27 09:23:40 +01:00 committed by GitHub
commit 37fab4a103
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 14 deletions

View File

@ -16,13 +16,14 @@ interface Parser.Http
codeunitSatisfies,
strFromRaw,
anyRawString,
digits,
},
]
# https://www.ietf.org/rfc/rfc2616.txt
Method : [Options, Get, Post, Put, Delete, Head, Trace, Connect, Patch]
HttpVersion : Str
HttpVersion : { major : U8, minor : U8 }
Request : {
method : Method,
@ -34,7 +35,7 @@ Request : {
Response : {
httpVersion : HttpVersion,
statusCode : Str,
statusCode : U16,
status : Str,
headers : List [Header Str Str],
body : List U8,
@ -71,18 +72,19 @@ requestUri =
sp = codeunit ' '
crlf = string "\r\n"
# TODO: The 'digit' and 'digits' from Parser.Str are causing repl.expect to blow up
digit = codeunitSatisfies \c -> c >= '0' && c <= '9'
digits = digit |> oneOrMore |> map strFromRaw
httpVersion : Parser RawStr HttpVersion
httpVersion =
const (\major -> \minor -> "\(major).\(minor)")
const (\major -> \minor -> { major, minor })
|> skip (string "HTTP/")
|> keep digits
|> skip (codeunit '.')
|> keep digits
expect
actual = parseStr httpVersion "HTTP/1.1"
expected = Ok { major: 1, minor: 1 }
actual == expected
Header : [Header Str Str]
stringWithoutColon : Parser RawStr Str
@ -139,7 +141,7 @@ expect
expected = Ok {
method: Get,
uri: "/things?id=1",
httpVersion: "1.1",
httpVersion: { major: 1, minor: 1 },
headers: [
Header "Host" "bar.example",
Header "Accept-Encoding" "gzip, deflate",
@ -167,7 +169,7 @@ expect
expected = Ok {
method: Options,
uri: "/resources/post-here/",
httpVersion: "1.1",
httpVersion: { major: 1, minor: 1 },
headers: [
Header "Host" "bar.example",
Header "Accept" "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
@ -234,8 +236,8 @@ expect
parseStr response responseText
expected =
Ok {
httpVersion: "1.1",
statusCode: "200",
httpVersion: { major: 1, minor: 1 },
statusCode: 200,
status: "OK",
headers: [
Header "Content-Type" "text/html; charset=utf-8",

View File

@ -178,10 +178,10 @@ digit : Parser RawStr U8
digit =
digitParsers =
List.range { start: At '0', end: At '9' }
|> List.map \digitNum ->
digitNum
|> List.map \digitCodeUnit ->
digitCodeUnit
|> codeunit
|> map \_ -> digitNum
|> map \_ -> digitCodeUnit - '0'
oneOf digitParsers