1
1
import { Request , Response } from "express"
2
2
import * as path from "path"
3
- import * as qs from "qs"
4
3
import * as pluginapi from "../../../typings/pluginapi"
5
4
import { HttpCode , HttpError } from "../../common/http"
6
5
import { ensureProxyEnabled , authenticated , ensureAuthenticated , ensureOrigin , redirect , self } from "../http"
7
6
import { proxy as _proxy } from "../proxy"
8
7
9
- const getProxyTarget = ( req : Request , passthroughPath ?: boolean ) : string => {
10
- if ( passthroughPath ) {
11
- return `http://0.0.0.0:${ req . params . port } /${ req . originalUrl } `
12
- }
13
- const query = qs . stringify ( req . query )
14
- return encodeURI ( `http://0.0.0.0:${ req . params . port } ${ req . params [ 0 ] || "" } ${ query ? `?${ query } ` : "" } ` )
8
+ const getProxyTarget = ( req : Request ) : string => {
9
+ // If there is a base path, strip it out.
10
+ const base = ( req as any ) . base || ""
11
+ return `http://0.0.0.0:${ req . params . port } /${ req . originalUrl . slice ( base . length ) } `
15
12
}
16
13
17
14
export async function proxy (
@@ -34,15 +31,14 @@ export async function proxy(
34
31
throw new HttpError ( "Unauthorized" , HttpCode . Unauthorized )
35
32
}
36
33
34
+ // The base is used for rewriting (redirects, target).
37
35
if ( ! opts ?. passthroughPath ) {
38
- // Absolute redirects need to be based on the subpath when rewriting.
39
- // See proxy.ts.
40
36
; ( req as any ) . base = req . path . split ( path . sep ) . slice ( 0 , 3 ) . join ( path . sep )
41
37
}
42
38
43
39
_proxy . web ( req , res , {
44
40
ignorePath : true ,
45
- target : getProxyTarget ( req , opts ?. passthroughPath ) ,
41
+ target : getProxyTarget ( req ) ,
46
42
} )
47
43
}
48
44
@@ -55,8 +51,14 @@ export async function wsProxy(
55
51
ensureProxyEnabled ( req )
56
52
ensureOrigin ( req )
57
53
await ensureAuthenticated ( req )
54
+
55
+ // The base is used for rewriting (redirects, target).
56
+ if ( ! opts ?. passthroughPath ) {
57
+ ; ( req as any ) . base = req . path . split ( path . sep ) . slice ( 0 , 3 ) . join ( path . sep )
58
+ }
59
+
58
60
_proxy . ws ( req , req . ws , req . head , {
59
61
ignorePath : true ,
60
- target : getProxyTarget ( req , opts ?. passthroughPath ) ,
62
+ target : getProxyTarget ( req ) ,
61
63
} )
62
64
}
0 commit comments