package org.webbitserver.handler;

import org.webbitserver.HttpControl;
import org.webbitserver.HttpHandler;
import org.webbitserver.HttpRequest;
import org.webbitserver.HttpResponse;

import java.net.URI;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PathMatchHandler implements HttpHandler {

    private final Pattern pathPattern;
    private final HttpHandler httpHandler;

    public PathMatchHandler(Pattern pathPattern, HttpHandler httpHandler) {
        this.pathPattern = pathPattern;
        this.httpHandler = httpHandler;
    }

    public PathMatchHandler(String path, HttpHandler httpHandler) {
        this(Pattern.compile(path), httpHandler);
    }

    @Override
    public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
        String path = URI.create(request.uri()).getPath();
        Matcher matcher = pathPattern.matcher(path);
        if (matcher.matches()) {
            httpHandler.handleHttpRequest(request, response, control);
        } else {
            control.nextHandler();
        }
    }
}

@@ -823,24 +823,53 @@
 ption %7B%0A
-        
+%09%09%09Matcher matcher;%0A%0A%09%09%09try%0A%09%09%09%7B%0A%09%09%09%09
 String p
@@ -915,24 +915,12 @@
 ();%0A
-        Matcher 
+%09%09%09%09
 matc
@@ -956,19 +956,290 @@
 h);%0A
-        if 
+%09%09%09%7D%0A%09%09%09catch(Exception e)%0A%09%09%09%7B%0A%09%09%09%09response.status(400);%0A%09%09%09%09response.header(%22Content-Type%22, %22text/plain%22);%0A%09%09%09%09response.content(%22Internal server error. We are notified about the problem and will fix it. Sorry for any inconvenience.%22);%0A%09%09%09%09response.end();%0A%0A%09%09%09%09return;%0A%09%09%09%7D%0A%0A%09%09%09%09if
 (mat
@@ -1253,31 +1253,28 @@
 tches())
- %7B%0A            
+%0A%09%09%09%09%7B%0A%09%09%09%09%09
 httpHand
@@ -1328,37 +1328,34 @@
 l);%0A
-        %7D else %7B%0A            
+%09%09%09%09%7D%0A%09%09%09%09else%0A%09%09%09%09%7B%0A%09%09%09%09%09
 cont
@@ -1373,24 +1373,20 @@
 dler();%0A
-        
+%09%09%09%09
 %7D%0A    %7D%0A

Throw HTTP error 400 on misformerd URI.