Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix parse broken when header value is null #575

Merged
merged 6 commits into from
Jul 15, 2024

Conversation

laglangyue
Copy link
Contributor

@laglangyue laglangyue commented Jul 9, 2024

motivation

fix #574

test

by ci

@@ -876,6 +876,11 @@ class HttpHeaderSpec extends AnyFreeSpec with Matchers {
parse("User-Agent", "(" * 10000).errors.head shouldEqual
ErrorInfo("Illegal HTTP header 'User-Agent': Illegal header value", "Header comment nested too deeply")
}

"should not broken when header-value is null" in {
parse("Content-Disposition", null).errors.head.isInstanceOf[ErrorInfo] shouldBe true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use:

parse("Content-Disposition", null).errors.head shouldBe an[ErrorInfo]

Your version will return an error like false os not true if it fails. My version will return a much more informative message.

}).info
val info = error match {
case e: ParseError => parser.parseError(e).info
case e if e.getMessage == null => ErrorInfo()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code should be reverted and the change should be in HeaderParser.scala instead. I have the change in #576. I created 576 to test if that approach works but I'd prefer to let you complete this PR. View 576 as a set of suggested changes for this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your suggestion, e.toString() is good for NPE in java8/java11


"should not broken when header-value is null" in {
val errors = parse("Content-Disposition", null).errors
errors.size shouldBe 1
Copy link
Contributor

@pjfanning pjfanning Jul 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errors should have size 1 is a better way to do this. When this fails, scalatest will say something like
List(a1, a2) did not have size 1 - where a1 and a2 are actually the toString results of those list elements.

With errors.size shouldBe 1 fails, you will get an error like 2 was not 1. I much prefer getting the detailed message from my assertion. I would strongly recommend that you read up on scalatest. The assertions are carefully designed to give us lots of context when assertions fail. But if you go off and use the simplest assertions then you get very little info when things fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

@pjfanning pjfanning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

case IllegalUriException(info) => info
case NonFatal(e) => ErrorInfo.fromCompoundString(e.getMessage)
case IllegalUriException(info) => info
case NonFatal(e) if e.getMessage == null => ErrorInfo.fromCompoundString(e.toString)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use eq

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.getMessage eq null ? I don't think it is good. eq is used to replace the equals method, and when compared to null, it is usually to use ==

@pjfanning pjfanning merged commit 41e4f46 into apache:main Jul 15, 2024
10 checks passed
@pjfanning
Copy link
Contributor

Thanks @laglangyue - could you create a new PR that cherry picks this commit to the 1.0.x branch?

@pjfanning pjfanning added this to the 1.1.0-M2 milestone Jul 15, 2024
pjfanning pushed a commit to pjfanning/incubator-pekko-http that referenced this pull request Jul 19, 2024
* fix parse broken when header value is null

* fix style

* revert not need change

* improve match case

* revert HttpHeader and update  HeaderParser.failure

* update assert

---------

Co-authored-by: tangjiafu <[email protected]>
pjfanning added a commit that referenced this pull request Jul 19, 2024
* fix parse broken when header value is null

* fix style

* revert not need change

* improve match case

* revert HttpHeader and update  HeaderParser.failure

* update assert

---------

Co-authored-by: Laglangyue <[email protected]>
Co-authored-by: tangjiafu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Uncaught NPE when parsing a header value
3 participants