mirror of
https://github.com/square/okhttp.git
synced 2025-08-07 12:42:57 +03:00
Okio upgrade post kotlin (#4814)
Okio 2.2.2 upgrade and adopt kotlin friendly API
This commit is contained in:
@@ -15,7 +15,7 @@ buildscript {
|
|||||||
'junit': '4.12',
|
'junit': '4.12',
|
||||||
'kotlin': '1.3.20',
|
'kotlin': '1.3.20',
|
||||||
'moshi': '1.8.0',
|
'moshi': '1.8.0',
|
||||||
'okio': '1.17.2',
|
'okio': '2.2.2',
|
||||||
]
|
]
|
||||||
|
|
||||||
ext.deps = [
|
ext.deps = [
|
||||||
|
@@ -17,7 +17,7 @@ package okhttp3.dnsoverhttps
|
|||||||
|
|
||||||
import okio.Buffer
|
import okio.Buffer
|
||||||
import okio.ByteString
|
import okio.ByteString
|
||||||
import okio.Utf8
|
import okio.utf8Size
|
||||||
import java.io.EOFException
|
import java.io.EOFException
|
||||||
import java.net.InetAddress
|
import java.net.InetAddress
|
||||||
import java.net.UnknownHostException
|
import java.net.UnknownHostException
|
||||||
@@ -47,7 +47,7 @@ object DnsRecordCodec {
|
|||||||
val nameBuf = Buffer()
|
val nameBuf = Buffer()
|
||||||
val labels = host.split('.').dropLastWhile { it.isEmpty() }.toTypedArray()
|
val labels = host.split('.').dropLastWhile { it.isEmpty() }.toTypedArray()
|
||||||
for (label in labels) {
|
for (label in labels) {
|
||||||
val utf8ByteCount = Utf8.size(label)
|
val utf8ByteCount = label.utf8Size()
|
||||||
if (utf8ByteCount != label.length.toLong()) {
|
if (utf8ByteCount != label.length.toLong()) {
|
||||||
throw IllegalArgumentException("non-ascii hostname: $host")
|
throw IllegalArgumentException("non-ascii hostname: $host")
|
||||||
}
|
}
|
||||||
@@ -56,7 +56,7 @@ object DnsRecordCodec {
|
|||||||
}
|
}
|
||||||
nameBuf.writeByte(0) // end
|
nameBuf.writeByte(0) // end
|
||||||
|
|
||||||
nameBuf.copyTo(this, 0, nameBuf.size())
|
nameBuf.copyTo(this, 0, nameBuf.size)
|
||||||
writeShort(type)
|
writeShort(type)
|
||||||
writeShort(1) // CLASS_IN
|
writeShort(1) // CLASS_IN
|
||||||
}.readByteString()
|
}.readByteString()
|
||||||
|
@@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package okhttp3.dnsoverhttps;
|
package okhttp3.dnsoverhttps;
|
||||||
|
|
||||||
|
import java.io.EOFException;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
@@ -140,7 +141,7 @@ public class DnsOverHttpsTest {
|
|||||||
fail();
|
fail();
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
assertThat(ioe).hasMessage("google.com");
|
assertThat(ioe).hasMessage("google.com");
|
||||||
assertThat(ioe.getCause()).isInstanceOf(RuntimeException.class);
|
assertThat(ioe.getCause()).isInstanceOf(EOFException.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -26,13 +26,15 @@ import okhttp3.internal.http.HttpMethod
|
|||||||
import okhttp3.internal.http.StatusLine
|
import okhttp3.internal.http.StatusLine
|
||||||
import okhttp3.internal.io.FileSystem
|
import okhttp3.internal.io.FileSystem
|
||||||
import okhttp3.internal.platform.Platform
|
import okhttp3.internal.platform.Platform
|
||||||
|
import okio.buffer
|
||||||
import okio.Buffer
|
import okio.Buffer
|
||||||
import okio.BufferedSink
|
import okio.BufferedSink
|
||||||
import okio.BufferedSource
|
import okio.BufferedSource
|
||||||
import okio.ByteString
|
import okio.ByteString
|
||||||
|
import okio.ByteString.Companion.decodeBase64
|
||||||
|
import okio.ByteString.Companion.encodeUtf8
|
||||||
import okio.ForwardingSink
|
import okio.ForwardingSink
|
||||||
import okio.ForwardingSource
|
import okio.ForwardingSource
|
||||||
import okio.Okio
|
|
||||||
import okio.Sink
|
import okio.Sink
|
||||||
import okio.Source
|
import okio.Source
|
||||||
import java.io.Closeable
|
import java.io.Closeable
|
||||||
@@ -328,7 +330,7 @@ class Cache internal constructor(
|
|||||||
while (delegate.hasNext()) {
|
while (delegate.hasNext()) {
|
||||||
try {
|
try {
|
||||||
delegate.next().use { snapshot ->
|
delegate.next().use { snapshot ->
|
||||||
val metadata = Okio.buffer(snapshot.getSource(ENTRY_METADATA))
|
val metadata = snapshot.getSource(ENTRY_METADATA).buffer()
|
||||||
nextUrl = metadata.readUtf8LineStrict()
|
nextUrl = metadata.readUtf8LineStrict()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -506,7 +508,7 @@ class Cache internal constructor(
|
|||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
internal constructor(rawSource: Source) {
|
internal constructor(rawSource: Source) {
|
||||||
try {
|
try {
|
||||||
val source = Okio.buffer(rawSource)
|
val source = rawSource.buffer()
|
||||||
url = source.readUtf8LineStrict()
|
url = source.readUtf8LineStrict()
|
||||||
requestMethod = source.readUtf8LineStrict()
|
requestMethod = source.readUtf8LineStrict()
|
||||||
val varyHeadersBuilder = Headers.Builder()
|
val varyHeadersBuilder = Headers.Builder()
|
||||||
@@ -571,7 +573,7 @@ class Cache internal constructor(
|
|||||||
|
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
fun writeTo(editor: DiskLruCache.Editor) {
|
fun writeTo(editor: DiskLruCache.Editor) {
|
||||||
val sink = Okio.buffer(editor.newSink(ENTRY_METADATA))
|
val sink = editor.newSink(ENTRY_METADATA).buffer()
|
||||||
sink.writeUtf8(url).writeByte('\n'.toInt())
|
sink.writeUtf8(url).writeByte('\n'.toInt())
|
||||||
sink.writeUtf8(requestMethod).writeByte('\n'.toInt())
|
sink.writeUtf8(requestMethod).writeByte('\n'.toInt())
|
||||||
sink.writeDecimalLong(varyHeaders.size().toLong()).writeByte('\n'.toInt())
|
sink.writeDecimalLong(varyHeaders.size().toLong()).writeByte('\n'.toInt())
|
||||||
@@ -620,7 +622,7 @@ class Cache internal constructor(
|
|||||||
for (i in 0 until length) {
|
for (i in 0 until length) {
|
||||||
val line = source.readUtf8LineStrict()
|
val line = source.readUtf8LineStrict()
|
||||||
val bytes = Buffer()
|
val bytes = Buffer()
|
||||||
bytes.write(ByteString.decodeBase64(line)!!)
|
bytes.write(line.decodeBase64()!!)
|
||||||
result.add(certificateFactory.generateCertificate(bytes.inputStream()))
|
result.add(certificateFactory.generateCertificate(bytes.inputStream()))
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
@@ -689,13 +691,13 @@ class Cache internal constructor(
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
val source = snapshot.getSource(ENTRY_BODY)
|
val source = snapshot.getSource(ENTRY_BODY)
|
||||||
bodySource = Okio.buffer(object : ForwardingSource(source) {
|
bodySource = object : ForwardingSource(source) {
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
override fun close() {
|
override fun close() {
|
||||||
snapshot.close()
|
snapshot.close()
|
||||||
super.close()
|
super.close()
|
||||||
}
|
}
|
||||||
})
|
}.buffer()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun contentType(): MediaType? {
|
override fun contentType(): MediaType? {
|
||||||
@@ -720,7 +722,7 @@ class Cache internal constructor(
|
|||||||
private const val ENTRY_COUNT = 2
|
private const val ENTRY_COUNT = 2
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun key(url: HttpUrl): String = ByteString.encodeUtf8(url.toString()).md5().hex()
|
fun key(url: HttpUrl): String = url.toString().encodeUtf8().md5().hex()
|
||||||
|
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
|
@@ -17,6 +17,7 @@ package okhttp3
|
|||||||
|
|
||||||
import okhttp3.internal.tls.CertificateChainCleaner
|
import okhttp3.internal.tls.CertificateChainCleaner
|
||||||
import okio.ByteString
|
import okio.ByteString
|
||||||
|
import okio.ByteString.Companion.decodeBase64
|
||||||
import java.security.cert.Certificate
|
import java.security.cert.Certificate
|
||||||
import java.security.cert.X509Certificate
|
import java.security.cert.X509Certificate
|
||||||
import java.util.Objects
|
import java.util.Objects
|
||||||
@@ -254,11 +255,11 @@ class CertificatePinner internal constructor(
|
|||||||
when {
|
when {
|
||||||
pin.startsWith("sha1/") -> {
|
pin.startsWith("sha1/") -> {
|
||||||
this.hashAlgorithm = "sha1/"
|
this.hashAlgorithm = "sha1/"
|
||||||
this.hash = ByteString.decodeBase64(pin.substring("sha1/".length))!!
|
this.hash = pin.substring("sha1/".length).decodeBase64()!!
|
||||||
}
|
}
|
||||||
pin.startsWith("sha256/") -> {
|
pin.startsWith("sha256/") -> {
|
||||||
this.hashAlgorithm = "sha256/"
|
this.hashAlgorithm = "sha256/"
|
||||||
this.hash = ByteString.decodeBase64(pin.substring("sha256/".length))!!
|
this.hash = pin.substring("sha256/".length).decodeBase64()!!
|
||||||
}
|
}
|
||||||
else -> throw IllegalArgumentException("pins must start with 'sha256/' or 'sha1/': $pin")
|
else -> throw IllegalArgumentException("pins must start with 'sha256/' or 'sha1/': $pin")
|
||||||
}
|
}
|
||||||
|
@@ -15,7 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package okhttp3
|
package okhttp3
|
||||||
|
|
||||||
import okio.ByteString
|
import okio.ByteString.Companion.encode
|
||||||
import java.nio.charset.Charset
|
import java.nio.charset.Charset
|
||||||
import java.nio.charset.StandardCharsets.ISO_8859_1
|
import java.nio.charset.StandardCharsets.ISO_8859_1
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ object Credentials {
|
|||||||
charset: Charset = ISO_8859_1
|
charset: Charset = ISO_8859_1
|
||||||
): String {
|
): String {
|
||||||
val usernameAndPassword = "$username:$password"
|
val usernameAndPassword = "$username:$password"
|
||||||
val encoded = ByteString.encodeString(usernameAndPassword, charset).base64()
|
val encoded = usernameAndPassword.encode(charset).base64()
|
||||||
return "Basic $encoded"
|
return "Basic $encoded"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -58,7 +58,7 @@ class FormBody internal constructor(
|
|||||||
*/
|
*/
|
||||||
private fun writeOrCountBytes(sink: BufferedSink?, countBytes: Boolean): Long {
|
private fun writeOrCountBytes(sink: BufferedSink?, countBytes: Boolean): Long {
|
||||||
var byteCount = 0L
|
var byteCount = 0L
|
||||||
val buffer: Buffer = if (countBytes) Buffer() else sink!!.buffer()
|
val buffer: Buffer = if (countBytes) Buffer() else sink!!.buffer
|
||||||
|
|
||||||
for (i in 0 until encodedNames.size) {
|
for (i in 0 until encodedNames.size) {
|
||||||
if (i > 0) buffer.writeByte('&'.toInt())
|
if (i > 0) buffer.writeByte('&'.toInt())
|
||||||
@@ -68,7 +68,7 @@ class FormBody internal constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (countBytes) {
|
if (countBytes) {
|
||||||
byteCount = buffer.size()
|
byteCount = buffer.size
|
||||||
buffer.clear()
|
buffer.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -32,6 +32,7 @@ import okhttp3.Headers;
|
|||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
import okhttp3.internal.platform.Platform;
|
||||||
import okio.Buffer;
|
import okio.Buffer;
|
||||||
import okio.ByteString;
|
import okio.ByteString;
|
||||||
|
|
||||||
@@ -171,13 +172,18 @@ public final class HttpHeaders {
|
|||||||
for (int h = 0; h < responseHeaders.size(); h++) {
|
for (int h = 0; h < responseHeaders.size(); h++) {
|
||||||
if (headerName.equalsIgnoreCase(responseHeaders.name(h))) {
|
if (headerName.equalsIgnoreCase(responseHeaders.name(h))) {
|
||||||
Buffer header = new Buffer().writeUtf8(responseHeaders.value(h));
|
Buffer header = new Buffer().writeUtf8(responseHeaders.value(h));
|
||||||
parseChallengeHeader(result, header);
|
try {
|
||||||
|
parseChallengeHeader(result, header);
|
||||||
|
} catch (EOFException e) {
|
||||||
|
Platform.get().log(Platform.WARN, "Unable to parse challenge", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void parseChallengeHeader(List<Challenge> result, Buffer header) {
|
private static void parseChallengeHeader(List<Challenge> result, Buffer header)
|
||||||
|
throws EOFException {
|
||||||
String peek = null;
|
String peek = null;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -237,15 +243,17 @@ public final class HttpHeaders {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Returns true if any commas were skipped. */
|
/** Returns true if any commas were skipped. */
|
||||||
private static boolean skipWhitespaceAndCommas(Buffer buffer) {
|
private static boolean skipWhitespaceAndCommas(Buffer buffer) throws EOFException {
|
||||||
boolean commaFound = false;
|
boolean commaFound = false;
|
||||||
while (!buffer.exhausted()) {
|
while (!buffer.exhausted()) {
|
||||||
byte b = buffer.getByte(0);
|
byte b = buffer.getByte(0);
|
||||||
if (b == ',') {
|
if (b == ',') {
|
||||||
buffer.readByte(); // Consume ','.
|
// Consume ','.
|
||||||
|
buffer.readByte();
|
||||||
commaFound = true;
|
commaFound = true;
|
||||||
} else if (b == ' ' || b == '\t') {
|
} else if (b == ' ' || b == '\t') {
|
||||||
buffer.readByte(); // Consume space or tab.
|
// Consume space or tab.
|
||||||
|
buffer.readByte();
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -253,7 +261,7 @@ public final class HttpHeaders {
|
|||||||
return commaFound;
|
return commaFound;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int skipAll(Buffer buffer, byte b) {
|
private static int skipAll(Buffer buffer, byte b) throws EOFException {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
while (!buffer.exhausted() && buffer.getByte(0) == b) {
|
while (!buffer.exhausted() && buffer.getByte(0) == b) {
|
||||||
count++;
|
count++;
|
||||||
@@ -267,7 +275,7 @@ public final class HttpHeaders {
|
|||||||
* each sequence. Returns the unescaped string, or null if the buffer isn't prefixed with a
|
* each sequence. Returns the unescaped string, or null if the buffer isn't prefixed with a
|
||||||
* double-quoted string.
|
* double-quoted string.
|
||||||
*/
|
*/
|
||||||
private static String readQuotedString(Buffer buffer) {
|
private static String readQuotedString(Buffer buffer) throws EOFException {
|
||||||
if (buffer.readByte() != '\"') throw new IllegalArgumentException();
|
if (buffer.readByte() != '\"') throw new IllegalArgumentException();
|
||||||
Buffer result = new Buffer();
|
Buffer result = new Buffer();
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -276,13 +284,15 @@ public final class HttpHeaders {
|
|||||||
|
|
||||||
if (buffer.getByte(i) == '"') {
|
if (buffer.getByte(i) == '"') {
|
||||||
result.write(buffer, i);
|
result.write(buffer, i);
|
||||||
buffer.readByte(); // Consume '"'.
|
// Consume '"'.
|
||||||
|
buffer.readByte();
|
||||||
return result.readUtf8();
|
return result.readUtf8();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buffer.size() == i + 1L) return null; // Dangling escape.
|
if (buffer.size() == i + 1L) return null; // Dangling escape.
|
||||||
result.write(buffer, i);
|
result.write(buffer, i);
|
||||||
buffer.readByte(); // Consume '\'.
|
// Consume '\'.
|
||||||
|
buffer.readByte();
|
||||||
result.write(buffer, 1L); // The escaped character.
|
result.write(buffer, 1L); // The escaped character.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -17,6 +17,7 @@ package okhttp3.internal.http2
|
|||||||
|
|
||||||
import okhttp3.internal.Util
|
import okhttp3.internal.Util
|
||||||
import okio.ByteString
|
import okio.ByteString
|
||||||
|
import okio.ByteString.Companion.encodeUtf8
|
||||||
|
|
||||||
/** HTTP header: the name is an ASCII string, but the value can be UTF-8. */
|
/** HTTP header: the name is an ASCII string, but the value can be UTF-8. */
|
||||||
class Header(
|
class Header(
|
||||||
@@ -25,12 +26,12 @@ class Header(
|
|||||||
/** Value in UTF-8 encoding. */
|
/** Value in UTF-8 encoding. */
|
||||||
@JvmField val value: ByteString
|
@JvmField val value: ByteString
|
||||||
) {
|
) {
|
||||||
@JvmField internal val hpackSize = 32 + name.size() + value.size()
|
@JvmField internal val hpackSize = 32 + name.size + value.size
|
||||||
|
|
||||||
// TODO: search for toLowerCase and consider moving logic here.
|
// TODO: search for toLowerCase and consider moving logic here.
|
||||||
constructor(name: String, value: String) : this(ByteString.encodeUtf8(name), ByteString.encodeUtf8(value))
|
constructor(name: String, value: String) : this(name.encodeUtf8(), value.encodeUtf8())
|
||||||
|
|
||||||
constructor(name: ByteString, value: String) : this(name, ByteString.encodeUtf8(value))
|
constructor(name: ByteString, value: String) : this(name, value.encodeUtf8())
|
||||||
|
|
||||||
override fun equals(other: Any?): Boolean {
|
override fun equals(other: Any?): Boolean {
|
||||||
return other is Header
|
return other is Header
|
||||||
@@ -51,7 +52,7 @@ class Header(
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
// Special header names defined in HTTP/2 spec.
|
// Special header names defined in HTTP/2 spec.
|
||||||
@JvmField val PSEUDO_PREFIX: ByteString = ByteString.encodeUtf8(":")
|
@JvmField val PSEUDO_PREFIX: ByteString = ":".encodeUtf8()
|
||||||
|
|
||||||
const val RESPONSE_STATUS_UTF8 = ":status"
|
const val RESPONSE_STATUS_UTF8 = ":status"
|
||||||
const val TARGET_METHOD_UTF8 = ":method"
|
const val TARGET_METHOD_UTF8 = ":method"
|
||||||
@@ -59,10 +60,10 @@ class Header(
|
|||||||
const val TARGET_SCHEME_UTF8 = ":scheme"
|
const val TARGET_SCHEME_UTF8 = ":scheme"
|
||||||
const val TARGET_AUTHORITY_UTF8 = ":authority"
|
const val TARGET_AUTHORITY_UTF8 = ":authority"
|
||||||
|
|
||||||
@JvmField val RESPONSE_STATUS: ByteString = ByteString.encodeUtf8(RESPONSE_STATUS_UTF8)
|
@JvmField val RESPONSE_STATUS: ByteString = RESPONSE_STATUS_UTF8.encodeUtf8()
|
||||||
@JvmField val TARGET_METHOD: ByteString = ByteString.encodeUtf8(TARGET_METHOD_UTF8)
|
@JvmField val TARGET_METHOD: ByteString = TARGET_METHOD_UTF8.encodeUtf8()
|
||||||
@JvmField val TARGET_PATH: ByteString = ByteString.encodeUtf8(TARGET_PATH_UTF8)
|
@JvmField val TARGET_PATH: ByteString = TARGET_PATH_UTF8.encodeUtf8()
|
||||||
@JvmField val TARGET_SCHEME: ByteString = ByteString.encodeUtf8(TARGET_SCHEME_UTF8)
|
@JvmField val TARGET_SCHEME: ByteString = TARGET_SCHEME_UTF8.encodeUtf8()
|
||||||
@JvmField val TARGET_AUTHORITY: ByteString = ByteString.encodeUtf8(TARGET_AUTHORITY_UTF8)
|
@JvmField val TARGET_AUTHORITY: ByteString = TARGET_AUTHORITY_UTF8.encodeUtf8()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user