15
15
*/
16
16
package okhttp3
17
17
18
+ import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
18
19
import java.io.Closeable
19
20
import java.io.File
20
21
import java.io.Flushable
@@ -425,7 +426,7 @@ class Cache internal constructor(
425
426
}
426
427
427
428
private class Entry {
428
- private val url: String
429
+ private val url: HttpUrl
429
430
private val varyHeaders: Headers
430
431
private val requestMethod: String
431
432
private val protocol: Protocol
@@ -436,7 +437,7 @@ class Cache internal constructor(
436
437
private val sentRequestMillis: Long
437
438
private val receivedResponseMillis: Long
438
439
439
- private val isHttps: Boolean get() = url.startsWith( " https:// " )
440
+ private val isHttps: Boolean get() = url.scheme == " https"
440
441
441
442
/* *
442
443
* Reads an entry from an input stream. A typical entry looks like this:
@@ -490,9 +491,14 @@ class Cache internal constructor(
490
491
* optional. If present, it contains the TLS version.
491
492
*/
492
493
@Throws(IOException ::class ) constructor (rawSource: Source ) {
493
- try {
494
+ rawSource.use {
494
495
val source = rawSource.buffer()
495
- url = source.readUtf8LineStrict()
496
+ val urlLine = source.readUtf8LineStrict()
497
+ // Choice here is between failing with a correct RuntimeException
498
+ // or mostly silently with an IOException
499
+ url = urlLine.toHttpUrlOrNull() ? : throw IOException (" Cache corruption for $urlLine " ).also {
500
+ Platform .get().log(" cache corruption" , Platform .WARN , it)
501
+ }
496
502
requestMethod = source.readUtf8LineStrict()
497
503
val varyHeadersBuilder = Headers .Builder ()
498
504
val varyRequestHeaderLineCount = readInt(source)
@@ -536,13 +542,11 @@ class Cache internal constructor(
536
542
} else {
537
543
handshake = null
538
544
}
539
- } finally {
540
- rawSource.close()
541
545
}
542
546
}
543
547
544
548
constructor (response: Response ) {
545
- this .url = response.request.url.toString()
549
+ this .url = response.request.url
546
550
this .varyHeaders = response.varyHeaders()
547
551
this .requestMethod = response.request.method
548
552
this .protocol = response.protocol
@@ -557,7 +561,7 @@ class Cache internal constructor(
557
561
@Throws(IOException ::class )
558
562
fun writeTo (editor : DiskLruCache .Editor ) {
559
563
editor.newSink(ENTRY_METADATA ).buffer().use { sink ->
560
- sink.writeUtf8(url).writeByte(' \n ' .toInt())
564
+ sink.writeUtf8(url.toString() ).writeByte(' \n ' .toInt())
561
565
sink.writeUtf8(requestMethod).writeByte(' \n ' .toInt())
562
566
sink.writeDecimalLong(varyHeaders.size.toLong()).writeByte(' \n ' .toInt())
563
567
for (i in 0 until varyHeaders.size) {
@@ -618,8 +622,8 @@ class Cache internal constructor(
618
622
private fun writeCertList (sink : BufferedSink , certificates : List <Certificate >) {
619
623
try {
620
624
sink.writeDecimalLong(certificates.size.toLong()).writeByte(' \n ' .toInt())
621
- for (i in 0 until certificates.size ) {
622
- val bytes = certificates[i] .encoded
625
+ for (element in certificates) {
626
+ val bytes = element .encoded
623
627
val line = bytes.toByteString().base64()
624
628
sink.writeUtf8(line).writeByte(' \n ' .toInt())
625
629
}
@@ -629,7 +633,7 @@ class Cache internal constructor(
629
633
}
630
634
631
635
fun matches (request : Request , response : Response ): Boolean {
632
- return url == request.url.toString() &&
636
+ return url == request.url &&
633
637
requestMethod == request.method &&
634
638
varyMatches(response, varyHeaders, request)
635
639
}
0 commit comments