fix: update header parsing to use isToken function and add tests for valid tokens#482
Merged
lesismal merged 1 commit intolesismal:masterfrom Jul 16, 2025
Merged
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes HTTP header parsing by updating the validation logic to use the isToken function instead of isAlpha, ensuring compliance with HTTP RFC specifications for valid token characters in header field names.
- Updates header parsing logic to use
isTokenfunction for more comprehensive character validation - Adds comprehensive test coverage for valid HTTP header token characters including numeric and special characters
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| nbhttp/parser.go | Updates header parsing validation from isAlpha to isToken function |
| nbhttp/parser_test.go | Adds new test function to validate numeric and special characters in header names |
Comments suppressed due to low confidence (1)
nbhttp/parser_test.go:47
- The test only covers numeric characters in header names. Consider adding separate test cases for individual character categories (numbers, special characters) and edge cases like empty headers or invalid characters to improve test coverage and clarity.
data := []byte("POST / HTTP/1.1\r\n123456789: value \r\n\r\n")
Comment on lines
+47
to
+56
| data := []byte("POST / HTTP/1.1\r\n123456789: value \r\n\r\n") | ||
| err := testParser(t, false, data) | ||
| if err != nil { | ||
| t.Fatalf("test failed: %v", err) | ||
| } | ||
| data = []byte("POST / HTTP/1.1\r\n!#$%&'*+-.^_`|~: value \r\n\r\n") | ||
| err = testParser(t, false, data) | ||
| if err != nil { | ||
| t.Fatalf("test failed: %v", err) | ||
| } |
There was a problem hiding this comment.
[nitpick] The special characters test uses all valid token characters in a single header name. Consider testing these characters individually or in smaller groups to make test failures easier to diagnose and ensure each character is properly validated.
Suggested change
| data := []byte("POST / HTTP/1.1\r\n123456789: value \r\n\r\n") | |
| err := testParser(t, false, data) | |
| if err != nil { | |
| t.Fatalf("test failed: %v", err) | |
| } | |
| data = []byte("POST / HTTP/1.1\r\n!#$%&'*+-.^_`|~: value \r\n\r\n") | |
| err = testParser(t, false, data) | |
| if err != nil { | |
| t.Fatalf("test failed: %v", err) | |
| } | |
| // Test numeric characters | |
| data := []byte("POST / HTTP/1.1\r\n123456789: value \r\n\r\n") | |
| err := testParser(t, false, data) | |
| if err != nil { | |
| t.Fatalf("test failed: %v", err) | |
| } | |
| // Test special characters in smaller groups | |
| data = []byte("POST / HTTP/1.1\r\n!#$%&'*: value \r\n\r\n") | |
| err = testParser(t, false, data) | |
| if err != nil { | |
| t.Fatalf("test failed: %v", err) | |
| } | |
| data = []byte("POST / HTTP/1.1\r\n+-.^_: value \r\n\r\n") | |
| err = testParser(t, false, data) | |
| if err != nil { | |
| t.Fatalf("test failed: %v", err) | |
| } | |
| data = []byte("POST / HTTP/1.1\r\n`|~: value \r\n\r\n") | |
| err = testParser(t, false, data) | |
| if err != nil { | |
| t.Fatalf("test failed: %v", err) | |
| } |
Owner
|
非常感谢! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#481