From: Fredrik Tolf Date: Tue, 13 Oct 2009 18:07:58 +0000 (+0200) Subject: Merge branch 'master' of git.dolda2000.com:/srv/git/r/jsvc X-Git-Url: http://www.dolda2000.com/gitweb/?a=commitdiff_plain;h=a7c50bd905f146c3a28c991e72f43bc9079f3ed8;hp=b5f270350bfadea58c6b14516422df00202e9d46;p=jsvc.git Merge branch 'master' of git.dolda2000.com:/srv/git/r/jsvc --- diff --git a/build.xml b/build.xml index b42373a..c51aa71 100644 --- a/build.xml +++ b/build.xml @@ -13,7 +13,6 @@ - @@ -36,23 +35,6 @@ - - - - - - - - - - - - - - - - - diff --git a/etc/test.jsvc.properties b/etc/test.jsvc.properties deleted file mode 100644 index 2a50054..0000000 --- a/etc/test.jsvc.properties +++ /dev/null @@ -1,2 +0,0 @@ -jsvc.j2ee.appname = JSvc Test War -jsvc.bootstrap = dolda.jsvc.test.Bootstrap diff --git a/src/dolda/jsvc/test/Bootstrap.java b/src/dolda/jsvc/test/Bootstrap.java deleted file mode 100644 index ad4c38f..0000000 --- a/src/dolda/jsvc/test/Bootstrap.java +++ /dev/null @@ -1,15 +0,0 @@ -package dolda.jsvc.test; - -import dolda.jsvc.*; -import dolda.jsvc.util.*; - -public class Bootstrap { - public static Responder responder() { - Multiplexer root = new Multiplexer(); - root.file("test", new TestResponder()); - root.file("", new StaticContent(Bootstrap.class, "static/index.html", false, "text/html")); - root.file("css", new StaticContent(Bootstrap.class, "static/test.css", false, "text/css")); - root.dir("foo", new StaticContent(Bootstrap.class, "static/foo", true, "text/plain; charset=utf-8")); - return(Misc.stdroot(root)); - } -} diff --git a/src/dolda/jsvc/test/TestResponder.java b/src/dolda/jsvc/test/TestResponder.java deleted file mode 100644 index e765d2e..0000000 --- a/src/dolda/jsvc/test/TestResponder.java +++ /dev/null @@ -1,19 +0,0 @@ -package dolda.jsvc.test; - -import dolda.jsvc.*; -import dolda.jsvc.util.*; -import java.io.*; - -public class TestResponder extends SimpleWriter { - public TestResponder() { - super("plain"); - } - - public void respond(Request req, PrintWriter out) { - out.println(req.url()); - out.println(req.path()); - out.println(req.inheaders()); - out.println(req.ctx().starttime()); - out.println(req.remoteaddr() + "<->" + req.localaddr()); - } -} diff --git a/src/dolda/jsvc/test/static/foo/a b/src/dolda/jsvc/test/static/foo/a deleted file mode 100644 index 6e1f517..0000000 --- a/src/dolda/jsvc/test/static/foo/a +++ /dev/null @@ -1 +0,0 @@ -Test A diff --git a/src/dolda/jsvc/test/static/foo/b b/src/dolda/jsvc/test/static/foo/b deleted file mode 100644 index d4e395a..0000000 --- a/src/dolda/jsvc/test/static/foo/b +++ /dev/null @@ -1 +0,0 @@ -Test B diff --git a/src/dolda/jsvc/test/static/index.html b/src/dolda/jsvc/test/static/index.html deleted file mode 100644 index 8553fc9..0000000 --- a/src/dolda/jsvc/test/static/index.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - -Test - - - -

Hello world!

-

Test

- - diff --git a/src/dolda/jsvc/test/static/test.css b/src/dolda/jsvc/test/static/test.css deleted file mode 100644 index eb67f25..0000000 --- a/src/dolda/jsvc/test/static/test.css +++ /dev/null @@ -1,3 +0,0 @@ -body { - font-family: sans; -} diff --git a/src/dolda/jsvc/util/Cache.java b/src/dolda/jsvc/util/Cache.java new file mode 100644 index 0000000..6fd7d7a --- /dev/null +++ b/src/dolda/jsvc/util/Cache.java @@ -0,0 +1,28 @@ +package dolda.jsvc.util; + +import dolda.jsvc.*; +import java.util.*; + +public class Cache { + public static void checkmtime(Request req, long mtime) { + /* Since the HTTP time format is (reasonably enough) precise + * only to seconds, any extra milliseconds must be trimmed + * off, or the mtime will almost certainly not match. */ + Date mdate = new Date((mtime / 1000) * 1000); + String ims = req.inheaders().get("If-Modified-Since"); + if(ims != null) { + Date cldate; + try { + cldate = Http.parsedate(ims); + } catch(java.text.ParseException e) { + throw(Restarts.stdresponse(400, "The If-Modified-Since header is not parseable.")); + } + if(mdate.compareTo(cldate) <= 0) { + req.status(304); + req.outheaders().put("Content-Length", "0"); + throw(Restarts.done()); + } + } + req.outheaders().put("Last-Modified", Http.fmtdate(mdate)); + } +} diff --git a/src/dolda/jsvc/util/Multiplexer.java b/src/dolda/jsvc/util/Multiplexer.java index df9e4c2..8b003b7 100644 --- a/src/dolda/jsvc/util/Multiplexer.java +++ b/src/dolda/jsvc/util/Multiplexer.java @@ -5,10 +5,10 @@ import java.util.*; public class Multiplexer implements Responder { private Responder def; - private Collection subs = new LinkedList(); + private Collection matchers = new LinkedList(); - private static interface Sub { - boolean match(Request req); + public static interface Matcher { + public boolean match(Request req); } public Multiplexer(Responder def) { @@ -24,7 +24,7 @@ public class Multiplexer implements Responder { } public void file(final String path, final Responder responder) { - subs.add(new Sub() { + add(new Matcher() { public boolean match(Request req) { if(req.path().equals(path)) { responder.respond(req); @@ -37,7 +37,7 @@ public class Multiplexer implements Responder { public void dir(String path, final Responder responder) { final String fp = Misc.stripslashes(path, true, true); - subs.add(new Sub() { + add(new Matcher() { public boolean match(Request req) { if(req.path().equals(fp)) { throw(Restarts.redirect(fp + "/")); @@ -50,9 +50,13 @@ public class Multiplexer implements Responder { }); } + public void add(Matcher m) { + matchers.add(m); + } + public void respond(Request req) { - for(Sub s : subs) { - if(s.match(req)) + for(Matcher m : matchers) { + if(m.match(req)) return; } def.respond(req); diff --git a/src/dolda/jsvc/util/Restarts.java b/src/dolda/jsvc/util/Restarts.java index 7725f28..d4aab70 100644 --- a/src/dolda/jsvc/util/Restarts.java +++ b/src/dolda/jsvc/util/Restarts.java @@ -77,4 +77,11 @@ public class Restarts { public static RequestRestart stdresponse(int code) { return(stdresponse(code, "An error occurred", Misc.statustext(code))); } + + public static RequestRestart done() { + return(new RequestRestart() { + public void respond(Request req) { + } + }); + } } diff --git a/src/dolda/jsvc/util/StaticContent.java b/src/dolda/jsvc/util/StaticContent.java index 0d4208e..685669c 100644 --- a/src/dolda/jsvc/util/StaticContent.java +++ b/src/dolda/jsvc/util/StaticContent.java @@ -36,25 +36,10 @@ public class StaticContent implements Responder { } if(in == null) throw(Restarts.stdresponse(404)); - String ims = req.inheaders().get("If-Modified-Since"); - Date mtime = new Date((req.ctx().starttime() / 1000) * 1000); - if(ims != null) { - Date d; - try { - d = Http.parsedate(ims); - } catch(java.text.ParseException e) { - throw(Restarts.stdresponse(400)); - } - if(mtime.compareTo(d) <= 0) { - req.status(304); - req.outheaders().put("Content-Length", "0"); - return; - } - } + Cache.checkmtime(req, req.ctx().starttime()); try { try { req.outheaders().put("Content-Type", mimetype); - req.outheaders().put("Last-Modified", Http.fmtdate(mtime)); Misc.cpstream(in, req.output()); } finally { in.close();