-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #619 from hexagonkt/develop
Use same model and handlers for HTTP client and server
- Loading branch information
Showing
147 changed files
with
2,804 additions
and
1,612 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package com.hexagonkt.http.model | ||
|
||
interface HttpCall<I : HttpRequest, O : HttpResponse> { | ||
val request: I | ||
val response: O | ||
} | ||
data class HttpCall( | ||
val request: HttpRequestPort = HttpRequest(), | ||
val response: HttpResponsePort = HttpResponse(), | ||
) |
96 changes: 59 additions & 37 deletions
96
http/src/main/kotlin/com/hexagonkt/http/model/HttpRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,65 @@ | ||
package com.hexagonkt.http.model | ||
|
||
import com.hexagonkt.http.formatQueryString | ||
import java.net.URL | ||
import com.hexagonkt.http.* | ||
import com.hexagonkt.http.model.HttpMethod.GET | ||
import com.hexagonkt.http.model.HttpProtocol.HTTP | ||
import java.security.cert.X509Certificate | ||
|
||
// TODO 'formParameters' are a kind of 'part' and both are handled as part of the 'body' | ||
// they could be handled as a special kind of type in body processing (List<HttpPartPort>) | ||
interface HttpRequest : HttpMessage { | ||
val method: HttpMethod // "GET" | ||
val protocol: HttpProtocol // "http" | ||
val host: String // "example.com" | ||
val port: Int // 80 | ||
val path: String // "/foo" servlet path + path info | ||
val queryParameters: QueryParameters | ||
val parts: List<HttpPart> // hash of multipart parts | ||
val formParameters: FormParameters | ||
val accept: List<ContentType> | ||
val authorization: Authorization? | ||
data class HttpRequest( | ||
override val method: HttpMethod = GET, | ||
override val protocol: HttpProtocol = HTTP, | ||
override val host: String = "localhost", | ||
override val port: Int = 80, | ||
override val path: String = "", | ||
override val queryParameters: QueryParameters = QueryParameters(), | ||
override val headers: Headers = Headers(), | ||
override val body: Any = "", | ||
override val parts: List<HttpPart> = emptyList(), | ||
override val formParameters: FormParameters = FormParameters(), | ||
override val cookies: List<Cookie> = emptyList(), | ||
override val contentType: ContentType? = null, | ||
override val certificateChain: List<X509Certificate> = emptyList(), | ||
override val accept: List<ContentType> = emptyList(), | ||
override val contentLength: Long = -1L, | ||
override val authorization: Authorization? = null, | ||
) : HttpRequestPort { | ||
|
||
fun partsMap(): Map<String, HttpPart> = | ||
parts.associateBy { it.name } | ||
init { | ||
checkHeaders(headers) | ||
} | ||
|
||
fun url(): URL = | ||
if (queryParameters.isEmpty()) | ||
URL("${protocol.schema}://$host:$port/$path") | ||
else | ||
URL("${protocol.schema}://$host:$port/$path?${formatQueryString(queryParameters)}") | ||
|
||
fun userAgent(): String? = | ||
headers["user-agent"]?.value | ||
|
||
fun referer(): String? = | ||
headers["referer"]?.value | ||
|
||
fun origin(): String? = | ||
headers["origin"]?.value | ||
|
||
fun authorization(): Authorization? = | ||
headers["authorization"] | ||
?.value | ||
?.split(" ", limit = 2) | ||
?.let { Authorization(it.first(), it.last()) } | ||
override fun with( | ||
body: Any, | ||
headers: Headers, | ||
contentType: ContentType?, | ||
method: HttpMethod, | ||
protocol: HttpProtocol, | ||
host: String, | ||
port: Int, | ||
path: String, | ||
queryParameters: QueryParameters, | ||
parts: List<HttpPart>, | ||
formParameters: FormParameters, | ||
cookies: List<Cookie>, | ||
accept: List<ContentType>, | ||
authorization: Authorization?, | ||
certificateChain: List<X509Certificate>, | ||
): HttpRequestPort = | ||
copy( | ||
body = body, | ||
headers = headers, | ||
contentType = contentType, | ||
method = method, | ||
protocol = protocol, | ||
host = host, | ||
port = port, | ||
path = path, | ||
queryParameters = queryParameters, | ||
parts = parts, | ||
formParameters = formParameters, | ||
cookies = cookies, | ||
accept = accept, | ||
authorization = authorization, | ||
certificateChain = certificateChain, | ||
) | ||
} |
70 changes: 70 additions & 0 deletions
70
http/src/main/kotlin/com/hexagonkt/http/model/HttpRequestPort.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.hexagonkt.http.model | ||
|
||
import com.hexagonkt.http.formatQueryString | ||
import java.net.URL | ||
import java.security.cert.X509Certificate | ||
|
||
// TODO 'formParameters' are a kind of 'part' and both are handled as part of the 'body' | ||
// they could be handled as a special kind of type in body processing (List<HttpPartPort>) | ||
interface HttpRequestPort : HttpMessage { | ||
val method: HttpMethod // "GET" | ||
val protocol: HttpProtocol // "http" | ||
val host: String // "example.com" | ||
val port: Int // 80 | ||
val path: String // "/foo" servlet path + path info | ||
val queryParameters: QueryParameters | ||
val parts: List<HttpPart> // hash of multipart parts | ||
val formParameters: FormParameters | ||
val accept: List<ContentType> | ||
val authorization: Authorization? | ||
|
||
val certificateChain: List<X509Certificate> | ||
val contentLength: Long // length of request.body (or 0) | ||
|
||
fun with( | ||
body: Any = this.body, | ||
headers: Headers = this.headers, | ||
contentType: ContentType? = this.contentType, | ||
method: HttpMethod = this.method, | ||
protocol: HttpProtocol = this.protocol, | ||
host: String = this.host, | ||
port: Int = this.port, | ||
path: String = this.path, | ||
queryParameters: QueryParameters = this.queryParameters, | ||
parts: List<HttpPart> = this.parts, | ||
formParameters: FormParameters = this.formParameters, | ||
cookies: List<Cookie> = this.cookies, | ||
accept: List<ContentType> = this.accept, | ||
authorization: Authorization? = this.authorization, | ||
certificateChain: List<X509Certificate> = this.certificateChain, | ||
): HttpRequestPort | ||
|
||
fun certificate(): X509Certificate? = | ||
certificateChain.firstOrNull() | ||
|
||
fun partsMap(): Map<String, HttpPart> = | ||
parts.associateBy { it.name } | ||
|
||
fun url(): URL = | ||
when { | ||
queryParameters.isEmpty() && port == 80 -> "${protocol.schema}://$host/$path" | ||
queryParameters.isEmpty() -> "${protocol.schema}://$host:$port/$path" | ||
else -> "${protocol.schema}://$host:$port/$path?${formatQueryString(queryParameters)}" | ||
} | ||
.let(::URL) | ||
|
||
fun userAgent(): String? = | ||
headers["user-agent"]?.value | ||
|
||
fun referer(): String? = | ||
headers["referer"]?.value | ||
|
||
fun origin(): String? = | ||
headers["origin"]?.value | ||
|
||
fun authorization(): Authorization? = | ||
headers["authorization"] | ||
?.value | ||
?.split(" ", limit = 2) | ||
?.let { Authorization(it.first(), it.last()) } | ||
} |
Oops, something went wrong.