Compare commits
4 Commits
1803bc8a0d
...
3484070e58
Author | SHA1 | Date |
---|---|---|
Nick Chambers | 3484070e58 | |
Nick Chambers | 1b2cd7eccc | |
Nick Chambers | e24fce1662 | |
Nick Chambers | f967eda6d4 |
|
@ -1,4 +1,5 @@
|
|||
/build/
|
||||
/tmp/
|
||||
/sources.txt
|
||||
/cobweb.jar
|
||||
.DS_Store
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue