Android: mysterious shielded single quote in Uri coded title name

I use PlaceAutocomplete to get the name of the place, which I then encode and send to the remote host as part of the GET request (in the URL).

On the remote host, I found that some user devices send single quotes that are escaped, which causes a problem because the place name is sent as part of the URL.

eg. " Andy Place " is sent as " Andy\ Place "

and the backslash \ is a problem when part of the URL is for obvious reasons.

... but basically it doesnโ€™t happen ... for example. on my own test devices this is not ... the place is sent / received as " Andy Place ".

I am trying to go through the chain from beginning to end to see where unintended shielding can occur. I tried to summarize the chain below, leaving some things in this path (for example, I not only send placeName, I send it with other things) ... I hope that I have included all the important bits:

 Place place = PlaceAutocomplete.getPlace(this, data); String placeName = (String) place.getName(); EditText_placeName.setText(placeName); // ... later ... String newPlaceName = EditText_placeName.getText().toString(); String encodedPlaceName = Uri.encode(newPlaceName); URL url = new URL(strBaseURL + '/' + encodedPlaceName); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); responseCode = connection.getResponseCode(); 

Is there anything in this chain that can enter an unwanted escape character in the url on some devices but not others?

As a kind of footnote, I know that sending POST data (in the body) and not GET (as part of the URL) is likely to be better (probably completely solve my problem) and this is actually what I am moving forward now, but I would still like to know how this symbol \ passes through the above chain, at least partially out of curiosity.

+5
source share
1 answer

OK. I understood this, and the โ€œproblemโ€ actually occurs after the processing chain mentioned in my initial post. I will tell you in detail about what is really happening, just in case, when it helps someone else in this situation.

\' really appears as a result of how the string was written to the node.js. application Whenever a timeout occurs in the node.js application (which happens only occasionally, and as it turns out, has nothing to do with the problem \' ), I stop processing and return an error message.

For diagnostic purposes, I add a URL that has timed out to the Error object (either I create it myself or one that already exists), so that I can again access this URL to try to recreate the problem and see what could cause a timeout.

So something like:

 var err = new Error('oh dear'); err.recreate = theURLString; // the URL for the request that led to the problem console.log(err); 

As it happens, when I register this err on the console (as indicated above), i.e. when \' is entered ... i.e. any character ' in theURLString is output as \' .

Therefore, I assume that this should be the implementation of the .toString() of the Error object that does this. When I do the following:

 console.log(JSON.stringify(err)); 

... the sequence \' not displayed.

or even just the original URL string:

 console.log(theURLString); 

Thus, the problem (such as it was) was only at the logging stage, and not earlier. And this did not happen with every request having ' in the url, simply because not every request with ' caused a timeout (usually the problem was simply overloaded with resources). Everything that was before just threw me away.

+5
source

Source: https://habr.com/ru/post/1268953/


All Articles