Add minimal HTTP server and shared network type packages
This commit is contained in:
1 parent
79b015ee56
commit
01ceecd7df
28 files changed
+2642
-55
No files matched your search
@@ -1,17 +1,6 @@
|
||||
package lyng.io.http
|
||||
|
||||
/*
|
||||
Response/header view that behaves like a map for the first value of each header name.
|
||||
Multi-valued headers are exposed through `getAll`.
|
||||
*/
|
||||
extern class HttpHeaders : Map<String, String> {
|
||||
/* Return the first value for the given header name, or null when absent. */
|
||||
fun get(name: String): String?
|
||||
/* Return all values for the given header name, preserving wire order when available. */
|
||||
fun getAll(name: String): List<String>
|
||||
/* Return distinct header names present in this response. */
|
||||
fun names(): List<String>
|
||||
}
|
||||
import lyng.io.http.types
|
||||
|
||||
/* Mutable request descriptor for programmatic HTTP calls. */
|
||||
extern class HttpRequest {
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package lyng.io.http.server
|
||||
|
||||
import lyng.io.http.types
|
||||
import lyng.io.ws.types
|
||||
|
||||
/* Immutable parsed incoming server request. */
|
||||
extern class ServerRequest {
|
||||
val method: String
|
||||
val target: String
|
||||
val path: String
|
||||
val query: String?
|
||||
val headers: HttpHeaders
|
||||
val body: Buffer
|
||||
fun text(): String
|
||||
fun isWebSocketUpgrade(): Bool
|
||||
}
|
||||
|
||||
/* Active server-side WebSocket session. */
|
||||
extern class ServerWebSocket {
|
||||
fun isOpen(): Bool
|
||||
fun sendText(text: String): void
|
||||
fun sendBytes(data: Buffer): void
|
||||
fun receive(): WsMessage?
|
||||
fun close(code: Int = 1000, reason: String = ""): void
|
||||
}
|
||||
|
||||
/* Mutable exchange object for one incoming request. */
|
||||
extern class ServerExchange {
|
||||
val request: ServerRequest
|
||||
fun respond(status: Int = 200, body: Buffer? = null): void
|
||||
fun respondText(status: Int = 200, bodyText: String = ""): void
|
||||
fun setHeader(name: String, value: String): void
|
||||
fun addHeader(name: String, value: String): void
|
||||
fun acceptWebSocket(handler: (ServerWebSocket, ServerExchange) -> Object?): void
|
||||
fun isHandled(): Bool
|
||||
}
|
||||
|
||||
/* Running listener handle. */
|
||||
extern class HttpServerHandle {
|
||||
fun localPort(): Int
|
||||
fun close(): void
|
||||
}
|
||||
|
||||
/* Exact-path HTTP/WebSocket server with built-in router. */
|
||||
extern class HttpServer {
|
||||
fun get(path: String, handler: (ServerExchange) -> Object?): HttpServer
|
||||
fun post(path: String, handler: (ServerExchange) -> Object?): HttpServer
|
||||
fun put(path: String, handler: (ServerExchange) -> Object?): HttpServer
|
||||
fun delete(path: String, handler: (ServerExchange) -> Object?): HttpServer
|
||||
fun any(path: String, handler: (ServerExchange) -> Object?): HttpServer
|
||||
fun ws(path: String, handler: (ServerWebSocket, ServerExchange) -> Object?): HttpServer
|
||||
fun fallback(handler: (ServerExchange) -> Object?): HttpServer
|
||||
fun listen(port: Int, host: String? = null, backlog: Int = 128): HttpServerHandle
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package lyng.io.http.types
|
||||
|
||||
/*
|
||||
Response/header view that behaves like a map for the first value of each header name.
|
||||
Multi-valued headers are exposed through `getAll`.
|
||||
*/
|
||||
extern class HttpHeaders : Map<String, String> {
|
||||
/* Return the first value for the given header name, or null when absent. */
|
||||
fun get(name: String): String?
|
||||
/* Return all values for the given header name, preserving wire order when available. */
|
||||
fun getAll(name: String): List<String>
|
||||
/* Return distinct header names present in this response. */
|
||||
fun names(): List<String>
|
||||
}
|
||||
@@ -1,30 +1,6 @@
|
||||
package lyng.io.net
|
||||
|
||||
/* Address family for resolved or bound endpoints. */
|
||||
enum IpVersion {
|
||||
IPV4,
|
||||
IPV6
|
||||
}
|
||||
|
||||
/* Concrete socket endpoint. */
|
||||
extern class SocketAddress {
|
||||
/* Numeric or host-form address string. */
|
||||
val host: String
|
||||
/* Transport port number. */
|
||||
val port: Int
|
||||
/* Address family. */
|
||||
val ipVersion: IpVersion
|
||||
/* True when obtained from DNS resolution rather than raw bind input. */
|
||||
val resolved: Bool
|
||||
/* Stable printable form such as `127.0.0.1:4040` or `[::1]:4040`. */
|
||||
override fun toString(): String
|
||||
}
|
||||
|
||||
/* Datagram payload paired with sender/peer address. */
|
||||
extern class Datagram {
|
||||
val data: Buffer
|
||||
val address: SocketAddress
|
||||
}
|
||||
import lyng.io.net.types
|
||||
|
||||
/* Connected TCP socket. */
|
||||
extern class TcpSocket {
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package lyng.io.net.types
|
||||
|
||||
/* Address family for resolved or bound endpoints. */
|
||||
enum IpVersion {
|
||||
IPV4,
|
||||
IPV6
|
||||
}
|
||||
|
||||
/* Concrete socket endpoint. */
|
||||
extern class SocketAddress {
|
||||
/* Numeric or host-form address string. */
|
||||
val host: String
|
||||
/* Transport port number. */
|
||||
val port: Int
|
||||
/* Address family. */
|
||||
val ipVersion: IpVersion
|
||||
/* True when obtained from DNS resolution rather than raw bind input. */
|
||||
val resolved: Bool
|
||||
/* Stable printable form such as `127.0.0.1:4040` or `[::1]:4040`. */
|
||||
override fun toString(): String
|
||||
}
|
||||
|
||||
/* Datagram payload paired with sender/peer address. */
|
||||
extern class Datagram {
|
||||
val data: Buffer
|
||||
val address: SocketAddress
|
||||
}
|
||||
@@ -1,14 +1,6 @@
|
||||
package lyng.io.ws
|
||||
|
||||
/* Received WebSocket message. */
|
||||
extern class WsMessage {
|
||||
/* True when this message carries text payload. */
|
||||
val isText: Bool
|
||||
/* Text payload for text messages, otherwise null. */
|
||||
val text: String?
|
||||
/* Binary payload for binary messages, otherwise null. */
|
||||
val data: Buffer?
|
||||
}
|
||||
import lyng.io.ws.types
|
||||
|
||||
/* Active WebSocket client session. */
|
||||
extern class WsSession {
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package lyng.io.ws.types
|
||||
|
||||
/* Received WebSocket message. */
|
||||
extern class WsMessage {
|
||||
/* True when this message carries text payload. */
|
||||
val isText: Bool
|
||||
/* Text payload for text messages, otherwise null. */
|
||||
val text: String?
|
||||
/* Binary payload for binary messages, otherwise null. */
|
||||
val data: Buffer?
|
||||
}
|
||||
Reference in new issue
Block a user