Skip to content

Commit

Permalink
preserve path and query encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
shalom938 committed Jan 10, 2025
1 parent 0795204 commit 164e4eb
Showing 1 changed file with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,37 @@ class ApiProxyResourceHandler(val project: Project) : CefResourceHandler {
return URI(SettingsState.getInstance().apiUrl).toURL()
}

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

//This method should be tested.
//should always be used to build the proxy url when needed,
//it should make sure to preserve encoding of path and query.
//see unit test : org.digma.intellij.plugin.ui.jcef.proxy.ApiProxyTests.testUrls
//some constructors of URI and URL will encode the path and query, some don't.
//here we make sure to preserve the path and query as they were received from jcef

//here we make sure to preserve the path and query as they were received from jcef.
//we uee URI constructors that don't encode the path and query. there are constructors of
// URI that will encode the path and query, in that case we will end up with double encoding.
// one constructor that will validate and encode illegal characters in url and will just encode path and query is
//java.net.URI.URI(String scheme,
// String userInfo, String host, int port,
// String path, String query, String fragment)
//we use a constructor that does not encode.

//this URI constructor will not encode path and query
val requestUrl = URI(cefRawRequestUrl).toURL()


/*
Implementation note:
this code will just concatenate some string and build a url.
it supports everything we need to support now and preserves the original encoding of the
path and query as received from jcef.
*/
var apiUrl = apiBaseUrl.toString()
if (requestUrl.path != null) {
apiUrl = apiUrl.removeSuffix("/").plus(requestUrl.path.removePrefix(URL_PREFIX))
Expand All @@ -57,6 +78,39 @@ class ApiProxyResourceHandler(val project: Project) : CefResourceHandler {
}

return URI(apiUrl).toURL()


/*
this constructor of java.net.URI expects that path and query be decoded strings, it will remove illegal url characters
and replace them , will actually encode the string. if the string is already encoded we will end up with double encoding.
'%23' will become %2523.
other constructors of java.net.URI do not behave the same and do not double encode.
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
because it supports all the http spec.
our solution is to decode path and query before sending to this constructor.
and make sure that this code is tested because it may change between JVM versions.
*/

/*
Implementation note:
this code will use a URI constructor that expects the path and query to be legal url characters and will encode
illegal characters.in order to use this constructor we need to decode path and query, and rely on the constructor that will
encode them again. but this decoding -> encoding may have edge cases that will not completely preserve the original encoding.
the advantage of this constructor is that it fully supports the http protocol and will do all the necessary validations.
todo: currently we don't use this constructor but it can be used with decoding path and query.
*/
// return URI(
// apiBaseUrl.protocol,
// apiBaseUrl.userInfo,
// apiBaseUrl.host,
// apiBaseUrl.port,
// requestUrl.path?.let {
// URLDecoder.decode(it.removePrefix(URL_PREFIX), StandardCharsets.UTF_8)
// },
// requestUrl.query?.let { URLDecoder.decode(it, StandardCharsets.UTF_8) },
// null
// ).toURL()

}
}

Expand Down

0 comments on commit 164e4eb

Please sign in to comment.