GithubHelp home page GithubHelp logo

Comments (6)

dom96 avatar dom96 commented on June 11, 2024

What does Sinatra do in this case?

from jester.

ThomasTJdev avatar ThomasTJdev commented on June 11, 2024

With Sinatra it differs whether it is pure URL parameter vs Javascript value with POST-request. On GET-request it parses like decodeUrl().

Minimal example with Sinatra

Sinatra backend

require 'sinatra'

post '/' do
  author = params['author']
  msg = params["msg"]

  puts author
  puts msg
  author
end

Javascript frontend

# B1
fetch("/?author=Message with plus+++signs", {
    method: "POST",
    body: new URLSearchParams( { msg: "Message with plus+++signs" })
})

Output on backend
The above Javascript (B1) uses inline URL parameters and formatted body. The server echo (puts) these values:

127.0.0.1 - - [23/May/2023:16:02:36 +0200] "POST /?author=Message%20with%20plus+++signs HTTP/1.1" 200 25 0.0034
Message with plus   signs  <== This is the @author
Message with plus+++signs  <== This is the @msg

What does Sinatra?

I'm no Ruby man.. I spent some time going through the code, but with no direct solution. is_frozen, HEADER_PARAMS..

from jester.

dom96 avatar dom96 commented on June 11, 2024

With Sinatra it differs whether it is pure URL parameter vs Javascript value with POST-request

Sounds like that is what we should do then :)

from jester.

ThomasTJdev avatar ThomasTJdev commented on June 11, 2024

With Sinatra it differs whether it is pure URL parameter vs Javascript value with POST-request

Sounds like that is what we should do then :)

Hehe, got you ;) .

Core problem

So, in Jester it all boils down to that non-URL params is already decoded within parseUrlQuery(). So when also decoding within the @-template we double decode - and that's some mess.

result[decodeUrl(key)] = decodeUrl(val)

Approaches

I have tried three approaches. But both 1 and 2 have breaking changes...

First try

  1. Solution (1) changes how we transport the params around. Instead of a Table[string, string] we now use var Table[string, tuple[value: string, frozen: bool]]. By doing this we identify the frozen parameters when using @. BUT now we will break code with:
for k, v in request.params:
# => Currently just a string
# => New: We now have tuple[string, bool]

master...ThomasTJdev:jester:decodeUrlParms

Second try

  1. Solution (2) removes the default decoding of non-URL params. Then we can always use @ since nothing is decoded as default. BUT this introduces a breaking change for code with:
for k, v in request.params:
# => `v` is not decoded, so users have to decode the data

master...ThomasTJdev:jester:decodeUrlParms2

Third try

  1. Solution (3) just ensures that everything is decoded - even though it is not needed. We currently decode() non-URL params by default, so this code then just default decode() URL-params too. This has no breaking changes but adds an overhead with the decodeUrl().

EDIT
This is breaking change for users accessing the raw parameters. Example:

Current

  var params: string
  for k, v in c.req.params:
    if params != "":
      params.add("&")
    params.add(k & "=" & v)

New

  var params: string
  for k, v in c.req.params:
    if params != "":
      params.add("&")
    params.add(k & "=" & encodeUrl(v))

master...ThomasTJdev:jester:decodeUrlParms3

from jester.

dom96 avatar dom96 commented on June 11, 2024

Wow, very thorough. Thank you. I think the "Third try" is the best, but I don't have strong feelings to be honest :)

from jester.

ThomasTJdev avatar ThomasTJdev commented on June 11, 2024

I care a lot about your library here :) ! Third option is added as a PR.

from jester.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.