• R/O
  • SSH
  • HTTPS

webserver: Commit


Commit MetaInfo

Revision64 (tree)
Time2010-12-02 23:14:40
Authorhoritaku

Log Message

Gzip対応

Change Summary

Incremental Difference

--- branch/Basic 0.2x/config/web_server.ini (revision 63)
+++ branch/Basic 0.2x/config/web_server.ini (revision 64)
@@ -1,3 +1,6 @@
1-irDir=./docRoot
1+irDir=./docRoot3
22 icDir=./config/reflectConfig.ini
3-port=8080
\ No newline at end of file
3+userSecury=false
4+compress=gzip
5+
6+port=80
\ No newline at end of file
--- branch/Basic 0.2x/src/servercore/job/JobWorker.java (revision 63)
+++ branch/Basic 0.2x/src/servercore/job/JobWorker.java (revision 64)
@@ -1,5 +1,7 @@
11 package servercore.job;
22
3+import java.io.ByteArrayOutputStream;
4+import java.io.FileOutputStream;
35 import java.io.IOException;
46 import java.io.InputStream;
57 import java.io.OutputStream;
@@ -11,6 +13,7 @@
1113 import java.util.HashMap;
1214 import java.util.Locale;
1315 import java.util.Set;
16+import java.util.zip.GZIPOutputStream;
1417
1518 import servercore.data.Cookie;
1619 import servercore.data.HtmlParam;
@@ -25,7 +28,10 @@
2528 HttpRequest req = analyzeReq(ips);
2629 req.normarizePath(HttpServer.getRootDirPath());
2730 String ext = req.getExtesion();
31+ boolean useGzip = useEncodingOK(req.getHeader().get("Accept-Encoding"),
32+ HttpServer.getUseCompress());
2833
34+
2935 System.out.print(req.getMethod() + "\t" + req.getRowPath() + "\t");
3036
3137 // ブラウザがWebSocketを要求したとき
@@ -32,8 +38,13 @@
3238 if("ws".equals(ext)) {
3339 websocketHandShake(req, ops, ips);
3440 }
35- HttpResponse res = security(req);
36- if(res.getStatusCore() == 200) {
41+ HttpResponse res = null;
42+ if(HttpServer.useSecurity()) {
43+ res = security(req);
44+ if(res.getStatusCore() == 200) {
45+ res = GeneratePage.execute(req);
46+ }
47+ } else {
3748 res = GeneratePage.execute(req);
3849 }
3950 System.out.println(res.getStatusCore());
@@ -43,6 +54,25 @@
4354 String LastMod = editTimeZone(res.getLastModified());
4455 if(res.getStatusCore() == 200) {
4556 resStr += "HTTP/1.1 200 OK\r\n";
57+
58+ if(useGzip) {
59+ try {
60+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
61+ GZIPOutputStream gos = new GZIPOutputStream(baos);
62+ byte[] content = res.getResponseByte();
63+ for(int i=0; i<content.length; i++){
64+ gos.write(content[i]);
65+ }
66+ gos.close();
67+ byte[] gzip_content = baos.toByteArray();
68+ baos.close();
69+ res.setResponseByte(gzip_content);
70+ } catch(IOException ioe) {
71+ ioe.printStackTrace();
72+ useGzip = false;
73+ }
74+ }
75+
4676 } else if(res.getStatusCore() == 201) {
4777 resStr += "HTTP/1.1 201 Created\r\n";
4878 } else if(res.getStatusCore() == 204) {
@@ -85,6 +115,11 @@
85115 resStr += "Content-Type: text/plain\r\n";
86116 }
87117
118+ // gzip圧縮
119+ if(useGzip && res.getStatusCore() == 200) {
120+ resStr += "Content-Encoding: gzip\r\n";
121+ }
122+
88123 resStr += "\r\n";
89124
90125 ops.write(resStr.getBytes());
@@ -328,4 +363,10 @@
328363 }
329364 return bytes;
330365 }
366+ public boolean useEncodingOK(String accept_encoding, String encode_algo) {
367+ if(accept_encoding == null || encode_algo == null || "".equals(encode_algo)) {
368+ return false;
369+ }
370+ return accept_encoding.indexOf(encode_algo) >= 0;
371+ }
331372 }
--- branch/Basic 0.2x/src/servercore/http/HttpServer.java (revision 63)
+++ branch/Basic 0.2x/src/servercore/http/HttpServer.java (revision 64)
@@ -13,6 +13,11 @@
1313 private static String rootDirPath = "./";
1414
1515 private static boolean isRunning = false;
16+
17+ private static boolean useSecurity = false;
18+
19+ private static String useCompress = null;
20+
1621 /**
1722 * 接続ポート番号を設定
1823 * @param portNum
@@ -50,6 +55,32 @@
5055 public static String getRootDirPath() {
5156 return rootDirPath;
5257 }
58+
59+ public static void setUseSecurity(boolean flg) {
60+ useSecurity = flg;
61+ }
62+ public static void setUseSecurity(String flg) {
63+ if(flg == null) {
64+ return;
65+ }
66+ flg = flg.toLowerCase();
67+ if("true".equals(flg)) {
68+ useSecurity = true;
69+ } else if("false".equals(flg)) {
70+ useSecurity = false;
71+ }
72+ }
73+ public static boolean useSecurity() {
74+ return useSecurity;
75+ }
76+
77+ public static void setUseCompress(String alg) {
78+ useCompress = alg;
79+ }
80+ public static String getUseCompress() {
81+ return useCompress;
82+ }
83+
5384 public void run() {
5485 LogWriter log = new LogWriter(httpServerLogPath);
5586 TcpServer server = new TcpServer();
--- branch/Basic 0.2x/src/servercore/http/HttpResponse.java (revision 63)
+++ branch/Basic 0.2x/src/servercore/http/HttpResponse.java (revision 64)
@@ -8,6 +8,7 @@
88 private long lastModified = 0;
99 private long contentLength = 0;
1010 private HashMap<String, String> header = null;
11+ private boolean IsGzip = false;
1112 public byte[] getResponseByte() {
1213 return this.responseByte;
1314 }
@@ -51,4 +52,10 @@
5152 }
5253 header.put(key, value);
5354 }
55+ public boolean isGzip() {
56+ return IsGzip;
57+ }
58+ public void isThisGzip(boolean flg) {
59+ IsGzip = flg;
60+ }
5461 }
--- branch/Basic 0.2x/src/event/CUIStart.java (revision 63)
+++ branch/Basic 0.2x/src/event/CUIStart.java (revision 64)
@@ -8,6 +8,8 @@
88 EasyIni ini = new EasyIni("./config/web_server.ini");
99 HttpServer.setRecvPort(ini.getPro("port"));
1010 HttpServer.setRootDirPath(ini.getPro("irDir"));
11+ HttpServer.setUseSecurity(ini.getPro("userSecury"));
12+ HttpServer.setUseCompress(ini.getPro("compress"));
1113 HttpServer server = new HttpServer();
1214 server.start();
1315
Show on old repository browser