@@ -22,6 +22,7 @@ package net.ccbluex.netty.http
2222import io.netty.buffer.Unpooled
2323import io.netty.handler.codec.http.*
2424import net.ccbluex.netty.http.HttpServer.Companion.logger
25+ import net.ccbluex.netty.http.model.RequestContext
2526import net.ccbluex.netty.http.util.httpBadRequest
2627import net.ccbluex.netty.http.util.httpInternalServerError
2728import net.ccbluex.netty.http.util.httpNotFound
@@ -30,20 +31,20 @@ import net.ccbluex.netty.http.model.RequestObject
3031internal class HttpConductor (private val server : HttpServer ) {
3132
3233 /* *
33- * Processes the incoming request object and returns the response.
34+ * Processes the incoming request context and returns the response.
3435 *
35- * @param requestObject The incoming request object .
36+ * @param context The request context to process .
3637 * @return The response to the request.
3738 */
38- fun processRequestObject ( requestObject : RequestObject ) = runCatching {
39- val context = requestObject. context
39+ fun processRequestContext ( context : RequestContext ) = runCatching {
40+ val content = context.contentBuffer.toString()
4041 val method = context.httpMethod
4142
42- logger.debug(" Request {}" , requestObject )
43+ logger.debug(" Request {}" , context )
4344
4445 if (! context.headers[" content-length" ].isNullOrEmpty() &&
45- context.headers[" content-length" ]?.toInt() != requestObject. content.toByteArray(Charsets .UTF_8 ).size) {
46- logger.warn(" Received incomplete request: $requestObject " )
46+ context.headers[" content-length" ]?.toInt() != content.toByteArray(Charsets .UTF_8 ).size) {
47+ logger.warn(" Received incomplete request: $context " )
4748 return @runCatching httpBadRequest(" Incomplete request" )
4849 }
4950
@@ -63,21 +64,24 @@ internal class HttpConductor(private val server: HttpServer) {
6364 return @runCatching response
6465 }
6566
66- server.routeController.findRoute(context.path, method)?.let { route ->
67- logger.debug(" Found route {}" , route)
68- return @runCatching route.handler(requestObject)
69- }
67+ val (node, params, remaining) = server.routeController.processPath(context.path, method) ? :
68+ return @runCatching httpNotFound(context.path, " Route not found" )
7069
71- if (method == HttpMethod .GET ) {
72- server.routeController.findFileServant(context.path)?.let { (fileServant, path) ->
73- logger.debug(" Found file servant {}" , fileServant)
74- return @runCatching fileServant.handleFileRequest(path)
75- }
76- }
70+ logger.debug(" Found destination {}" , node)
71+ val requestObject = RequestObject (
72+ uri = context.uri,
73+ path = context.path,
74+ remainingPath = remaining,
75+ method = method,
76+ body = content,
77+ params = params,
78+ queryParams = context.params,
79+ headers = context.headers
80+ )
7781
78- httpNotFound(context.path, " Route not found " )
82+ return @runCatching node.handleRequest(requestObject )
7983 }.getOrElse {
80- logger.error(" Error while processing request object: $requestObject " , it)
84+ logger.error(" Error while processing request object: $context " , it)
8185 httpInternalServerError(it.message ? : " Unknown error" )
8286 }
8387
0 commit comments