Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Added Test Cases #75

Merged
merged 30 commits into from
Jul 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ src/LICENSE.txt
.gradle
# Maven
target
/test/
28 changes: 26 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.kttdevelopment</groupId>
<artifactId>simplehttpserver</artifactId>
<version>03.05.01</version>
<version>03.05.02</version>
<packaging>jar</packaging>

<url>https://github.com/Ktt-Development/simplehttpserver</url>
Expand All @@ -17,7 +17,11 @@
<developerConnection>scm:git:[email protected]:Ktt-Development/simplehttpserver.git</developerConnection>
<tag>HEAD</tag>
</scm>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -57,7 +61,27 @@
</execution>
</executions>
</plugin>

<!-- tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<useFile>false</useFile>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- tests -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ static SimpleHttpExchange create(final HttpExchange exchange){

headerMap.put("header-name", headerMatcher.group(1));
headerMap.put("header-value", headerMatcher.group(2));
headerMap.put("parameter", val);
headerMap.put("parameters", val);
}
postHeaders.put((String) headerMap.get("header-name"), headerMap);
}
Expand All @@ -170,7 +170,7 @@ static SimpleHttpExchange create(final HttpExchange exchange){
row.put("value",pair.substring(pair.indexOf("\r\n\r\n")+4,pair.lastIndexOf("\r\n")));

postMap.put(
((HashMap<String, String>) postHeaders.get("Content-Disposition").get("parameter")).get("name"),
((HashMap<String, String>) postHeaders.get("Content-Disposition").get("parameters")).get("name"),
row
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @see SimpleHttpsServer
* @see SimpleHttpHandler
* @since 02.00.00
* @version 03.05.01
* @version 03.05.02
* @author Ktt Development
*/
@SuppressWarnings("SpellCheckingInspection")
Expand Down Expand Up @@ -179,6 +179,7 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
* Returns the address that the server is binded to.
*
* @return binded address
* @throws NullPointerException if server is not binded to a port
*
* @see InetSocketAddress
* @since 02.00.00
Expand Down Expand Up @@ -421,10 +422,10 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
public abstract String getRandomContext();

/**
* Generates a randome context that can be used on the server.
* Generates a random context that can be used on the server.
*
* @param context where to generate the random context
* @return randome safe context
* @return random safe context
*
* @see #getRandomContext()
* @see com.kttdevelopment.simplehttpserver.handler.TemporaryHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @see SimpleHttpServer
* @since 02.00.00
* @version 03.05.00
* @version 03.05.02
* @author Ktt Development
*/
@SuppressWarnings("SpellCheckingInspection")
Expand Down Expand Up @@ -168,11 +168,16 @@ public synchronized final HttpContext createContext(final String path, final Htt

@Override
public synchronized final void removeContext(final String path){
server.removeContext(getContext(path));
for(final HttpContext context : contexts.keySet()){
if(context.getPath().equalsIgnoreCase(getContext(path))){
contexts.remove(context);
break;
try{
server.removeContext(getContext(path));
}catch(final IllegalArgumentException e){
throw e;
}finally{
for(final HttpContext context : contexts.keySet()){
if(context.getPath().equalsIgnoreCase(getContext(path))){
contexts.remove(context);
break;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public final byte[] getBytes(){
else
try{
return adapter.getBytes(file,Files.readAllBytes(file.toPath())); // read and adapt bytes
}catch(final IOException ignored){
}catch(final IOException e){
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

/**
* A temporary handler handles a single request and then removes itself from the server. This can be used for single use downloads, or media file hosting.
* For time based expiry the handler will only remove itself if a user connects to the handler after the time expires.
*
* @see HttpHandler
* @see SimpleHttpServer#getRandomContext()
* @see SimpleHttpServer#getRandomContext(String)
* @since 03.03.00
* @version 03.05.00
* @version 03.05.02
* @author Ktt Development
*/
public class TemporaryHandler implements HttpHandler {

private SimpleHttpServer server = null;
private final HttpHandler handler;

private boolean hasExpiry;
Expand All @@ -30,23 +32,57 @@ public class TemporaryHandler implements HttpHandler {
*
* @param handler handler to use
*
* @see HttpHandler
* @since 03.03.00
* @author Ktt Development
*/
public TemporaryHandler(final HttpHandler handler){
this.handler = handler;
}

/**
* Creates a temporary handler that removes itself after the first connection.
*
* @param server simple http server; required if you want {@link SimpleHttpServer#getContexts()} to work properly
* @param handler handler to use
*
* @see SimpleHttpServer
* @see HttpHandler
* @since 03.05.02
*/
public TemporaryHandler(final SimpleHttpServer server, final HttpHandler handler){
this.server = server;
this.handler = handler;
}

/**
* Creates a temporary handler that removes itself after the first connection, or after the time expires.
*
* @param handler handler to use
* @param maxTime how long the handler may exists for in milliseconds
*
* @see HttpHandler
* @since 03.03.00
* @author Ktt Development
*/
public TemporaryHandler(final HttpHandler handler, final long maxTime){
this(null,handler,maxTime);
}

/**
* Creates a temporary handler that removes itself after the first connection, or after the time expires.
*
* @param server simple http server; required if you want {@link SimpleHttpServer#getContexts()} to work properly
* @param handler handler to use
* @param maxTime how long the handler may exists for in milliseconds
*
* @see SimpleHttpServer
* @see HttpHandler
* @since 03.05.02
* @author Ktt Development
*/
public TemporaryHandler(final SimpleHttpServer server, final HttpHandler handler, final long maxTime){
this.server = server;
this.handler = handler;

hasExpiry = true;
Expand All @@ -59,7 +95,10 @@ public TemporaryHandler(final HttpHandler handler, final long maxTime){
public final void handle(final HttpExchange exchange) throws IOException{
if(!hasExpiry || System.currentTimeMillis() < expiry)
handler.handle(exchange);
exchange.getHttpContext().getServer().removeContext(exchange.getHttpContext());
if(server == null)
exchange.getHttpContext().getServer().removeContext(exchange.getHttpContext());
else
server.removeContext(exchange.getHttpContext());
exchange.close();
}

Expand Down
Loading