From 3971b249d9706000e658478945a415dc39ab4386 Mon Sep 17 00:00:00 2001 From: Nick Chambers Date: Thu, 18 Apr 2024 10:22:03 -0500 Subject: [PATCH] Add basic support for inspecting requests --- .../spookyinternet/cobweb/CobwebServer.java | 26 +++++++++++++++++-- src/com/spookyinternet/cobweb/HttpThread.java | 7 ++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/com/spookyinternet/cobweb/CobwebServer.java b/src/com/spookyinternet/cobweb/CobwebServer.java index de069a7..0a1109f 100644 --- a/src/com/spookyinternet/cobweb/CobwebServer.java +++ b/src/com/spookyinternet/cobweb/CobwebServer.java @@ -1,21 +1,43 @@ package com.spookyinternet.cobweb; import java.net.*; +import java.util.*; public class CobwebServer { private ServerSocket srv; + private Map> glances; + private Map> routes; public CobwebServer(int port, byte[] host) throws Exception { this.srv = new ServerSocket(port, 64, InetAddress.getByAddress(host)); + this.glances = new HashMap>(); + this.routes = new HashMap>(); } - public void on(String get, String path, HttpRequestFunction fn) { + public void inspect(String method, String path, HttpInspectFunction fn) throws Exception { + String norm_method = method.toLowerCase(); + + if(this.glances.get(norm_method) == null) { + this.glances.put(norm_method, new ArrayList()); + } + + this.glances.get(norm_method).add(new HttpRoute(path, fn)); + } + + public void on(String method, String path, HttpRequestFunction fn) throws Exception { + String norm_method = method.toLowerCase(); + + if(this.routes.get(norm_method) == null) { + this.routes.put(norm_method, new ArrayList()); + } + + this.routes.get(norm_method).add(new HttpRoute(path, fn)); } public void run() throws Exception { while(true) { Socket cli = this.srv.accept(); - new HttpThread(cli).start(); + new HttpThread(cli, this.glances, this.routes).start(); } } } diff --git a/src/com/spookyinternet/cobweb/HttpThread.java b/src/com/spookyinternet/cobweb/HttpThread.java index 8e2171b..58484ba 100644 --- a/src/com/spookyinternet/cobweb/HttpThread.java +++ b/src/com/spookyinternet/cobweb/HttpThread.java @@ -2,12 +2,17 @@ package com.spookyinternet.cobweb; import java.io.*; import java.net.*; +import java.util.*; public class HttpThread extends Thread { private Socket sock; + private Map> glances; + private Map> routes; - public HttpThread(Socket sock) { + public HttpThread(Socket sock, Map> glances, Map> routes) { this.sock = sock; + this.glances = glances; + this.routes = routes; } public void run() {