Skip to content
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

Added method for parsing string body #350

Merged
merged 7 commits into from
May 27, 2024
Merged

Added method for parsing string body #350

merged 7 commits into from
May 27, 2024

Conversation

Pask423
Copy link
Contributor

@Pask423 Pask423 commented May 22, 2024

No description provided.

@Pask423 Pask423 requested a review from adamw May 22, 2024 11:40
@@ -11,4 +11,6 @@ private[sttp] object UriCompatibility {
}

def encodeQuery(s: String, enc: String): String = URIUtils.encodeURIComponent(s)

def encodeBodyPart(s: String, enc: String): String = URIUtils.encodeURIComponent(s)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's for encoding a part of a multipart body?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but not sure if we have multipart support for JS anywhere, I added it for sake of compatibility with other implementations of UriCompatibility

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sounds good, ok

@@ -18,5 +16,7 @@ private[sttp] object UriCompatibility {
Rfc3986.encode(Rfc3986.Host)(noSpecialChars)
}

def encodeQuery(s: String, enc: String): String = URLEncoder.encode(s, enc)
def encodeQuery(s: String, enc: String): String = Rfc3986.encode(s, enc, Rfc3986.Unreserved)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we also want to change how queries are encoded?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact the naming here is misleading it was not used for encoding Queries rather the All from the comment below

@@ -709,7 +709,7 @@ object Uri extends UriInterpolator {

object QuerySegmentEncoding {

/** Encodes all reserved characters using [[java.net.URLEncoder.encode()]]. */
/** Encodes all reserved characters using [[sttp.model.internal.Rfc3986]] with characters from [[sttp.model.internal.Rfc3986.Unreserved]]. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the All encoding is specifically created to encode all characters, as oppose to Standard, which only encodes the ones mentioned in the specs. So I wouldn't change this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously this was encoded by URLEncoder so in fact only change now is that ~ won't be encoded

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I this change required to fix the bug in sttp? It feels there's a couple of changes done at one time

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it will be a good idea to have same encoding here and for multiparts, I can revert it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep the changes minimal just to address the sttp bug. We can discuss other changes, but in separate PRs. That way we can better keep track of things that might get broken by the changes.

encode(s, "utf-8", allowedCharacters, spaceAsPlus, encodePlus)


def encode(s: String, enc: String, allowedCharacters: Set[Char]): String =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing comment

): String = {
val sb = new StringBuilder()
// based on https://gist.github.com/teigen/5865923
for (b <- s.getBytes("UTF-8")) {
for (b <- s.getBytes(enc)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use while loop to avoid boxing/unboxing of byte values

import org.scalatest.matchers.should.Matchers
import sttp.model.Uri._

class UriTestsHelper extends AnyFunSuite with Matchers with TryValues {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are we testing here, and why is this different from UriTests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS code seem to treat ~ as unreserved character while JVM one is encoding it so this case requited platform specific approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so these are platform-specific tests? the name doesn't suggest this at all

at a minimum, we need clear comments on why these tests are separate; but better (or additionally), we should rename the class. I think in other instances, we have a UriTestsExtensions trait, which is extended by UriTests, and is empty for unsupported platforms (see e.g. in tapir). So maybe let's do the same here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I will go with UriTestsExtensions

@adamw adamw merged commit 3cf523b into master May 27, 2024
10 checks passed
@adamw adamw deleted the url-spec-compat branch May 27, 2024 07:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants