scoop:scoop.http.client:0.1.0
src/sse/request.scoop
Selected production source · src/sse/request.scoop
1
package scoop.http.client3
import scoop.http.*5
internal fun validateClientSseRequest(request: HttpRequest, options: HttpSseOptions): String {6
if (clientPresent(options.reconnect) && !request.body.isReplayable)7
throw HttpSseException(HttpSseFailureReason.BodyNotReplayable, request.url)8
if (9
request.options.cacheMode ==10
HttpCacheMode.OnlyIfCached ||11
HttpCacheControl.parseFields(request.fields).contains("only-if-cached")12
)13
throw IllegalArgumentException(Some("SSE requires a network response"))14
val ids = request.fields.findAll("Last-Event-ID".toByteArray())15
if (ids.size > 1L || (ids.size != 0L && clientPresent(options.initialLastEventId)))16
throw IllegalArgumentException(Some("SSE needs a single initial Last-Event-ID"))17
val id =18
options.initialLastEventId ?:19
if (ids.size == 0L)20
""21
else22
String.fromUtf8(ids[0L].value)23
HttpField("Last-Event-ID".toByteArray(), id.toByteArray()).validate()24
return id25
}27
internal fun clientSseRequest(state: ClientSseState): HttpRequest {28
val original = state.original29
val fields = ArrayList<HttpField>()30
for (field in original.suppliedFields)31
if (!field.hasName("Last-Event-ID".toByteArray()))32
fields.add(field)33
if (!clientCacheHas(original.fields, "Accept"))34
fields.add(HttpField("Accept".toByteArray(), "text/event-stream".toByteArray()))35
if (!clientCacheHas(original.fields, "Cache-Control"))36
fields.add(HttpField("Cache-Control".toByteArray(), "no-cache".toByteArray()))37
if (state.lastEventId != "")38
fields.add(HttpField("Last-Event-ID".toByteArray(), state.lastEventId.toByteArray()))39
val options = original.options40
val decoding = options.decoding ?: state.client.config.decoding41
val prepared =42
httpCallOptions {43
this.duplex = options.duplex44
this.expectContinue = options.expectContinue45
this.continueWait = options.continueWait46
this.decoding =47
Some(48
httpDecodingOptions {49
this.mode = HttpContentDecoding.Automatic50
this.bufferSize = decoding.bufferSize51
this.maxWindowBytes = decoding.maxWindowBytes52
}53
)54
this.timeouts = options.timeouts55
this.onInformational = options.onInformational56
this.redirects = options.redirects57
this.retries = options.retries58
this.authentication = options.authentication59
this.proxy = options.proxy60
this.useCookies = options.useCookies61
this.cookieContext = options.cookieContext62
this.cacheMode = HttpCacheMode.Bypass63
this.observer = options.observer64
}65
return httpRequest(original.url) {66
this.method = original.method67
this.fields = fields68
this.body = original.body69
this.options = prepared70
}71
}