Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>com.kttdevelopment</groupId>
<artifactId>simplehttpserver</artifactId>
<version>4.3.0</version>
<version>4.3.1</version>

<name>simplehttpserver</name>
<description>📕 SimpleHttpServer :: Simplified implementation of the sun http server :: Simplified handlers to execute complex operations</description>
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/com/kttdevelopment/simplehttpserver/ContextUtil.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package com.kttdevelopment.simplehttpserver;

import java.util.regex.Pattern;

/**
* A utility class used to generate uniform contexts. Applications do not use this class.
*
* @since 03.05.03
* @version 03.05.03
* @version 4.3.1
* @author Ktt Development
*/
public abstract class ContextUtil {

// replace consecutive slashes and back slashes with a forward slash
private static final Pattern forwardSlashRegex = Pattern.compile("/{2,}|\\\\+");
// remove start and end slashes as well as whitespace
private static final Pattern stripSlashRegex = Pattern.compile("^\\s*/*|/*\\s*$");

/**
* Generates a uniform context with forward slashes removing any consecutive slashes.
*
Expand All @@ -22,11 +29,11 @@ public abstract class ContextUtil {
* @author Ktt Development
*/
public static String getContext(final String context, final boolean leadingSlash, final boolean trailingSlash){
final String linSlash = context.replace('\\','/').replaceAll("/{2,}","/");
if(linSlash.isBlank() || linSlash.equals("/")) // handle blank or '/' contexts
return leadingSlash || trailingSlash ? "/" : "";
final String ltSlash = (!(linSlash.charAt(0) == '/') ? '/' : "") + linSlash + (!(linSlash.charAt(linSlash.length()-1) == '/') ? '/' : "");
return ltSlash.substring(leadingSlash ? 0 : 1, ltSlash.length() + (trailingSlash ? 0 : -1));
final String linSlash = forwardSlashRegex.matcher(context).replaceAll("/");
final String strippedSlash = stripSlashRegex.matcher(linSlash).replaceAll("");
return strippedSlash.length() == 0
? leadingSlash || trailingSlash ? "/" : ""
: (leadingSlash ? "/" : "") + strippedSlash + (trailingSlash ? "/" : "");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ public final void testContexts(){
new test("testNoneBackSlash" , "\\testNoneBackSlash\\" , false , false),
new test("/testBackSlash/" , "\\testBackSlash\\" , true , true ),
new test("/testConsecutiveBackSlash/" , "\\\\testConsecutiveBackSlash\\\\", true , true ),
new test("/testConsecutiveForwardSlash/" , "//testConsecutiveForwardSlash//" , true , true )
new test("/testConsecutiveForwardSlash/" , "//testConsecutiveForwardSlash//" , true , true ),
new test("/testWhitespace/" , " /testWhitespace/ " , true , true),
new test("/ testWhitespace /" , "/ testWhitespace /" , true , true),
new test(" testWhitespace " , "/ testWhitespace /" , false , false),
new test("testWhitespace" , " testWhitespace " , false , false),
new test("/testWhitespace/" , " /testWhitespace/ " , true , true),
};

for(final test test : tests)
Expand All @@ -66,24 +71,24 @@ public testJoin(final String expected, final boolean leadingSlash, final boolean
@Test
public final void testJoin(){
final testJoin[] tests = {
new testJoin("testBlank" , false , false ,"testBlank",""),
new testJoin("/testBlank/" , true , true ,"testBlank",""),
new testJoin("testBlank" , false , false ,"","testBlank"),
new testJoin("/testBlank/" , true , true ,"","testBlank"),
new testJoin("" , false , false ,"",""),
new testJoin("/" , true , true ,"",""),
new testJoin("trailing/slash" , false , false ,"trailing/","slash/"),
new testJoin("/trailing/slash/" , true , true ,"trailing/","slash/"),
new testJoin("leading/slash" , false , false ,"leading/","slash/"),
new testJoin("/leading/slash/" , true , true ,"leading/","slash/"),
new testJoin("double/slash" , false , false ,"/double/","/slash/"),
new testJoin("/double/slash/" , true , true ,"/double/","/slash/"),
new testJoin("no/slash" , false , false ,"no","slash"),
new testJoin("/no/slash/" , true , true ,"no","slash"),
new testJoin("consecutive/slash" , false , false ,"//consecutive//","//slash//"),
new testJoin("/consecutive/slash/" , true , true ,"//consecutive//","//slash//"),
new testJoin("mixed/slash" , false , false ,"\\mixed\\","//slash//"),
new testJoin("/mixed/slash/" , true , true ,"\\mixed\\","//slash//"),
new testJoin("testBlank" , false , false , "testBlank" , ""),
new testJoin("/testBlank/" , true , true , "testBlank" , ""),
new testJoin("testBlank" , false , false , "" , "testBlank"),
new testJoin("/testBlank/" , true , true , "" , "testBlank"),
new testJoin("" , false , false , "" , ""),
new testJoin("/" , true , true , "" , ""),
new testJoin("trailing/slash" , false , false , "trailing/" , "slash/"),
new testJoin("/trailing/slash/" , true , true , "trailing/" , "slash/"),
new testJoin("leading/slash" , false , false , "leading/" , "slash/"),
new testJoin("/leading/slash/" , true , true , "leading/" , "slash/"),
new testJoin("double/slash" , false , false , "/double/" , "/slash/"),
new testJoin("/double/slash/" , true , true , "/double/" , "/slash/"),
new testJoin("no/slash" , false , false , "no" , "slash"),
new testJoin("/no/slash/" , true , true , "no" , "slash"),
new testJoin("consecutive/slash" , false , false , "//consecutive//" , "//slash//"),
new testJoin("/consecutive/slash/" , true , true , "//consecutive//" , "//slash//"),
new testJoin("mixed/slash" , false , false , "\\mixed\\" , "//slash//"),
new testJoin("/mixed/slash/" , true , true , "\\mixed\\" , "//slash//"),
};

for(final testJoin test : tests)
Expand Down