Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
plugins {
kotlin("multiplatform") version "1.9.21"
}

group = "com.github.gimlet2.kottpd"
version = "0.2.1"

repositories {
mavenCentral()
}

kotlin {
jvm("desktop") {
compilations.all {
kotlinOptions.jvmTarget = "20"
}
withJava()
}
val hostOs = System.getProperty("os.name")
val isMingwX64 = hostOs.startsWith("Windows")

val nativeTarget = when {
hostOs == "Mac OS X" -> macosX64("native")
hostOs == "Linux" -> linuxX64("native")
isMingwX64 -> mingwX64("native")
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}

nativeTarget.apply {
binaries {
executable {
baseName = "kottpd"
}
staticLib{
baseName = "kottpd"
}
}
}
sourceSets {
val commonMain by getting
val nativeMain by getting
val desktopMain by getting
val desktopTest by getting

}
}
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
185 changes: 185 additions & 0 deletions gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions gradlew.bat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.github.gimlet2.kottpd

import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.Socket
import java.util.*
/**
* Created by Andrei Chernyshev on 1/14/22.
*/
class ClientThread(val socket: Socket, val match: (HttpRequest) -> (HttpRequest, HttpResponse) -> Any) {

class ClientThread(val socket: Socket, val match: (HttpRequest) -> (HttpRequest, HttpResponse) -> Any) : Runnable {

override fun run() {
val input = BufferedReader(InputStreamReader(socket.inputStream))
val out = socket.outputStream
fun run() {
val input = socket.reader
val out = socket.writer
val request = readRequest(input) {
LinkedHashMap<String, String>().apply {
input.lineSequence().takeWhile(String::isNotBlank).forEach { line ->
input.lines().takeWhile(String::isNotBlank).forEach { line: String ->
line.split(":").let {
put(it[0], it[1].trim())
}
Expand All @@ -28,10 +26,9 @@ class ClientThread(val socket: Socket, val match: (HttpRequest) -> (HttpRequest,
socket.close()
}

fun readRequest(reader: BufferedReader, eval: () -> Map<String, String>): HttpRequest {
reader.readLine().split(' ').let {
fun readRequest(reader: Reader, eval: () -> Map<String, String>): HttpRequest {
reader.line().split(' ').let {
return HttpRequest(HttpMethod.valueOf(it[0]), it[1], it[2], eval(), reader)
}
}
}

5 changes: 5 additions & 0 deletions src/commonMain/kotlin/com/github/gimlet2/kottpd/Closable.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.gimlet2.kottpd

interface Closable {
fun close()
}
16 changes: 16 additions & 0 deletions src/commonMain/kotlin/com/github/gimlet2/kottpd/HttpRequest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.gimlet2.kottpd

/**
* Created by Andrei Chernyshev on 1/14/22.
*/
data class HttpRequest(
val method: HttpMethod,
val url: String,
val httpVersion: String,
val headers: Map<String, String>,
val stream: Reader
) {
val content: String by lazy {
(1..headers.getOrElse("Content-Length") { "0" }.toInt()).fold("") { a, b -> a + stream.read().toChar() }
}
}
Loading