Skip to content

Commit

Permalink
Refactor HTTP features!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
jaguililla committed Oct 2, 2024
1 parent 9fd9ce3 commit 1d40b80
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 13 deletions.
6 changes: 4 additions & 2 deletions http/http/src/main/kotlin/com/hexagontk/http/HttpFeature.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package com.hexagontk.http
/**
* Toolkit feature that may or may not be implemented by a server adapter.
*
* TODO Take advantage in tests to fire only tests that apply
*
* @property ZIP Request and response compression.
* @property COOKIES .
* @property MULTIPART .
* @property WEBSOCKETS Support for server Web Sockets.
* @property SSE Support for Server Sent Events.
*
* TODO To be used in clients/servers (take advantage in tests to fire only tests that apply)
*/
enum class HttpFeature {
ZIP,
Expand Down
2 changes: 2 additions & 0 deletions http/http_client/api/http_client.api
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public abstract interface class com/hexagontk/http/client/HttpClientPort {
public abstract fun sse (Lcom/hexagontk/http/model/HttpRequestPort;)Ljava/util/concurrent/Flow$Publisher;
public abstract fun startUp (Lcom/hexagontk/http/client/HttpClient;)V
public abstract fun started ()Z
public abstract fun supportedFeatures ()Ljava/util/Set;
public abstract fun supportedProtocols ()Ljava/util/Set;
public abstract fun ws (Ljava/lang/String;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;)Lcom/hexagontk/http/model/ws/WsSession;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.hexagontk.http.client

import com.hexagontk.http.HttpFeature
import com.hexagontk.http.model.HttpProtocol
import com.hexagontk.http.model.HttpRequestPort
import com.hexagontk.http.model.HttpResponsePort
import com.hexagontk.http.model.ServerEvent
Expand Down Expand Up @@ -27,4 +29,18 @@ interface HttpClientPort {
onPong: WsSession.(data: ByteArray) -> Unit = {},
onClose: WsSession.(status: Int, reason: String) -> Unit,
): WsSession

/**
* Return the client adapter's supported features.
*
* @return Set of supported features.
*/
fun supportedFeatures(): Set<HttpFeature>

/**
* Return the server adapter's supported protocols.
*
* @return Set of supported protocols.
*/
fun supportedProtocols(): Set<HttpProtocol>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.hexagontk.http.client

import com.hexagontk.http.HttpFeature
import com.hexagontk.http.model.*
import com.hexagontk.http.model.HttpProtocol.HTTP
import com.hexagontk.http.model.ws.WsSession
import java.net.URI
import java.util.concurrent.Flow.Publisher
Expand Down Expand Up @@ -68,4 +70,10 @@ object VoidHttpClient : HttpClientPort {
onClose(status, reason)
}
}

override fun supportedFeatures(): Set<HttpFeature> =
emptySet()

override fun supportedProtocols(): Set<HttpProtocol> =
setOf(HTTP)
}
2 changes: 2 additions & 0 deletions http/http_client_java/api/http_client_java.api
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public final class com/hexagontk/http/client/java/JavaHttpClient : com/hexagontk
public fun sse (Lcom/hexagontk/http/model/HttpRequestPort;)Ljava/util/concurrent/Flow$Publisher;
public fun startUp (Lcom/hexagontk/http/client/HttpClient;)V
public fun started ()Z
public fun supportedFeatures ()Ljava/util/Set;
public fun supportedProtocols ()Ljava/util/Set;
public fun ws (Ljava/lang/String;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;)Lcom/hexagontk/http/model/ws/WsSession;
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ package com.hexagontk.http.client.java

import com.hexagontk.core.security.createKeyManagerFactory
import com.hexagontk.core.security.createTrustManagerFactory
import com.hexagontk.http.CHECKED_HEADERS
import com.hexagontk.http.SslSettings
import com.hexagontk.http.*
import com.hexagontk.http.HttpFeature.*
import com.hexagontk.http.client.HttpClient
import com.hexagontk.http.client.HttpClientPort
import com.hexagontk.http.client.HttpClientSettings
import com.hexagontk.http.formatQueryString
import com.hexagontk.http.handlers.bodyToBytes
import com.hexagontk.http.model.*
import com.hexagontk.http.model.HttpProtocol.H2C
import com.hexagontk.http.model.HttpProtocol.HTTP2
import com.hexagontk.http.model.HttpProtocol.*
import com.hexagontk.http.model.HttpResponse
import com.hexagontk.http.model.ws.WsSession
import com.hexagontk.http.parseContentType
import java.net.CookieManager
import java.net.HttpCookie
import java.net.URI
Expand Down Expand Up @@ -114,6 +111,12 @@ class JavaHttpClient(
throw UnsupportedOperationException("SSE not supported")
}

override fun supportedFeatures(): Set<HttpFeature> =
setOf(ZIP, COOKIES)

override fun supportedProtocols(): Set<HttpProtocol> =
setOf(HTTP, HTTPS, HTTP2)

private fun sslContext(sslSettings: SslSettings): SSLContext {
val sslContext = SSLContext.getInstance("TLSv1.3")

Expand Down
2 changes: 2 additions & 0 deletions http/http_client_jetty/api/http_client_jetty.api
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class com/hexagontk/http/client/jetty/JettyHttpClient : com/hexagontk/htt
public fun sse (Lcom/hexagontk/http/model/HttpRequestPort;)Ljava/util/concurrent/Flow$Publisher;
public fun startUp (Lcom/hexagontk/http/client/HttpClient;)V
public fun started ()Z
public fun supportedFeatures ()Ljava/util/Set;
public fun supportedProtocols ()Ljava/util/Set;
public fun ws (Ljava/lang/String;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;)Lcom/hexagontk/http/model/ws/WsSession;
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import com.hexagontk.core.media.TEXT_EVENT_STREAM
import com.hexagontk.core.security.loadKeyStore
import com.hexagontk.http.handlers.bodyToBytes
import com.hexagontk.http.CHECKED_HEADERS
import com.hexagontk.http.HttpFeature
import com.hexagontk.http.HttpFeature.*
import com.hexagontk.http.client.HttpClient
import com.hexagontk.http.client.HttpClientPort
import com.hexagontk.http.client.HttpClientSettings
import com.hexagontk.http.model.HttpResponse
import com.hexagontk.http.model.*
import com.hexagontk.http.model.CookieSameSite.*
import com.hexagontk.http.model.HttpProtocol.HTTP
import com.hexagontk.http.model.HttpProtocol.HTTPS
import com.hexagontk.http.model.ws.WsSession
import com.hexagontk.http.parseContentType
import org.eclipse.jetty.client.HttpResponseException
Expand Down Expand Up @@ -138,6 +142,12 @@ open class JettyHttpClient : HttpClientPort {
return clientPublisher
}

override fun supportedFeatures(): Set<HttpFeature> =
setOf(ZIP, COOKIES, MULTIPART, SSE)

override fun supportedProtocols(): Set<HttpProtocol> =
setOf(HTTP, HTTPS, HttpProtocol.HTTP2)

private fun convertJettyResponse(
adapterHttpClient: HttpClient, adapterJettyClient: JettyClient, response: Response
): HttpResponse {
Expand Down
1 change: 1 addition & 0 deletions http/http_client_jetty_ws/api/http_client_jetty_ws.api
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class com/hexagontk/http/client/jetty/ws/JettyWsHttpClient : com/he
public fun <init> ()V
public fun shutDown ()V
public fun startUp (Lcom/hexagontk/http/client/HttpClient;)V
public fun supportedFeatures ()Ljava/util/Set;
public fun ws (Ljava/lang/String;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;)Lcom/hexagontk/http/model/ws/WsSession;
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.hexagontk.http.client.jetty.ws

import com.hexagontk.core.urlOf
import com.hexagontk.http.HttpFeature
import com.hexagontk.http.HttpFeature.*
import com.hexagontk.http.client.HttpClient
import com.hexagontk.http.client.jetty.JettyHttpClient
import com.hexagontk.http.model.ws.WsSession
Expand Down Expand Up @@ -44,4 +46,7 @@ class JettyWsHttpClient : JettyHttpClient() {

return JettyClientWsSession(uri, session)
}

override fun supportedFeatures(): Set<HttpFeature> =
super.supportedFeatures() + WEBSOCKETS
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.hexagontk.http.model.HttpResponse
import com.hexagontk.http.model.HttpResponsePort
import com.hexagontk.http.server.HttpServer
import com.hexagontk.http.HttpFeature
import com.hexagontk.http.HttpFeature.ZIP
import com.hexagontk.http.HttpFeature.*
import com.hexagontk.http.server.HttpServerPort
import io.helidon.common.socket.SocketOptions
import io.helidon.http.Method
Expand Down Expand Up @@ -134,7 +134,7 @@ class HelidonHttpServer(
setOf(HTTP, HTTPS, HTTP2)

override fun supportedFeatures(): Set<HttpFeature> =
setOf(ZIP)
setOf(ZIP, COOKIES)

override fun options(): Map<String, *> =
fieldsMapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.hexagontk.http.server.jetty
import com.hexagontk.core.Platform
import com.hexagontk.core.fieldsMapOf
import com.hexagontk.http.HttpFeature
import com.hexagontk.http.HttpFeature.ZIP
import com.hexagontk.http.HttpFeature.*
import com.hexagontk.http.model.HttpProtocol
import com.hexagontk.http.model.HttpProtocol.*
import com.hexagontk.http.server.HttpServer
Expand Down Expand Up @@ -135,7 +135,7 @@ class JettyServletHttpServer(
setOf(HTTP, HTTPS, HTTP2)

override fun supportedFeatures(): Set<HttpFeature> =
setOf(ZIP)
setOf(ZIP, COOKIES, MULTIPART)

override fun options(): Map<String, *> =
fieldsMapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ open class NettyHttpServer(
setOf(HTTP, HTTPS, HTTP2)

override fun supportedFeatures(): Set<HttpFeature> =
setOf(ZIP, WEBSOCKETS, SSE)
setOf(ZIP, COOKIES, MULTIPART, WEBSOCKETS, SSE)

override fun options(): Map<String, *> =
fieldsMapOf(
Expand Down

0 comments on commit 1d40b80

Please sign in to comment.