Class ModifyBody

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

public final class ModifyBody extends Object implements HttpFiltersSource
Replaces text in request and response bodies, as mitmproxy's modify_body: every match of a regular expression in the decoded body becomes the replacement, in the messages a rule's filter selects. Request bodies are edited before the request goes on, response bodies before the response reaches the client.

 ModifyBody.of("|~bs Example|Example Domain|Rewritten Domain",   // responses mentioning Example
               "|~q & ~d api.example.com|\"debug\":false|\"debug\":true")   // request bodies
 

Specs are [|flow-filter]|regex|[@]replacement, the separator being the first character. The regex runs over the body's bytes (. matches line ends too), and the replacement is inserted literally, with Python's backslash escapes decoded; @path reads it from a file. Without a filter a rule edits requests and responses alike; ~q limits it to requests (no response yet) and ~s to responses.

Only messages a rule might select are buffered: the decision is made from the heads, and a filter that also looks at bodies (~b) gets the body buffered and then checked. Messages whose codings cannot be decoded and event streams are never buffered. A response body larger than ModifyBody.Builder.maxBodySize streams through unchanged; a request body that large is answered with 413, as for any filter that buffers requests. The proxy narrows Accept-Encoding to codings it can decode, re-applies the body's coding after editing (zstd becomes gzip) and fixes Content-Length.

  • Method Details

    • of

      public static ModifyBody of(String... specs)
      Body rules from specs.
      Parameters:
      specs - specs of the form [|flow-filter]|regex|[@]replacement
      Returns:
      the addon
      Throws:
      IllegalArgumentException - if a spec is invalid, or names a file that cannot be read
    • builder

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

      public List<ModifyBody.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