Пример #1
0
  public void testExecuteCancelStalledConnect() throws Exception {
    final StubProgressMonitor monitor = new StubProgressMonitor();
    HttpClient client = new HttpClient();
    WebLocation location = new WebLocation(TestUrl.DEFAULT.getConnectionTimeout().toString());
    HostConfiguration hostConfiguration =
        WebUtil.createHostConfiguration(client, location, monitor);

    GetMethod method = new GetMethod(location.getUrl());
    try {
      Runnable runner =
          new Runnable() {
            public void run() {
              try {
                Thread.sleep(500);
              } catch (InterruptedException e) {
              }
              monitor.canceled = true;
            }
          };
      new Thread(runner).start();
      WebUtil.execute(client, hostConfiguration, method, monitor);
      client.executeMethod(method);
      fail("Expected OperationCanceledException");
    } catch (OperationCanceledException expected) {
      assertTrue(monitor.isCanceled());
    } catch (ConnectException ignored) {
      System.err.println("Skipping testExecuteCancelStalledConnect() due to blocking firewall");
    } finally {
      WebUtil.releaseConnection(method, monitor);
    }
  }
 private static HttpClient createHttpClient(String userAgent) {
   HttpClient httpClient = new HttpClient();
   httpClient.setHttpConnectionManager(WebUtil.getConnectionManager());
   httpClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
   WebUtil.configureHttpClient(httpClient, userAgent);
   return httpClient;
 }
Пример #3
0
 public void testCreateHostConfigurationProxy() throws Exception {
   StubProgressMonitor monitor = new StubProgressMonitor();
   HttpClient client = new HttpClient();
   WebUtil.createHostConfiguration(
       client,
       new WebLocation(
           TestUrl.DEFAULT.getHttpOk().toString(),
           null,
           null,
           new IProxyProvider() {
             public Proxy getProxyForHost(String host, String proxyType) {
               assertEquals(IProxyData.HTTP_PROXY_TYPE, proxyType);
               return null;
             }
           }),
       monitor);
   WebUtil.createHostConfiguration(
       client,
       new WebLocation(
           TestUrl.DEFAULT.getHttpsOk().toString(),
           null,
           null,
           new IProxyProvider() {
             public Proxy getProxyForHost(String host, String proxyType) {
               assertEquals(IProxyData.HTTPS_PROXY_TYPE, proxyType);
               return null;
             }
           }),
       monitor);
 }
 public CommonXmlRpcClient(AbstractWebLocation location, HttpClient client) {
   this.location = location;
   this.httpClient = createHttpClient(DEFAULT_USER_AGENT);
   this.authScope =
       new AuthScope(
           WebUtil.getHost(location.getUrl()),
           WebUtil.getPort(location.getUrl()),
           null,
           AuthScope.ANY_SCHEME);
 }
Пример #5
0
  public void testGetUserAgent() {
    String userAgent = WebUtil.getUserAgent(null);
    assertEquals(userAgent, WebUtil.getUserAgent(""));
    assertEquals(-1, userAgent.indexOf("null"));
    assertEquals(-1, userAgent.indexOf("  "));
    assertEquals(0, userAgent.indexOf("Mylyn"));

    userAgent = WebUtil.getUserAgent("abc");
    assertEquals(-1, userAgent.indexOf("null"));
    assertEquals(-1, userAgent.indexOf("  "));
    assertEquals(0, userAgent.indexOf("Mylyn"));
    assertTrue(userAgent.contains(" abc "));
  }
Пример #6
0
 public void testGetTitleFromUrl() throws Exception {
   assertEquals(
       "Eclipse Mylyn Open Source Project",
       WebUtil.getTitleFromUrl(new WebLocation(TestUrl.DEFAULT.getHttpOk().toString()), null));
   // disabled: fails in environments where the DNS resolver redirects for unknown hosts
   //		try {
   //			String title = WebUtil.getTitleFromUrl(new WebLocation("http://invalidurl"), null);
   //			fail("Expected UnknownHostException, got: " + title);
   //		} catch (UnknownHostException e) {
   //		}
   String url = "http://" + proxyAddress.getHostName() + ":" + proxyAddress.getPort() + "/";
   testProxy.addResponse(TestProxy.OK);
   assertNull(WebUtil.getTitleFromUrl(new WebLocation(url), null));
 }
Пример #7
0
  public void testExecute() throws Exception {
    StubProgressMonitor monitor = new StubProgressMonitor();
    HttpClient client = new HttpClient();
    WebLocation location = new WebLocation(TestUrl.DEFAULT.getHttpOk().toString());
    HostConfiguration hostConfiguration =
        WebUtil.createHostConfiguration(client, location, monitor);

    GetMethod method = new GetMethod(location.getUrl());
    try {
      int result = WebUtil.execute(client, hostConfiguration, method, monitor);
      assertEquals(HttpStatus.SC_OK, result);
    } finally {
      WebUtil.releaseConnection(method, monitor);
    }
  }
Пример #8
0
  public void testLocationSslConnectProxyProxyCredentials() throws Exception {
    String url = "https://foo/bar";
    final Proxy proxy = new AuthenticatedProxy(Type.HTTP, proxyAddress, "proxyUser", "proxyPass");
    AbstractWebLocation location =
        new WebLocation(
            url,
            null,
            null,
            new IProxyProvider() {
              public Proxy getProxyForHost(String host, String proxyType) {
                return proxy;
              }
            });
    HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(client, location, null);
    ;
    client.getParams().setAuthenticationPreemptive(true);

    testProxy.addResponse(TestProxy.SERVICE_UNVAILABLE);

    GetMethod method = new GetMethod("/");
    int statusCode = client.executeMethod(hostConfiguration, method);
    assertEquals(503, statusCode);

    Message request = testProxy.getRequest();
    assertEquals("CONNECT foo:443 HTTP/1.1", request.request);
    assertEquals(
        "Basic cHJveHlVc2VyOnByb3h5UGFzcw==", request.getHeaderValue("Proxy-Authorization"));
  }
Пример #9
0
  public void testConnectCancelStalledConnect() throws Exception {
    final StubProgressMonitor monitor = new StubProgressMonitor();
    String host = "google.com";
    int port = 9999;

    try {
      Runnable runner =
          new Runnable() {
            public void run() {
              try {
                Thread.sleep(500);
              } catch (InterruptedException e) {
              }
              monitor.canceled = true;
            }
          };
      new Thread(runner).start();
      WebUtil.connect(new Socket(), new InetSocketAddress(host, port), 5000, monitor);
      fail("Expected OperationCanceledException");
    } catch (OperationCanceledException expected) {
      assertTrue(monitor.isCanceled());
    } catch (ConnectException ignored) {
      System.err.println("Skipping testConnectCancelStalledConnect() due to blocking firewall");
    }
  }
Пример #10
0
  public void testLocationSslConnectProxy() throws Exception {
    String url = "https://foo/bar";
    final Proxy proxy = new Proxy(Type.HTTP, proxyAddress);
    AbstractWebLocation location =
        new WebLocation(
            url,
            null,
            null,
            new IProxyProvider() {
              public Proxy getProxyForHost(String host, String proxyType) {
                return proxy;
              }
            });
    HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(client, location, null);
    ;

    testProxy.addResponse(TestProxy.SERVICE_UNVAILABLE);

    GetMethod method = new GetMethod("/");
    int statusCode = client.executeMethod(hostConfiguration, method);
    assertEquals(503, statusCode);

    Message request = testProxy.getRequest();
    assertEquals("CONNECT foo:443 HTTP/1.1", request.request);
  }
Пример #11
0
  public void testLocationSslConnectProxyNoProxyCredentials() throws Exception {
    String url = "https://foo/bar";
    final Proxy proxy = new Proxy(Type.HTTP, proxyAddress);
    AbstractWebLocation location =
        new WebLocation(
            url,
            null,
            null,
            new IProxyProvider() {
              public Proxy getProxyForHost(String host, String proxyType) {
                return proxy;
              }
            });
    HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(client, location, null);
    ;

    Message response = new Message("HTTP/1.1 407 Proxy authentication required");
    response.headers.add("Proxy-Authenticate: Basic realm=\"Foo\"");
    testProxy.addResponse(response);
    testProxy.addResponse(TestProxy.SERVICE_UNVAILABLE);

    GetMethod method = new GetMethod("/");
    int statusCode = client.executeMethod(hostConfiguration, method);
    assertEquals(407, statusCode);

    Message request = testProxy.getRequest();
    assertEquals("CONNECT foo:443 HTTP/1.1", request.request);

    assertFalse("Expected HttpClient to close connection", testProxy.hasRequest());
  }
Пример #12
0
  public void testLocationConnectProxyProxyCredentialsHttpAuth() throws Exception {
    String url = "http://foo/bar";
    final Proxy proxy = new AuthenticatedProxy(Type.HTTP, proxyAddress, "proxyUser", "proxyPass");
    WebLocation location =
        new WebLocation(
            url,
            "",
            "",
            new IProxyProvider() {
              public Proxy getProxyForHost(String host, String proxyType) {
                return proxy;
              }
            });
    location.setCredentials(AuthenticationType.HTTP, "user", "pass");

    HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(client, location, null);
    client.getParams().setAuthenticationPreemptive(true);

    testProxy.addResponse(TestProxy.OK);

    GetMethod method = new GetMethod(url);
    int statusCode = client.executeMethod(hostConfiguration, method);
    assertEquals(200, statusCode);

    Message request = testProxy.getRequest();
    assertEquals("GET http://foo/bar HTTP/1.1", request.request);
    assertEquals(
        "Basic cHJveHlVc2VyOnByb3h5UGFzcw==", request.getHeaderValue("Proxy-Authorization"));
    assertEquals("Basic dXNlcjpwYXNz", request.getHeaderValue("Authorization"));
  }
Пример #13
0
  public void testLocationConnectProxyHttpAuth() throws Exception {
    String url = "http://foo/bar";
    final Proxy proxy = new Proxy(Type.HTTP, proxyAddress);
    WebLocation location =
        new WebLocation(
            url,
            "",
            "",
            new IProxyProvider() {
              public Proxy getProxyForHost(String host, String proxyType) {
                return proxy;
              }
            });
    location.setCredentials(AuthenticationType.HTTP, "user", "pass");
    HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(client, location, null);
    client.getParams().setAuthenticationPreemptive(true);

    Message response = new Message("HTTP/1.1 401 Authentication required");
    response.headers.add("WWW-Authenticate: Basic realm=\"Foo\"");
    testProxy.addResponse(response);
    testProxy.addResponse(TestProxy.OK);

    GetMethod method = new GetMethod(url);
    int statusCode = client.executeMethod(hostConfiguration, method);
    assertEquals(401, statusCode);

    Message request = testProxy.getRequest();
    assertEquals("GET http://foo/bar HTTP/1.1", request.request);
    assertEquals("Basic dXNlcjpwYXNz", request.getHeaderValue("Authorization"));
  }
Пример #14
0
 AuthenticationCredentials updateCredentials() {
   // update configuration with latest values
   AuthenticationCredentials credentials = location.getCredentials(AuthenticationType.REPOSITORY);
   if (credentials != null) {
     Credentials httpCredentials =
         WebUtil.getHttpClientCredentials(credentials, WebUtil.getHost(location.getUrl()));
     httpClient.getState().setCredentials(authScope, httpCredentials);
     //			if (CoreUtil.TEST_MODE) {
     //				System.err.println(" Setting credentials: " + httpCredentials); //$NON-NLS-1$
     //			}
     httpClient.getState().setCredentials(authScope, httpCredentials);
   } else {
     httpClient.getState().clearCredentials();
   }
   return credentials;
 }
Пример #15
0
  public void testExecuteAlreadyCancelled() throws Exception {
    StubProgressMonitor monitor = new StubProgressMonitor();
    HttpClient client = new HttpClient();
    WebLocation location = new WebLocation(TestUrl.DEFAULT.getHttpOk().toString());
    HostConfiguration hostConfiguration =
        WebUtil.createHostConfiguration(client, location, monitor);

    GetMethod method = new GetMethod(location.getUrl());
    try {
      monitor.canceled = true;
      WebUtil.execute(client, hostConfiguration, method, monitor);
      fail("Expected InterruptedIOException");
    } catch (OperationCanceledException expected) {
    } finally {
      WebUtil.releaseConnection(method, monitor);
    }
  }
Пример #16
0
  public void testLocationConnectSslClientCert() throws Exception {
    if (CommonTestUtil.isCertificateAuthBroken()) {
      return; // skip test
    }

    String url = "https://mylyn.org/secure/";
    AbstractWebLocation location = new WebLocation(url);
    HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(client, location, null);

    if (!((PollingSslProtocolSocketFactory) hostConfiguration.getProtocol().getSocketFactory())
        .hasKeyManager()) {
      return; // skip test if keystore property is not set
    }

    GetMethod method = new GetMethod(WebUtil.getRequestPath(url));
    int statusCode = client.executeMethod(hostConfiguration, method);
    assertEquals(200, statusCode);
  }
Пример #17
0
  public void testConfigureClient() throws Exception {
    WebLocation location = new WebLocation(TestUrl.DEFAULT.getHttpOk().toString());

    WebUtil.createHostConfiguration(client, location, null /*monitor*/);

    HttpConnectionManagerParams params = client.getHttpConnectionManager().getParams();
    assertEquals(
        CoreUtil.TEST_MODE ? 2 : MAX_HTTP_HOST_CONNECTIONS_DEFAULT,
        params.getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION));
    assertEquals(
        CoreUtil.TEST_MODE ? 20 : MAX_HTTP_TOTAL_CONNECTIONS_DEFAULT,
        params.getMaxTotalConnections());
  }
Пример #18
0
  public void testConfigureHttpClient() {
    HttpClient client = new HttpClient();

    WebUtil.configureHttpClient(client, "");
    assertEquals(
        WebUtil.getUserAgent(""), client.getParams().getParameter(HttpMethodParams.USER_AGENT));

    WebUtil.configureHttpClient(client, null);
    assertEquals(
        WebUtil.getUserAgent(""), client.getParams().getParameter(HttpMethodParams.USER_AGENT));

    WebUtil.configureHttpClient(client, "myagent");
    assertTrue(
        -1
            != client
                .getParams()
                .getParameter(HttpMethodParams.USER_AGENT)
                .toString()
                .indexOf("myagent"));

    // TODO test timeouts
  }
Пример #19
0
 /**
  * Default encoding needs to be set to non-UTF8 encoding for this test to be meaningful, e.g.
  * <code>-Dfile.encoding=ISO-8859-1</code>.
  */
 public void testGetTitleFromUrlUtf8() throws Exception {
   String message =
       "HTTP/1.1 200 OK\n"
           + "Date: Sat, 03 Jan 2009 14:40:23 GMT\n"
           + "Connection: close\n"
           + "Content-Type: text/html; charset=UTF-8\n"
           + "Content-Length: 30\n"
           + "\n"
           + "<html><title>\u00C3\u00BC</title></html>";
   testProxy.addResponse(message);
   String url = "http://" + proxyAddress.getHostName() + ":" + proxyAddress.getPort() + "/";
   assertEquals("\u00FC", WebUtil.getTitleFromUrl(new WebLocation(url), null));
 }
Пример #20
0
  public void testLocationConnect() throws Exception {
    String url = "http://" + proxyAddress.getHostName() + ":" + proxyAddress.getPort() + "/";
    AbstractWebLocation location = new WebLocation(url, null, null, null);
    HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(client, location, null);

    testProxy.addResponse(TestProxy.OK);

    GetMethod method = new GetMethod("/");
    int statusCode = client.executeMethod(hostConfiguration, method);
    assertEquals(200, statusCode);

    Message request = testProxy.getRequest();
    assertEquals("GET / HTTP/1.1", request.request);
  }
Пример #21
0
  public void testLocationConnectSsl() throws Exception {
    String url = "https://" + proxyAddress.getHostName() + ":" + proxyAddress.getPort() + "/";
    AbstractWebLocation location = new WebLocation(url, null, null, null);
    HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(client, location, null);

    testProxy.setCloseOnConnect(true);

    GetMethod method = new GetMethod("/");
    try {
      int statusCode = client.executeMethod(hostConfiguration, method);
      fail("Expected SSLHandshakeException or connection reset, got status: " + statusCode);
    } catch (SSLHandshakeException e) {
    } catch (SocketException e) {
      assertEquals("Connection reset", e.getMessage());
    }

    assertFalse(testProxy.hasRequest());
  }
Пример #22
0
  public void testLocationSslConnectProxyTimeout() throws Exception {
    String url = "https://foo/bar";
    final Proxy proxy = new Proxy(Type.HTTP, proxyAddress);
    AbstractWebLocation location =
        new WebLocation(
            url,
            null,
            null,
            new IProxyProvider() {
              public Proxy getProxyForHost(String host, String proxyType) {
                return proxy;
              }
            });
    HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(client, location, null);

    testProxy.addResponse(TestProxy.OK);

    GetMethod method = new GetMethod("/");
    // avoid second attempt to connect to proxy to get exception right away
    method
        .getParams()
        .setParameter(
            HttpMethodParams.RETRY_HANDLER,
            new HttpMethodRetryHandler() {
              public boolean retryMethod(
                  HttpMethod method, IOException exception, int executionCount) {
                return false;
              }
            });
    try {
      int statusCode = client.executeMethod(hostConfiguration, method);
      fail("Expected SSLHandshakeException, got status: " + statusCode);
    } catch (SSLHandshakeException e) {
    } catch (SocketException e) {
      // connection reset, happens in some environments instead of SSLHandshakeExecption depending
      // on how much data has been written before the socket is closed
    }

    Message request = testProxy.getRequest();
    assertEquals("CONNECT foo:443 HTTP/1.1", request.request);
  }
Пример #23
0
  public void testReadTimeout() throws Exception {
    // wait 5 seconds for thread pool to be idle
    for (int i = 0; i < 10; i++) {
      if (((ThreadPoolExecutor) CommonsNetPlugin.getExecutorService()).getActiveCount() == 0) {
        break;
      }
      Thread.sleep(500);
    }
    assertEquals(0, ((ThreadPoolExecutor) CommonsNetPlugin.getExecutorService()).getActiveCount());

    String url = "http://" + proxyAddress.getHostName() + ":" + proxyAddress.getPort() + "/";
    AbstractWebLocation location = new WebLocation(url, null, null, null);
    HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(client, location, null);

    testProxy.addResponse(TestProxy.TIMEOUT);

    GetMethod method = new GetMethod("/");
    method.getParams().setSoTimeout(100);
    int statusCode = client.executeMethod(hostConfiguration, method);
    assertEquals(200, statusCode);

    PollingInputStream in =
        new PollingInputStream(
            new TimeoutInputStream(method.getResponseBodyAsStream(), 8192, 500L, -1),
            1,
            new NullProgressMonitor());
    try {
      in.read();
      fail("expected InterruptedIOException");
    } catch (InterruptedIOException e) {
      // expected
    } finally {
      in.close();
    }
    Thread.sleep(500);
    assertEquals(0, ((ThreadPoolExecutor) CommonsNetPlugin.getExecutorService()).getActiveCount());
  }
Пример #24
0
  protected void createXmlRpcClient() {
    config = new XmlRpcClientConfigImpl();
    config.setEncoding(DEFAULT_CHARSET);
    config.setTimeZone(TimeZone.getTimeZone(DEFAULT_TIME_ZONE));
    config.setContentLengthOptional(false);
    config.setConnectionTimeout(WebUtil.getConnectionTimeout());
    config.setReplyTimeout(WebUtil.getSocketTimeout());

    xmlrpc = new XmlRpcClient();
    xmlrpc.setConfig(config);
    // bug 307200: force factory that supports proper UTF-8 encoding
    xmlrpc.setXmlWriterFactory(new CharSetXmlWriterFactory());

    factory = new HttpClientTransportFactory(xmlrpc, httpClient);
    factory.setLocation(location);
    factory.setInterceptor(
        new HttpMethodInterceptor() {
          public void processRequest(HttpMethod method) {
            DigestScheme scheme = digestScheme;
            if (scheme != null) {
              if (DEBUG_AUTH) {
                System.err.println(location.getUrl() + ": Digest scheme is present"); // $NON-NLS-1$
              }
              Credentials creds = httpClient.getState().getCredentials(authScope);
              if (creds != null) {
                if (DEBUG_AUTH) {
                  System.err.println(
                      location.getUrl() + ": Setting digest scheme for request"); // $NON-NLS-1$
                }
                method.getHostAuthState().setAuthScheme(digestScheme);
                method.getHostAuthState().setAuthRequested(true);
              }
            }
          }

          @SuppressWarnings("null")
          public void processResponse(HttpMethod method) throws XmlRpcException {
            if (isContentTypeCheckingEnabled()) {
              Header contentTypeHeader = method.getResponseHeader("Content-Type"); // $NON-NLS-1$
              if (contentTypeHeader == null
                  || !DEFAULT_CONTENT_TYPE.equals(contentTypeHeader.getValue())) {
                throw new XmlRpcIllegalContentTypeException(
                    NLS.bind(
                        "The server returned an unexpected content type: ''{0}''",
                        contentTypeHeader.getValue()),
                    contentTypeHeader.getValue()); // $NON-NLS-1$
              }
            }
            AuthScheme authScheme = method.getHostAuthState().getAuthScheme();
            if (authScheme instanceof DigestScheme) {
              digestScheme = (DigestScheme) authScheme;
              if (DEBUG_AUTH) {
                System.err.println(location.getUrl() + ": Received digest scheme"); // $NON-NLS-1$
              }
            }
          }
        });
    xmlrpc.setTransportFactory(factory);

    try {
      config.setServerURL(new URL(location.getUrl()));
    } catch (MalformedURLException e) {
      throw new RuntimeException(e);
    }
  }
Пример #25
0
  public void testUrlParsers() {
    String url = "https://example.com:444/folder/file.txt";
    assertEquals(444, WebUtil.getPort(url));
    assertEquals("example.com", WebUtil.getHost(url));
    assertEquals("/folder/file.txt", WebUtil.getRequestPath(url));

    url = "http://example.com/";
    assertEquals(80, WebUtil.getPort(url));
    assertEquals("example.com", WebUtil.getHost(url));
    assertEquals("/", WebUtil.getRequestPath(url));

    url = "http://example.com";
    assertEquals(80, WebUtil.getPort(url));
    assertEquals("example.com", WebUtil.getHost(url));
    assertEquals("", WebUtil.getRequestPath(url));

    url = "https://example.com:321";
    assertEquals(321, WebUtil.getPort(url));
    assertEquals("example.com", WebUtil.getHost(url));
    assertEquals("", WebUtil.getRequestPath(url));

    url = "example.com:321";
    assertEquals(321, WebUtil.getPort(url));
    assertEquals("example.com", WebUtil.getHost(url));
    assertEquals("", WebUtil.getRequestPath(url));

    url = "https://example.com:444/folder/file.txt?search=https://example.com:812/folder/file.txt";
    assertEquals(444, WebUtil.getPort(url));
    assertEquals("example.com", WebUtil.getHost(url));
    assertEquals(
        "/folder/file.txt?search=https://example.com:812/folder/file.txt",
        WebUtil.getRequestPath(url));

    url = "https://example.com/folder/file.txt?search=https://example.com:812/folder/file.txt";
    assertEquals(443, WebUtil.getPort(url));
    assertEquals("example.com", WebUtil.getHost(url));
    assertEquals(
        "/folder/file.txt?search=https://example.com:812/folder/file.txt",
        WebUtil.getRequestPath(url));

    url =
        "https://jira.codehaus.org/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?&pid=11093&resolution=-1&sorter/field=updated&sorter/order=DESC&tempMax=1000";
    assertEquals(443, WebUtil.getPort(url));
    assertEquals("jira.codehaus.org", WebUtil.getHost(url));
    assertEquals(
        "/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?&pid=11093&resolution=-1&sorter/field=updated&sorter/order=DESC&tempMax=1000",
        WebUtil.getRequestPath(url));
  }