Kladskull
Kladskull

Reputation: 10732

Runtime.exec() not behaving as expected

So I have a string I want to exec, a curl string... when it gets exec'd it is butchering my user-agent string...

Here is the string I am exec'ing...

/usr/bin/curl  -L --no-keepalive --max-time 30 --connect-timeout 30 --insecure --max-redirs 10 --stderr /var/folders/+j/+jqu+V1eEoSalBbXTff74U+++TI/-Tmp-/output7756019899402490058.tmp --cookie-jar /var/folders/+j/+jqu+V1eEoSalBbXTff74U+++TI/-Tmp-/cookies4551380191209065239.tmp --user-agent "1 2 3 4 5" --dump-header /var/folders/+j/+jqu+V1eEoSalBbXTff74U+++TI/-Tmp-/headers159122813500476027.tmp http://test.com

Here is the code I use to exec it

Process pr = null;
Runtime run = Runtime.getRuntime();
try {
    pr = run.exec(cmdline.split(" "));

    A ret = f.f(pr);

    pr.waitFor();

    return ret;
} catch (Exception ex) {
    throw new RuntimeException("Executing " + cmdline, ex);
} finally {
    try {
        // close all those bloody streams
        pr.getErrorStream().close();
        pr.getInputStream().close();
        pr.getOutputStream().close();
    } catch (IOException ex) {
        Log.get().exception(Log.Level.Error, "Closing stream: ", ex);
    }
}

Here is the apache logs with the user-agent messed up...

192.168.1.105 - - [07/Feb/2012:20:59:38 -0500] "GET / HTTP/1.1" 200 6791 "-" "\"1"

The expected result in apache should show the FULL user agent (in this case 1 2 3 4 5)

192.168.1.105 - - [07/Feb/2012:20:59:38 -0500] "GET / HTTP/1.1" 200 6791 "-" "1 2 3 4 5"

Upvotes: 0

Views: 365

Answers (2)

Woot4Moo
Woot4Moo

Reputation: 24316

I recommend passing it in delimited differently. I would use semicolon (;) or any non-volatile delimiter. and split the string that way. The thing to remember here is you do not care about what gets passed into your program only what you are willing to execute. Therefore your cmdLine variable should look like this:

--user-agent; "1 2 3 4 5"; --dump-header;

use String.trim() as necessary.

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160191

You're splitting on spaces, and "1 2 3 4 5" has spaces in it.

Upvotes: 3

Related Questions