annotate src/goodjava/webserver/handlers/FileHandler.java @ 1769:9d9683e76496

bug fix
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 11 Jun 2023 19:17:26 -0600
parents fa066aaa068c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1347
diff changeset
1 package goodjava.webserver.handlers;
1139
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.File;
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.FileInputStream;
1140
bf03d687eaff webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 1139
diff changeset
5 import java.io.IOException;
bf03d687eaff webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 1139
diff changeset
6 import java.text.DateFormat;
bf03d687eaff webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 1139
diff changeset
7 import java.text.SimpleDateFormat;
1604
8a7519dee55c FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
8 import java.text.ParseException;
1140
bf03d687eaff webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 1139
diff changeset
9 import java.util.Date;
1604
8a7519dee55c FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
10 import java.util.TimeZone;
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1347
diff changeset
11 import goodjava.webserver.Handler;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1347
diff changeset
12 import goodjava.webserver.Request;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1347
diff changeset
13 import goodjava.webserver.Response;
1604
8a7519dee55c FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
14 import goodjava.webserver.Status;
1139
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16
1167
7e6f28c769a1 better handlers
Franklin Schmidt <fschmidt@gmail.com>
parents: 1165
diff changeset
17 public class FileHandler implements Handler {
7e6f28c769a1 better handlers
Franklin Schmidt <fschmidt@gmail.com>
parents: 1165
diff changeset
18 final File dir;
1139
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 public FileHandler() {
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 this(".");
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 }
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 public FileHandler(String pathname) {
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 this(new File(pathname));
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 }
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 public FileHandler(File dir) {
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 if( !dir.isDirectory() )
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 throw new RuntimeException("must be a directory");
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 this.dir = dir;
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 }
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33
1168
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
34 File file(Request request) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
35 return new File(dir,request.path);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
36 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
37
1139
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 public Response handle(Request request) {
1140
bf03d687eaff webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 1139
diff changeset
39 try {
1168
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
40 File file = file(request);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
41 if( file.isFile() ) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
42 Response response = new Response();
1604
8a7519dee55c FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
43
8a7519dee55c FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
44 DateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
8a7519dee55c FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
45 fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
8a7519dee55c FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
46 String lastModified = fmt.format(new Date(file.lastModified()));
1607
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1604
diff changeset
47 String ifMod = (String)request.headers.get("If-Modified-Since");
1604
8a7519dee55c FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
48 if( ifMod != null ) {
8a7519dee55c FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
49 try {
8a7519dee55c FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
50 Date ifModDate = fmt.parse(ifMod);
8a7519dee55c FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
51 if( ifModDate != null && ifModDate.getTime() >= fmt.parse(lastModified).getTime() ) {
8a7519dee55c FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
52 response.status = Status.NOT_MODIFIED;
8a7519dee55c FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
53 return response;
8a7519dee55c FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
54 }
8a7519dee55c FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
55
8a7519dee55c FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
56 } catch(ParseException e) {}
8a7519dee55c FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
57 }
1607
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1604
diff changeset
58 response.headers.put("Last-Modified",lastModified);
1604
8a7519dee55c FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
59
1168
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
60 response.body = new Response.Body( file.length(), new FileInputStream(file) );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
61 return response;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
62 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
63 return null;
1140
bf03d687eaff webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 1139
diff changeset
64 } catch(IOException e) {
bf03d687eaff webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 1139
diff changeset
65 throw new RuntimeException(e);
1139
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 }
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 }
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 }