Compare commits

...

4 Commits

6 changed files with 79 additions and 20 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
/build/
/tmp/
/sources.txt
/cobweb.jar
.DS_Store

View File

@ -7,24 +7,21 @@ public class Cobweb {
private static final byte[] HOST = { 127, 0, 0, 1 };
public static void main(String[] args) {
CobwebServer srv = new CobwebServer(PORT, HOST);
try {
CobwebServer srv = new CobwebServer(PORT, HOST);
srv.on("get", "/hello/{?name}", (req) -> {
if(req.params.length > 0) {
return new HttpStaticResponse("Hello, " + req.params[0] + "!");
} else {
return new HttpStaticResponse("Hello, world!");
}
});
srv.on("get", "/hello/{?name}", (req) -> {
if(req.params.length > 0) {
return new HttpStaticResponse("Hello, " + req.params[0] + "!");
} else {
return new HttpStaticResponse("Hello, world!");
}
});
/*
srv.on("get", "/redirect", HttpPermanentRedirect("https://www.example.com"));
srv.on("get", "/*", WwwDir(WWW_DIR, [
AllowTrailingSlash,
AllowImpliedIndex,
AllowDirectoryListing
]));
*/
srv.run();
} catch(Exception err) {
System.out.println(err.getMessage());
err.printStackTrace();
}
}
}

View File

@ -1,11 +1,21 @@
package com.spookyinternet.cobweb;
import java.net.*;
public class CobwebServer {
public CobwebServer(int port, byte[] host) {
System.out.println("Hello, constructor!");
private ServerSocket srv;
public CobwebServer(int port, byte[] host) throws Exception {
this.srv = new ServerSocket(port, 64, InetAddress.getByAddress(host));
}
public void on(String get, String path, HttpRequestFunction fn) {
System.out.println("Hello, on!");
}
public void run() throws Exception {
while(true) {
Socket cli = this.srv.accept();
new HttpThread(cli).start();
}
}
}

View File

@ -1,5 +1,15 @@
package com.spookyinternet.cobweb;
import java.io.*;
public class HttpRequest {
public String[] params;
public HttpRequest(BufferedReader reader) {
HttpTokenStream tokens = new HttpTokenStream(reader);
while(tokens.hasNext()) {
HttpToken token = tokens.next();
}
}
}

View File

@ -0,0 +1,22 @@
package com.spookyinternet.cobweb;
import java.io.*;
import java.net.*;
public class HttpThread extends Thread {
private Socket sock;
public HttpThread(Socket sock) {
this.sock = sock;
}
public void run() {
try {
InputStream stream = this.sock.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
HttpRequest req = new HttpRequest(reader);
} catch(Exception err) {
return;
}
}
}

View File

@ -0,0 +1,19 @@
package com.spookyinternet.cobweb;
class HttpToken {
public int line;
public String value;
public boolean sol;
public boolean eol;
public HttpToken(int line, String value, boolean sol, boolean eol) {
this.line = line;
this.value = value;
this.sol = sol;
this.eol = eol;
}
public boolean complete() {
return this.sol && this.eol;
}
}