Skip to content
Merged
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
20 changes: 20 additions & 0 deletions src/main/kotlin/dev/typetype/server/CompressionConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.typetype.server

import io.ktor.http.ContentType
import io.ktor.server.application.Application
import io.ktor.server.application.install
import io.ktor.server.plugins.compression.Compression
import io.ktor.server.plugins.compression.excludeContentType
import io.ktor.server.plugins.compression.gzip

fun Application.configureCompression(): Unit {
install(Compression) {
gzip {
excludeContentType(ContentType.parse("application/vnd.apple.mpegurl"))
excludeContentType(ContentType.Image.Any)
excludeContentType(ContentType.Video.Any)
excludeContentType(ContentType.Audio.Any)
excludeContentType(ContentType.Application.OctetStream)
}
}
}
13 changes: 1 addition & 12 deletions src/main/kotlin/dev/typetype/server/Plugins.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import io.ktor.serialization.kotlinx.json.json
import io.ktor.server.application.Application
import io.ktor.server.application.install
import io.ktor.server.plugins.calllogging.CallLogging
import io.ktor.http.ContentType
import io.ktor.server.plugins.compression.Compression
import io.ktor.server.plugins.compression.excludeContentType
import io.ktor.server.plugins.compression.gzip
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.plugins.cors.routing.CORS
import io.ktor.server.plugins.ratelimit.RateLimit
Expand Down Expand Up @@ -55,14 +51,7 @@ fun Application.configurePlugins(authService: AuthService) {
install(ContentNegotiation) {
json(Json { ignoreUnknownKeys = true; encodeDefaults = true })
}
install(Compression) {
gzip {
excludeContentType(ContentType.parse("application/vnd.apple.mpegurl"))
excludeContentType(ContentType.Video.Any)
excludeContentType(ContentType.Audio.Any)
excludeContentType(ContentType.Application.OctetStream)
}
}
configureCompression()
val allowedOrigins = System.getenv("ALLOWED_ORIGINS")
?.split(",")
?.map { it.trim() }
Expand Down
34 changes: 34 additions & 0 deletions src/test/kotlin/dev/typetype/server/ProxyRoutesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class ProxyRoutesTest {
install(Compression) {
gzip {
excludeContentType(ContentType.parse("application/vnd.apple.mpegurl"))
excludeContentType(ContentType.Image.Any)
excludeContentType(ContentType.Video.Any)
excludeContentType(ContentType.Audio.Any)
excludeContentType(ContentType.Application.OctetStream)
Expand All @@ -69,4 +70,37 @@ class ProxyRoutesTest {
assertEquals(HttpStatusCode.PartialContent, response.status)
assertNull(response.headers["Content-Encoding"])
}

@Test
fun `GET proxy does not gzip image responses`() = testApplication {
coEvery { proxyService.pipe(any(), any()) } returns ExtractionResult.Success(
ProxyResponse(
status = 200,
contentType = "image/jpeg",
contentLength = 2048,
contentRange = null,
acceptRanges = null,
stream = ByteArrayInputStream(ByteArray(2048) { 1 }),
close = {},
)
)
application {
install(ContentNegotiation) { json() }
install(Compression) {
gzip {
excludeContentType(ContentType.parse("application/vnd.apple.mpegurl"))
excludeContentType(ContentType.Image.Any)
excludeContentType(ContentType.Video.Any)
excludeContentType(ContentType.Audio.Any)
excludeContentType(ContentType.Application.OctetStream)
}
}
routing { proxyRoutes(proxyService) }
}
val response = client.get("/proxy?url=https://example.com/thumb.jpg") {
header("Accept-Encoding", "gzip")
}
assertEquals(HttpStatusCode.OK, response.status)
assertNull(response.headers["Content-Encoding"])
}
}