Search in sources :

Example 41 with URIException

use of org.apache.commons.httpclient.URIException in project airavata by apache.

the class JSDLUtils method addDataStagingSourceElement.

public static void addDataStagingSourceElement(JobDefinitionType value, String uri, String fileSystem, String file, int flags) {
    JobDescriptionType jobDescr = getOrCreateJobDescription(value);
    try {
        uri = (uri == null) ? null : URIUtils.encodeAll(uri);
    } catch (URIException e) {
    }
    DataStagingType newDS = jobDescr.addNewDataStaging();
    CreationFlagEnumeration.Enum creationFlag = CreationFlagEnumeration.DONT_OVERWRITE;
    if ((flags & FLAG_OVERWRITE) != 0)
        creationFlag = CreationFlagEnumeration.OVERWRITE;
    if ((flags & FLAG_APPEND) != 0)
        creationFlag = CreationFlagEnumeration.APPEND;
    boolean deleteOnTerminate = (flags & FLAG_DELETE_ON_TERMINATE) != 0;
    newDS.setCreationFlag(creationFlag);
    newDS.setDeleteOnTermination(deleteOnTerminate);
    SourceTargetType source = newDS.addNewSource();
    source.setURI(uri);
    newDS.setFileName(file);
    if (fileSystem != null && !fileSystem.equals("Work")) {
        // $NON-NLS-1$
        newDS.setFilesystemName(fileSystem);
    }
}
Also used : DataStagingType(org.ggf.schemas.jsdl.x2005.x11.jsdl.DataStagingType) SourceTargetType(org.ggf.schemas.jsdl.x2005.x11.jsdl.SourceTargetType) URIException(org.apache.commons.httpclient.URIException) JobDescriptionType(org.ggf.schemas.jsdl.x2005.x11.jsdl.JobDescriptionType) CreationFlagEnumeration(org.ggf.schemas.jsdl.x2005.x11.jsdl.CreationFlagEnumeration)

Example 42 with URIException

use of org.apache.commons.httpclient.URIException in project ecf by eclipse.

the class ProxyRequestHandler method httpProxy.

private void httpProxy(final SimpleHttpServerConnection conn, final SimpleRequest request) throws IOException {
    RequestLine oldreqline = request.getRequestLine();
    URI uri = null;
    SimpleHost host = null;
    try {
        uri = new URI(oldreqline.getUri(), true);
        host = new SimpleHost(uri.getHost(), uri.getPort());
    } catch (URIException ex) {
        SimpleResponse response = ErrorResponse.getResponse(HttpStatus.SC_BAD_REQUEST);
        conn.writeResponse(response);
        return;
    }
    SimpleHttpServerConnection proxyconn = null;
    try {
        proxyconn = this.connmanager.openConnection(host);
    } catch (UnknownHostException e) {
        SimpleResponse response = ErrorResponse.getResponse(HttpStatus.SC_NOT_FOUND);
        conn.writeResponse(response);
        return;
    }
    try {
        proxyconn.setSocketTimeout(0);
        // Rewrite target url
        RequestLine newreqline = new RequestLine(oldreqline.getMethod(), uri.getEscapedPath(), oldreqline.getHttpVersion());
        request.setRequestLine(newreqline);
        // Remove proxy-auth headers if present
        request.removeHeaders("Proxy-Authorization");
        // Manage connection persistence
        Header connheader = request.getFirstHeader("Proxy-Connection");
        if (connheader != null) {
            if (connheader.getValue().equalsIgnoreCase("close")) {
                request.setHeader(new Header("Connection", "close"));
            }
        }
        request.removeHeaders("Proxy-Connection");
        proxyconn.writeRequest(request);
        SimpleResponse response = proxyconn.readResponse();
        if (response == null) {
            return;
        }
        response.setHeader(new Header("Via", "1.1 test (Test-Proxy)"));
        connheader = response.getFirstHeader("Connection");
        if (connheader != null) {
            String s = connheader.getValue();
            if (s.equalsIgnoreCase("close")) {
                response.setHeader(new Header("Proxy-Connection", "close"));
                conn.setKeepAlive(false);
                proxyconn.setKeepAlive(false);
                response.removeHeaders("Connection");
            }
            if (s.equalsIgnoreCase("keep-alive")) {
                response.setHeader(new Header("Proxy-Connection", "keep-alive"));
                conn.setKeepAlive(true);
                proxyconn.setKeepAlive(true);
                response.removeHeaders("Connection");
            }
        } else {
            // Use protocol default connection policy
            if (response.getHttpVersion().greaterEquals(HttpVersion.HTTP_1_1)) {
                conn.setKeepAlive(true);
                proxyconn.setKeepAlive(true);
            } else {
                conn.setKeepAlive(false);
                proxyconn.setKeepAlive(false);
            }
        }
        if ("HEAD".equalsIgnoreCase(request.getRequestLine().getMethod())) {
            // this is a head request, we don't want to send the actualy content
            response.setBody(null);
        }
        conn.writeResponse(response);
    } catch (HttpException e) {
        SimpleResponse response = ErrorResponse.getResponse(HttpStatus.SC_BAD_REQUEST);
        conn.writeResponse(response);
        proxyconn.setKeepAlive(false);
    } catch (IOException e) {
        LOG.warn(e.getMessage());
        proxyconn.setKeepAlive(false);
    } finally {
        this.connmanager.releaseConnection(host, proxyconn);
    }
}
Also used : URIException(org.apache.commons.httpclient.URIException) UnknownHostException(java.net.UnknownHostException) Header(org.apache.commons.httpclient.Header) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) URI(org.apache.commons.httpclient.URI)

Example 43 with URIException

use of org.apache.commons.httpclient.URIException in project zaproxy by zaproxy.

the class HttpMessage method mutateHttpMethod.

public void mutateHttpMethod(String method) {
    try {
        URI uri = getRequestHeader().getURI();
        String body = getRequestBody().toString();
        String prevMethod = getRequestHeader().getMethod();
        if (prevMethod.equalsIgnoreCase(method)) {
            return;
        }
        if (prevMethod.equals(HttpRequestHeader.POST)) {
            // Was POST, move all params onto the URL
            if (body != null && body.length() > 0) {
                StringBuilder sb = new StringBuilder();
                if (uri.getQuery() != null) {
                    sb.append(uri.getQuery());
                }
                String[] params = body.split("&");
                for (String param : params) {
                    if (sb.length() > 0) {
                        sb.append('&');
                    }
                    String[] nv = param.split("=");
                    if (nv.length == 1) {
                        // This effectively strips out the equals if theres
                        // no value
                        sb.append(nv[0]);
                    } else {
                        sb.append(param);
                    }
                }
                uri.setQuery(sb.toString());
            }
            // Clear the body
            body = "";
        } else if (method.equals(HttpRequestHeader.POST)) {
            // To be a port, move all URL query params into the body
            String query = uri.getQuery();
            if (query != null) {
                StringBuilder sb = new StringBuilder();
                String[] params = query.split("&");
                for (String param : params) {
                    if (sb.length() > 0) {
                        sb.append('&');
                    }
                    sb.append(param);
                    String[] nv = param.split("=");
                    if (nv.length == 1) {
                        // Cope with URL params with no values e.g.
                        // http://www.example.com/test?key
                        sb.append('=');
                    }
                }
                body = sb.toString();
                uri.setQuery(null);
            }
        }
        if (prevMethod.equalsIgnoreCase(HttpRequestHeader.CONNECT)) {
            String scheme;
            if (getRequestHeader().getHostPort() == 443) {
                scheme = "https://";
            } else {
                scheme = "http://";
            }
            uri = new URI(scheme + uri, true);
        } else if (method.equals(HttpRequestHeader.CONNECT)) {
            uri = URI.fromAuthority(uri.getAuthority());
        }
        getRequestHeader().setMessage(method + " " + uri + " " + getRequestHeader().getVersion() + "\r\n" + getRequestHeader().getHeadersAsString());
        getRequestBody().setBody(body);
    } catch (HttpMalformedHeaderException e) {
        // Ignore?
        log.error(e.getMessage(), e);
    } catch (URIException e) {
        log.error(e.getMessage(), e);
    }
}
Also used : URIException(org.apache.commons.httpclient.URIException) URI(org.apache.commons.httpclient.URI)

Example 44 with URIException

use of org.apache.commons.httpclient.URIException in project zaproxy by zaproxy.

the class HttpRequestHeader method parseURI.

public static URI parseURI(String sUri) throws URIException {
    URI uri;
    int len = sUri.length();
    StringBuilder sb = new StringBuilder(len);
    char[] charray = new char[1];
    String s;
    for (int i = 0; i < len; i++) {
        char ch = sUri.charAt(i);
        // String ch = sUri.substring(i, i+1);
        if (DELIM_UNWISE.indexOf(ch) >= 0) {
            // check if unwise or delim in RFC.  If so, encode it.
            charray[0] = ch;
            s = new String(charray);
            try {
                s = URLEncoder.encode(s, "UTF8");
            } catch (UnsupportedEncodingException e1) {
            }
            sb.append(s);
        } else if (ch == '%') {
            try {
                String hex = sUri.substring(i + 1, i + 3);
                Integer.parseInt(hex, 16);
                sb.append(ch);
            } catch (Exception e) {
                charray[0] = ch;
                s = new String(charray);
                try {
                    s = URLEncoder.encode(s, "UTF8");
                } catch (UnsupportedEncodingException e1) {
                }
                sb.append(s);
            }
        } else if (ch == ' ') {
            // if URLencode, '+' will be appended.
            sb.append("%20");
        } else {
            sb.append(ch);
        }
    }
    uri = new URI(sb.toString(), true);
    return uri;
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) URI(org.apache.commons.httpclient.URI) URIException(org.apache.commons.httpclient.URIException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 45 with URIException

use of org.apache.commons.httpclient.URIException 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());
            setProxyAuth(requestClient);
        }
    } else if (param.isUseProxy(hostName)) {
        requestClient = clientViaProxy;
    } else {
        requestClient = client;
    }
    if (this.initiator == CHECK_FOR_UPDATES_INITIATOR) {
        // Use the 'strict' SSLConnector, i.e. 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()));
            setProxyAuth(requestClient);
        }
    }
    method.getParams().setBooleanParameter(HttpMethodDirector.PARAM_RESOLVE_HOSTNAME, param.shouldResolveRemoteHostname(hostName));
    // ZAP: Check if a custom state is being used
    if (state != null) {
        // Make sure cookies are enabled
        method.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        setProxyAuth(state);
    }
    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)

Aggregations

URIException (org.apache.commons.httpclient.URIException)52 URI (org.apache.commons.httpclient.URI)31 IOException (java.io.IOException)9 HttpMethod (org.apache.commons.httpclient.HttpMethod)8 Header (org.apache.commons.httpclient.Header)7 HttpClient (org.apache.commons.httpclient.HttpClient)6 ArrayList (java.util.ArrayList)5 Matcher (java.util.regex.Matcher)5 EntityEnclosingMethod (org.apache.commons.httpclient.methods.EntityEnclosingMethod)5 GetMethod (org.apache.commons.httpclient.methods.GetMethod)5 DatabaseException (org.parosproxy.paros.db.DatabaseException)5 HttpMalformedHeaderException (org.parosproxy.paros.network.HttpMalformedHeaderException)4 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 InputStreamReader (java.io.InputStreamReader)3 PatternSyntaxException (java.util.regex.PatternSyntaxException)3 HttpException (org.apache.commons.httpclient.HttpException)3 HttpMessage (org.parosproxy.paros.network.HttpMessage)3 InvalidParameterException (java.security.InvalidParameterException)2 HashMap (java.util.HashMap)2