ocdownloader icon indicating copy to clipboard operation
ocdownloader copied to clipboard

Fix URL check

Open Jwiggiff opened this issue 3 years ago • 0 comments

This pull request changes the URL check for http downloads. Rather than using the previous REGEX, it uses the URL constructor built in to JavaScript. The previous REGEX had some weaknesses, including any URLs that contained square brackets (and probably some other characters as well). The new method uses the URL constructor in JavaScript. Rather than checking with a REGEX, it attempts to construct a new URL object using the provided URL string within a try/catch statement. This will throw an error on a malformed URL, which then returns false. This process is illustrated below.

try {
  new URL(URLString);
  return true; // Is a valid url since it was able to construct a URL object
} catch (_) {
  return false; // Invalid url since an error was thrown when constructing the object
}

Jwiggiff avatar Jan 20 '22 03:01 Jwiggiff