Class DefaultHttpProxyServer

java.lang.Object
org.littleshoot.proxy.impl.DefaultHttpProxyServer
All Implemented Interfaces:
HttpProxyServer

public class DefaultHttpProxyServer extends Object implements HttpProxyServer

Primary implementation of an HttpProxyServer.

DefaultHttpProxyServer is bootstrapped by calling bootstrap() or bootstrapFromFile(String), and then calling DefaultHttpProxyServer.DefaultHttpProxyServerBootstrap.start(). For example:

 DefaultHttpProxyServer server =
         DefaultHttpProxyServer
                 .bootstrap()
                 .withPort(8090)
                 .start();
 
  • Field Details

    • LOG

      private static final org.slf4j.Logger LOG
    • TRAFFIC_SHAPING_CHECK_INTERVAL_MS

      private static final long TRAFFIC_SHAPING_CHECK_INTERVAL_MS
      The interval in ms at which the GlobalTrafficShapingHandler will run to compute and throttle the proxy-to-server bandwidth.
      See Also:
    • MAX_INITIAL_LINE_LENGTH_DEFAULT

      private static final int MAX_INITIAL_LINE_LENGTH_DEFAULT
      See Also:
    • MAX_HEADER_SIZE_DEFAULT

      private static final int MAX_HEADER_SIZE_DEFAULT
      See Also:
    • MAX_CHUNK_SIZE_DEFAULT

      private static final int MAX_CHUNK_SIZE_DEFAULT
      See Also:
    • FALLBACK_PROXY_ALIAS

      private static final String FALLBACK_PROXY_ALIAS
      The proxy alias to use in the Via header if no explicit proxy alias is specified and the hostname of the local machine cannot be resolved.
      See Also:
    • serverGroup

      private final ServerGroup serverGroup
      Our ServerGroup. Multiple proxy servers can share the same ServerGroup in order to reuse threads and other such resources.
    • transportProtocol

      private final TransportProtocol transportProtocol
    • requestedAddress

      private final InetSocketAddress requestedAddress
    • localAddress

      private final InetSocketAddress localAddress
    • boundAddress

      private volatile InetSocketAddress boundAddress
    • sslEngineSource

      private final SslEngineSource sslEngineSource
    • authenticateSslClients

      private final boolean authenticateSslClients
    • proxyAuthenticator

      private final ProxyAuthenticator proxyAuthenticator
    • chainProxyManager

      private final ChainedProxyManager chainProxyManager
    • mitmManager

      private final MitmManager mitmManager
    • filtersSource

      private final HttpFiltersSource filtersSource
    • transparent

      private final boolean transparent
    • connectTimeout

      private volatile int connectTimeout
    • idleConnectionTimeout

      private volatile int idleConnectionTimeout
    • serverResolver

      private final HostResolver serverResolver
    • globalTrafficShapingHandler

      private volatile GlobalTrafficShapingHandler globalTrafficShapingHandler
    • maxInitialLineLength

      private final int maxInitialLineLength
    • maxHeaderSize

      private final int maxHeaderSize
    • maxChunkSize

      private final int maxChunkSize
    • allowRequestsToOriginServer

      private final boolean allowRequestsToOriginServer
    • acceptProxyProtocol

      private final boolean acceptProxyProtocol
    • sendProxyProtocol

      private final boolean sendProxyProtocol
    • proxyAlias

      private final String proxyAlias
      The alias or pseudonym for this proxy, used when adding the Via header.
    • stopped

      private final AtomicBoolean stopped
      True when the proxy has already been stopped by calling stop() or abort().
    • activityTrackers

      private final Collection<ActivityTracker> activityTrackers
      Track all ActivityTrackers for tracking proxying activity.
    • allChannels

      private final ChannelGroup allChannels
      Keep track of all channels created by this proxy server for later shutdown when the proxy is stopped.
    • jvmShutdownHook

      private final Thread jvmShutdownHook
      JVM shutdown hook to shutdown this proxy server. Declared as a class-level variable to allow removing the shutdown hook when the proxy server is stopped normally.
  • Constructor Details

    • DefaultHttpProxyServer

      private DefaultHttpProxyServer(ServerGroup serverGroup, TransportProtocol transportProtocol, InetSocketAddress requestedAddress, SslEngineSource sslEngineSource, boolean authenticateSslClients, ProxyAuthenticator proxyAuthenticator, ChainedProxyManager chainProxyManager, MitmManager mitmManager, HttpFiltersSource filtersSource, boolean transparent, int idleConnectionTimeout, Collection<ActivityTracker> activityTrackers, int connectTimeout, HostResolver serverResolver, long readThrottleBytesPerSecond, long writeThrottleBytesPerSecond, InetSocketAddress localAddress, String proxyAlias, int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean allowRequestsToOriginServer, boolean acceptProxyProtocol, boolean sendProxyProtocol)
      Creates a new proxy server.
      Parameters:
      serverGroup - our ServerGroup for shared thread pools and such
      transportProtocol - The protocol to use for data transport
      requestedAddress - The address on which this server will listen
      sslEngineSource - (optional) if specified, this Proxy will encrypt inbound connections from clients using an SSLEngine obtained from this SslEngineSource.
      authenticateSslClients - Indicate whether or not to authenticate clients when using SSL
      proxyAuthenticator - (optional) If specified, requests to the proxy will be authenticated using HTTP BASIC authentication per the provided ProxyAuthenticator
      chainProxyManager - The proxy to send requests to if chaining proxies. Typically null.
      mitmManager - The MitmManager to use for man in the middle'ing CONNECT requests
      filtersSource - Source for HttpFilters
      transparent - If true, this proxy will run as a transparent proxy. This will not modify the response, and will only modify the request to amend the URI if the target is the origin server (to comply with RFC 7230 section 5.3.1).
      idleConnectionTimeout - The timeout (in seconds) for auto-closing idle connections.
      activityTrackers - for tracking activity on this proxy
      connectTimeout - number of milliseconds to wait to connect to the upstream server
      serverResolver - the HostResolver to use for resolving server addresses
      readThrottleBytesPerSecond - read throttle bandwidth
      writeThrottleBytesPerSecond - write throttle bandwidth
      maxInitialLineLength -
      maxHeaderSize -
      maxChunkSize -
      allowRequestsToOriginServer - when true, allow the proxy to handle requests that contain an origin-form URI, as defined in RFC 7230 5.3.1
      acceptProxyProtocol - when true, the proxy will accept a proxy protocol header from client
      sendProxyProtocol - when true, the proxy will send a proxy protocol header to the server
  • Method Details

    • bootstrap

      public static HttpProxyServerBootstrap bootstrap()
      Bootstrap a new DefaultHttpProxyServer starting from scratch.
    • bootstrapFromFile

      public static HttpProxyServerBootstrap bootstrapFromFile(String path)
      Bootstrap a new DefaultHttpProxyServer using defaults from the given file.
    • createGlobalTrafficShapingHandler

      private GlobalTrafficShapingHandler createGlobalTrafficShapingHandler(TransportProtocol transportProtocol, long readThrottleBytesPerSecond, long writeThrottleBytesPerSecond)
      Creates a new GlobalTrafficShapingHandler for this HttpProxyServer, using this proxy's proxyToServerEventLoop.
    • isTransparent

      boolean isTransparent()
    • getIdleConnectionTimeout

      public int getIdleConnectionTimeout()
      Specified by:
      getIdleConnectionTimeout in interface HttpProxyServer
    • setIdleConnectionTimeout

      public void setIdleConnectionTimeout(int idleConnectionTimeout)
      Specified by:
      setIdleConnectionTimeout in interface HttpProxyServer
    • getConnectTimeout

      public int getConnectTimeout()
      Description copied from interface: HttpProxyServer
      Returns the maximum time to wait, in milliseconds, to connect to a server.
      Specified by:
      getConnectTimeout in interface HttpProxyServer
    • setConnectTimeout

      public void setConnectTimeout(int connectTimeoutMs)
      Description copied from interface: HttpProxyServer
      Sets the maximum time to wait, in milliseconds, to connect to a server.
      Specified by:
      setConnectTimeout in interface HttpProxyServer
    • getServerResolver

      public HostResolver getServerResolver()
    • getLocalAddress

      public InetSocketAddress getLocalAddress()
    • getListenAddress

      public InetSocketAddress getListenAddress()
      Description copied from interface: HttpProxyServer
      Return the address on which this proxy is listening.
      Specified by:
      getListenAddress in interface HttpProxyServer
    • setThrottle

      public void setThrottle(long readThrottleBytesPerSecond, long writeThrottleBytesPerSecond)
      Description copied from interface: HttpProxyServer

      Set the read/write throttle bandwidths (in bytes/second) for this proxy.

      Specified by:
      setThrottle in interface HttpProxyServer
    • getReadThrottle

      public long getReadThrottle()
    • getWriteThrottle

      public long getWriteThrottle()
    • getMaxInitialLineLength

      public int getMaxInitialLineLength()
    • getMaxHeaderSize

      public int getMaxHeaderSize()
    • getMaxChunkSize

      public int getMaxChunkSize()
    • isAllowRequestsToOriginServer

      public boolean isAllowRequestsToOriginServer()
    • isAcceptProxyProtocol

      public boolean isAcceptProxyProtocol()
    • isSendProxyProtocol

      public boolean isSendProxyProtocol()
    • clone

      public HttpProxyServerBootstrap clone()
      Description copied from interface: HttpProxyServer

      Clone the existing server, with a port 1 higher and everything else the same. If the proxy was started with port 0 (JVM-assigned port), the cloned proxy will also use a JVM-assigned port.

      The new server will share event loops with the original server. The event loops will use whatever name was given to the first server in the clone group. The server group will not terminate until the original server and all clones terminate.

      Specified by:
      clone in interface HttpProxyServer
      Overrides:
      clone in class Object
      Returns:
      a bootstrap that allows customizing and starting the cloned server
    • stop

      public void stop()
      Description copied from interface: HttpProxyServer
      Stops the server and all related clones. Waits for traffic to stop before shutting down.
      Specified by:
      stop in interface HttpProxyServer
    • abort

      public void abort()
      Description copied from interface: HttpProxyServer
      Stops the server and all related clones immediately, without waiting for traffic to stop.
      Specified by:
      abort in interface HttpProxyServer
    • doStop

      protected void doStop(boolean graceful)
      Performs cleanup necessary to stop the server. Closes all channels opened by the server and unregisters this server from the server group.
      Parameters:
      graceful - when true, waits for requests to terminate before stopping the server
    • registerChannel

      protected void registerChannel(Channel channel)
      Register a new Channel with this server, for later closing.
    • unregisterChannel

      protected void unregisterChannel(Channel channel)
    • closeAllChannels

      protected void closeAllChannels(boolean graceful)
      Closes all channels opened by this proxy server.
      Parameters:
      graceful - when false, attempts to shutdown all channels immediately and ignores any channel-closing exceptions
    • start

      private HttpProxyServer start()
    • doStart

      private void doStart()
    • getChainProxyManager

      protected ChainedProxyManager getChainProxyManager()
    • getMitmManager

      protected MitmManager getMitmManager()
    • getSslEngineSource

      protected SslEngineSource getSslEngineSource()
    • getProxyAuthenticator

      protected ProxyAuthenticator getProxyAuthenticator()
    • getFiltersSource

      public HttpFiltersSource getFiltersSource()
    • getActivityTrackers

      protected Collection<ActivityTracker> getActivityTrackers()
    • getProxyAlias

      public String getProxyAlias()
    • getProxyToServerWorkerFor

      protected EventLoopGroup getProxyToServerWorkerFor(TransportProtocol transportProtocol)