Skip to content

Commit 164e4eb

Browse files
committed
preserve path and query encoding
1 parent 0795204 commit 164e4eb

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

src/main/kotlin/org/digma/intellij/plugin/ui/jcef/ApiProxyResourceHandler.kt

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,37 @@ class ApiProxyResourceHandler(val project: Project) : CefResourceHandler {
3838
return URI(SettingsState.getInstance().apiUrl).toURL()
3939
}
4040

41+
/**
42+
* buildProxyUrl builds the proxied url from base url and the raw request we have from jcef.
43+
* it should preserve encoding as received from the original url. the resulting url should be
44+
* encoded the same as the original url we got from jcef.
45+
*/
4146
fun buildProxyUrl(apiBaseUrl: URL, cefRawRequestUrl: String): URL {
4247

4348
//This method should be tested.
4449
//should always be used to build the proxy url when needed,
4550
//it should make sure to preserve encoding of path and query.
4651
//see unit test : org.digma.intellij.plugin.ui.jcef.proxy.ApiProxyTests.testUrls
4752
//some constructors of URI and URL will encode the path and query, some don't.
48-
//here we make sure to preserve the path and query as they were received from jcef
49-
53+
//here we make sure to preserve the path and query as they were received from jcef.
54+
//we uee URI constructors that don't encode the path and query. there are constructors of
55+
// URI that will encode the path and query, in that case we will end up with double encoding.
56+
// one constructor that will validate and encode illegal characters in url and will just encode path and query is
57+
//java.net.URI.URI(String scheme,
58+
// String userInfo, String host, int port,
59+
// String path, String query, String fragment)
60+
//we use a constructor that does not encode.
61+
62+
//this URI constructor will not encode path and query
5063
val requestUrl = URI(cefRawRequestUrl).toURL()
64+
65+
66+
/*
67+
Implementation note:
68+
this code will just concatenate some string and build a url.
69+
it supports everything we need to support now and preserves the original encoding of the
70+
path and query as received from jcef.
71+
*/
5172
var apiUrl = apiBaseUrl.toString()
5273
if (requestUrl.path != null) {
5374
apiUrl = apiUrl.removeSuffix("/").plus(requestUrl.path.removePrefix(URL_PREFIX))
@@ -57,6 +78,39 @@ class ApiProxyResourceHandler(val project: Project) : CefResourceHandler {
5778
}
5879

5980
return URI(apiUrl).toURL()
81+
82+
83+
/*
84+
this constructor of java.net.URI expects that path and query be decoded strings, it will remove illegal url characters
85+
and replace them , will actually encode the string. if the string is already encoded we will end up with double encoding.
86+
'%23' will become %2523.
87+
other constructors of java.net.URI do not behave the same and do not double encode.
88+
but we still want to use this constructor because we need to build one url from two urls,using the URL class api is comfortable and safe
89+
because it supports all the http spec.
90+
our solution is to decode path and query before sending to this constructor.
91+
and make sure that this code is tested because it may change between JVM versions.
92+
*/
93+
94+
/*
95+
Implementation note:
96+
this code will use a URI constructor that expects the path and query to be legal url characters and will encode
97+
illegal characters.in order to use this constructor we need to decode path and query, and rely on the constructor that will
98+
encode them again. but this decoding -> encoding may have edge cases that will not completely preserve the original encoding.
99+
the advantage of this constructor is that it fully supports the http protocol and will do all the necessary validations.
100+
todo: currently we don't use this constructor but it can be used with decoding path and query.
101+
*/
102+
// return URI(
103+
// apiBaseUrl.protocol,
104+
// apiBaseUrl.userInfo,
105+
// apiBaseUrl.host,
106+
// apiBaseUrl.port,
107+
// requestUrl.path?.let {
108+
// URLDecoder.decode(it.removePrefix(URL_PREFIX), StandardCharsets.UTF_8)
109+
// },
110+
// requestUrl.query?.let { URLDecoder.decode(it, StandardCharsets.UTF_8) },
111+
// null
112+
// ).toURL()
113+
60114
}
61115
}
62116

0 commit comments

Comments
 (0)