Okhttp: Public API for toHumanReadableAscii(String) ?

Created on 14 Feb 2017  路  3Comments  路  Source: square/okhttp

At least one Android device, the _Social Mobile X325 (Life Wireless)_, ships to users with an invalid character (Unicode En-Dash - Hexidecimal code 0x2013) in its default device user-agent. When this is passed as a user-agent via okhttp3, a crash occurs.

This specific device's user-agent is:
Dalvik/1.6.0 (Linux; U; Android 4.4.2; X325鈥揕ocked to Life Wireless Build/KOT49H)

I haven't discovered any other devices cause this crash, and the X325 has very few users (I'm seeing ~60 users in a population of 1.1 million). Given the rarity of this device, I'm not sure whether it's worth adding a function to sanitize the user-agent for invalid characters in okhttp3, but wanted to report the crash anyway.

java.lang.IllegalArgumentException: Unexpected char 0x2013 at 56 in User-Agent value: Dalvik/1.6.0 (Linux; U; Android 4.4.2; X325鈥揕ocked to Life Wireless Build/KOT49H)
    at okhttp3.Headers$Builder.d(Headers.java:320)
    at okhttp3.Headers$Builder.c(Headers.java:300)
    at okhttp3.Request$Builder.a(Request.java:163)
    at my.appname.webservice.RestClient$2.a(RestClient.java:74)
    at okhttp3.internal.http.RealInterceptorChain.a(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.a(RealInterceptorChain.java:67)
    at okhttp3.logging.HttpLoggingInterceptor.a(HttpLoggingInterceptor.java:145)
    at okhttp3.internal.http.RealInterceptorChain.a(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.a(RealInterceptorChain.java:67)
    at okhttp3.RealCall.f(RealCall.java:170)
    at okhttp3.RealCall.a(RealCall.java:33)
    at okhttp3.RealCall$AsyncCall.b(RealCall.java:120)
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:848)
enhancement

Most helpful comment

Here's what I do to guard against odd device names, for example, in my headers.
Credit goes to Jesse; I'm certain he posted this originally somewhere.

/** Returns {@code s} with control characters and non-ASCII characters replaced with '?'. */
static String toHumanReadableAscii(String s) {
  for (int i = 0, length = s.length(), c; i < length; i += Character.charCount(c)) {
    c = s.codePointAt(i);
    if (c > '\u001f' && c < '\u007f') continue;

    Buffer buffer = new Buffer();
    buffer.writeUtf8(s, 0, i);
    for (int j = i; j < length; j += Character.charCount(c)) {
      c = s.codePointAt(j);
      buffer.writeUtf8CodePoint(c > '\u001f' && c < '\u007f' ? c : '?');
    }
    return buffer.readUtf8();
  }
  return s;
}

All 3 comments

This is related to #2896 , and the guidance was to strip non-ASCII characters prior to using them - however, I wanted to open this issue since this occurs when sending the device's default user-agent (which, unlike a custom user-agent, shouldn't need pre-processing).

If okhttp3 won't strip non-ASCII characters from the User-Agent, then I can certainly implement it on my end - but since this issue is caused due to a problem with the X325 phone's default user agent, others using this library may also be experiencing the same crash for X325 users.

Here's what I do to guard against odd device names, for example, in my headers.
Credit goes to Jesse; I'm certain he posted this originally somewhere.

/** Returns {@code s} with control characters and non-ASCII characters replaced with '?'. */
static String toHumanReadableAscii(String s) {
  for (int i = 0, length = s.length(), c; i < length; i += Character.charCount(c)) {
    c = s.codePointAt(i);
    if (c > '\u001f' && c < '\u007f') continue;

    Buffer buffer = new Buffer();
    buffer.writeUtf8(s, 0, i);
    for (int j = i; j < length; j += Character.charCount(c)) {
      c = s.codePointAt(j);
      buffer.writeUtf8CodePoint(c > '\u001f' && c < '\u007f' ? c : '?');
    }
    return buffer.readUtf8();
  }
  return s;
}

Won't fix.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

HyakYegoryaln picture HyakYegoryaln  路  3Comments

theotherp picture theotherp  路  3Comments

sargunv picture sargunv  路  3Comments

vanshg picture vanshg  路  3Comments

SandroMachado picture SandroMachado  路  3Comments