Class ServerReplay

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

public final class ServerReplay extends Object implements HttpFiltersSource
Answers requests from recorded responses instead of contacting servers, as mitmproxy's server_replay. Recordings come from HAR files (HarRecorder, browsers, mitmproxy) or WARC files (WarcRecorder):

 ServerReplay replay = ServerReplay.builder()
         .load(Path.of("flows.har"))
         .ignoreParams("_", "cachebust")
         .extra(ServerReplay.Extra.status(404))   // unmatched requests get 404 instead of the server
         .build();
 MicroProxy.bootstrap().withFiltersSource(replay).start();
 

Matching follows mitmproxy's _hash: a request matches a recording with the same scheme, method, path, body, host, port and query parameters (in order), each part left out with an option: ServerReplay.Builder.ignoreContent, ServerReplay.Builder.ignoreHost, ServerReplay.Builder.ignorePort, ServerReplay.Builder.ignoreParams, ServerReplay.Builder.ignorePayloadParams (for application/x-www-form-urlencoded bodies); ServerReplay.Builder.useHeaders adds request headers. Requests with a body are buffered to compare it (unless the body is ignored).

Use. Each recording answers once, in recorded order, so a sequence of identical requests gets the recorded sequence of responses; with ServerReplay.Builder.reuse every recording keeps answering. Requests with no recording left are forwarded to the server, or, with ServerReplay.Builder.extra, killed (the connection closed) or answered with a status. Replayed responses have their Date, Expires, Last-Modified and Set-Cookie expiry shifted by the time since they were recorded (ServerReplay.Builder.refresh, on by default), and reach trackers and logs as ResponseSource.REPLAY.

All recordings are held in memory. Exchanges without a response, and responses cut short when they were recorded, are not replayed.

  • Method Details

    • builder

      public static ServerReplay.Builder builder()
      Starts a builder with no recordings and mitmproxy's defaults.
      Returns:
      a new builder
    • remaining

      public int remaining()
      Returns how many recorded responses are left to replay (all of them, with reuse).
      Returns:
      how many recorded responses are left to replay (all of them, with reuse)
    • 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