diff --git a/tests.json b/tests.json index 4cbcff48..7e1ec45f 100644 --- a/tests.json +++ b/tests.json @@ -2783,5 +2783,78 @@ "stderr": "", "returncode": 0 } + }, + { + "input": { + "arguments": [ + "http://example.org/?a=&b=1" + ] + }, + "expected": { + "stdout": "http://example.org/?a=&b=1\n", + "stderr": "", + "returncode": 0 + } + }, + { + "input": { + "arguments": [ + "http://example.org/?a=1&b=" + ] + }, + "expected": { + "stdout": "http://example.org/?a=1&b=\n", + "stderr": "", + "returncode": 0 + } + }, + { + "input": { + "arguments": [ + "http://example.org/?a=1&b=&c=2" + ] + }, + "expected": { + "stdout": "http://example.org/?a=1&b=&c=2\n", + "stderr": "", + "returncode": 0 + } + }, + { + "input": { + "arguments": [ + "http://example.org/?a=1&b=&c=2", + "--json" + ] + }, + "expected": { + "stdout":[ + { + "url": "http://example.org/?a=1&b=&c=2", + "parts": { + "scheme": "http", + "host": "example.org", + "path": "/", + "query": "a=1&b=&c=2" + }, + "params": [ + { + "key": "a", + "value": "1" + }, + { + "key": "b", + "value": "" + }, + { + "key": "c", + "value": "2" + } + ] + } + ], + "stderr": "", + "returncode": 0 + } } ] diff --git a/trurl.c b/trurl.c index f8a9f4a0..c7d9cee7 100644 --- a/trurl.c +++ b/trurl.c @@ -1378,24 +1378,38 @@ static struct string *memdupzero(char *source, size_t len, bool *modified) else { int llen; int rlen; + int rightside; /* decode both sides */ left = decodequery(source, (int)(sep - source), &llen); if(!left) goto error; - right = decodequery(sep + 1, - (int)len - (int)(sep - source) - 1, &rlen); - if(!right) - goto error; + + /* length on the right side of '=': */ + rightside = (int)len - (int)(sep - source) - 1; + + if(rightside) { + right = decodequery(sep + 1, + (int)len - (int)(sep - source) - 1, &rlen); + if(!right) + goto error; + } + else { + right = NULL; + rlen = 0; + } + /* encode both sides again */ el = encodequery(left, llen); if(!el) goto error; - er = encodequery(right, rlen); - if(!er) - goto error; + if(right) { + er = encodequery(right, rlen); + if(!er) + goto error; + } - encode = curl_maprintf("%s=%s", el, er); + encode = curl_maprintf("%s=%s", el, er ? er : ""); if(!encode) goto error; }