11import { Request , Response } from "express"
22import * as path from "path"
3- import * as qs from "qs"
43import * as pluginapi from "../../../typings/pluginapi"
54import { HttpCode , HttpError } from "../../common/http"
65import { ensureProxyEnabled , authenticated , ensureAuthenticated , ensureOrigin , redirect , self } from "../http"
76import { proxy as _proxy } from "../proxy"
87
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 ) } `
1512}
1613
1714export async function proxy (
@@ -34,15 +31,14 @@ export async function proxy(
3431 throw new HttpError ( "Unauthorized" , HttpCode . Unauthorized )
3532 }
3633
34+ // The base is used for rewriting (redirects, target).
3735 if ( ! opts ?. passthroughPath ) {
38- // Absolute redirects need to be based on the subpath when rewriting.
39- // See proxy.ts.
4036 ; ( req as any ) . base = req . path . split ( path . sep ) . slice ( 0 , 3 ) . join ( path . sep )
4137 }
4238
4339 _proxy . web ( req , res , {
4440 ignorePath : true ,
45- target : getProxyTarget ( req , opts ?. passthroughPath ) ,
41+ target : getProxyTarget ( req ) ,
4642 } )
4743}
4844
@@ -55,8 +51,14 @@ export async function wsProxy(
5551 ensureProxyEnabled ( req )
5652 ensureOrigin ( req )
5753 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+
5860 _proxy . ws ( req , req . ws , req . head , {
5961 ignorePath : true ,
60- target : getProxyTarget ( req , opts ?. passthroughPath ) ,
62+ target : getProxyTarget ( req ) ,
6163 } )
6264}
0 commit comments