From bd434ff285e313eb71880698df9eb428d0b08f02 Mon Sep 17 00:00:00 2001 From: NBTX Date: Sun, 25 Aug 2019 02:41:30 +0100 Subject: [PATCH] Improve URL validation check. (See https://github.com/liyasthomas/postwoman/issues/56) --- assets/css/styles.scss | 1 - pages/index.vue | 23 +++++++++++------------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/assets/css/styles.scss b/assets/css/styles.scss index e403b7d7f..6fc3f46ab 100644 --- a/assets/css/styles.scss +++ b/assets/css/styles.scss @@ -132,7 +132,6 @@ fieldset.blue legend { color: #57b5f9; } -<<<<<<< HEAD fieldset.gray { border-color: #9B9B9B; } diff --git a/pages/index.vue b/pages/index.vue index b7d26f0ed..c3b4e4f05 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -15,7 +15,7 @@
  • - +
  • @@ -23,7 +23,7 @@
  • - +
  • @@ -235,14 +235,13 @@ noHistoryToClear() { return this.history.length === 0; }, - urlNotValid() { - const pattern = new RegExp('^(https?:\\/\\/)?' + - '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + - '((\\d{1,3}\\.){3}\\d{1,3}))' + - '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + - '(\\?[;&a-z\\d%_.~+=-]*)?' + - '(\\#[-a-z\\d_]*)?$', 'i'); - return !pattern.test(this.url) + isValidURL() { + const protocol = '^(https?:\\/\\/)?'; + + const validIP = new RegExp(protocol + "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"); + const validHostname = new RegExp(protocol + "(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$"); + + return validIP.test(this.url) || validHostname.test(this.url); }, rawRequestBody() { const { @@ -302,8 +301,8 @@ }) }, sendRequest() { - if (this.urlNotValid) { - alert('Please check the formatting of the URL') + if (!this.isValidURL()) { + alert('Please check the formatting of the URL'); return } const n = new Date().toLocaleTimeString()