Skip to content

Commit 1a8f666

Browse files
committed
Add https support
1 parent 42ca173 commit 1a8f666

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

src/express/Express.java

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.sun.istack.internal.NotNull;
44
import com.sun.net.httpserver.HttpServer;
5+
import com.sun.net.httpserver.HttpsConfigurator;
6+
import com.sun.net.httpserver.HttpsServer;
57
import express.events.Action;
68
import express.events.HttpRequest;
79
import express.expressfilter.ExpressFilterImpl;
@@ -34,6 +36,7 @@ public class Express extends ExpressMiddleware {
3436
private Executor executor;
3537
private String hostname;
3638
private HttpServer httpServer;
39+
private HttpsConfigurator httpsConfigurator;
3740

3841
{
3942
// Initialize
@@ -57,12 +60,40 @@ public Express(@NotNull String hostname) {
5760
this.hostname = hostname;
5861
}
5962

63+
/**
64+
* Default, will bind the server to "localhost"
65+
*
66+
* @param httpsConfigurator The HttpsConfigurator for https
67+
*/
68+
public Express(HttpsConfigurator httpsConfigurator) {
69+
this.httpsConfigurator = httpsConfigurator;
70+
}
71+
72+
/**
73+
* Create an express instance and bind the server to an hostname.
74+
* Default is "Localhost"
75+
*
76+
* @param hostname The host name
77+
* @param httpsConfigurator The HttpsConfigurator for https
78+
*/
79+
public Express(@NotNull String hostname, HttpsConfigurator httpsConfigurator) {
80+
this.hostname = hostname;
81+
this.httpsConfigurator = httpsConfigurator;
82+
}
83+
6084
/**
6185
* Default, will bind the server to "localhost"
6286
*/
6387
public Express() {
6488
}
6589

90+
/**
91+
* @return True if the server uses https.
92+
*/
93+
public boolean isSecure() {
94+
return httpsConfigurator != null;
95+
}
96+
6697
/**
6798
* Add an listener which will be called when an url with this parameter is called.
6899
*
@@ -163,6 +194,15 @@ public void all(@NotNull String context, @NotNull HttpRequest request) {
163194
HANDLER.add(1, new ExpressFilterImpl(this, "*", context, request));
164195
}
165196

197+
/**
198+
* Add an listener for all request methods and contexts.
199+
*
200+
* @param request Will be fired on all requests.
201+
*/
202+
public void all(@NotNull HttpRequest request) {
203+
HANDLER.add(1, new ExpressFilterImpl(this, "*", "*", request));
204+
}
205+
166206
/**
167207
* Add an listener for GET request's.
168208
*
@@ -283,8 +323,19 @@ public void listen(Action onStart, int port) throws IOException {
283323
// Fire worker threads
284324
WORKER.forEach(ExpressFilterWorker::start);
285325

286-
// Create http server
287-
httpServer = HttpServer.create(new InetSocketAddress(this.hostname, port), 0);
326+
InetSocketAddress socketAddress = new InetSocketAddress(this.hostname, port);
327+
328+
if (httpsConfigurator != null) {
329+
330+
// Create https server
331+
httpServer = HttpsServer.create(socketAddress, 0);
332+
((HttpsServer) httpServer).setHttpsConfigurator(httpsConfigurator);
333+
} else {
334+
335+
// Create http server
336+
httpServer = HttpServer.create(socketAddress, 0);
337+
}
338+
288339
httpServer.setExecutor(executor); // Set thread executor
289340
httpServer.createContext("/", HANDLER); // Set handler for all contexts
290341
httpServer.start(); // Start server

src/express/http/response/Response.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,17 @@ public String getContentType() {
129129
*
130130
* @param contentType - The contentType
131131
*/
132-
public void setContentType(String contentType) {
133-
this.contentType = contentType;
132+
public void setContentType(MediaType contentType) {
133+
this.contentType = contentType.getMIME();
134134
}
135135

136136
/**
137137
* Set the contentType for this response.
138138
*
139139
* @param contentType - The contentType
140140
*/
141-
public void setContentType(MediaType contentType) {
142-
this.contentType = contentType.getMIME();
141+
public void setContentType(String contentType) {
142+
this.contentType = contentType;
143143
}
144144

145145
/**

0 commit comments

Comments
 (0)