Skip to content
Permalink
Browse files

Convert Hpack to Kotlin (#4987)

* Rename Hpack.java to .kt

* Convert Hpack to Kotlin
  • Loading branch information...
swankjesse committed Apr 28, 2019
1 parent fc7ac8a commit 7f800e6106917f13137283ab54cf62f73b76fdbd
@@ -53,7 +53,7 @@
}

private final Buffer bytesIn = new Buffer();
private final Hpack.Reader hpackReader = new Hpack.Reader(4096, bytesIn);
private final Hpack.Reader hpackReader = new Hpack.Reader(bytesIn, 4096);

private final Story story;

@@ -17,6 +17,7 @@ package okhttp3.tls

import okhttp3.internal.Util.verifyAsIpAddress
import okio.ByteString
import okio.ByteString.Companion.toByteString
import org.bouncycastle.asn1.ASN1Encodable
import org.bouncycastle.asn1.DERSequence
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo
@@ -123,7 +124,7 @@ class HeldCertificate(private val keyPair: KeyPair, private val certificate: X50
fun certificatePem(): String {
return buildString {
append("-----BEGIN CERTIFICATE-----\n")
encodeBase64Lines(ByteString.of(*certificate.encoded))
encodeBase64Lines(certificate.encoded.toByteString())
append("-----END CERTIFICATE-----\n")
}
}
@@ -137,7 +138,7 @@ class HeldCertificate(private val keyPair: KeyPair, private val certificate: X50
fun privateKeyPkcs8Pem(): String {
return buildString {
append("-----BEGIN PRIVATE KEY-----\n")
encodeBase64Lines(ByteString.of(*keyPair.private.encoded))
encodeBase64Lines(keyPair.private.encoded.toByteString())
append("-----END PRIVATE KEY-----\n")
}
}
@@ -159,7 +160,7 @@ class HeldCertificate(private val keyPair: KeyPair, private val certificate: X50

private fun pkcs1Bytes(): ByteString {
val privateKeyInfo = PrivateKeyInfo.getInstance(keyPair.private.encoded)
return ByteString.of(*privateKeyInfo.parsePrivateKey().toASN1Primitive().encoded)
return privateKeyInfo.parsePrivateKey().toASN1Primitive().encoded.toByteString()
}

private fun StringBuilder.encodeBase64Lines(data: ByteString) {
@@ -29,9 +29,9 @@ import okhttp3.internal.platform.Platform
import okio.Buffer
import okio.BufferedSink
import okio.BufferedSource
import okio.ByteString
import okio.ByteString.Companion.decodeBase64
import okio.ByteString.Companion.encodeUtf8
import okio.ByteString.Companion.toByteString
import okio.ForwardingSink
import okio.ForwardingSource
import okio.Sink
@@ -636,7 +636,7 @@ class Cache internal constructor(
sink.writeDecimalLong(certificates.size.toLong()).writeByte('\n'.toInt())
for (i in 0 until certificates.size) {
val bytes = certificates[i].encoded
val line = ByteString.of(*bytes).base64()
val line = bytes.toByteString().base64()
sink.writeUtf8(line).writeByte('\n'.toInt())
}
} catch (e: CertificateEncodingException) {
@@ -18,6 +18,7 @@ package okhttp3
import okhttp3.internal.tls.CertificateChainCleaner
import okio.ByteString
import okio.ByteString.Companion.decodeBase64
import okio.ByteString.Companion.toByteString
import java.security.cert.Certificate
import java.security.cert.X509Certificate
import javax.net.ssl.SSLPeerUnverifiedException
@@ -284,10 +285,10 @@ data class CertificatePinner internal constructor(
}

internal fun sha1(x509Certificate: X509Certificate): ByteString =
ByteString.of(*x509Certificate.publicKey.encoded).sha1()
x509Certificate.publicKey.encoded.toByteString().sha1()

internal fun sha256(x509Certificate: X509Certificate): ByteString =
ByteString.of(*x509Certificate.publicKey.encoded).sha256()
x509Certificate.publicKey.encoded.toByteString().sha256()

internal fun newPin(pattern: String, pin: String): Pin {
val canonicalHostname = when {
@@ -15,7 +15,6 @@
*/
package okhttp3.internal.http2

import okhttp3.internal.Util
import okio.ByteString
import okio.ByteString.Companion.encodeUtf8

@@ -26,16 +25,14 @@ data class Header(
/** Value in UTF-8 encoding. */
@JvmField val value: ByteString
) {
@JvmField internal val hpackSize = 32 + name.size + value.size
@JvmField val hpackSize = 32 + name.size + value.size

// TODO: search for toLowerCase and consider moving logic here.
constructor(name: String, value: String) : this(name.encodeUtf8(), value.encodeUtf8())

constructor(name: ByteString, value: String) : this(name, value.encodeUtf8())

override fun toString(): String {
return Util.format("%s: %s", name.utf8(), value.utf8())
}
override fun toString(): String = "${name.utf8()}: ${value.utf8()}"

companion object {
// Special header names defined in HTTP/2 spec.

0 comments on commit 7f800e6

Please sign in to comment.
You can’t perform that action at this time.