Skip to content

Commit 839a8ff

Browse files
committed
[WIP] HttpParams
1 parent 8c36360 commit 839a8ff

File tree

17 files changed

+1120
-452
lines changed

17 files changed

+1120
-452
lines changed

ktserver-jdk/src/main/kotlin/com/coditory/ktserver/nio/HttpAsyncSocketChannel.kt

Lines changed: 0 additions & 50 deletions
This file was deleted.

ktserver-jdk/src/main/kotlin/com/coditory/ktserver/nio/KtJdkServer.kt

Lines changed: 20 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,49 @@
11
package com.coditory.ktserver.nio
22

3+
import com.coditory.ktserver.HttpExchange
4+
import com.coditory.ktserver.HttpSerDeserializer
35
import com.coditory.ktserver.HttpServer
6+
import com.coditory.ktserver.http.HttpParams
7+
import com.coditory.ktserver.http.HttpRequest
8+
import com.coditory.ktserver.http.HttpRequestMethod
9+
import com.coditory.ktserver.serialization.ScoredHttpSerDeserializer
410
import com.coditory.quark.uri.Ports
511
import kotlinx.coroutines.CoroutineScope
612
import kotlinx.coroutines.Dispatchers
713
import kotlinx.coroutines.launch
8-
import kotlinx.coroutines.runBlocking
9-
import kotlinx.coroutines.suspendCancellableCoroutine
10-
import kotlinx.io.Buffer
1114
import kotlinx.io.asSource
1215
import kotlinx.io.buffered
13-
import kotlinx.io.readString
1416
import java.net.InetSocketAddress
15-
import java.nio.channels.AsynchronousServerSocketChannel
16-
import java.nio.channels.AsynchronousSocketChannel
17-
import kotlin.coroutines.resume
18-
import kotlin.coroutines.resumeWithException
17+
import com.sun.net.httpserver.HttpExchange as JdkHttpExchange
1918
import com.sun.net.httpserver.HttpServer as JdkHttpServer
2019

2120
class KtJdkServer(
2221
val port: Int = Ports.getNextAvailable(),
2322
val backlog: Int = 0,
2423
val requestScope: CoroutineScope = CoroutineScope(Dispatchers.IO),
24+
val serde: HttpSerDeserializer = ScoredHttpSerDeserializer.default(),
2525
) : HttpServer {
26-
private val server = JdkHttpServer.create(InetSocketAddress(port), 0).apply {
27-
executor = null // creates a default executor
26+
private val server = JdkHttpServer.create(InetSocketAddress(port), backlog).apply {
27+
executor = null // creates a default executor that runs on callers thread
2828
createContext("/") { exchange ->
2929
requestScope.launch {
30-
val src = exchange.requestBody.asSource().buffered()
31-
src.readString()
3230
handleRequest(exchange)
3331
}
3432
}
3533
}
3634

37-
override fun start() {
38-
val server = AsynchronousServerSocketChannel.open().bind(InetSocketAddress(port), backlog)
39-
runBlocking {
40-
val channel = accept(server)
41-
val asyncChannel = HttpAsyncSocketChannel(channel)
42-
Buffer
43-
}
44-
}
45-
46-
suspend fun accept(server: AsynchronousServerSocketChannel): AsynchronousSocketChannel {
47-
suspendCancellableCoroutine { cont ->
48-
server.accept(
49-
null,
50-
object : java.nio.channels.CompletionHandler<AsynchronousSocketChannel, Void?> {
51-
override fun completed(result: AsynchronousSocketChannel, attachment: Void?) {
52-
cont.resume(result)
53-
}
54-
55-
override fun failed(exc: Throwable, attachment: Void?) {
56-
cont.resumeWithException(exc)
57-
}
58-
},
59-
)
60-
}
35+
private suspend fun handleRequest(srcExchange: JdkHttpExchange) {
36+
val request = HttpRequest(
37+
method = HttpRequestMethod.valueOf(srcExchange.requestMethod),
38+
uri = srcExchange.requestURI,
39+
headers = HttpParams.fromMultiMap(srcExchange.requestHeaders.toMap()),
40+
deserializer = serde,
41+
source = srcExchange.requestBody.asSource().buffered(),
42+
)
43+
val exchange = HttpExchange(request = request)
6144
}
6245

63-
private suspend fun handleRequest(channel: HttpAsyncSocketChannel) {
64-
try {
65-
// Read the request from the client
66-
val requestLine = source.readUtf8Line()
67-
println("Received request: $requestLine")
68-
69-
// Prepare an HTTP-like response
70-
val body = "Hello from non-blocking Okio!"
71-
val response = """
72-
HTTP/1.1 200 OK
73-
Content-Type: text/plain
74-
Content-Length: ${body.length}
75-
76-
$body
77-
""".trimIndent()
78-
79-
// Write the response to the client
80-
sink.writeUtf8(response)
81-
sink.flush()
82-
} catch (e: Exception) {
83-
println("Error while handling client: ${e.message}")
84-
} finally {
85-
channel.close()
86-
}
46+
override fun start() {
8747
}
8848

8949
override fun stop() {

ktserver-jdk/src/main/kotlin/com/coditory/ktserver/nio/Nio.kt

Lines changed: 0 additions & 114 deletions
This file was deleted.

ktserver/src/main/kotlin/com/coditory/ktserver/HttpDeserializer.kt

Lines changed: 0 additions & 90 deletions
This file was deleted.

ktserver/src/main/kotlin/com/coditory/ktserver/HttpExchange.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.coditory.ktserver
22

33
import com.coditory.ktserver.http.HttpRequest
44

5-
interface HttpExchange {
6-
val request: HttpRequest
7-
val channel: HttpChannel
8-
}
5+
data class HttpExchange(
6+
val request: HttpRequest,
7+
)

ktserver/src/main/kotlin/com/coditory/ktserver/HttpHandler.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ interface HttpHandler {
1111
}
1212

1313
class SimpleHttpHandler(
14-
val method: HttpRequestMethod,
14+
val method: HttpRequestMethod? = null,
1515
override val pathPattern: PathPattern,
16-
val consumes: String,
16+
val consumes: String? = null,
17+
val produces: String? = null,
1718
) : HttpHandler {
1819
override fun matches(request: HttpRequest): Boolean {
19-
return request.method == method && pathPattern.matches(request.uri.path)
20+
if (method != null && method != request.method) return false
21+
if (!pathPattern.matches(request.uri.path)) return false
22+
if (consumes == null && produces == null) return true
23+
24+
return pathPattern.matches(request.uri.path)
2025
}
2126

2227
override suspend fun handle(exchange: HttpExchange) {

0 commit comments

Comments
 (0)