-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an option to disable URL Encoding #1352
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix breaking ABI and run ./gradlew -Poverwrite.output=true :binary-compatibility-validator:jvmTest
to ensure that all original methods exists.
*/ | ||
fun List<Pair<String, String?>>.formUrlEncode(): String = StringBuilder().apply { |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
Thanks for reviewing this @Prototik . Could you review this again? |
ReplaceWith("formUrlEncode(UrlEncodingOption.DEFAULT)", "io.ktor.http.UrlEncodingOption"), | ||
DeprecationLevel.HIDDEN | ||
) | ||
fun List<Pair<String, String?>>.formUrlEncode(): String = formUrlEncode(UrlEncodingOption.DEFAULT) |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
@@ -75,6 +88,14 @@ fun parametersOf(vararg pairs: Pair<String, List<String>>): Parameters = Paramet | |||
@Suppress("KDocMissingDocumentation") | |||
@InternalAPI | |||
class ParametersImpl(values: Map<String, List<String>> = emptyMap()) : Parameters, StringValuesImpl(true, values) { | |||
private var option: UrlEncodingOption = super.urlEncodingOption |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a hack for me.
Move urlEncodingOption
to primary constructor and provide a secondary deprecated constructor to keep abi (the same applies to ParametersBuilder
too).
All other things LGFM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the code looks straightforward to me 🙂 Thanks a lot for many useful advice!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgfm
The only question here: why do we disable escaping at all? The format you mentioned is not URL parameters at all so the only you need is to specify raw query string without parameters. Disabling escaping for sure will lead to security issues and strange bugs. |
I totally agree with you. Raw query string would be much better. Actually I tried to add raw query support, but I couldn't come up with any solution. If you have any ideas, please let me know.
By the way, it seems that So, the answer to this question
I hope this would help other developers who need to call legacy APIs... |
My personal use case is that the bittorrent protocal specifies that data encoded in the URL must be in binary format when making announce calls. It's a binary protocall after all.
So, when I make the following request val response = client.get<ByteArray>(announceUrl) {
url {
parameters.append("uploaded", uploaded.toString())
parameters.append("downloaded", downloaded.toString())
parameters.append("compact", if (acceptCompact) "1" else "0")
parameters.append(
"info_hash",
infoHash.chunked(2).joinToString("") { "%$it" }
)
parameters.append("peer_id", peerId)
parameters.append("port", port.toString())
parameters.append("left", "${torrentSize - downloaded}")
}
} I expect that the
but instead I get this.
notice here that the |
Is there any more discussion on this that needs to happen? I've since moved to retrofit which allows optional encoding via Retrofit is heavy handed for my use case and for the time being limits multiplatform capabilities. |
I'm affected by this as well in a pretty unpleasant way, and am wondering about getting this added 🤔 |
Some news about this issue? |
ef1c930
to
517116b
Compare
I've rebased this branch on the latest master. Please let me know if I need to change the code. I stopped using Ktor because of this issue, but updating this again because I received messages from some people who faced similar problems. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* Options for URL Encoding. | ||
* Keys and values are encoded only when [encodeKey] and [encodeValue] are `true` respectively. | ||
*/ | ||
@KtorExperimentalAPI |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need this annotation anymore
Thanks for the PR! Merged. |
Fixes [KTOR-553](https://youtrack.jetbrains.com/issue/KTOR-553) * Add UrlEncodingOption * Recover original code to keep binary backward compatibility * Improve test code for ParametersBuilder * Simplify args for @deprecated annotation * Replace urlEncodingOption function in Parameters with property * Refactor constructors of ParametersBuilder and ParametersImpl * rebase onto upstream master
Fixes [KTOR-553](https://youtrack.jetbrains.com/issue/KTOR-553) * Add UrlEncodingOption * Recover original code to keep binary backward compatibility * Improve test code for ParametersBuilder * Simplify args for @deprecated annotation * Replace urlEncodingOption function in Parameters with property * Refactor constructors of ParametersBuilder and ParametersImpl * rebase onto upstream master
Fixes [KTOR-553](https://youtrack.jetbrains.com/issue/KTOR-553) * Add UrlEncodingOption * Recover original code to keep binary backward compatibility * Improve test code for ParametersBuilder * Simplify args for @deprecated annotation * Replace urlEncodingOption function in Parameters with property * Refactor constructors of ParametersBuilder and ParametersImpl * rebase onto upstream master
Fixes [KTOR-553](https://youtrack.jetbrains.com/issue/KTOR-553) * Add UrlEncodingOption * Recover original code to keep binary backward compatibility * Improve test code for ParametersBuilder * Simplify args for @deprecated annotation * Replace urlEncodingOption function in Parameters with property * Refactor constructors of ParametersBuilder and ParametersImpl * rebase onto upstream master
Fixes [KTOR-553](https://youtrack.jetbrains.com/issue/KTOR-553) * Add UrlEncodingOption * Recover original code to keep binary backward compatibility * Improve test code for ParametersBuilder * Simplify args for @deprecated annotation * Replace urlEncodingOption function in Parameters with property * Refactor constructors of ParametersBuilder and ParametersImpl * rebase onto upstream master
Fixes [KTOR-553](https://youtrack.jetbrains.com/issue/KTOR-553) * Add UrlEncodingOption * Recover original code to keep binary backward compatibility * Improve test code for ParametersBuilder * Simplify args for @deprecated annotation * Replace urlEncodingOption function in Parameters with property * Refactor constructors of ParametersBuilder and ParametersImpl * rebase onto upstream master
Fixes [KTOR-553](https://youtrack.jetbrains.com/issue/KTOR-553) * Add UrlEncodingOption * Recover original code to keep binary backward compatibility * Improve test code for ParametersBuilder * Simplify args for @deprecated annotation * Replace urlEncodingOption function in Parameters with property * Refactor constructors of ParametersBuilder and ParametersImpl * rebase onto upstream master
Fixes [KTOR-553](https://youtrack.jetbrains.com/issue/KTOR-553) * Add UrlEncodingOption * Recover original code to keep binary backward compatibility * Improve test code for ParametersBuilder * Simplify args for @deprecated annotation * Replace urlEncodingOption function in Parameters with property * Refactor constructors of ParametersBuilder and ParametersImpl * rebase onto upstream master
Fixes [KTOR-553](https://youtrack.jetbrains.com/issue/KTOR-553) * Add UrlEncodingOption * Recover original code to keep binary backward compatibility * Improve test code for ParametersBuilder * Simplify args for @deprecated annotation * Replace urlEncodingOption function in Parameters with property * Refactor constructors of ParametersBuilder and ParametersImpl * rebase onto upstream master
Fixes [KTOR-553](https://youtrack.jetbrains.com/issue/KTOR-553) * Add UrlEncodingOption * Recover original code to keep binary backward compatibility * Improve test code for ParametersBuilder * Simplify args for @deprecated annotation * Replace urlEncodingOption function in Parameters with property * Refactor constructors of ParametersBuilder and ParametersImpl * rebase onto upstream master
Fixes [KTOR-553](https://youtrack.jetbrains.com/issue/KTOR-553) * Add UrlEncodingOption * Recover original code to keep binary backward compatibility * Improve test code for ParametersBuilder * Simplify args for @deprecated annotation * Replace urlEncodingOption function in Parameters with property * Refactor constructors of ParametersBuilder and ParametersImpl * rebase onto upstream master
Fixes [KTOR-553](https://youtrack.jetbrains.com/issue/KTOR-553) * Add UrlEncodingOption * Recover original code to keep binary backward compatibility * Improve test code for ParametersBuilder * Simplify args for @deprecated annotation * Replace urlEncodingOption function in Parameters with property * Refactor constructors of ParametersBuilder and ParametersImpl * rebase onto upstream master
Fixes [KTOR-553](https://youtrack.jetbrains.com/issue/KTOR-553) * Add UrlEncodingOption * Recover original code to keep binary backward compatibility * Improve test code for ParametersBuilder * Simplify args for @deprecated annotation * Replace urlEncodingOption function in Parameters with property * Refactor constructors of ParametersBuilder and ParametersImpl * rebase onto upstream master
Fixes [KTOR-553](https://youtrack.jetbrains.com/issue/KTOR-553) * Add UrlEncodingOption * Recover original code to keep binary backward compatibility * Improve test code for ParametersBuilder * Simplify args for @deprecated annotation * Replace urlEncodingOption function in Parameters with property * Refactor constructors of ParametersBuilder and ParametersImpl * rebase onto upstream master
Subsystem
ktor-http/common
Motivation
ktor-client cannot be used for URLs which are not supported by the URL Encoding format. It's because ktor-client always uses
URLBuidler
and there's no way to bypass the query encoding.This PR adds an option to disable URL Encoding via
ParametersBuilder
(which can be passed toURLBuidler
) without breaking changes.(I mentioned in this issue that
/api?k1=v1;k1=v1
(non-encoded;
) is preventing us to use ktor-client. We needed to switch to another library just because of one character in the URL.)Solution
formUrlEncodeTo()
method is doing actual encoding. Added an option to bypass the encoding toParametersBuilder
and passed it to the method.UrlEncodingOption
enum which provides encoding options (DEFAULT
,KEY_ONLY
,VALUE_ONLY
,NO_ENCODING
)ParametersBuilder
constructorParameters
interface as a method and toParametersImpl
as a private propertyformUrlEncodeTo()
method to do URL Encoding only when it's enabled by the optionDEFAULT
(i.e. keys and values are encoded) so as not to break existing code