Skip to content

Commit

Permalink
Merge pull request #740 from podverse/develop
Browse files Browse the repository at this point in the history
Release v4.16.10
  • Loading branch information
mitchdowney authored Apr 19, 2024
2 parents f4bbe21 + 46450f1 commit f9bbcad
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "podverse-api",
"version": "4.16.9",
"version": "4.16.10",
"description": "Data API, database migration scripts, and backend services for all Podverse models.",
"contributors": [
"Mitch Downey"
Expand Down Expand Up @@ -175,6 +175,7 @@
"eslint-plugin-jsx-a11y": "6.x",
"eslint-plugin-react": "7.x",
"eslint-plugin-react-hooks": "1.x",
"follow-redirects": "^1.15.6",
"googleapis": "45.0.0",
"http-errors": "1.7.3",
"husky": "3.1.0",
Expand Down
38 changes: 38 additions & 0 deletions src/lib/request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as requestPromiseNative from 'request-promise-native'
import { config } from '~/config'
const { userAgent } = config
const { http, https } = require('follow-redirects')

export const request = async (url: string, options?: any) => {
const headers = (options && options.headers) || {}
Expand All @@ -15,3 +16,40 @@ export const request = async (url: string, options?: any) => {

return response
}

export const getFinalRedirectedUrl = (url: string) => {
return new Promise<string>((resolve) => {
const parsedOrginalUrl = new URL(url)
if (url.startsWith('https://')) {
const frRequest = https.request(
{
method: 'HEAD',
hostname: parsedOrginalUrl.hostname,
path: parsedOrginalUrl.pathname + parsedOrginalUrl.search,
Headers: {
'User-Agent': userAgent
}
},
(response) => {
resolve(response.responseUrl)
}
)
frRequest.end()
} else {
const frRequest = http.request(
{
method: 'HEAD',
hostname: parsedOrginalUrl.hostname,
path: parsedOrginalUrl.pathname + parsedOrginalUrl.search,
Headers: {
'User-Agent': userAgent
}
},
(response) => {
resolve(response.responseUrl)
}
)
frRequest.end()
}
})
}
22 changes: 4 additions & 18 deletions src/routes/tools.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as Router from 'koa-router'
import { config } from '~/config'
import { emitRouterError } from '~/lib/errors'
import { request } from '~/lib/request'
const createError = require('http-errors')
import { getFinalRedirectedUrl } from '~/lib/request'

const router = new Router({ prefix: `${config.apiPrefix}${config.apiVersion}/tools` })

Expand All @@ -25,24 +24,11 @@ router.post('/findHTTPS', async (ctx) => {

const extractHTTPSFromURL = async (url, tries) => {
try {
const res = await request(url, { followRedirect: true, method: 'head' })
if (tries > 5) {
throw new createError.NotFound('Secure URL for ' + url + ' was not found. Too many redirects')
} else if (!res.location) {
if (url.startsWith('https://')) {
return url
} else {
try {
const attemptHttpsUrl = url.replace('http://', 'https://')
// If no error is thrown,then assume it is a valid url.
await request(attemptHttpsUrl, { followRedirect: true, method: 'head' })
return attemptHttpsUrl
} catch (error) {
throw new createError.NotFound('Secure URL for ' + url + ' was not found.')
}
}
return url
} else {
return extractHTTPSFromURL(res.location, tries + 1)
const redirectedUrl = await getFinalRedirectedUrl(url)
return redirectedUrl
}
} catch (error) {
if (['301', '302', '303', '307', '308'].includes(String(error.statusCode))) {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4360,7 +4360,7 @@ flush-write-stream@^1.0.0:
inherits "^2.0.3"
readable-stream "^2.3.6"

follow-redirects@^1.15.0:
follow-redirects@^1.15.0, follow-redirects@^1.15.6:
version "1.15.6"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.6.tgz#7f815c0cda4249c74ff09e95ef97c23b5fd0399b"
integrity sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==
Expand Down

0 comments on commit f9bbcad

Please sign in to comment.