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

[TIMOB-24695] Android: URL auto-encoding improvements #9178

Merged
merged 4 commits into from Jun 28, 2017

Conversation

jquick-axway
Copy link
Contributor

JIRA: https://jira.appcelerator.org/browse/TIMOB-24695

Changes:

  • No longer encodes legal chars !, $, ', (, ), +, ,, /, :, ;, ?, and @ in query params.
  • No longer encodes legal chars $, &, +, ,, ;, and = in URL's "username:password" component.
  • No longer encodes legal chars $, &, +, ,, ;, =, :, @, /, and ? in URL #fragment.
  • Spaces in the URL's query paramaters are now encoded as + instead of %20.
  • Given URL's %-encoded characters are now always preserved.
    • Used to decode %2F to / in URL path, which can break the path.

Test Procedure:

  1. Run the below code on an Android device that has Internet access.
  2. Tap the "URL Space Test" button.
  3. An alert dialog will appear. Verify that it displays the following:
    Received HTTP Request: POST /Space%20Test?test=Space+Test HTTP/1.1
  4. Close the alert dialog.
  5. Tap the "URL Path %2F Test" button.
  6. An alert dialog will appear. Verify that it displays the following:
    Received HTTP Request: POST /Slash%2FTest HTTP/1.1
  7. Close the alert dialog.
  8. Tap the "URL Query Encoding Test" button.
  9. An alert dialog will appear. Verify that it displays the following:
    Received HTTP Request: POST /?test=%22(!$'+,/:;?@)%22 HTTP/1.1
  10. Close the alert dialog.
  11. Tap the "URL Query Slash Test" button.
  12. An alert dialog will appear. Verify that it displays the following:
    Received HTTP Request: POST /?test=abc/xyz HTTP/1.1
  13. Close the alert dialog.
  14. Verify that an image is displayed at the bottom of the screen. (Was loaded via URL.)

Test Code:

var listenSocket = Ti.Network.Socket.createTCP(
{
	host : "localhost",
	port : 40404,
	accepted : function(e)
	{
		Ti.Stream.pump(e.inbound, function(e)
		{
			var message = "<empty>";
			if (e.buffer) {
				message = e.buffer.toString();
				var index = message.indexOf("\n");
				if (index > 0) {
					message = message.substr(0, index);
				}
			}
			alert("Received HTTP Request:\n" + message);
			listenSocket.accept({ timeout: 30000 });
		}, 1024, true);
	},
});
listenSocket.listen();
listenSocket.accept({ timeout: 30000 });

function createHttpButton(text, url) {
	var newButton = Ti.UI.createButton(
	{
		title: text,
		top: "5%",
	});
	newButton.addEventListener("click", function(e)
	{
		var httpClient = Ti.Network.createHTTPClient();
		httpClient.open("POST", url);
		httpClient.send();
	});
	return newButton;
}

var window = Titanium.UI.createWindow({ layout: "vertical" });
window.add(createHttpButton("URL Space Test", "http://localhost:40404/Space Test?test=Space Test"));
window.add(createHttpButton("URL Path %2F Test", "http://localhost:40404/Slash%2FTest"));
window.add(createHttpButton("URL Query Encoding Test", "http://localhost:40404/?test=\"(!$'+,/:;?@)\""));
window.add(createHttpButton("URL Query Slash Test", "http://localhost:40404?test=abc/xyz"));
window.add(Ti.UI.createImageView(
{
	image: "http://scontent.xx.fbcdn.net.rsz.io/v/t1.0-9/s720x720/17795851_419371511758070_7481565181564022529_n.jpg?oh=7772aea44d2f40fdbd42285ca61d7aac&oe=59771D60?mode=crop&width=333&height=250",
	top: "5%",
}));
window.open();

- No longer encodes legal chars '?', '/', '@', and ':' in query params.
- No longer encodes legal chars '$', '&', '+', ',', ';', and '=' in URL's "username:password" component.
- No longer encodes legal chars '$', '&', '+', ',', ';', '=', ':', '@', '/', and '?' in URL #fragment.
- Spaces in the URL's query paramaters are now encoded as '+' instead of "%20".
- Given URL's %-encoded characters are now always preserved.
  * Used to decode "%26" and "%3D" to '&' and '=' in query params, breaking the query.
  * Used to decode "%2F" to '/' in URL path, which can break the path.
- No longer encodes legal chars '!', '$', ''', '(', ')', '+', ',', and ';' in query params.
- Worked-around Android bug caused by TIMOB-24695 fix where a URL without a path would wrongly copy query param characters proceeding an unencoded '/' into the URL's path.
Copy link
Contributor

@garymathews garymathews left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CR: PASS

Looks great!

queryString = queryString.replace("%27", "'");
queryString = queryString.replace("%28", "(");
queryString = queryString.replace("%29", ")");
queryString = queryString.replace("%2B", "+");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, possibility to use regex here:

// this will cover %2B and %2b
queryString = queryString.replaceAll("(?i)%2b", "+");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea. I'll go do it now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to punt on this. The String.replaceAll() regex always does a string allocation, even if no substring replacements are done (I just verified this today). Versus String.replace() will return a reference to itself if no substrings were found, avoiding an unnecessary allocation. Hmm...

@lokeshchdhry
Copy link
Contributor

FR Passed.

Ran the code above & the auto encoding for the URL's works as expected.

Studio Ver: 4.9.0.201705302345
SDK Ver: 6.2.0 local build
OS Ver: 10.12.3
Xcode Ver: Xcode 8.3.3
Appc NPM: 4.2.9
Appc CLI: 6.2.2
Ti CLI Ver: 5.0.14
Alloy Ver: 1.9.11
Node Ver: 6.10.1
Java Ver: 1.8.0_101
Devices: ⇨ google Pixel --- Android 7.1.1
⇨ google Nexus 5 --- Android 6.0.1

@lokeshchdhry lokeshchdhry merged commit 09b24f3 into tidev:master Jun 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants