Permalink
Browse files

Merge branch 'master' of github.com:vert-x/mod-web-server

  • Loading branch information...
2 parents ebc64ca + c0cf79c commit d69806690b18248facc6f1280d91df087734e4a0 @purplefox purplefox committed Aug 2, 2012
Showing with 37 additions and 14 deletions.
  1. +13 −11 README.md
  2. +24 −3 src/main/java/org/vertx/mods/WebServer.java
View
@@ -18,24 +18,26 @@ The web-server configuration is as follows:
"host", <host>,
"port", <port>
"static_files": <static_files>,
-
- "ssl": <ssl>,
+ "gzip_files": <gzip_files>
+
+ "ssl": <ssl>,
"key_store_password": <key_store_password>,
"key_store_path": <key_store_path>,
-
+
"bridge": <bridge>,
"inbound_permitted": <inbound_permitted>,
"outbound_permitted": <outbound_permitted>,
"sjs_config": <sjs_config>,
"auth_timeout": <auth_timeout>,
"auth_address": <auth_address>
}
-
+
* `web_root`. This is the root directory from where files will be served. *Anything that you place here or in sub directories will be externally accessible*. Default is `web`.
* `index_page`. The page to serve when the root `/` is requested. Default is `index.html`.
* `host`. The host or ip address to listen at for connections. `0.0.0.0` means listen at all available addresses. Default is `0.0.0.0`.
* `port`. The port to listen at for connections. Default is `80`.
-* `static_files`. Should the server serve static files? Default is `true`.
+* `static_files`. Should the server serve static files? Default is `true`.
+* `gzip_files`. Should the server serve pre-compressed files? `true`: check file "fileName.gz", and send it back, if not found, then fallback to "fileName". `false`: send back "fileName", do not check "fileName.gz" unnecessarily. Default is `false`.
* `ssl`. Should the server use `https` as a protocol? Default is `false`.
* `key_store_password`. Password of Java keystore which holds the server certificate. Only used if `ssl` is `true`. Default is `wibble`.
* `key_store_path`. Path to keystore which holds the server certificate. Default is `server-keystore.jks`. Only used if `ssl` is `true`. *Don't put the keystore under your webroot!*.
@@ -44,7 +46,7 @@ The web-server configuration is as follows:
* `outbound_permitted`. This is an array of JSON objects representing the outbound permitted matches on the bridge. Only used if `bridge` is `true`. See the core manual for a full description of what these are. Defaults to `[]`.
* `sjs_config`. This is a JSON object representing the configuration of the SockJS bridging application. You'd normally use this for specifying the url at which SockJS will connect to bridge from client side JS to the server. Only used if `bridge` is `true`. Default to `{"prefix": "/eventbus"}`.
* `auth_timeout`. The bridge can also cache authorisations. This determines how long the bridge will cache one for. Default value is five minutes.
-* `auth_address`. The bridge can also call an authorisation manager to do authorisation. This is the address to which it will send authorisation messages. Default value is `vertx.basicauthmanager.authorise`.
+* `auth_address`. The bridge can also call an authorisation manager to do authorisation. This is the address to which it will send authorisation messages. Default value is `vertx.basicauthmanager.authorise`.
## Examples
@@ -53,21 +55,21 @@ Here are some examples:
### Simple static file web server
-This serves files from the web directory
+This serves files from the web directory
{
- "host": mycompany.com
+ "host": mycompany.com
{
-
+
### Simple https server
{
"host": mycompany.com,
"ssl": true,
"key_store_path": "mycert.jks",
"key_store_password": "sausages"
- }
-
+ }
+
### Event bus bridge
Pure event bus bridge that doesn't serve static files
@@ -18,6 +18,7 @@
import org.vertx.java.busmods.BusModBase;
import org.vertx.java.core.Handler;
+import org.vertx.java.core.file.impl.PathAdjuster;
import org.vertx.java.core.http.HttpServer;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.json.JsonArray;
@@ -40,6 +41,7 @@
private String webRootPrefix;
private String indexPage;
+ private boolean gzipFiles;
public void start() {
super.start();
@@ -67,6 +69,7 @@ public void start() {
getOptionalStringConfig("auth_address", "vertx.basicauthmanager.authorise"));
}
+ gzipFiles = getOptionalBooleanConfig("gzip_files", false);
String webRoot = getOptionalStringConfig("web_root", "web");
String index = getOptionalStringConfig("index_page", "index.html");
webRootPrefix = webRoot + File.separator;
@@ -76,14 +79,32 @@ public void start() {
}
public void handle(HttpServerRequest req) {
+ // browser gzip capability check
+ String acceptEncoding = req.headers().get("accept-encoding");
+ boolean acceptEncodingGzip = acceptEncoding == null ? false : acceptEncoding.contains("gzip");
+
+ String fileName = webRootPrefix + req.path;
if (req.path.equals("/")) {
req.response.sendFile(indexPage);
- } else if (!req.path.contains("src")) {
- req.response.sendFile(webRootPrefix + req.path);
+ } else if (!req.path.contains("..")) {
+ // try to send *.gz file
+ if (gzipFiles && acceptEncodingGzip) {
+ File file = new File(PathAdjuster.adjust(fileName + ".gz"));
+ if (file.exists()) {
+ // found file with gz extension
+ req.response.putHeader("content-encoding", "gzip");
+ req.response.sendFile(fileName + ".gz");
+ } else {
+ // not found gz file, try to send uncompressed file
+ req.response.sendFile(fileName);
+ }
+ } else {
+ // send not gzip file
+ req.response.sendFile(fileName);
+ }
} else {
req.response.statusCode = 404;
req.response.end();
}
}
}
-

0 comments on commit d698066

Please sign in to comment.