From 3756916f9e74e7890a45fdda6a4cff87d9685520 Mon Sep 17 00:00:00 2001 From: iyifr Date: Fri, 6 Sep 2024 23:18:09 +0100 Subject: [PATCH] feat: more request utilities. --- bin/main.dart | 1 + lib/src/trie.dart | 2 +- lib/utils/req_utils.dart | 25 ++++++++----------------- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/bin/main.dart b/bin/main.dart index db4fd3d..25a9681 100644 --- a/bin/main.dart +++ b/bin/main.dart @@ -10,6 +10,7 @@ void main() async { onRequest: (event) { // PER REQUEST local statešŸ˜» event.context["user"] = 'Ogunlepon'; + print(getRequestUrl(event)); setResponseHeader(event, header: HttpHeaders.contentTypeHeader, diff --git a/lib/src/trie.dart b/lib/src/trie.dart index 063a319..d488dcb 100644 --- a/lib/src/trie.dart +++ b/lib/src/trie.dart @@ -118,7 +118,7 @@ class Trie { params.forEach((key, value) { if (value["leaf"] == true) { - theprms[key] = pathPieces.last; + theprms[key] = pathPieces.lastOrNull ?? ""; } else { List nw = value["prev"].split("/"); var placeholderChunks = nw..removeWhere((item) => item.isEmpty); diff --git a/lib/utils/req_utils.dart b/lib/utils/req_utils.dart index 66e48e9..4c72484 100644 --- a/lib/utils/req_utils.dart +++ b/lib/utils/req_utils.dart @@ -22,23 +22,14 @@ String? getRequestHost(H4Event event) { return event.node["value"]?.headers.value(HttpHeaders.hostHeader); } -// Future>?> readMultipartFormData(H4Event event) async { -// var contentType = event.node["value"]?.headers.contentType; -// if (contentType?.mimeType == 'multipart/form-data') { -// var request = event.node["value"]; - -// final boundary = contentType!.parameters['boundary']; -// if (boundary != null) { -// return await handleMultipartFormdata(request!, boundary); -// } else { -// print('Invalid multipart/form-data request: missing boundary'); -// } -// } else { -// print( -// 'Unsupported content type: ${contentType?.mimeType} expected multipart/form-data'); -// } -// return null; -// } +String? getRequestUrl(H4Event event) { + return '${getRequestProtocol(event)}://${getRequestHost(event)}${event.path}'; +} + +/// ### Get the request protocol. +getRequestProtocol(H4Event event) { + return event.node["value"]?.headers.value("x-forwarded-proto") ?? "http"; +} class FormData { final Map> _data = {};