Skip to content
Open
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
21 changes: 21 additions & 0 deletions HttpProxy/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.21'
}

group 'http-proxy'
version '1.0-SNAPSHOT'

repositories {
mavenCentral()
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
1 change: 1 addition & 0 deletions HttpProxy/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kotlin.code.style=official
172 changes: 172 additions & 0 deletions HttpProxy/gradlew

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

84 changes: 84 additions & 0 deletions HttpProxy/gradlew.bat

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

2 changes: 2 additions & 0 deletions HttpProxy/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'http-proxy'

42 changes: 42 additions & 0 deletions HttpProxy/src/main/kotlin/ru/hse/Acceptor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ru.hse

import java.net.ServerSocket
import java.util.logging.Logger
import kotlin.concurrent.thread

class Acceptor(val port: Int, clientHandler: ClientHandler) {

private val listener: Thread
private val workers: MutableList<Thread> = ArrayList()

init {
listener = thread(start = false, name = "Acceptor") {
try {
val serverSocket = ServerSocket(port)
serverSocket.use {
while (!Thread.currentThread().isInterrupted) {
val socket = it.accept()
workers.add(clientHandler.run(socket))
}
}
} catch (e: Exception) {
logger.severe("Closing client acceptor: ${e.message}")
e.printStackTrace()
}
}
}

fun start() {
listener.start()
logger.info("Proxy started on port $port")
}

fun stop() {
listener.interrupt()
workers.forEach(Thread::interrupt)
}

companion object {
val logger: Logger = Logger.getLogger(Acceptor::class.java.name)
}
}
21 changes: 21 additions & 0 deletions HttpProxy/src/main/kotlin/ru/hse/Cache.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.hse

interface Cache<K, V> {
fun lookUp(request: K): V?
fun addPage(request: K, response: V, expiration: Long = 0)

companion object {
fun canBeCached(headers: List<String>): Boolean {
val cachePolicy = headers.find { it.contains("Cache-Control") }?.split(' ')?.get(2) ?: return true
return !cachePolicy.contains("no-cache") && !cachePolicy.contains("no-store")
}

fun getExpirationTime(headers: List<String>): Long {
val expiration = headers.find { it.contains("Cache-Control") }?.split(' ')?.get(2) ?: return 0
if (expiration.contains("max-age="))
return expiration.split('=')[2].toLong()
return 0
}
}

}
Loading