scoop:scoop.http.client:0.1.0
src/interceptors/chain.scoop
Selected production source · src/interceptors/chain.scoop
1
package scoop.http.client3
import scoop.http.*4
import scoop.url.Url6
/**7
* A suspend interceptor around a logical call. It may invoke the chain once while active and is8
* responsible for returning a compatible owned result.9
*/10
public class HttpInterceptor public constructor(11
internal val intercept: suspend (HttpCallChain) -> HttpCallResult12
)14
/**15
* The current logical request and one-shot continuation into later interceptors or transport.16
*/17
public class HttpCallChain internal constructor(18
internal val invocation: ClientInvocation,19
internal val next: Long,20
/**21
* The current request passed to this interceptor.22
*/23
public val request: HttpRequest24
) {25
/**26
* The caller-supplied fields, before client-managed framing and authority.27
*/28
public val suppliedFields: Array<HttpField> = request.suppliedFields.toArray()29
/**30
* Upgrade options when this chain belongs to a WebSocket handshake.31
*/32
public val webSocketOptions: HttpWebSocketOptions? = invocation.sequence.webSocket33
internal var active: Boolean = true34
internal var proceeded: Boolean = false36
/**37
* Invokes the remainder of the chain once while active, optionally replacing URL, method,38
* fields, and body. WebSocket chains require a replayable empty GET.39
*/40
public suspend fun proceed(41
url: Url = request.url,42
method: String = request.method,43
fields: List<HttpField> = suppliedFields,44
body: RequestBody = request.body45
): HttpCallResult {46
invocation.client.lock.withLock {47
if (!active || proceeded)48
throw IllegalStateException(49
Some("HTTP interceptor chain can proceed only once while active")50
)51
proceeded = true52
}53
invocation.requireActive()54
val selected =55
httpRequest(url) {56
this.method = method57
this.fields = fields58
this.body = body59
this.options = this @HttpCallChain.request.options60
}61
if (clientPresent(webSocketOptions)) {62
if (63
method !=64
"GET" ||65
body.contentLength !=66
Some(0L) ||67
!body.isReplayable ||68
body.trailerNames.size !=69
0L70
)71
throw IllegalArgumentException(72
Some("WebSocket interceptor requires GET with a replayable empty body")73
)74
val websocket =75
httpWebSocketRequest(clientWebSocketUrl(url)) {76
this.fields = fields77
this.options = this @HttpCallChain.request.options78
}79
validateClientWebSocket(websocket, webSocketOptions!!)80
}81
return invokeClientInterceptor(invocation, next, selected)82
}83
}85
internal suspend fun invokeClientInterceptor(86
invocation: ClientInvocation,87
index: Long,88
request: HttpRequest89
): HttpCallResult {90
invocation.requireActive()91
val interceptors = invocation.client.config.interceptors92
if (index == interceptors.size) {93
val result = runClientSequence(invocation.client, request, invocation.sequence)94
rememberClientInvocationResult(invocation, result)95
return result96
}97
val chain = HttpCallChain(invocation, index + 1L, request)98
try {99
val result = interceptors[index].intercept(chain)100
rememberClientInvocationResult(invocation, result)101
return result102
} finally {103
invocation.client.lock.withLock { chain.active = false }104
}105
}