From 885e439879b82fda7ef2690cf23ba83537619b0b Mon Sep 17 00:00:00 2001 From: Matthew Kerwin Date: Mon, 2 Apr 2018 19:02:31 +1000 Subject: [PATCH 1/3] write a number algorithm that matches ABNF This algorithm automatically enforces the ABNF, including length limits. It still punts on the actual strtoi/strtof part. Closes #475 ? --- draft-ietf-httpbis-header-structure.md | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/draft-ietf-httpbis-header-structure.md b/draft-ietf-httpbis-header-structure.md index 1c1e40b8d..5b0f60e02 100644 --- a/draft-ietf-httpbis-header-structure.md +++ b/draft-ietf-httpbis-header-structure.md @@ -354,12 +354,21 @@ Example-IntegerHeader: 42 NOTE: This algorithm parses both Integers and Floats {{float}}, and returns the corresponding structure. -1. If the first character of input_string is not "-" or a DIGIT, fail parsing. -2. Let input_number be the result of consuming input_string up to (but not including) the first character that is not in DIGIT, "-", and ".". -3. If any character of input_number after the first is "-", fail parsing. -4. If input_number contains ".", parse it as a floating point number and let output_number be the result. -5. Otherwise, parse input_number as an integer and let output_number be the result. -6. Return output_number. +1. Let type be "integer". +2. Let sign be 1. +3. Let input_number be an empty string. +4. If the first character of input_string is "-", remove it from input_string and set sign to -1. +5. If the first character of input_string is not a DIGIT, fail parsing. +6. While input_string is not empty: + 1. Let char be the result of removing the first character of input_string. + 2. If char is a DIGIT, append it to input_number. + 3. Else, if type is "integer" and char is ".", append char to input_number and set type to "float". + 4. Otherwise, fail parsing. + 5. If type is "integer" and input_number contains more than 19 characters, fail parsing. + 6. If type is "float" and input_number contains more than 16 characters, fail parsing. +7. If type is "integer", parse input_number as an integers and let output_number be the result. +8. Otherwise, parse input_number as a float and let output_number be the result. +9. Return the product of output_number and sign. ## Floats {#float} From 9d08b682780a475691ffbf0c4692a58a1044f6e7 Mon Sep 17 00:00:00 2001 From: Matthew Kerwin Date: Mon, 2 Apr 2018 20:23:58 +1000 Subject: [PATCH 2/3] handle empty string, final "." in float --- draft-ietf-httpbis-header-structure.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/draft-ietf-httpbis-header-structure.md b/draft-ietf-httpbis-header-structure.md index 5b0f60e02..a21e00660 100644 --- a/draft-ietf-httpbis-header-structure.md +++ b/draft-ietf-httpbis-header-structure.md @@ -358,17 +358,20 @@ NOTE: This algorithm parses both Integers and Floats {{float}}, and returns the 2. Let sign be 1. 3. Let input_number be an empty string. 4. If the first character of input_string is "-", remove it from input_string and set sign to -1. -5. If the first character of input_string is not a DIGIT, fail parsing. -6. While input_string is not empty: +5. If input_string is empty, fail parsing. +6. If the first character of input_string is not a DIGIT, fail parsing. +7. While input_string is not empty: 1. Let char be the result of removing the first character of input_string. 2. If char is a DIGIT, append it to input_number. 3. Else, if type is "integer" and char is ".", append char to input_number and set type to "float". 4. Otherwise, fail parsing. 5. If type is "integer" and input_number contains more than 19 characters, fail parsing. 6. If type is "float" and input_number contains more than 16 characters, fail parsing. -7. If type is "integer", parse input_number as an integers and let output_number be the result. -8. Otherwise, parse input_number as a float and let output_number be the result. -9. Return the product of output_number and sign. +8. If type is "integer", parse input_number as an integers and let output_number be the result. +9. Otherwise: + 1. If the final character of input_number is ".", fail parsing. + 2. Parse input_number as a float and let output_number be the result. +0. Return the product of output_number and sign. ## Floats {#float} From 347d45b77d1c134332849a12ca7007e06379db39 Mon Sep 17 00:00:00 2001 From: Matthew Kerwin Date: Mon, 2 Apr 2018 20:47:44 +1000 Subject: [PATCH 3/3] typo in number algorithm --- draft-ietf-httpbis-header-structure.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/draft-ietf-httpbis-header-structure.md b/draft-ietf-httpbis-header-structure.md index a21e00660..18abca1f7 100644 --- a/draft-ietf-httpbis-header-structure.md +++ b/draft-ietf-httpbis-header-structure.md @@ -367,7 +367,7 @@ NOTE: This algorithm parses both Integers and Floats {{float}}, and returns the 4. Otherwise, fail parsing. 5. If type is "integer" and input_number contains more than 19 characters, fail parsing. 6. If type is "float" and input_number contains more than 16 characters, fail parsing. -8. If type is "integer", parse input_number as an integers and let output_number be the result. +8. If type is "integer", parse input_number as an integer and let output_number be the result. 9. Otherwise: 1. If the final character of input_number is ".", fail parsing. 2. Parse input_number as a float and let output_number be the result.