16
16
17
17
import { Path } from '../Path' ;
18
18
import { RepoInfo } from '../../RepoInfo' ;
19
- import { warnIfPageIsSecure , fatal } from '../util' ;
19
+ import { warnIfPageIsSecure , warn , fatal } from '../util' ;
20
20
21
21
/**
22
22
* @param {!string } pathString
@@ -37,6 +37,26 @@ function decodePath(pathString: string): string {
37
37
return pathStringDecoded ;
38
38
}
39
39
40
+ /**
41
+ * @param {!string } queryString
42
+ * @return {!{[key:string]:string} } key value hash
43
+ */
44
+ function decodeQuery ( queryString : string ) : { [ key : string ] : string } {
45
+ let results = { } ;
46
+ if ( queryString . startsWith ( '?' ) ) {
47
+ queryString = queryString . substring ( 1 ) ;
48
+ }
49
+ for ( const segment of queryString . split ( '&' ) ) {
50
+ const kv = segment . split ( '=' ) ;
51
+ if ( kv . length === 2 ) {
52
+ results [ decodeURIComponent ( kv [ 0 ] ) ] = decodeURIComponent ( kv [ 1 ] ) ;
53
+ } else {
54
+ warn ( 'Invalid query string segment: ' + segment ) ;
55
+ }
56
+ }
57
+ return results ;
58
+ }
59
+
40
60
/**
41
61
*
42
62
* @param {!string } dataURL
@@ -119,13 +139,23 @@ export const parseURL = function(
119
139
dataURL = dataURL . substring ( colonInd + 2 ) ;
120
140
}
121
141
122
- // Parse host and path .
142
+ // Parse host, path, and query string .
123
143
let slashInd = dataURL . indexOf ( '/' ) ;
124
144
if ( slashInd === - 1 ) {
125
145
slashInd = dataURL . length ;
126
146
}
127
- host = dataURL . substring ( 0 , slashInd ) ;
128
- pathString = decodePath ( dataURL . substring ( slashInd ) ) ;
147
+ let questionMarkInd = dataURL . indexOf ( '?' ) ;
148
+ if ( questionMarkInd === - 1 ) {
149
+ questionMarkInd = dataURL . length ;
150
+ }
151
+ host = dataURL . substring ( 0 , Math . min ( slashInd , questionMarkInd ) ) ;
152
+ if ( slashInd < questionMarkInd ) {
153
+ // For pathString, questionMarkInd will always come after slashInd
154
+ pathString = decodePath ( dataURL . substring ( slashInd , questionMarkInd ) ) ;
155
+ }
156
+ let queryParams = decodeQuery (
157
+ dataURL . substring ( Math . min ( dataURL . length , questionMarkInd ) )
158
+ ) ;
129
159
130
160
// If we have a port, use scheme for determining if it's secure.
131
161
colonInd = host . indexOf ( ':' ) ;
@@ -146,6 +176,10 @@ export const parseURL = function(
146
176
} else if ( parts [ 0 ] . slice ( 0 , colonInd ) . toLowerCase ( ) === 'localhost' ) {
147
177
domain = 'localhost' ;
148
178
}
179
+ // Support `ns` query param if subdomain not already set
180
+ if ( subdomain === '' && 'ns' in queryParams ) {
181
+ subdomain = queryParams [ 'ns' ] ;
182
+ }
149
183
}
150
184
151
185
return {
0 commit comments