Skip to content

Commit

Permalink
Update README - v0.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwep committed Mar 30, 2018
1 parent 049f80e commit 0d12b34
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

**This project is currently in progress, feel free to [contribute](https://github.com/Simonwep/java-express/graphs/contributors) / [report](https://github.com/Simonwep/java-express/issues) issues! :)**

**[0.0.7-alpha](https://github.com/Simonwep/java-express/releases/tag/0.0.7) is ready, check it out!**
**[0.0.8-alpha](https://github.com/Simonwep/java-express/releases/tag/0.0.8) is ready, check it out!**

```java
Express app = new Express();
Expand Down Expand Up @@ -242,6 +242,7 @@ res.setContentType(String type); // Set the content type
res.isClosed(); // Check if the response is already closed
res.getHeader(String key); // Get the value from an header field via key
res.setHeader(String key, String val); // Add an specific response header
res.sendAttachment(Path file) // Sends an file as attachment
res.send(String str); // Send an string as response
res.send(Path path); // Send an file as response
res.send(); // Send empty response
Expand Down Expand Up @@ -277,6 +278,11 @@ req.getCookies(); // Returns all cookies
req.getIp(); // Returns the client IP-Address
req.getUserAgent(); // Returns the client user agent
req.getURI(); // Returns the request URI
req.isFresh(); // Returns true if the connection is fresh, false otherwise (see code inline-doc)
req.isStale(); // Returns the opposite of req.fresh;
req.isSecure(); // Returns true when the connection is over HTTPS, false otherwise
req.isXHR(); // Returns true if the 'X-Requested-With' header field is 'XMLHttpRequest'
req.getProtocol(); // Returns the connection protocol
req.getAuthorization(); // Returns the request authorization
req.hasAuthorization(); // Check if the request has an authorization
req.pipe(OutputStream stream, int buffersize); // Pipe the request body to an outputstream
Expand Down
92 changes: 91 additions & 1 deletion src/express/http/request/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpsExchange;
import express.Express;
import express.filter.Filter;
import express.http.Cookie;
Expand All @@ -15,6 +16,8 @@
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

Expand All @@ -27,9 +30,11 @@ public class Request {

private final Express EXPRESS;

private final String PROTOCOL; // Request protocoll
private final URI URI; // Request URI
private final InputStream BODY; // Request body
private final Headers HEADERS; // Request Headers
private final boolean SECURE;
private final String CONTENT_TYPE; // Request content-type
private final long CONTENT_LENGTH; // Request content-length
private final String METHOD; // Request method
Expand All @@ -56,6 +61,9 @@ public Request(HttpExchange exchange, Express express) {
this.BODY = exchange.getRequestBody();
this.INET = exchange.getRemoteAddress();

this.PROTOCOL = exchange.getProtocol();
this.SECURE = exchange instanceof HttpsExchange; // Can be suckered?

// Parse content length
String contentLength = HEADERS.get("Content-Length") != null ? HEADERS.get("Content-Length").get(0) : null;
this.CONTENT_LENGTH = contentLength != null ? Long.parseLong(contentLength) : -1;
Expand Down Expand Up @@ -223,6 +231,89 @@ public String getMethod() {
return this.METHOD;
}

/**
* Checks if the connection is 'fresh'
* It is true if the cache-control request header doesn’t have a no-cache directive, the if-modified-since request header is specified
* and last-modified request header is equal to or earlier than the modified response header or if-none-match request header is *.
*
* @return True if the connection is fresh, false otherwise.
*/
public boolean isFresh() {

if (HEADERS.containsKey("cache-control") && HEADERS.get("cache-control").get(0) != null && HEADERS.get("cache-control").get(0).equals("no-cache"))
return true;

if (HEADERS.containsKey("if-none-match") && HEADERS.get("if-none-match").get(0) != null && HEADERS.get("if-none-match").equals("*"))
return true;

if (HEADERS.containsKey("if-modified-since") && HEADERS.containsKey("last-modified") && HEADERS.containsKey("modified")) {
List<String> lmlist = HEADERS.get("last-modified");
List<String> mlist = HEADERS.get("modified");

// Check lists
if (lmlist.isEmpty() || mlist.isEmpty())
return false;

String lm = lmlist.get(0);
String m = mlist.get(0);

// Check header
if (lm != null && m != null) {
try {

// Try to convert it
Instant lmi = Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(lm));
Instant mi = Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(m));

if (lmi.isBefore(mi) || lmi.equals(mi)) {
return true;
}

} catch (Exception ignored) {
}
}
}

return false;
}

/**
* Indicates whether the request is “stale,” and is the opposite of req.fresh
*
* @return The opposite of req.fresh;
*/
public boolean isStale() {
return !isFresh();
}

/**
* Returns whenever the connection is over HTTPS.
*
* @return True when the connection is over HTTPS, false otherwise.
*/
public boolean isSecure() {
return SECURE;
}

/**
* Returns true if the 'X-Requested-With' header field is 'XMLHttpRequest'.
* Indicating that the request was made by a client library such as jQuery.
*
* @return True if the 'X-Requested-With' header field is 'XMLHttpRequest'.
*/
public boolean isXHR() {
return HEADERS.containsKey("X-Requested-With") && !HEADERS.get("X-Requested-With").isEmpty() && HEADERS.get("X-Requested-With").get(0).equals("XMLHttpRequest");
}

/**
* The connection protocol HTTP/1.0, HTTP/1.1 etc.
*
* @return The connection protocol.
*/
public String getProtocol() {
return PROTOCOL;
}

/**
* If there is an Authorization header, it was parsed and saved
* in a Authorization Object.
Expand Down Expand Up @@ -321,5 +412,4 @@ public Express getApp() {
return EXPRESS;
}


}
17 changes: 17 additions & 0 deletions src/express/http/response/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,23 @@ public void send(String s) {
close();
}

/**
* Sets the 'Content-Disposition' header to 'attachment' and his
* Content-Disposition "filename=" parameter to the file name.
* Normally this triggers an download event client-side.
*
* @param file The file which will be send as attachment.
*/
public void sendAttachment(@NotNull Path file) {
if (isClosed() || !Files.isRegularFile(file))
return;

String dispo = "attachment; filename=\"" + file.getFileName() + "\"";
setHeader("Content-Disposition", dispo);

send(file);
}

/**
* Send an entire file as response
* The mime type will be automatically detected.
Expand Down

0 comments on commit 0d12b34

Please sign in to comment.