• R/O
  • SSH
  • HTTPS

webserver: Commit


Commit MetaInfo

Revision47 (tree)
Time2010-10-03 09:00:05
Authorhoritaku

Log Message

修正

Change Summary

Incremental Difference

--- branch/Basic 0.2x/reflectAction/refrect_action/Test.java (revision 46)
+++ branch/Basic 0.2x/reflectAction/refrect_action/Test.java (revision 47)
@@ -1,15 +1,18 @@
11 package refrect_action;
22
33 import servercore.http.HttpRequest;
4+import servercore.http.HttpResponse;
45 import servercore.servelet.reflect.Action;
56
67 public class Test extends Action{
78
89 @Override
9- public StringBuffer execute(HttpRequest request) {
10+ public HttpResponse execute(HttpRequest request) {
1011 StringBuffer db = new StringBuffer();
1112 db.append("aiueo");
12- return db;
13+ HttpResponse res = new HttpResponse();
14+ res.setResponseByte(db.toString().getBytes());
15+ return res;
1316 }
1417
1518 }
--- branch/Basic 0.2x/src/servercore/http/HttpRequest.java (revision 46)
+++ branch/Basic 0.2x/src/servercore/http/HttpRequest.java (revision 47)
@@ -21,6 +21,7 @@
2121 private HashMap<String, String> headerMap = new HashMap<String, String>();
2222 private HtmlParam getParams = null;
2323 private HtmlParam postParams = null;
24+ private HashMap<String, String> cookieValues = new HashMap<String, String>();
2425 public String getExtesion() {
2526 return extension;
2627 }
@@ -78,4 +79,16 @@
7879 public HtmlParam getPostParams() {
7980 return this.postParams;
8081 }
82+ public void setHeader(HashMap<String, String> map) {
83+ this.headerMap = map;
84+ }
85+ public HashMap<String, String> getHeader() {
86+ return this.headerMap;
87+ }
88+ public void setCookieValues(HashMap<String, String> map) {
89+ this.cookieValues = map;
90+ }
91+ public HashMap<String, String> getCookieValues() {
92+ return this.cookieValues;
93+ }
8194 }
--- branch/Basic 0.2x/src/servercore/http/HttpResponse.java (revision 46)
+++ branch/Basic 0.2x/src/servercore/http/HttpResponse.java (revision 47)
@@ -1,10 +1,13 @@
11 package servercore.http;
22
3+import java.util.HashMap;
4+
35 public class HttpResponse {
46 private int statusCode;
57 private byte[] responseByte = new byte[0];
68 private long lastModified = 0;
79 private long contentLength = 0;
10+ private HashMap<String, String> header = null;
811 public byte[] getResponseByte() {
912 return this.responseByte;
1013 }
@@ -36,4 +39,16 @@
3639 this.contentLength = len;
3740 }
3841
42+ public void setHeader(HashMap<String, String> map) {
43+ this.header = map;
44+ }
45+ public HashMap<String, String> getHeader() {
46+ return this.header;
47+ }
48+ public void addHeader(String key, String value) {
49+ if(header == null) {
50+ header = new HashMap<String, String>();
51+ }
52+ header.put(key, value);
53+ }
3954 }
--- branch/Basic 0.2x/src/servercore/data/HtmlParam.java (revision 46)
+++ branch/Basic 0.2x/src/servercore/data/HtmlParam.java (revision 47)
@@ -24,4 +24,7 @@
2424
2525 return new HtmlParam(map);
2626 }
27+ public String get(String key) {
28+ return pair.get(key);
29+ }
2730 }
--- branch/Basic 0.2x/src/servercore/servelet/reflect/Action.java (revision 46)
+++ branch/Basic 0.2x/src/servercore/servelet/reflect/Action.java (revision 47)
@@ -1,8 +1,9 @@
11 package servercore.servelet.reflect;
22
33 import servercore.http.HttpRequest;
4+import servercore.http.HttpResponse;
45
56 public abstract class Action {
6- public abstract StringBuffer execute(HttpRequest request) ;
7+ public abstract HttpResponse execute(HttpRequest request) ;
78
89 }
--- branch/Basic 0.2x/src/servercore/job/JobWorker.java (revision 46)
+++ branch/Basic 0.2x/src/servercore/job/JobWorker.java (revision 47)
@@ -6,7 +6,9 @@
66 import java.io.File;
77 import java.sql.Date;
88 import java.text.SimpleDateFormat;
9+import java.util.HashMap;
910 import java.util.Locale;
11+import java.util.Set;
1012
1113 import servercore.data.HtmlParam;
1214 import servercore.http.HttpRequest;
@@ -23,7 +25,10 @@
2325
2426 System.out.print(req.getMethod() + "\t" + req.getRowPath() + "\t");
2527
26- HttpResponse res = GeneratePage.execute(req);
28+ HttpResponse res = security(req);
29+ if(res.getStatusCore() == 200) {
30+ res = GeneratePage.execute(req);
31+ }
2732 System.out.println(res.getStatusCore());
2833
2934 String resStr = "";
@@ -31,6 +36,12 @@
3136 String LastMod = editTimeZone(res.getLastModified());
3237 if(res.getStatusCore() == 200) {
3338 resStr += "HTTP/1.1 200 OK\r\n";
39+ } else if(res.getStatusCore() == 204) {
40+ resStr += "HTTP/1.1 204 No Content\r\n";
41+ } else if(res.getStatusCore() == 301) {
42+ resStr += "HTTP/1.1 301 Moved Permanently\r\n";
43+ } else if(res.getStatusCore() == 403) {
44+ resStr += "HTTP/1.1 403 Fobbiden\r\n";
3445 } else if(res.getStatusCore() == 404) {
3546 resStr += "HTTP/1.1 404 Not found\r\n";
3647 } else if(res.getStatusCore() == 500) {
@@ -41,6 +52,13 @@
4152 resStr += "ETag:\"45aa-707-3c19898c\"r\n";
4253 resStr += "Accept-Ranges: bytes\r\n";
4354 resStr += "Content-Length: " + res.getContentLength() + "\r\n";
55+ if(res.getHeader() != null) {
56+ HashMap<String, String> map = res.getHeader();
57+ for(String key:map.keySet()) {
58+ String value = map.get(key);
59+ resStr += key + ": " + value + "\r\n";
60+ }
61+ }
4462 //resStr += "Connection: Keep-Alive\r\n";
4563
4664 // ファイルの拡張子を判断
@@ -82,6 +100,7 @@
82100 req.setRequestMethod(first_chars[0]);
83101 boolean finished = false;
84102 String postData = null;
103+ HashMap<String, String> header = new HashMap<String, String>();
85104 for(int i = 1;i < allLines.length;i++) {
86105 String line = allLines[i];
87106 if(" ".equals(line)) {
@@ -98,6 +117,11 @@
98117 break;
99118 }
100119 }
120+ } else {
121+ String[] set = line.split("\\: ");
122+ if(set != null && set.length == 2) {
123+ header.put(set[0], set[1]);
124+ }
101125 }
102126 }
103127 if(!finished) {
@@ -110,8 +134,53 @@
110134 req.setPostParams(HtmlParam.analyze(postData));
111135 }
112136 req.setPath(first_chars[1]);
137+ req.setHeader(header);
138+ req.setCookieValues(setCookie(req.getHeader().get("Cookie")));
113139 return req;
114140 }
141+ public HttpResponse security(HttpRequest req) {
142+ HttpResponse res = new HttpResponse();
143+ String path = req.getRowPath();
144+ if("/login_form.html".equals(path)) {
145+ res.setStatusCore(200);
146+ return res;
147+ }
148+ if("/js/jquery.js".equals(path)) {
149+ res.setStatusCore(200);
150+ return res;
151+ }
152+ if("/LoginCheck.do".equals(path)) {
153+ res.setStatusCore(200);
154+ return res;
155+ }
156+ String cookie = req.getCookieValues().get("SESSIONID");
157+ if("cookieok".equals(cookie)) {
158+ res.setStatusCore(200);
159+ return res;
160+ } else {
161+ res.setStatusCore(301);
162+ res.addHeader("Cache-Control", "no-cache");
163+ res.addHeader("Location", "/login_form.html");
164+ }
165+
166+ return res;
167+ }
168+ public HashMap<String, String> setCookie(String str) {
169+ HashMap<String, String> cookieValue = new HashMap<String, String>();
170+ if(str == null) {
171+ return cookieValue;
172+ }
173+
174+ String[] values = str.split("\\;");
175+ for(String set:values) {
176+ set = set.trim();
177+ String[] sets = set.split("\\=");
178+ if(sets != null && sets.length == 2) {
179+ cookieValue.put(sets[0], sets[1]);
180+ }
181+ }
182+ return cookieValue;
183+ }
115184 public String parsePath (String rowPath) {
116185 int index = rowPath.indexOf('?');
117186 if(index < 0) {
--- branch/Basic 0.2x/src/servercore/job/GeneratePage.java (revision 46)
+++ branch/Basic 0.2x/src/servercore/job/GeneratePage.java (revision 47)
@@ -21,10 +21,7 @@
2121 if("do".equals(ext)) {
2222 int index = cgiPath.indexOf(".do");
2323 String pkg = "refrect_action" + cgiPath.substring(0, index);
24- StringBuffer sb = executeCgi(req, pkg);
25- res.setStatusCore(200);
26- res.setResponseStr(sb.toString());
27- res.setLastModified(System.currentTimeMillis());
24+ res = executeCgi(req, pkg);
2825 return res;
2926 }
3027 ifThisIsFile = new File(realPath);
@@ -50,7 +47,7 @@
5047 return res;
5148 }
5249
53- private static StringBuffer executeCgi(HttpRequest req, String PackageName)
50+ private static HttpResponse executeCgi(HttpRequest req, String PackageName)
5451 throws Exception{
5552 Class<Action> cls = null;
5653
@@ -57,6 +54,6 @@
5754 cls = (Class<Action>)Class.forName(PackageName);
5855 Method method = cls.getMethod("execute", HttpRequest.class);
5956 // 実行する
60- return (StringBuffer)method.invoke(cls.newInstance(),req);
57+ return (HttpResponse)method.invoke(cls.newInstance(),req);
6158 }
6259 }
Show on old repository browser