Skip to content

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

@adamgorMSFT

Description

@adamgorMSFT

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

square/okhttp#6347
square/okhttp#4008
square/okhttp#7275
square/okhttp#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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions