scoop:scoop.http.asyncio:0.1.0
src/http1_writer.scoop
Selected production source · src/http1_writer.scoop
1
package scoop.http.asyncio3
import scoop.asyncio.*4
import scoop.http.*5
import scoop.platform.memory.NativeByteBuffer6
import scoop.time.Duration8
/**9
* The single active output body of an HTTP/1 exchange. Explicit finish writes the selected framing10
* terminator and trailers.11
*/12
public class AsyncHttp1Writer internal constructor(13
private val state: H1State,14
private val body: H1Output15
)16
: AsyncByteSink, AsyncCloseable {17
/**18
* Whether the output message was finished successfully.19
*/20
public val isComplete: Boolean get() = state.lock.withLock { body.life.completed }22
private fun requireActive() {23
if (body.life.disposed)24
throw IoClosedException()25
if (body.life.completed || !present(state.outgoing) || state.outgoing!! !== body)26
throw IllegalStateException(Some("this is not the active HTTP/1 output body"))27
}29
/**30
* Writes body bytes according to the selected framing, allowing short progress.31
*/32
public override suspend fun writeFrom(buffer: NativeByteBuffer, count: Long): Long {33
val operation =34
beginH1Operation<Http1BodyChunk>(35
state,36
false,37
{38
requireActive()39
httpCount(count, buffer.readableBytes)40
@Unsafe41
{42
val chunk =43
if (count == 0L)44
state.encoder.prepareBody(count)45
else46
buffer.withReadableBytes {47
_,48
_ -> state.encoder.prepareBody(count)49
}50
return@beginH1Operation chunk51
}52
},53
body.writeIdle54
)55
try {56
operation.job.ensureActive()57
if (count != 0L)58
writeH1Body(state, operation.prepared, buffer, count)59
} catch (error: Throwable) {60
failH1Operation(operation, error)61
}62
endH1Operation(operation)63
return count64
}66
/**67
* Drains accepted output and flushes the transport without ending the body.68
*/69
public override suspend fun flush() {70
val operation =71
beginH1Operation<Unit>(72
state,73
false,74
{75
if (!body.life.completed)76
requireActive()77
},78
body.writeIdle79
)80
try {81
operation.job.ensureActive()82
flushH1Transport(state)83
} catch (error: Throwable) {84
failH1Operation(operation, error)85
}86
endH1Operation(operation)87
}89
/**90
* Finishes the selected body framing and sends allowed trailers, then completes this writer.91
*/92
public suspend fun finish(trailers: List<HttpField> = ArrayList<HttpField>()) {93
val operation =94
beginH1Operation<Array<Byte>>(95
state,96
false,97
{98
requireActive()99
state.encoder.finish(trailers)100
},101
body.writeIdle102
)103
try {104
operation.job.ensureActive()105
writeH1Wire(state, operation.prepared)106
completeH1Output(state, body)107
} catch (error: Throwable) {108
failH1Operation(operation, error)109
}110
endH1Operation(operation)111
closeAfterH1Output(state, body)112
}114
/**115
* Closes the writer; abandoning unfinished output closes the connection rather than pretending116
* a complete message was sent.117
*/118
public override suspend fun close() {119
httpJob(state.loop, false)120
val incomplete =121
state.lock.withLock {122
if (body.life.disposed)123
false124
else {125
body.life.disposed = true;126
!body.life.completed127
}128
}129
if (incomplete)130
closeH1(state)131
}132
}134
internal fun completeH1Output(state: H1State, body: H1Output) {135
val listener =136
state.lock.withLock {137
body.life.completed = true138
state.outgoing = None139
if (body.finalResponse) {140
val request = state.requests.removeFirst()141
request.body.responseFinished = true142
resumeH1AfterRejection(state, request.body)143
}144
completeH1BodyLocked(body.life)145
}146
if (present(listener))147
listener!!.dispose()148
}150
internal suspend fun closeAfterH1Output(state: H1State, body: H1Output) {151
val closing =152
state.lock.withLock {153
state.closeAfterOutput ||154
(155
state.encoder.role ==156
HttpRole.Server &&157
state.encoder.mustClose &&158
!state.encoder.isSwitched159
) ||160
(161
body.finalResponse &&162
present(body.request) &&163
closeAfterH1InputLocked(state, body.request!!.body)164
)165
}166
if (closing)167
closeH1(state)168
}170
internal fun requireH1Output(state: H1State, role: HttpRole) {171
if (state.closeAfterOutput)172
throw IoClosedException()173
if (state.encoder.role != role)174
throw IllegalStateException(Some("wrong HTTP/1 send role"))175
if (present(state.outgoing))176
throw IllegalStateException(Some("previous HTTP/1 output body is not complete"))177
}179
internal suspend fun sendH1Request(180
state: H1State,181
head: HttpRequestHead,182
framing: Http1BodyFraming,183
writeIdle: Duration?184
): AsyncHttp1Writer {185
val operation =186
beginH1Operation<(H1Output, Array<Byte>)>(187
state,188
false,189
{190
requireH1Output(state, HttpRole.Client)191
val bytes = state.encoder.beginRequest(head, framing)192
state.decoder.expectResponse(head.method)193
state.clientPending += 1L194
val body = H1Output(false, writeIdle = writeIdle)195
state.outgoing = Some(body)196
(body, bytes)197
},198
writeIdle199
)200
val writer =201
try {202
operation.job.ensureActive()203
val(body, bytes) = operation.prepared204
writeH1Wire(state, bytes)205
listenH1Body(state, body.life, httpJob(state.loop, true))206
AsyncHttp1Writer(state, body)207
} catch (error: Throwable) {208
failH1Operation(operation, error)209
}210
endH1Operation(operation)211
return writer212
}214
internal suspend fun sendH1Response(215
state: H1State,216
head: HttpResponseHead,217
framing: Http1BodyFraming,218
writeIdle: Duration?219
): AsyncHttp1Writer {220
val operation =221
beginH1Operation<(H1Output, Array<Byte>)>(222
state,223
false,224
{225
requireH1Output(state, HttpRole.Server)226
val request =227
state.requests.firstOrNone() ?:228
throw IllegalStateException(Some("no HTTP/1 request to answer"))229
val bytes = state.encoder.beginResponse(head, request.method, framing)230
val body =231
H1Output(head.status >= 200U || head.status == 101U, Some(request), writeIdle)232
state.outgoing = Some(body)233
(body, bytes)234
},235
writeIdle236
)237
val writer =238
try {239
operation.job.ensureActive()240
val(body, bytes) = operation.prepared241
writeH1Wire(state, bytes)242
listenH1Body(state, body.life, httpJob(state.loop, true))243
AsyncHttp1Writer(state, body)244
} catch (error: Throwable) {245
failH1Operation(operation, error)246
}247
endH1Operation(operation)248
return writer249
}