1
1
import * as Router from 'koa-router'
2
2
import { config } from '~/config'
3
3
import { emitRouterError } from '~/lib/errors'
4
- import { request } from '~/lib/request'
5
- const createError = require ( 'http-errors' )
4
+ import { getFinalRedirectedUrl } from '~/lib/request'
6
5
7
6
const router = new Router ( { prefix : `${ config . apiPrefix } ${ config . apiVersion } /tools` } )
8
7
@@ -25,24 +24,11 @@ router.post('/findHTTPS', async (ctx) => {
25
24
26
25
const extractHTTPSFromURL = async ( url , tries ) => {
27
26
try {
28
- const res = await request ( url , { followRedirect : true , method : 'head' } )
29
27
if ( tries > 5 ) {
30
- throw new createError . NotFound ( 'Secure URL for ' + url + ' was not found. Too many redirects' )
31
- } else if ( ! res . location ) {
32
- if ( url . startsWith ( 'https://' ) ) {
33
- return url
34
- } else {
35
- try {
36
- const attemptHttpsUrl = url . replace ( 'http://' , 'https://' )
37
- // If no error is thrown,then assume it is a valid url.
38
- await request ( attemptHttpsUrl , { followRedirect : true , method : 'head' } )
39
- return attemptHttpsUrl
40
- } catch ( error ) {
41
- throw new createError . NotFound ( 'Secure URL for ' + url + ' was not found.' )
42
- }
43
- }
28
+ return url
44
29
} else {
45
- return extractHTTPSFromURL ( res . location , tries + 1 )
30
+ const redirectedUrl = await getFinalRedirectedUrl ( url )
31
+ return redirectedUrl
46
32
}
47
33
} catch ( error ) {
48
34
if ( [ '301' , '302' , '303' , '307' , '308' ] . includes ( String ( error . statusCode ) ) ) {
0 commit comments