forked from AnythingLinux/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleProxyResourceHandler.java
More file actions
181 lines (155 loc) · 6.5 KB
/
ConsoleProxyResourceHandler.java
File metadata and controls
181 lines (155 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.consoleproxy;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.cloud.consoleproxy.util.Logger;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
public class ConsoleProxyResourceHandler implements HttpHandler {
private static final Logger s_logger = Logger.getLogger(ConsoleProxyResourceHandler.class);
static Map<String, String> s_mimeTypes;
static {
s_mimeTypes = new HashMap<String, String>();
s_mimeTypes.put("jar", "application/java-archive");
s_mimeTypes.put("js", "text/javascript");
s_mimeTypes.put("css", "text/css");
s_mimeTypes.put("jpg", "image/jpeg");
s_mimeTypes.put("html", "text/html");
s_mimeTypes.put("htm", "text/html");
s_mimeTypes.put("log", "text/plain");
}
static Map<String, String> s_validResourceFolders;
static {
s_validResourceFolders = new HashMap<String, String>();
s_validResourceFolders.put("applet", "");
s_validResourceFolders.put("logs", "");
s_validResourceFolders.put("images", "");
s_validResourceFolders.put("js", "");
s_validResourceFolders.put("css", "");
s_validResourceFolders.put("html", "");
}
public ConsoleProxyResourceHandler() {
}
public void handle(HttpExchange t) throws IOException {
try {
if(s_logger.isDebugEnabled())
s_logger.debug("Resource Handler " + t.getRequestURI());
long startTick = System.currentTimeMillis();
doHandle(t);
if(s_logger.isDebugEnabled())
s_logger.debug(t.getRequestURI() + " Process time " + (System.currentTimeMillis() - startTick) + " ms");
} catch (IOException e) {
throw e;
} catch(Throwable e) {
s_logger.error("Unexpected exception, ", e);
t.sendResponseHeaders(500, -1); // server error
} finally {
t.close();
}
}
@SuppressWarnings("deprecation")
private void doHandle(HttpExchange t) throws Exception {
String path = t.getRequestURI().getPath();
if(s_logger.isInfoEnabled())
s_logger.info("Get resource request for " + path);
int i = path.indexOf("/", 1);
String filepath = path.substring(i + 1);
i = path.lastIndexOf(".");
String extension = (i == -1) ? "" : path.substring(i + 1);
String contentType = getContentType(extension);
if(!validatePath(filepath)) {
if(s_logger.isInfoEnabled())
s_logger.info("Resource access is forbidden, uri: " + path);
t.sendResponseHeaders(403, -1); // forbidden
return;
}
File f = new File ("./" + filepath);
if(f.exists()) {
long lastModified = f.lastModified();
String ifModifiedSince = t.getRequestHeaders().getFirst("If-Modified-Since");
if (ifModifiedSince != null) {
long d = Date.parse(ifModifiedSince);
if (d + 1000 >= lastModified) {
Headers hds = t.getResponseHeaders();
hds.set("Content-Type", contentType);
t.sendResponseHeaders(304, -1);
if(s_logger.isInfoEnabled())
s_logger.info("Sent 304 file has not been " +
"modified since " + ifModifiedSince);
return;
}
}
long length = f.length();
Headers hds = t.getResponseHeaders();
hds.set("Content-Type", contentType);
hds.set("Last-Modified", new Date(lastModified).toGMTString());
t.sendResponseHeaders(200, length);
responseFileContent(t, f);
if(s_logger.isInfoEnabled())
s_logger.info("Sent file " + path + " with content type " + contentType);
} else {
if(s_logger.isInfoEnabled())
s_logger.info("file does not exist" + path);
t.sendResponseHeaders(404, -1);
}
}
private static String getContentType(String extension) {
String key = extension.toLowerCase();
if(s_mimeTypes.containsKey(key)) {
return s_mimeTypes.get(key);
}
return "application/octet-stream";
}
private static void responseFileContent(HttpExchange t, File f) throws Exception {
OutputStream os = t.getResponseBody();
FileInputStream fis = new FileInputStream(f);
while (true) {
byte[] b = new byte[8192];
int n = fis.read(b);
if (n < 0) {
break;
}
os.write(b, 0, n);
}
fis.close();
os.close();
}
private static boolean validatePath(String path) {
int i = path.indexOf("/");
if(i == -1) {
if(s_logger.isInfoEnabled())
s_logger.info("Invalid resource path: can not start at resource root");
return false;
}
if(path.contains("..")) {
if(s_logger.isInfoEnabled())
s_logger.info("Invalid resource path: contains relative up-level navigation");
return false;
}
return isValidResourceFolder(path.substring(0, i));
}
private static boolean isValidResourceFolder(String name) {
return s_validResourceFolders.containsKey(name);
}
}