microsoft / microsoft/libHttpClient

Issue and proposed fix. Non ascii characters in header causes a IllegalArgumentException

Open
#731 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
295
Forks
144
Avg merge
2d 19h
Merged PRs (30d)
2

Description

No pending exception expected: java.lang.IllegalArgumentException: Unexpected char

https://github.com/square/okhttp/issues/6347
https://github.com/square/okhttp/issues/4008
https://github.com/square/okhttp/issues/7275
https://github.com/square/okhttp/pull/4296

In the case of using setHttpHeader("carrier", ""中国电信"").
Note: 中国电信 is a Chinese telecommunications (cell phone provider carrier).

No pending exception expected: java.lang.IllegalArgumentException: Unexpected char 0x4e2d at 243 in User-Agent value

at void okhttp3.Headers$Companion.checkValue(java.lang.String, java.lang.String) (Headers.kt:450)
at void okhttp3.Headers$Companion.access$checkValue(okhttp3.Headers$Companion, java.lang.String, java.lang.String) (Headers.kt:362)
at okhttp3.Headers$Builder okhttp3.Headers$Builder.add(java.lang.String, java.lang.String) (Headers.kt:261)
at okhttp3.Request$Builder okhttp3.Request$Builder.addHeader(java.lang.String, java.lang.String) (Request.kt:210)
at <snipped>.setHttpHeader(java.lang.String, java.lang.String) (HttpClientRequest.java

The currently existing code snippet causes an issue.

 public void setHttpHeader(String name, String value) {
        this.requestBuilder = requestBuilder.addHeader(name, value);
    }

I believe the possible fix is to add a
this.headersBuilder = new Headers.Builder();
and then switch setHttpHeader to
this.headersBuilder = headersBuilder.addUnsafeNonAscii(name, value);
Then in the doRequestAsync add this.requestBuilder.headers(this.headersBuilder.build()); before the OK_Client call
or to 'build and set' in every set. Just depends if you want to do a lot of duplicate work or not.

We are running a modified version of libhttpclient, so our fix is applied there. I'll see if/when I have time to PR this change into libHttpClient proper, but if not, above should help anyone who comes across similar issue.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the setHttpHeader entry point and the doRequestAsync path described in the issue, then reproduce the request with the Chinese carrier value. Compare header construction with the referenced Headers.Builder behavior and verify that the request completes without an IllegalArgumentException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.