v
v copied to clipboard
vweb : bug with cookies
Describe the bug
When I set or get a cookie, I set or get only the beginning of the cookie.
Expected Behavior
Get or set the entire cookie.
Current Behavior
The cookie length changes every time the code in the next paragraph is executed.
Reproduction Steps
module main
import vweb
import rand
struct App {
vweb.Context
}
fn main() {
app := App{}
vweb.run(app, 8888)
}
["/"]
fn (mut app App) index() vweb.Result {
cookie := rand.ascii(256)
app.set_cookie(name: "test", value: cookie)
println(cookie)
println(cookie.len)
received_cookie := app.get_cookie("test") or {""}
println(received_cookie)
println(received_cookie.len)
return app.text("ok")
}
Possible Solution
No response
Additional Information/Context
No response
V version
V 0.3.3 17fe763
Environment details (OS name and version, etc.)
V full version: V 0.3.3 7939ca2.17fe763
OS: linux, Zorin OS 16.2
Processor: 2 cpus, 64bit, little endian, Intel(R) Celeron(R) N4020 CPU @ 1.10GHz
getwd: /home/riccardo
vexe: /home/riccardo/v/v
vexe mtime: 2023-03-20 10:43:51
vroot: OK, value: /home/riccardo/v
VMODULES: OK, value: /home/riccardo/.vmodules
VTMP: OK, value: /tmp/v_1000
Git version: git version 2.25.1
Git vroot status: weekly.2023.11-39-g17fe7636
.git/config present: true
CC version: cc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3
Actually, it appears this is working correctly, just not intuitively.
The rand.ascii() function is generating the correct number of values, they are just not all printable values. Trying to print the cookie as a string will make it appear to be a different length every time due to this.
Using dump instead of println illustrates that this is happening.
Then get_cookie hits an issue because of this... it only gets up to the first non-printable character, so it is truncated.
In order for this code to work we would need a separate rand.printable_ascii() routine, or some other way to constrain the values.
ah okayyyyy thanks a lot :)
Try using https://modules.vlang.io/rand.html#string
or string_from_set
set_cookie should follow the official rules: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#attributes
If you try to set a cookie that doesn't follow the rules, it should return an error. Let's leave this open until that can be fixed.