public Request parse()

in src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/curl/BasicCurlParser.java [690:826]


    public Request parse(String commandLine) {
        String[] args = translateCommandline(commandLine);
        CLArgsParser parser = new CLArgsParser(args, OPTIONS);
        String error = parser.getErrorString();
        boolean isPostToGet = false;
        if (error == null) {
            List<CLOption> clOptions = parser.getArguments();
            Request request = new Request();
            for (CLOption option : clOptions) {
                if (option.getDescriptor().getId() == URL_OPT) {
                    request.setUrl(option.getArgument());
                } else if (option.getDescriptor().getId() == COMPRESSED_OPT) {
                    request.setCompressed(true);
                } else if (option.getDescriptor().getId() == HEADER_OPT) {
                    String nameAndValue = option.getArgument(0);
                    int indexOfColon = nameAndValue.indexOf(':');
                    if (indexOfColon >= 0) {
                        String name = nameAndValue.substring(0, indexOfColon).trim();
                        String value = nameAndValue.substring(indexOfColon + 1).trim();
                        request.addHeader(name, value);
                    } else if (nameAndValue.endsWith(";")) {
                            request.addHeader(nameAndValue.substring(0, nameAndValue.length() - 1), "");
                    } else {
                        LOGGER.warn("Could not parse header argument [{}] as it didn't contain a colon nor ended with a semicolon", nameAndValue);
                    }
                } else if (option.getDescriptor().getId() == METHOD_OPT) {
                    String value = option.getArgument(0);
                    request.setMethod(value);
                } else if (DATAS_OPT.contains(option.getDescriptor().getId())) {
                    String value = option.getArgument(0);
                    String dataOptionName = option.getDescriptor().getName();
                    if (value == null) {
                        value = "";
                    }
                    value = getPostDataByDifferentOption(value.trim(), dataOptionName);
                    if ("GET".equals(request.getMethod())) {
                        request.setMethod("POST");
                    }
                    request.setPostData(value);
                } else if (FORMS_OPT.contains(option.getDescriptor().getId())) {
                    String nameAndValue = option.getArgument(0);
                    int indexOfEqual = nameAndValue.indexOf('=');
                    String key = nameAndValue.substring(0, indexOfEqual).trim();
                    String value = nameAndValue.substring(indexOfEqual + 1).trim();
                    if ("form-string".equals(option.getDescriptor().getName())) {
                        request.addFormStringData(key, unquote(value));
                    } else {
                        if (value.charAt(0) == '@') {
                            request.addFormData(key, FileArgumentHolder.of(unquote(value.substring(1))));
                        } else {
                            request.addFormData(key, StringArgumentHolder.of(unquote(value)));
                        }
                    }
                    request.setMethod("POST");
                } else if (option.getDescriptor().getId() == USER_AGENT_OPT) {
                    request.addHeader("User-Agent", option.getArgument(0));
                } else if (option.getDescriptor().getId() == REFERER_OPT) {
                    request.addHeader("Referer", option.getArgument(0));
                } else if (option.getDescriptor().getId() == CONNECT_TIMEOUT_OPT) {
                    String value = option.getArgument(0);
                    request.setConnectTimeout(Double.parseDouble(value) * 1000);
                } else if (option.getDescriptor().getId() == COOKIE_OPT) {
                    String value = option.getArgument(0);
                    if (isValidCookie(value)) {
                        request.setCookies(value);
                    } else {
                        request.setFilepathCookie(value);
                    }
                } else if (option.getDescriptor().getId() == USER_OPT) {
                    String value = option.getArgument(0);
                    setAuthUserPasswd(value, request.getUrl(), request.getAuthorization());
                } else if (AUTH_OPT.contains(option.getDescriptor().getId())) {
                    String authOption = option.getDescriptor().getName();
                    setAuthMechanism(authOption, request.getAuthorization());
                } else if (SSL_OPT.contains(option.getDescriptor().getId())) {
                    request.setCaCert(option.getDescriptor().getName());
                } else if (option.getDescriptor().getId() == GET_OPT) {
                    isPostToGet = true;
                } else if (option.getDescriptor().getId() == DNS_OPT) {
                    String value = option.getArgument(0);
                    String[] dnsServer = value.split(",");
                    for (String s : dnsServer) {
                        request.addDnsServers(s);
                    }
                } else if (option.getDescriptor().getId() == NO_KEEPALIVE_OPT) {
                    request.setKeepAlive(false);
                } else if (option.getDescriptor().getId() == PROXY_OPT) {
                    String value = option.getArgument(0);
                    setProxyServer(request, value);
                } else if (option.getDescriptor().getId() == PROXY_USER_OPT) {
                    String value = option.getArgument(0);
                    setProxyServerUserInfo(request, value);
                } else if (option.getDescriptor().getId() == MAX_TIME_OPT) {
                    String value = option.getArgument(0);
                    request.setMaxTime(Double.parseDouble(value) * 1000);
                } else if (option.getDescriptor().getId() == HEAD_OPT) {
                    request.setMethod("HEAD");
                } else if (option.getDescriptor().getId() == INTERFACE_OPT) {
                    String value = option.getArgument(0);
                    request.setInterfaceName(value);
                } else if (option.getDescriptor().getId() == DNS_RESOLVER_OPT) {
                    String value = option.getArgument(0);
                    request.setDnsResolver(value);
                } else if (option.getDescriptor().getId() == LIMIT_RATE_OPT) {
                    String value = option.getArgument(0);
                    request.setLimitRate(value);
                } else if (option.getDescriptor().getId() == NOPROXY_OPT) {
                    String value = option.getArgument(0);
                    request.setNoproxy(value);
                } else if (IGNORE_OPTIONS_OPT.contains(option.getDescriptor().getId())) {
                    request.addOptionsIgnored(option.getDescriptor().getName());
                } else if (NOSUPPORT_OPTIONS_OPT.contains(option.getDescriptor().getId())) {
                    request.addOptionsNoSupport(option.getDescriptor().getName());
                } else if (PROPERTIES_OPT.contains(option.getDescriptor().getId())) {
                    request.addOptionsInProperties(
                            "--" + option.getDescriptor().getName() + " is in 'httpsampler.max_redirects(1062 line)'");
                } else if (option.getDescriptor().getId() == CLOption.TEXT_ARGUMENT
                        && !"CURL".equalsIgnoreCase(option.getArgument())) {
                    try {
                        request.setUrl(new URL(option.getArgument()).toExternalForm());
                    } catch (MalformedURLException ex) {
                        LOGGER.warn("Unhandled option {}", option.getArgument());
                    }
                }
            }
            if (isPostToGet) {
                String url = request.getUrl() + "?" + request.getPostData();
                request.setUrl(url);
                request.setPostData(null);
                request.setMethod("GET");
            }
            return request;
        } else {
            throw new IllegalArgumentException(
                    "Unexpected format for command line:" + commandLine + ", error:" + error);
        }
    }