1
0
mirror of https://github.com/square/okhttp.git synced 2025-04-19 07:42:15 +03:00

Empty constant of Headers, RequestBody, ResponseBody (#8720)

* `Empty` constant of Headers, RequestBody, ResponseBody

removed informal internal constants
This commit is contained in:
木葉 Scarlet 2025-04-05 19:39:37 +08:00 committed by GitHub
parent 422ba475b9
commit 33854ec0d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 47 additions and 41 deletions

View File

@ -23,14 +23,13 @@ import java.net.InetAddress
import java.net.Socket
import okhttp3.Headers
import okhttp3.Headers.Companion.headersOf
import okhttp3.internal.EMPTY_HEADERS
import okio.Buffer
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Timeout
@Timeout(30)
class RecordedRequestTest {
private val headers: Headers = EMPTY_HEADERS
private val headers: Headers = Headers.Empty
@Test fun testIPv4() {
val socket =

View File

@ -624,6 +624,7 @@ public final class okhttp3/Handshake$Companion {
public final class okhttp3/Headers : java/lang/Iterable, kotlin/jvm/internal/markers/KMappedMarker {
public static final field Companion Lokhttp3/Headers$Companion;
public static final field Empty Lokhttp3/Headers;
public final fun -deprecated_size ()I
public final fun byteCount ()J
public fun equals (Ljava/lang/Object;)Z
@ -1088,6 +1089,7 @@ public class okhttp3/Request$Builder {
public abstract class okhttp3/RequestBody {
public static final field Companion Lokhttp3/RequestBody$Companion;
public static final field Empty Lokhttp3/RequestBody;
public fun <init> ()V
public fun contentLength ()J
public abstract fun contentType ()Lokhttp3/MediaType;
@ -1202,6 +1204,7 @@ public class okhttp3/Response$Builder {
public abstract class okhttp3/ResponseBody : java/io/Closeable {
public static final field Companion Lokhttp3/ResponseBody$Companion;
public static final field Empty Lokhttp3/ResponseBody;
public fun <init> ()V
public final fun byteStream ()Ljava/io/InputStream;
public final fun byteString ()Lokio/ByteString;

View File

@ -624,6 +624,7 @@ public final class okhttp3/Handshake$Companion {
public final class okhttp3/Headers : java/lang/Iterable, kotlin/jvm/internal/markers/KMappedMarker {
public static final field Companion Lokhttp3/Headers$Companion;
public static final field Empty Lokhttp3/Headers;
public final fun -deprecated_size ()I
public final fun byteCount ()J
public fun equals (Ljava/lang/Object;)Z
@ -1088,6 +1089,7 @@ public class okhttp3/Request$Builder {
public abstract class okhttp3/RequestBody {
public static final field Companion Lokhttp3/RequestBody$Companion;
public static final field Empty Lokhttp3/RequestBody;
public fun <init> ()V
public fun contentLength ()J
public abstract fun contentType ()Lokhttp3/MediaType;
@ -1202,6 +1204,7 @@ public class okhttp3/Response$Builder {
public abstract class okhttp3/ResponseBody : java/io/Closeable {
public static final field Companion Lokhttp3/ResponseBody$Companion;
public static final field Empty Lokhttp3/ResponseBody;
public fun <init> ()V
public final fun byteStream ()Ljava/io/InputStream;
public final fun byteString ()Lokio/ByteString;

View File

@ -26,7 +26,6 @@ import java.security.cert.CertificateFactory
import java.util.TreeSet
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.internal.EMPTY_HEADERS
import okhttp3.internal.cache.CacheRequest
import okhttp3.internal.cache.CacheStrategy
import okhttp3.internal.cache.DiskLruCache
@ -823,7 +822,7 @@ class Cache internal constructor(
responseHeaders: Headers,
): Headers {
val varyFields = responseHeaders.varyFields()
if (varyFields.isEmpty()) return EMPTY_HEADERS
if (varyFields.isEmpty()) return Headers.Empty
val result = Headers.Builder()
for (i in 0 until requestHeaders.size) {

View File

@ -322,6 +322,10 @@ class Headers internal constructor(
}
companion object {
/** Empty headers. */
@JvmField
val Empty = Headers(emptyArray())
/**
* Returns headers for the alternating header names and values. There must be an even number of
* arguments, and they must alternate between header names and values.

View File

@ -24,7 +24,6 @@ import okhttp3.internal.canonicalUrl
import okhttp3.internal.commonAddHeader
import okhttp3.internal.commonCacheControl
import okhttp3.internal.commonDelete
import okhttp3.internal.commonEmptyRequestBody
import okhttp3.internal.commonGet
import okhttp3.internal.commonHead
import okhttp3.internal.commonHeader
@ -270,7 +269,7 @@ class Request internal constructor(
open fun post(body: RequestBody): Builder = commonPost(body)
@JvmOverloads
open fun delete(body: RequestBody? = commonEmptyRequestBody): Builder = commonDelete(body)
open fun delete(body: RequestBody? = RequestBody.Empty): Builder = commonDelete(body)
open fun put(body: RequestBody): Builder = commonPut(body)

View File

@ -101,6 +101,18 @@ abstract class RequestBody {
open fun isOneShot(): Boolean = commonIsOneShot()
companion object {
/** Empty request body. */
@JvmField
val Empty: RequestBody = EmptyRequestBody()
private class EmptyRequestBody : RequestBody() {
override fun contentType() = null
override fun contentLength() = 0L
override fun writeTo(sink: BufferedSink) {}
}
/**
* Returns a new request body that transmits this string. If [contentType] is non-null and lacks
* a charset, this will use UTF-8.

View File

@ -26,7 +26,6 @@ import okhttp3.internal.commonCacheControl
import okhttp3.internal.commonCacheResponse
import okhttp3.internal.commonClose
import okhttp3.internal.commonCode
import okhttp3.internal.commonEmptyResponse
import okhttp3.internal.commonHeader
import okhttp3.internal.commonHeaders
import okhttp3.internal.commonIsRedirect
@ -320,7 +319,7 @@ class Response internal constructor(
internal var message: String? = null
internal var handshake: Handshake? = null
internal var headers: Headers.Builder
internal var body: ResponseBody = commonEmptyResponse
internal var body: ResponseBody = ResponseBody.Empty
internal var networkResponse: Response? = null
internal var cacheResponse: Response? = null
internal var priorResponse: Response? = null

View File

@ -213,6 +213,18 @@ abstract class ResponseBody : Closeable {
}
companion object {
/** Empty response body. */
@JvmField
val Empty: ResponseBody = EmptyResponseBody()
private class EmptyResponseBody : ResponseBody() {
override fun contentType() = null
override fun contentLength() = 0L
override fun source() = Buffer()
}
/**
* Returns a new response body that transmits this string. If [contentType] is non-null and
* has a charset other than utf-8 the behaviour differs by platform.

View File

@ -17,11 +17,6 @@
package okhttp3.internal
import okhttp3.Headers
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.ResponseBody
import okhttp3.ResponseBody.Companion.toResponseBody
import okio.ArrayIndexOutOfBoundsException
import okio.Buffer
import okio.BufferedSink
@ -394,10 +389,6 @@ internal fun checkOffsetAndCount(
}
}
val commonEmptyHeaders: Headers = Headers.headersOf()
val commonEmptyRequestBody: RequestBody = EMPTY_BYTE_ARRAY.toRequestBody()
val commonEmptyResponse: ResponseBody = EMPTY_BYTE_ARRAY.toResponseBody()
internal fun <T> interleave(
a: Iterable<T>,
b: Iterable<T>,

View File

@ -40,23 +40,12 @@ import okhttp3.Headers
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.defaultPort
import okhttp3.OkHttpClient
import okhttp3.RequestBody
import okhttp3.Response
import okhttp3.ResponseBody
import okhttp3.internal.http2.Header
import okio.Buffer
import okio.BufferedSource
import okio.Source
@JvmField
internal val EMPTY_HEADERS: Headers = commonEmptyHeaders
@JvmField
internal val EMPTY_REQUEST: RequestBody = commonEmptyRequestBody
@JvmField
internal val EMPTY_RESPONSE: ResponseBody = commonEmptyResponse
/** GMT and UTC are equivalent for our purposes. */
@JvmField
internal val UTC: TimeZone = TimeZone.getTimeZone("GMT")!!

View File

@ -24,7 +24,6 @@ import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.internal.EMPTY_HEADERS
import okhttp3.internal.checkOffsetAndCount
import okhttp3.internal.discard
import okhttp3.internal.headersContentLength
@ -144,7 +143,7 @@ class Http1ExchangeCodec(
override fun trailers(): Headers {
check(state == STATE_CLOSED) { "too early; can't read the trailers yet" }
return trailers ?: EMPTY_HEADERS
return trailers ?: Headers.Empty
}
override fun flushRequest() {

View File

@ -22,8 +22,8 @@ import java.net.Socket
import java.util.concurrent.TimeUnit
import java.util.concurrent.locks.Condition
import java.util.concurrent.locks.ReentrantLock
import okhttp3.Headers
import okhttp3.internal.EMPTY_BYTE_ARRAY
import okhttp3.internal.EMPTY_HEADERS
import okhttp3.internal.assertThreadDoesntHoldLock
import okhttp3.internal.closeQuietly
import okhttp3.internal.concurrent.TaskRunner
@ -674,7 +674,7 @@ class Http2Connection internal constructor(
}
dataStream.receiveData(source, length)
if (inFinished) {
dataStream.receiveHeaders(EMPTY_HEADERS, true)
dataStream.receiveHeaders(Headers.Empty, true)
}
}

View File

@ -23,7 +23,6 @@ import java.util.ArrayDeque
import java.util.concurrent.locks.Condition
import java.util.concurrent.locks.ReentrantLock
import okhttp3.Headers
import okhttp3.internal.EMPTY_HEADERS
import okhttp3.internal.assertNotHeld
import okhttp3.internal.connection.Locks.newLockCondition
import okhttp3.internal.connection.Locks.withLock
@ -174,7 +173,7 @@ class Http2Stream internal constructor(
fun trailers(): Headers {
this.withLock {
if (source.finished && source.receiveBuffer.exhausted() && source.readBuffer.exhausted()) {
return source.trailers ?: EMPTY_HEADERS
return source.trailers ?: Headers.Empty
}
if (errorCode != null) {
throw errorException ?: StreamResetException(errorCode!!)

View File

@ -22,12 +22,11 @@ import java.time.Instant
import java.util.Date
import kotlin.test.assertFailsWith
import okhttp3.Headers.Companion.toHeaders
import okhttp3.internal.EMPTY_HEADERS
import org.junit.jupiter.api.Test
class HeadersJvmTest {
@Test fun byteCount() {
assertThat(EMPTY_HEADERS.byteCount()).isEqualTo(0L)
assertThat(Headers.Empty.byteCount()).isEqualTo(0L)
assertThat(
Headers
.Builder()

View File

@ -31,11 +31,11 @@ import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
import kotlin.test.assertFailsWith
import okhttp3.Headers
import okhttp3.Headers.Companion.headersOf
import okhttp3.TestUtil.headerEntries
import okhttp3.TestUtil.repeat
import okhttp3.internal.EMPTY_BYTE_ARRAY
import okhttp3.internal.EMPTY_HEADERS
import okhttp3.internal.concurrent.TaskFaker
import okhttp3.internal.concurrent.TaskRunner
import okhttp3.internal.connection.Locks.withLock
@ -557,7 +557,7 @@ class Http2ConnectionTest {
val stream = connection.newStream(headerEntries("a", "artichaut"), false)
connection.writePingAndAwaitPong()
assertThat(stream.takeHeaders()).isEqualTo(headersOf("headers", "bam"))
assertThat(stream.trailers()).isEqualTo(EMPTY_HEADERS)
assertThat(stream.trailers()).isEqualTo(Headers.Empty)
assertThat(connection.openStreamCount()).isEqualTo(0)
// Verify the peer received what was expected.
@ -808,7 +808,7 @@ class Http2ConnectionTest {
connection.writePingAndAwaitPong()
assertThat(stream.takeHeaders()).isEqualTo(headersOf("headers", "bam"))
assertThat(source.readUtf8(5)).isEqualTo("robot")
assertThat(stream.trailers()).isEqualTo(EMPTY_HEADERS)
assertThat(stream.trailers()).isEqualTo(Headers.Empty)
assertThat(connection.openStreamCount()).isEqualTo(0)
// Verify the peer received what was expected.

View File

@ -76,7 +76,6 @@ import okhttp3.TestUtil.assumeNotWindows
import okhttp3.TestUtil.repeat
import okhttp3.TestUtil.threadFactory
import okhttp3.internal.DoubleInetAddressDns
import okhttp3.internal.EMPTY_REQUEST
import okhttp3.internal.RecordingOkAuthenticator
import okhttp3.internal.connection.RealConnection
import okhttp3.internal.discard
@ -1623,7 +1622,7 @@ class HttpOverHttp2Test {
Request
.Builder()
.url(server.url("/"))
.method("DELETE", EMPTY_REQUEST)
.method("DELETE", RequestBody.Empty)
.build(),
)
val response = call.execute()