Skip to content

Parse headers in HTTPUtil without commons-httpclient 3 - #542

Open
NicoPiel wants to merge 2 commits into
OpenIntegrationEngine:mainfrom
NicoPiel:modernise/httpclient3-removal
Open

NicoPiel wants to merge 2 commits into
OpenIntegrationEngine:mainfrom
NicoPiel:modernise/httpclient3-removal

Conversation

@NicoPiel

@NicoPiel NicoPiel commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

What

HTTPUtil.parseHeaders no longer uses commons-httpclient 3's HttpParser. It now delegates to httpcore 4.4's AbstractMessageParser.parseHeaders (httpcore is already on the server classpath), instead of using a custom parser.

Why

This is one step toward dropping commons-httpclient 3.0.1. Following review, it uses an existing, maintained parser rather than hand-written parsing code.

Behaviour changes against httpclient 3

HTTPUtil is user-script API, so these are worth a line in the release notes. Well-formed headers (Name: value lines separated by CRLF or LF, ending at a blank line or EOF) parse the same as before. These corner cases change:

Input httpclient 3 httpcore 4.4
Malformed header (no colon, status line, empty name) org.apache.commons.httpclient.ProtocolException (an IOException), Unable to parse header: <line> org.apache.http.ProtocolException (not an IOException), Invalid header: <line>
Empty header name (:v) accepted as name "" rejected
Folded line before the first header ( orphan) silently ignored rejected
Whitespace-only line (\t) ends the header block treated as a folded continuation, so parsing continues
Line that is only a bare CR (\r\r\n) ends the header block rejected (Invalid header: [0x10])
Folded continuation of an empty value (X: then b) " b" "b"
Trailing whitespace on a folded line (a, b , \tc) "a b c" "a b c" (only leading whitespace of each fold is dropped)
Control characters around a value (\u000ba\u0001) trimmed (String.trim) kept; only spaces and tabs are trimmed
Non-ASCII on a JVM whose default charset isn't UTF-8 (e.g. Müller under ISO-8859-1) encoded with the platform charset, so mangled (M?ller) encoded as UTF-8, so the value round-trips exactly on every platform

Reviewer notes

  • The first commit adds 19 characterization tests that pass against the old httpclient 3 implementation. The second commit switches to httpcore and updates them to pin its behaviour; each changed assertion is one of the rows above. 21 tests now, and testNonAsciiValue no longer depends on the platform charset.
  • The string is now encoded as UTF-8 rather than with the platform charset. httpcore's decoder rejects malformed input, so keeping the platform charset would have made non-ASCII values throw MalformedInputException on, for example, Java 17 on Windows.
  • No header count or line length limit is applied (-1), matching the old unlimited behaviour.
  • This does not remove the jar yet. That needs the HttpStatus change and the WebDAV rewrite merged too.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Test Results

  146 files  + 1    146 suites  +1   4m 10s ⏱️ +21s
  997 tests +21    997 ✅ +21  0 💤 ±0  0 ❌ ±0 
1 279 runs  +21  1 273 ✅ +21  6 💤 ±0  0 ❌ ±0 

Results for commit bad2d5f. ± Comparison against base commit 4878ed1.

♻️ This comment has been updated with latest results.

gibson9583
gibson9583 previously approved these changes Oct 30, 2026

@gibson9583 gibson9583 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks safe to merge.

@mgaffigan mgaffigan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should not be implementing an HTTP parser. This is how you get more request smuggling vulnerabilities.

Change to some existing parser we're already using. That may break some corner case where a client is sending nonstandard requests - but such things are not reasonably avoidable.

@NicoPiel

Copy link
Copy Markdown
Contributor Author

Agreed. parseHeaders now delegates to httpcore 4.4's AbstractMessageParser.parseHeaders, so there's no custom parsing code left. I updated the characterization tests to pin httpcore's behaviour and listed every corner case that changes against httpclient 3 in the PR description. They're all malformed or unusual inputs: an empty name, an orphan folded line, whitespace-only and bare-CR lines, fold whitespace, control-character trimming, and the exception type and message.

mgaffigan
mgaffigan previously approved these changes Oct 30, 2026

@mgaffigan mgaffigan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks better - can we deprecate this method? Seems like it should not be used - doesn't handle repeated headers - and how is someone getting a set of http headers as a string?

@tonygermano tonygermano left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not approved yet. Switching to httpcore is the right call, and the characterization tests make the behaviour changes easy to follow. Three asks before merge:

  1. Encode the string as UTF-8. The new decoder rejects malformed input instead of replacing it, so on a JVM whose default charset isn't UTF-8 (e.g. Java 17 on Windows), any non-ASCII header value now throws MalformedInputException. httpclient 3 returned a mangled value instead. With UTF-8 on both ends, the round trip is exact on every platform:
    buffer.bind(new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8)));
    testNonAsciiValue can then drop its assumeTrue, and the description's "platform charset behaviour unchanged" note should be updated.
  2. Squash the inline-parser commit into the httpcore commit. We rebase-merge, so otherwise the hand-written parser lands on main with a message that no longer matches what ships. Since HTTPUtil is user-script API, please give the remaining commit a body listing the behaviour changes, for example:
    Parse HTTPUtil headers with httpcore instead of httpclient 3
    
    Delegate HTTPUtil.parseHeaders to httpcore 4.4's
    AbstractMessageParser.parseHeaders, which is already on the server
    classpath, so the userutil API no longer needs the EOL
    commons-httpclient 3 jar.
    
    Well-formed headers parse the same. Corner cases that change:
    - A malformed header throws org.apache.http.ProtocolException
      ("Invalid header: ..."), which is not an IOException, instead of
      httpclient 3's ProtocolException ("Unable to parse header").
    - Empty header names, a folded line before the first header and
      bare-CR lines are rejected.
    - A whitespace-only line is treated as a continuation instead of
      ending the header block.
    - Folding onto an empty value no longer adds a leading space.
    - Only spaces and tabs are trimmed from values, not other control
      characters.
    - The string is encoded as UTF-8 instead of the platform charset.
    
    Keeping the characterization tests as their own first commit is good.
  3. In HTTPUtilTest, use JUnit 4.13's assertThrows in assertParseFailure instead of try/fail/catch. If the wrong exception is thrown, it reports the expected and actual types and keeps the original as the cause:
    private static void assertParseFailure(String input, String badLine) {
        ProtocolException e = assertThrows(ProtocolException.class, () -> HTTPUtil.parseHeaders(input));
        assertEquals("Invalid header: " + badLine, e.getMessage());
    }
    This replaces the assertTrue and fail imports with import static org.junit.Assert.assertThrows;.

Maintainer follow-up

Mitch's question about deprecating parseHeaders deserves its own issue.

Claude checked:

  • The PR merges cleanly onto current main, and HTTPUtilTest passes (21 tests), both as-is and with the assertThrows helper from ask 3.
  • With -Dfile.encoding=ISO-8859-1 and the header X-Name: Müller, httpclient 3.0.1 returns M?ller and httpcore 4.4.13 throws MalformedInputException. With UTF-8 as the default, both return Müller.

🤖 Drafted with Claude Code

Pin the behaviour of the current commons-httpclient 3 based
implementation (folding, trimming, duplicates, termination, errors)
before replacing it.

Signed-off-by: Nico Piel <nicopiel@mailbox.org>
Delegate HTTPUtil.parseHeaders to httpcore 4.4's
AbstractMessageParser.parseHeaders, which is already on the server
classpath, so the userutil API no longer needs the EOL
commons-httpclient 3 jar.

Well-formed headers parse the same. Corner cases that change:
- A malformed header throws org.apache.http.ProtocolException
  ("Invalid header: ..."), which is not an IOException, instead of
  httpclient 3's ProtocolException ("Unable to parse header").
- Empty header names, a folded line before the first header and
  bare-CR lines are rejected.
- A whitespace-only line is treated as a continuation instead of
  ending the header block.
- Folding onto an empty value no longer adds a leading space.
- Trailing whitespace on a folded line is kept.
- Only spaces and tabs are trimmed from values, not other control
  characters.
- The string is encoded as UTF-8 instead of the platform charset.

Signed-off-by: Nico Piel <nicopiel@mailbox.org>
@NicoPiel

Copy link
Copy Markdown
Contributor Author

Thanks, all three are done:

  1. parseHeaders now encodes the string as UTF-8, and testNonAsciiValue no longer has the assumeTrue. I checked that Müller € round-trips under -Dfile.encoding=ISO-8859-1. The description lists the charset change.
  2. Squashed into one httpcore commit, with your suggested body plus a line for trailing whitespace on folded lines. The characterization tests stay as the first commit, and the branch is rebased onto current main.
  3. assertParseFailure now uses assertThrows.

The parseHeaders deprecation question can go in its own issue, as you suggested.

@tonygermano tonygermano left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approving. Thanks for working through all three asks. The UTF-8 encoding, the squashed commit with its behaviour list and assertThrows all look good, and the description now matches what ships.

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.

4 participants