Add basic support for inspecting requests

This commit is contained in:
Nick Chambers 2024-04-18 10:22:03 -05:00
parent da330a4e87
commit 3971b249d9
2 changed files with 30 additions and 3 deletions

View File

@ -1,21 +1,43 @@
package com.spookyinternet.cobweb;
import java.net.*;
import java.util.*;
public class CobwebServer {
private ServerSocket srv;
private Map<String, List<HttpRoute>> glances;
private Map<String, List<HttpRoute>> routes;
public CobwebServer(int port, byte[] host) throws Exception {
this.srv = new ServerSocket(port, 64, InetAddress.getByAddress(host));
this.glances = new HashMap<String, List<HttpRoute>>();
this.routes = new HashMap<String, List<HttpRoute>>();
}
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<HttpRoute>());
}
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<HttpRoute>());
}
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();
}
}
}

View File

@ -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<String, List<HttpRoute>> glances;
private Map<String, List<HttpRoute>> routes;
public HttpThread(Socket sock) {
public HttpThread(Socket sock, Map<String, List<HttpRoute>> glances, Map<String, List<HttpRoute>> routes) {
this.sock = sock;
this.glances = glances;
this.routes = routes;
}
public void run() {