Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jodd-http readFrom #657

Closed
sdragoncai opened this issue Nov 9, 2018 · 4 comments
Closed

jodd-http readFrom #657

sdragoncai opened this issue Nov 9, 2018 · 4 comments

Comments

@sdragoncai
Copy link

Code


HttpRequest request = HttpRequest.post("http://127.0.0.1:8086/test");
request.form("a", null);
request.form("b", "aaa");
System.out.println(new String(request.toByteArray()));
System.out.println(HttpRequest.readFrom(new ByteArrayInputStream(request.toByteArray())));

##Result

POST /test HTTP/1.1
Connection: Close
Content-Length: 7
Content-Type: application/x-www-form-urlencoded
Host: 127.0.0.1:8086
User-Agent: Jodd HTTP

a&b=aaa
POST /test HTTP/1.1
Connection: Close, Close
Content-Length: 9
Content-Type: application/x-www-form-urlencoded
Host: 127.0.0.1:8086
User-Agent: Jodd HTTP

a%26b=aaa

body is wrong,"&"被解析成"%26"

@sdragoncai
Copy link
Author

I can't get the value of B

@moh-sushi
Copy link
Member

System.out.println(new String(request.toByteArray()));  //  used charset not ISO_8859_1;  maybe UTF-8 ?
System.out.println(HttpRequest.readFrom(new ByteArrayInputStream(request.toByteArray())));  // ISO_8859_1 will be used

I think the difference is based on various used encodings.

Plz, change to your default jvm charset (e.g. UTF-8)

System.out.println(HttpRequest.readFrom(new ByteArrayInputStream(request.toByteArray()), "UTF-8"));

What is the output?

@sdragoncai
Copy link
Author

same result!
I think the parsing is wrong,at at jodd.http.HttpUtil.parseQuery(query,decode)

public static HttpMultiMap<String> parseQuery(final String query, final boolean decode) {

		HttpMultiMap<String> queryMap = HttpMultiMap.newCaseInsensitiveMap();

		int ndx, ndx2 = 0;
		while (true) {
			ndx = query.indexOf('=', ndx2);
			if (ndx == -1) {
				if (ndx2 < query.length()) {
					queryMap.add(query.substring(ndx2), null);
				}
				break;
			}
			String name = query.substring(ndx2, ndx);
			if (decode) {
				name = URLDecoder.decodeQuery(name);
			}

			ndx2 = ndx + 1;

			ndx = query.indexOf('&', ndx2);

			if (ndx == -1) {
				ndx = query.length();
			}

			String value = query.substring(ndx2, ndx);

			if (decode) {
				value = URLDecoder.decodeQuery(value);
			}

			queryMap.add(name, value);

			ndx2 = ndx + 1;
		}

		return queryMap;
	}

if "query" value is "&a&b=value1&c=value2&",the first parameter name is "&a&b"

Code

System.out.println(HttpUtil.parseQuery("&a&b=value1&c=value2&", true));

Result

&a&b: value1
c: value2

Expected Result

[empty string]:[null value]
a:[null value]
b: value1
c: value2
[empty string]:[null value]

@igr igr closed this as completed in 12c8022 Nov 17, 2018
@igr
Copy link
Member

igr commented Nov 17, 2018

Yap, the parsing was wrong :(

Thank you @sdragoncai!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants