@Override
  public Response handleRequest(HttpRequest request, JSONObject payload) throws JSONException {
    String sessionId = getSessionId(request);

    if (sessionId == null || sessionId.isEmpty()) {
      String dataKeys = Joiner.on(", ").join(request.data().keySet());

      log.warning("Unable to retrieve session id from request with data: [" + dataKeys + "]");

      return respondWithFailure(
          sessionId, new SelendroidException("No session id passed to the request."));
    }

    final ActiveSession session = getActiveSession(request);
    if (session == null) {
      return respondWithFailure(sessionId,
          new SelendroidException("No session found for sessionId: " + sessionId));
    }
    if (session.isInvalid()) {
      return respondWithFailure(sessionId,
          new SelendroidException(
              "The test session has been marked as invalid. " +
                  "This happens if a hardware device was disconnected but a " +
                  "test session was still active on the device."));
    }
    String url = "http://localhost:" + session.getSelendroidServerPort() + request.uri();

    String method = request.method();

    final AndroidDevice device = session.getDevice();

    try {
      Wait<AndroidDevice> wait =
        new FluentWait<AndroidDevice>(device)
          .withTimeout(PROXY_REQUEST_ATTEMPT_TIMEOUT_MS, TimeUnit.MILLISECONDS)
          .pollingEvery(PROXY_REQUEST_ATTEMPT_INTERVAL_MS, TimeUnit.MILLISECONDS);
      return wait.until(new Function<AndroidDevice, Response>() {
        @Override
        public Response apply(AndroidDevice device) {
          try {
            // Check if the instrumentation process died in the middle of the request
            if (session.instrumentationProcessFinished()) {
              return respondWithInstrumentationProcessFinished(
                sessionId,
                session.getInstrumentationProcessOutput(),
                session.getInstrumentationProcessError(),
                device);
            }

            JSONObject response = proxyRequestToDevice(request, session, url, method);
            if (response == null) { // Unknown command
              return new SelendroidResponse(sessionId, StatusCode.UNKNOWN_COMMAND);
            }

            Object value = response.opt("value");
            int statusCode = response.getInt("status");
            log.fine(
              String.format(
                "Response from selendroid-server, status %d:\n%s",
                statusCode,
                value));

            return new SelendroidResponse(sessionId, StatusCode.fromInteger(statusCode), value);
          } catch (Exception e) {
            log.log(Level.SEVERE, "Failed to proxy request to Selendroid Server, retrying.", e);
            return null;
          }
        }
      });
    } catch (TimeoutException e) {
      // Check if we timed out because of the instrumentation process dying
      if (session.instrumentationProcessFinished()) {
        return respondWithInstrumentationProcessFinished(
          sessionId,
          session.getInstrumentationProcessOutput(),
          session.getInstrumentationProcessError(),
          device);
      }

      // Last resort, we really don't know what happened
      return respondWithFailure(
        sessionId,
        new SelendroidException("Selendroid server on the device became unreachable"));
    }
  }
Proxies the request as-is to the device.
selendroid-standalone/src/main/java/io/selendroid/standalone/server/handler/ProxyToDeviceHandler.java