Skip to content

Commit

Permalink
Make sure the default user agent is ASCII.
Browse files Browse the repository at this point in the history
See #891
  • Loading branch information
swankjesse committed Aug 3, 2015
1 parent bf69c55 commit 0e7524d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.squareup.okhttp.internal.SingleInetAddressNetwork;
import com.squareup.okhttp.internal.SslContextBuilder;
import com.squareup.okhttp.internal.Util;
import com.squareup.okhttp.internal.Version;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import com.squareup.okhttp.mockwebserver.RecordedRequest;
Expand Down Expand Up @@ -3144,13 +3145,24 @@ private void zeroLengthPayload(String method)
assertEquals("foo", request.getHeader("User-Agent"));
}

@Test public void userAgentDefaultsToJavaVersion() throws Exception {
/** https://github.com/square/okhttp/issues/891 */
@Test public void userAgentSystemPropertyIsNotAscii() throws Exception {
server.enqueue(new MockResponse().setBody("abc"));

System.setProperty("http.agent", "a\nb\ud83c\udf69c\ud83c\udf68d\u007fe");
assertContent("abc", client.open(server.getUrl("/")));

RecordedRequest request = server.takeRequest();
assertTrue(request.getHeader("User-Agent").startsWith("Java"));
assertEquals("a?b?c?d?e", request.getHeader("User-Agent"));
}

@Test public void userAgentDefaultsToOkHttpVersion() throws Exception {
server.enqueue(new MockResponse().setBody("abc"));

assertContent("abc", client.open(server.getUrl("/")));

RecordedRequest request = server.takeRequest();
assertEquals(Version.userAgent(), request.getHeader("User-Agent"));
}

@Test public void interceptorsNotInvoked() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.squareup.okhttp.Route;
import com.squareup.okhttp.internal.Internal;
import com.squareup.okhttp.internal.Platform;
import com.squareup.okhttp.internal.Util;
import com.squareup.okhttp.internal.Version;
import com.squareup.okhttp.internal.http.HttpDate;
import com.squareup.okhttp.internal.http.HttpEngine;
import com.squareup.okhttp.internal.http.HttpMethod;
Expand Down Expand Up @@ -368,7 +370,7 @@ private HttpEngine newHttpEngine(String method, Connection connection,

private String defaultUserAgent() {
String agent = System.getProperty("http.agent");
return agent != null ? agent : ("Java" + System.getProperty("java.version"));
return agent != null ? Util.toHumanReadableAscii(agent) : Version.userAgent();
}

/**
Expand Down
17 changes: 17 additions & 0 deletions okhttp/src/main/java/com/squareup/okhttp/internal/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,21 @@ public static String hostHeader(HttpUrl url) {
? url.host() + ":" + url.port()
: url.host();
}

/** Returns {@code s} with control characters and non-ASCII characters replaced with '?'. */
public 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;
}
}

0 comments on commit 0e7524d

Please sign in to comment.