Skip to content

Commit

Permalink
Exception when websocket closes by client.
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-smith committed Sep 9, 2023
1 parent 6584e09 commit 3a644df
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.sshtools</groupId>
<artifactId>uhttpd</artifactId>
<version>0.9.3</version>
<version>0.9.4-SNAPSHOT</version>
<name>µTTPD - A very small HTTP/HTTPS server</name>
<properties>
<maven.compiler.target>11</maven.compiler.target>
Expand Down
60 changes: 51 additions & 9 deletions src/main/java/com/sshtools/uhttpd/UHTTPD.java
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,19 @@ default Iterable<Part> asParts() {
*/
Optional<String> contentType();

/**
* A convenience method to get if a part that is a piece of {@link FormData} exists.
* <p>
* This cannot be used if the content has already been retrieved as a stream or
* parts.
*
* @param name name
* @return form data.
*/
default boolean hasFormData(String name) {
return ofFormData(name).isPresent();
}

/**
* A convenience method to get a part that is a piece of {@link FormData} given
* it's name.
Expand Down Expand Up @@ -546,6 +559,20 @@ default Optional<Named> ofNamed(String name) {
return ofPart(name, Named.class);
}

/**
* A convenience method to get if a part exists.
* <p>
* This cannot be used if the content has already been retrieved as a stream or
* parts.
*
* @param name name
* @param clazz type of part
* @return form data.
*/
default <P extends Part> boolean hasPart(String name, Class<P> clazz) {
return ofPart(name, clazz).isPresent();
}

/**
* Get a part given it's name and class.
* <p>
Expand Down Expand Up @@ -1652,7 +1679,7 @@ default int asInt() {
}

default Optional<Integer> ofInt() {
return ofString().map(v -> Integer.parseInt(v));
return ofString().map(v -> v.equals("") ? null : Integer.parseInt(v));
}

default long asLong() {
Expand Down Expand Up @@ -3555,8 +3582,14 @@ public void close() throws IOException {
buf.flip();
calcChunkingAndClose(tx);
respondWithHeaders(tx);
nioChan.write(buf);
nioChan.close();
try {
nioChan.write(buf);
}
catch(IOException ioe) {
}
finally {
nioChan.close();
}
}
}

Expand Down Expand Up @@ -4951,7 +4984,7 @@ public void get(Transaction req) throws Exception {
}
}
req.responseLength(conx.getContentLengthLong());
req.responseType(conx.getContentType());
req.responseType(bestMimeType(urlToFilename(url), conx.getContentType()));
req.header(HDR_LAST_MODIFIED, formatDate(lastMod));

req.response(url.openStream());
Expand Down Expand Up @@ -5183,27 +5216,36 @@ public static HttpBasicAuthenticationBuilder httpBasicAuthentication(
return new HttpBasicAuthenticationBuilder(authenticator);
}

public static String mimeType(String fileName) {
return URLConnection.guessContentTypeFromName(fileName);
public static String bestMimeType(String fileName, String detectedType) {
if(detectedType == null || detectedType.isEmpty() || detectedType.equalsIgnoreCase("content/unknown")) {
System.out.println(MessageFormat.format("Getting mime type for file {0}", fileName));
var type = URLConnection.guessContentTypeFromName(fileName);
System.out.println(MessageFormat.format(" Content type is {0}", type));
return type;
}
return detectedType;
}

public static String mimeType(URL url) {
try {
URLConnection conx = url.openConnection();
try {
String contentType = conx.getContentType();
return contentType;
return bestMimeType(urlToFilename(url), conx.getContentType());
} finally {
try {
conx.getInputStream().close();
} catch (IOException ioe) {
}
}
} catch (IOException ioe) {
return mimeType(Paths.get(url.getPath()).getFileName().toString());
return bestMimeType(urlToFilename(url), null);
}
}

public static String urlToFilename(URL url) {
return Paths.get(url.getPath()).getFileName().toString();
}

public static RootContextBuilder server() {
return new RootContextBuilder();
}
Expand Down

0 comments on commit 3a644df

Please sign in to comment.