Class MapLocal

java.lang.Object
org.microproxy.extras.MapLocal
All Implemented Interfaces:
HttpFiltersSource

public final class MapLocal extends Object implements HttpFiltersSource
Answers requests from local files, as mitmproxy's map_local. A rule maps URLs matching a regular expression to a file, served for every match, or to a directory, where the part of the URL after the match (or the regex's first group) names the file:

 MapLocal.of("|example.com/static/|/srv/static",        // example.com/static/app.js -> /srv/static/app.js
             "|~m GET|example.com/favicon.ico|/srv/icon.ico")
 

Specs are [|flow-filter]|url-regex|file-or-directory, the separator being the first character; the path must exist. For a directory, the URL's remaining path (without its query) is percent-decoded, and name, then name/index.html are tried, then the same with characters outside [0-9a-zA-Z-_.=(),/] replaced by _; an empty remainder serves index.html. The response is 200 with a Content-Type guessed from the file name. When a rule matched but none of its candidates exists, the answer is 404. Paths that would leave the directory (.., absolute parts, or symbolic links pointing outside) are never served, and get 404 too. Files are read whole for each response.

  • Method Details

    • of

      public static MapLocal of(String... specs)
      Map local rules from specs.
      Parameters:
      specs - specs of the form [|flow-filter]|url-regex|file-or-directory
      Returns:
      the addon
      Throws:
      IllegalArgumentException - if a spec is invalid or its path does not exist
    • builder

      public static MapLocal.Builder builder()
      Starts a builder with no rules.
      Returns:
      a new builder
    • rules

      public List<MapLocal.Rule> rules()
      Returns the rules, in order.
      Returns:
      the rules, in order
    • filterRequest

      public HttpFilters filterRequest(HttpRequest originalRequest, FlowContext flowContext)
      Description copied from interface: HttpFiltersSource
      Returns the filters for a request, or null to proxy it unfiltered.
      Specified by:
      filterRequest in interface HttpFiltersSource
      Parameters:
      originalRequest - a copy of the request as received from the client
      flowContext - the client connection the request arrived on
      Returns:
      the per-request filters, or null to leave the request unfiltered