コード例 #1
0
ファイル: HttpProxy.java プロジェクト: Nextzero/Jetty
      private void tunnel(HttpDestination destination, final Connection connection) {
        String target = destination.getOrigin().getAddress().asString();
        Origin.Address proxyAddress = destination.getConnectAddress();
        HttpClient httpClient = destination.getHttpClient();
        Request connect =
            httpClient
                .newRequest(proxyAddress.getHost(), proxyAddress.getPort())
                .scheme(HttpScheme.HTTP.asString())
                .method(HttpMethod.CONNECT)
                .path(target)
                .header(HttpHeader.HOST, target)
                .timeout(httpClient.getConnectTimeout(), TimeUnit.MILLISECONDS);

        connection.send(
            connect,
            new Response.CompleteListener() {
              @Override
              public void onComplete(Result result) {
                if (result.isFailed()) {
                  tunnelFailed(result.getFailure());
                } else {
                  Response response = result.getResponse();
                  if (response.getStatus() == 200) {
                    tunnelSucceeded();
                  } else {
                    tunnelFailed(
                        new HttpResponseException(
                            "Received " + response + " for " + result.getRequest(), response));
                  }
                }
              }
            });
      }
コード例 #2
0
 private void tunnel(final Connection connection) {
   String target = address.getHost() + ":" + address.getPort();
   Request connect =
       client
           .newRequest(proxyAddress.getHost(), proxyAddress.getPort())
           .scheme(HttpScheme.HTTP.asString())
           .method(HttpMethod.CONNECT)
           .path(target)
           .header(HttpHeader.HOST.asString(), target)
           .timeout(client.getConnectTimeout(), TimeUnit.MILLISECONDS);
   connection.send(
       connect,
       new Response.CompleteListener() {
         @Override
         public void onComplete(Result result) {
           if (result.isFailed()) {
             failed(result.getFailure());
             connection.close();
           } else {
             Response response = result.getResponse();
             if (response.getStatus() == 200) {
               delegate.succeeded(connection);
             } else {
               failed(
                   new HttpResponseException(
                       "Received " + response + " for " + result.getRequest(), response));
               connection.close();
             }
           }
         }
       });
 }
コード例 #3
0
ファイル: HttpProxy.java プロジェクト: Nextzero/Jetty
 @Override
 public URI getURI() {
   String scheme = isSecure() ? HttpScheme.HTTPS.asString() : HttpScheme.HTTP.asString();
   return URI.create(new Origin(scheme, getAddress()).asString());
 }
コード例 #4
0
ファイル: Response.java プロジェクト: sslavic/jetty.project
  @Override
  public String encodeURL(String url) {
    final Request request = _channel.getRequest();
    SessionManager sessionManager = request.getSessionManager();
    if (sessionManager == null) return url;

    HttpURI uri = null;
    if (sessionManager.isCheckingRemoteSessionIdEncoding() && URIUtil.hasScheme(url)) {
      uri = new HttpURI(url);
      String path = uri.getPath();
      path = (path == null ? "" : path);
      int port = uri.getPort();
      if (port < 0) port = HttpScheme.HTTPS.asString().equalsIgnoreCase(uri.getScheme()) ? 443 : 80;
      if (!request.getServerName().equalsIgnoreCase(uri.getHost())
          || request.getServerPort() != port
          || !path.startsWith(
              request
                  .getContextPath())) // TODO the root context path is "", with which every non null
                                      // string starts
      return url;
    }

    String sessionURLPrefix = sessionManager.getSessionIdPathParameterNamePrefix();
    if (sessionURLPrefix == null) return url;

    if (url == null) return null;

    // should not encode if cookies in evidence
    if (request.isRequestedSessionIdFromCookie()) {
      int prefix = url.indexOf(sessionURLPrefix);
      if (prefix != -1) {
        int suffix = url.indexOf("?", prefix);
        if (suffix < 0) suffix = url.indexOf("#", prefix);

        if (suffix <= prefix) return url.substring(0, prefix);
        return url.substring(0, prefix) + url.substring(suffix);
      }
      return url;
    }

    // get session;
    HttpSession session = request.getSession(false);

    // no session
    if (session == null) return url;

    // invalid session
    if (!sessionManager.isValid(session)) return url;

    String id = sessionManager.getNodeId(session);

    if (uri == null) uri = new HttpURI(url);

    // Already encoded
    int prefix = url.indexOf(sessionURLPrefix);
    if (prefix != -1) {
      int suffix = url.indexOf("?", prefix);
      if (suffix < 0) suffix = url.indexOf("#", prefix);

      if (suffix <= prefix) return url.substring(0, prefix + sessionURLPrefix.length()) + id;
      return url.substring(0, prefix + sessionURLPrefix.length()) + id + url.substring(suffix);
    }

    // edit the session
    int suffix = url.indexOf('?');
    if (suffix < 0) suffix = url.indexOf('#');
    if (suffix < 0) {
      return url
          + ((HttpScheme.HTTPS.is(uri.getScheme()) || HttpScheme.HTTP.is(uri.getScheme()))
                  && uri.getPath() == null
              ? "/"
              : "")
          + // if no path, insert the root path
          sessionURLPrefix
          + id;
    }

    return url.substring(0, suffix)
        + ((HttpScheme.HTTPS.is(uri.getScheme()) || HttpScheme.HTTP.is(uri.getScheme()))
                && uri.getPath() == null
            ? "/"
            : "")
        + // if no path so insert the root path
        sessionURLPrefix
        + id
        + url.substring(suffix);
  }