How to judge if a URL is already encoded with encodeURI? - vbscript

I'm trying to do it in VBScript/JScript, to avoid re-encoding.
Should I judge if there is "%" ? Does "%" have other uses in URL?
Thanks.
Edit: Oh, the original encoding function may not be encodeURI.
I'm trying to collect URLs from the browser, and store them after encoding with encodeURI.
But if the URL is already encoded, another encoding will make it wrong.

I might try decoding it and comparing the result to the original URL. If it changed or got shorter in length your original URL was probably already encoded.

iterate over the chars in the url and test for characters that aren't allowed
in an url.
if there are any encode it.
if there aren't any illegal characters, it doesn't matter

Related

Server.URLEncode started to replace blank with plus ("+") instead of percent-20 ("%20")

Given this piece of code:
<%
Response.Write Server.URLEncode("a doc file.asp")
%>
It output this for a while (like Javascript call encodeURI):
a%20doc%20file.asp
Now, for unknow reason, I get:
a+doc+file%2Easp
I'm not sure of what I touched to make this happen (maybe the file content encoding ANSI/UTF-8). Why did this happen and how can I get the first behavior of Server.URLEncode, ie using a percent encoding?
Classic ASP hasn't been updated in nearly 20 years, so Server.URLEncode still uses the RFC-1866 standard, which specifies spaces be encoded as + symbols (which is a hangover from an old application/x-www-form-urlencoded media type), you must be mistaken in thinking it was encoding spaces as %20 at some point, not unless there's an IIS setting you can change that I'm unaware of.
More modern languages use the RFC-3986 standard for encoding URLs, which is why Javascript's encodeURI function returns spaces encoded as %20.
Both + and %20 should be treated exactly the same when decoded by any browser thanks to RFC backwards compatibility, but it's generally considered best to use %20 when encoding spaces in a URL as it's the more modern standard now, and some decoding functions (such as Javascript's decodeURIComponent) won't recognise + symbols as spaces and will fail to properly decode URLs that use them over %20.
You can always use a custom function to encode spaces as %20:
function URL_encode(ByVal url)
url = Server.URLEncode(url)
url = replace(url,"+","%20")
URL_encode = url
end function

How to handle the javascript decoding error?

When the code is implemented, some characters cannot be decoded. I am getting a bunch of question marks like ??. How can I fix this?
HtmlInput inputBox2 = (HtmlInput)currentPage.getHtmlElementById("classNo");
inputBox2.setValueAttribute("2016同學15");
ScriptResult result = currentPage.executeJavaScript("javascript:Search(2)");
I found this in the compiler: ScriptResult[result=net.sourceforge.htmlunit.corejs.javascript.Undefined#24d7aac3 page=HtmlPage(http://www.xx.org/classNo=2016??15)#1330510442]
You might try to use URL-encoding for some ASCII and all non ASCII characters.
e.g. space by %20
Here is a web site explaning the
HTML URL Encoding Reference.
You can also interactive encode strings there.
Your "2016同學15" would be encoded as:
"2016%E5%90%8C%E5%AD%B815"

Catch 22 of Cocoa URL encoding

After checking how others try to do URL encoding in Cocoa (like How do I URL encode a string, Swift - encode URL, etc.) I still have no clue how to correctly URL encode in Cocoa if
URLs come from externally therefore their structure (parts) not known ahead
can be encoded or pure URL strings
BONUS can be relative and local file URLs
I do not want to encode blindly always all the characters but according to rfc3986 (rfc2396, rfc1738, rfc1808)
The catch 22:
stringByAddingPercentEscapesUsingEncoding: converts lazily so the preferred method would be using stringByAddingPercentEncodingWithAllowedCharacters: for each url components one by one
[NSURL URLWithString:], [NSURLComponents componentsWithString:] and companions will fail if the incoming string is not (at least partially) encoded, but if I pass a stringByAddingPercentEscapesUsingEncoding: encoded string than the component splitting will fail (f.e. the encoded # will confuse the splitter and the fragment will be treated the part of a possible query section
How to URL encode correctly in this case without writing my own URL parser, encoder?
You should read all of Apple's release note discussion of this subject, but in particular this part may be most relevant for your case:
If you need to percent-encode an entire URL string, you can use this
code to encode a NSString intended to be a URL (in urlStringToEncode):
NSString *percentEncodedURLString =
[[NSURL URLWithDataRepresentation:[urlStringToEncode dataUsingEncoding:NSUTF8StringEncoding] relativeToURL:nil] relativeString];
(The CoreFoundation equivalent to URLWithDataRepresentation: is
CFURLCreateWithBytes() using the encoding kCFStringEncodingUTF8 with a
fall back to kCFStringEncodingISOLatin1 if kCFStringEncodingUTF8
fails.)
Basically, +URLWithDataRepresentation:relativeToURL: does its best to make a proper URL from the provided bytes. Given that you can't guarantee almost anything about the input, there can't be any promises that it will get it "right" (because "right" isn't well defined in that case), but it's probably your best hope.

Ajax Push Engine (APE): How to Send Binary Data or Special Characters

This code example is from the APE official website: http://www.ape-project.org/
var client = new APE.Client();
client.load();
client.core.join('testChannel');
client.request.send('foo', {ping: 'ho hey', fieldWidthBinaryDataOrSpecialCharacters: '+/'});
client.onRaw('bar', function(raw, pipe) {
console.log('echo : ' + raw.data.echo);
console.log('Receiving : ' + raw.data.hello);
});
When I receive the data at the server side, I found that the special characters +/ has been URL encoded (%2B%2F).
Is APE always using GET? If we use POST, I think we can post any data including Binary data, right? But how to use POST in JSON?
My case is, even I don't use the Binary format, I have to use the Base64. But the standard Base64 uses +/ which is not URL safe. You might suggest using the URL safe version of Base64, but URLSafeBase64 is not standard and it might also create other problems.
Am I misunderstanding something?
Thanks.
Peter
I finally did two-step encoding:
The Client Side:
Base64 Encoding;
Server Side:
URL Decoding; (It seems the URL encoding is automatically done somewhere in the APE)
Base64 Decoding.
P.S.: If any one knows how to do it in a more efficient way of transferring binary data, please let me know, and I will re-mark your answer as the correct one.

When should I escape urls?

I have a URL and escaped it using:
url = "http://ec4.images-xxx.com/images/I/41-%2B6wMiewL._SL135_.jpg"
url = URI.escape(url)
puts url => "http://ec4.images-xxx.com/images/I/41-%252B6wMiewL._SL135_.jpg"
From the result I can see that URI escaped the previously escaped %2B again which became %252B, which is not correct.
I want to know how to make sure when one URL should be escaped. Or, is there a smart method that knows when to escape and when not to escape?
Your first string is already properly URI encoded, so when you try to re-encode it, the URI.escape method is encoding the '%' with '%25' (URI encoding for '+').
If you're really not sure whether your string has been URI encoded or not, you could try to decode it first, and compare it with the original. If they're the same, then it hasn't been encoded.

Resources