Skip to content

Commit 0575045

Browse files
committed
https-request: refactor URL parsing
The function testing whether a given URL 404s already parses a URL into the options required by the `https.request()` function. Let's make that code reusable for the upcoming function to retrieve HTML from a given URL. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 81c2bcf commit 0575045

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

GitForWindowsHelper/https-request.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,23 @@ const httpsRequest = async (context, hostname, method, requestPath, body, header
6161
})
6262
}
6363

64-
const doesURLReturn404 = async url => {
64+
const parseURL = url => {
6565
const match = url.match(/^https:\/\/([^/]+?)(:\d+)?(\/.*)?$/)
6666
if (!match) throw new Error(`Could not parse URL ${url}`)
6767

68-
const https = require('https')
69-
const options = {
70-
method: 'HEAD',
68+
return {
69+
method: 'GET',
7170
host: match[1],
7271
port: Number.parseInt(match[2] || '443'),
7372
path: match[3] || '/'
7473
}
74+
}
75+
76+
const doesURLReturn404 = async url => {
77+
const options = parseURL(url)
78+
options.method = 'HEAD'
79+
80+
const https = require('https')
7581
return new Promise((resolve, reject) => {
7682
https.request(options, res => {
7783
if (res.error) reject(res.error)

0 commit comments

Comments
 (0)