Search in sources :

Example 6 with Protocol

use of org.apache.commons.httpclient.protocol.Protocol in project zaproxy by zaproxy.

the class HttpConnection method open.

/**
     * Establishes a connection to the specified host and port
     * (via a proxy if specified).
     * The underlying socket is created from the {@link ProtocolSocketFactory}.
     *
     * @throws IOException if an attempt to establish the connection results in an
     *   I/O error.
     */
public void open() throws IOException {
    LOG.trace("enter HttpConnection.open()");
    final String host = (proxyHostName == null) ? hostName : proxyHostName;
    final int port = (proxyHostName == null) ? portNumber : proxyPortNumber;
    assertNotOpen();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Open connection to " + host + ":" + port);
    }
    try {
        if (this.socket == null) {
            usingSecureSocket = isSecure() && !isProxied();
            // use the protocol's socket factory unless this is a secure
            // proxied connection
            ProtocolSocketFactory socketFactory = null;
            if (isSecure() && isProxied()) {
                Protocol defaultprotocol = Protocol.getProtocol("http");
                socketFactory = defaultprotocol.getSocketFactory();
            } else {
                socketFactory = this.protocolInUse.getSocketFactory();
            }
            this.socket = socketFactory.createSocket(host, port, localAddress, 0, this.params);
        }
        /*
            "Nagling has been broadly implemented across networks, 
            including the Internet, and is generally performed by default 
            - although it is sometimes considered to be undesirable in 
            highly interactive environments, such as some client/server 
            situations. In such cases, nagling may be turned off through 
            use of the TCP_NODELAY sockets option." */
        socket.setTcpNoDelay(this.params.getTcpNoDelay());
        socket.setSoTimeout(this.params.getSoTimeout());
        int linger = this.params.getLinger();
        if (linger >= 0) {
            socket.setSoLinger(linger > 0, linger);
        }
        int sndBufSize = this.params.getSendBufferSize();
        if (sndBufSize >= 0) {
            socket.setSendBufferSize(sndBufSize);
        }
        int rcvBufSize = this.params.getReceiveBufferSize();
        if (rcvBufSize >= 0) {
            socket.setReceiveBufferSize(rcvBufSize);
        }
        int outbuffersize = socket.getSendBufferSize();
        if ((outbuffersize > 2048) || (outbuffersize <= 0)) {
            outbuffersize = 2048;
        }
        int inbuffersize = socket.getReceiveBufferSize();
        if ((inbuffersize > 2048) || (inbuffersize <= 0)) {
            inbuffersize = 2048;
        }
        inputStream = new BufferedInputStream(socket.getInputStream(), inbuffersize);
        outputStream = new BufferedOutputStream(socket.getOutputStream(), outbuffersize);
        isOpen = true;
    } catch (IOException e) {
        // Connection wasn't opened properly
        // so close everything out
        closeSocketAndStreams();
        throw e;
    }
}
Also used : ProtocolSocketFactory(org.apache.commons.httpclient.protocol.ProtocolSocketFactory) SecureProtocolSocketFactory(org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory) BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) Protocol(org.apache.commons.httpclient.protocol.Protocol) BufferedOutputStream(java.io.BufferedOutputStream)

Example 7 with Protocol

use of org.apache.commons.httpclient.protocol.Protocol in project zaproxy by zaproxy.

the class HttpMethodBase method generateRequestLine.

// ------------------------------------------------- Static Utility Methods
/**
     * Generates HTTP request line according to the specified attributes.
     *
     * @param connection the {@link HttpConnection connection} used to execute
     *        this HTTP method
     * @param name the method name generate a request for
     * @param requestPath the path string for the request
     * @param query the query string for the request
     * @param version the protocol version to use (e.g. HTTP/1.0)
     *
     * @return HTTP request line
     */
protected static String generateRequestLine(HttpConnection connection, String name, String requestPath, String query, String version) {
    LOG.trace("enter HttpMethodBase.generateRequestLine(HttpConnection, " + "String, String, String, String)");
    StringBuffer buf = new StringBuffer();
    // Append method name
    buf.append(name);
    buf.append(" ");
    // Absolute or relative URL?
    if (!connection.isTransparent()) {
        Protocol protocol = connection.getProtocol();
        buf.append(protocol.getScheme().toLowerCase(Locale.ENGLISH));
        buf.append("://");
        buf.append(connection.getHost());
        if ((connection.getPort() != -1) && (connection.getPort() != protocol.getDefaultPort())) {
            buf.append(":");
            buf.append(connection.getPort());
        }
    }
    // Append path, if any
    if (requestPath == null) {
        buf.append("/");
    } else {
        if (!connection.isTransparent() && !requestPath.startsWith("/")) {
            buf.append("/");
        }
        buf.append(requestPath);
    }
    // Append query, if any
    if (query != null) {
        // ZAP: If commented out to not change the intended request URI (i.e. if the query component starts with a "?" char)
        //if (query.indexOf("?") != 0) {
        buf.append("?");
        //}
        buf.append(query);
    }
    // Append protocol
    buf.append(" ");
    buf.append(version);
    buf.append("\r\n");
    return buf.toString();
}
Also used : Protocol(org.apache.commons.httpclient.protocol.Protocol)

Example 8 with Protocol

use of org.apache.commons.httpclient.protocol.Protocol in project zaproxy by zaproxy.

the class HttpSender method executeMethod.

public int executeMethod(HttpMethod method, HttpState state) throws IOException {
    int responseCode = -1;
    String hostName;
    hostName = method.getURI().getHost();
    method.setDoAuthentication(true);
    HostConfiguration hc = null;
    HttpClient requestClient;
    if (isConnectionUpgrade(method)) {
        requestClient = new HttpClient(new ZapHttpConnectionManager());
        if (param.isUseProxy(hostName)) {
            requestClient.getHostConfiguration().setProxy(param.getProxyChainName(), param.getProxyChainPort());
            if (param.isUseProxyChainAuth()) {
                requestClient.getState().setProxyCredentials(getAuthScope(param), getNTCredentials(param));
            }
        }
    } else if (param.isUseProxy(hostName)) {
        requestClient = clientViaProxy;
    } else {
        requestClient = client;
    }
    if (this.initiator == CHECK_FOR_UPDATES_INITIATOR) {
        // Use the 'strict' SSLConnector, ie one that performs all the usual cert checks
        // The 'standard' one 'trusts' everything
        // This is to ensure that all 'check-for update' calls are made to the expected https urls
        // without this is would be possible to intercept and change the response which could result
        // in the user downloading and installing a malicious add-on
        hc = new HostConfiguration() {

            @Override
            public synchronized void setHost(URI uri) {
                try {
                    setHost(new HttpHost(uri.getHost(), uri.getPort(), getProtocol()));
                } catch (URIException e) {
                    throw new IllegalArgumentException(e.toString());
                }
            }

            ;
        };
        hc.setHost(hostName, method.getURI().getPort(), new Protocol("https", (ProtocolSocketFactory) new SSLConnector(false), 443));
        if (param.isUseProxy(hostName)) {
            hc.setProxyHost(new ProxyHost(param.getProxyChainName(), param.getProxyChainPort()));
            if (param.isUseProxyChainAuth()) {
                requestClient.getState().setProxyCredentials(getAuthScope(param), getNTCredentials(param));
            }
        }
    }
    // ZAP: Check if a custom state is being used
    if (state != null) {
        // Make sure cookies are enabled
        method.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    }
    responseCode = requestClient.executeMethod(hc, method, state);
    return responseCode;
}
Also used : HostConfiguration(org.apache.commons.httpclient.HostConfiguration) ZapHttpConnectionManager(org.zaproxy.zap.ZapHttpConnectionManager) URI(org.apache.commons.httpclient.URI) ProtocolSocketFactory(org.apache.commons.httpclient.protocol.ProtocolSocketFactory) URIException(org.apache.commons.httpclient.URIException) HttpHost(org.apache.commons.httpclient.HttpHost) HttpClient(org.apache.commons.httpclient.HttpClient) ProxyHost(org.apache.commons.httpclient.ProxyHost) Protocol(org.apache.commons.httpclient.protocol.Protocol)

Example 9 with Protocol

use of org.apache.commons.httpclient.protocol.Protocol in project translationstudio8 by heartsome.

the class ServiceUtil method getService.

public static IService getService() throws MalformedURLException {
    //		Service srvcModel = new ObjectServiceFactory().create(IService.class);
    //		XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
    //				.newInstance().getXFire());
    //
    //		IService srvc = (IService) factory.create(srvcModel, Constants.CONNECT_URL);
    //		return srvc;
    ProtocolSocketFactory easy = new EasySSLProtocolSocketFactory();
    Protocol protocol = new Protocol(HTTP_TYPE, easy, PORT);
    Protocol.registerProtocol(HTTP_TYPE, protocol);
    Service serviceModel = new ObjectServiceFactory().create(IService.class, SERVICE_NAME, SERVICE_NAMESPACE, null);
    IService service = (IService) new XFireProxyFactory().create(serviceModel, SERVICE_URL);
    Client client = ((XFireProxy) Proxy.getInvocationHandler(service)).getClient();
    client.addOutHandler(new DOMOutHandler());
    client.setProperty(CommonsHttpMessageSender.GZIP_ENABLED, Boolean.FALSE);
    client.setProperty(CommonsHttpMessageSender.DISABLE_EXPECT_CONTINUE, "1");
    client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "0");
    return service;
}
Also used : ProtocolSocketFactory(org.apache.commons.httpclient.protocol.ProtocolSocketFactory) EasySSLProtocolSocketFactory(org.codehaus.xfire.transport.http.EasySSLProtocolSocketFactory) ObjectServiceFactory(org.codehaus.xfire.service.binding.ObjectServiceFactory) DOMOutHandler(org.codehaus.xfire.util.dom.DOMOutHandler) Service(org.codehaus.xfire.service.Service) Protocol(org.apache.commons.httpclient.protocol.Protocol) Client(org.codehaus.xfire.client.Client) XFireProxy(org.codehaus.xfire.client.XFireProxy) XFireProxyFactory(org.codehaus.xfire.client.XFireProxyFactory) EasySSLProtocolSocketFactory(org.codehaus.xfire.transport.http.EasySSLProtocolSocketFactory)

Example 10 with Protocol

use of org.apache.commons.httpclient.protocol.Protocol in project oxCore by GluuFederation.

the class HTTPFileDownloader method getResource.

public static String getResource(String path, String contentType, String user, String password) {
    boolean isUseAuthentication = (user != null) && (password != null);
    if (!path.contains("://")) {
        path = "http://" + path;
    }
    String result = null;
    GetMethod method = new GetMethod(path);
    try {
        method.setRequestHeader("Accept", contentType);
        if (getEasyhttps() == null) {
            setEasyhttps(new Protocol("https", new EasySSLProtocolSocketFactory(), 443));
        }
        Protocol.registerProtocol("https", getEasyhttps());
        final HttpClient httpClient;
        if (isUseAuthentication) {
            httpClient = createHttpClientWithBasicAuth(user, password);
        } else {
            httpClient = new HttpClient();
        }
        httpClient.executeMethod(method);
        if (method.getStatusCode() == HttpStatus.SC_OK) {
            result = method.getResponseBodyAsString();
        }
    } catch (IOException ex) {
        result = null;
        log.error(String.format("Failed to get resource %s", path), ex);
    } catch (Exception ex) {
        result = null;
        log.error(String.format("Failed to get resource %s", path), ex);
    } finally {
        method.releaseConnection();
    }
    return result;
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException) Protocol(org.apache.commons.httpclient.protocol.Protocol) IOException(java.io.IOException) EasySSLProtocolSocketFactory(org.xdi.util.EasySSLProtocolSocketFactory)

Aggregations

Protocol (org.apache.commons.httpclient.protocol.Protocol)12 ProtocolSocketFactory (org.apache.commons.httpclient.protocol.ProtocolSocketFactory)6 IOException (java.io.IOException)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 HttpClient (org.apache.commons.httpclient.HttpClient)2 URI (org.apache.commons.httpclient.URI)2 URIException (org.apache.commons.httpclient.URIException)2 SecureProtocolSocketFactory (org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory)2 Client (org.codehaus.xfire.client.Client)2 XFireProxy (org.codehaus.xfire.client.XFireProxy)2 XFireProxyFactory (org.codehaus.xfire.client.XFireProxyFactory)2 Service (org.codehaus.xfire.service.Service)2 ObjectServiceFactory (org.codehaus.xfire.service.binding.ObjectServiceFactory)2 EasySSLProtocolSocketFactory (org.codehaus.xfire.transport.http.EasySSLProtocolSocketFactory)2 DOMOutHandler (org.codehaus.xfire.util.dom.DOMOutHandler)2 SpanEventRecorder (com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1