Closed
Bug 644013
Opened 15 years ago
Closed 15 years ago
improve url validation
Categories
(Cloud Services :: Share: Firefox Client, defect)
Tracking
(Not tracked)
RESOLVED
FIXED
People
(Reporter: mixedpuppy, Assigned: mixedpuppy)
Details
(Whiteboard: [landed])
Attachments
(1 file, 2 obsolete files)
|
3.51 KB,
patch
|
philikon
:
review+
|
Details | Diff | Splinter Review |
We grab a bunch of urls out of content for sharing, and often they are malformed. There is a quick hack to do a simple validation, but I'm sure there is something in mozilla to do real url validation. see _validURL in panel.js of the addon.
| Assignee | ||
Comment 1•15 years ago
|
||
| Assignee | ||
Comment 2•15 years ago
|
||
I'm beginning to think the best solution is a regular expression since I cannot find any api in firefox that provides validation of good urls. Using nsIURI/nsIURL does not catch some invalid urls (below). Here's the regex I came up with:
^\w+?://\w+(?:\.\w+)*(?::\d+)?(?:/.*)?$
It works with the following set (correctly identifying valid urls).
http://invalid.comhttp://invalid.com
http:///invalid/path
http://invalid.com:foo/somepath
http://www.invalid.orghttp://s3.www.invalid.org/images/small_logo.png
http://valid/
http://valid.com
http://valid.com/somepath
http://valid.com:80/somepath
https://valid.com:80/somepath#foobar?test=1
http://s3.www.valid.org/images/small_logo.png
Anyone have a better idea?
Assignee: nobody → mixedpuppy
Comment 3•15 years ago
|
||
From http://search.cpan.org/~gaas/URI-1.58/URI.pm#PARSING_URIs_WITH_REGEXP
As an alternative to this module, the following (official) regular expression can be used to decode a URI:
my($scheme, $authority, $path, $query, $fragment) =
$uri =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|;
| Assignee | ||
Comment 4•15 years ago
|
||
| Assignee | ||
Updated•15 years ago
|
Whiteboard: [needs test]
Comment 5•15 years ago
|
||
Re [needs test]: This is a small enough function that the tests can be xpcshell-tests.
| Assignee | ||
Comment 6•15 years ago
|
||
in order to avoid setup of the panel I had to split the specific code I wanted to test into a function and export it, is there a better way?
Attachment #523457 -
Attachment is obsolete: true
Attachment #523612 -
Flags: review?(philipp)
Comment 7•15 years ago
|
||
Comment on attachment 523612 [details] [diff] [review]
fix with test
>+Cu.import("resource://gre/modules/NetUtil.jsm");
Unused import ;)
>+// First test urls that should fail validation
>+gTests.push(function test_url_validation_fail() {
>+ for (var i=0; i < bad_urls.length; i++) {
>+ do_check_eq(validateURL(bad_urls[i]), null);
>+ }
>+ run_next_test();
>+});
>+
>+// Test some good urls now
>+gTests.push(function test_url_validation_success() {
>+ for (var i=0; i < good_urls.length; i++) {
>+ do_check_false(validateURL(good_urls[i]) === null);
There's do_check_neq(), although we do know what the return value of validateURL() is when the URL is valid (it's the URL), so why not test for that?
>+ }
>+ run_next_test();
>+});
>+
>+
>+function run_test() {
>+ run_next_test();
>+}
That's a little more complex than it has to be. gTests + run_text_test() is only necessary when the tests are async. Here you can just do:
function run_test() {
// First test urls that should fail validation.
for (var i=0; i < bad_urls.length; i++) {
do_check_eq(validateURL(bad_urls[i]), null);
}
// Test some good urls now.
for (var i=0; i < good_urls.length; i++) {
do_check_eq(validateURL(good_urls[i]), good_urls[i]);
}
}
r=me with that.
Attachment #523612 -
Flags: review?(philipp) → review+
| Assignee | ||
Comment 8•15 years ago
|
||
Attachment #523612 -
Attachment is obsolete: true
Attachment #523618 -
Flags: review?(philipp)
| Assignee | ||
Comment 9•15 years ago
|
||
landing with previous r+ from philikon, all changes made, tests included
Status: NEW → RESOLVED
Closed: 15 years ago
Resolution: --- → FIXED
Whiteboard: [needs test] → [landed]
Updated•15 years ago
|
Attachment #523618 -
Flags: review?(philipp) → review+
Comment 10•15 years ago
|
||
You need to log in
before you can comment on or make changes to this bug.
Description
•