nedpals / vex

Easy-to-use, modular web framework built for V
https://nedpals.github.io/vex
MIT License
339 stars 28 forks source link

Why is the variable address the same for each request #35

Closed xiusin closed 3 years ago

xiusin commented 3 years ago

https://github.com/nedpals/vex/blob/master/router/router.v#L44

pub fn (r Router) receive(method string, path string, raw_headers []string, body []byte) (int, []byte, []byte) {
    def_header := ('\r\n' + raw_headers.join('\r\n')).bytes()
    req_path := urllib.parse(path) or {
        internal_err_body := r.respond_error(500)
        return 500, def_header, internal_err_body
    }
    mut req := ctx.Req{
        method: method
        path: req_path.path
        raw_query: req_path.raw_query
        ctx: r.ctx
    }
    mut res := ctx.Resp{}
    req.parse_headers(raw_headers)
    println(ptr_str(&res.headers)) // here

print :

[VEX] HTTP Server has started.
[VEX] Running On http://localhost:6789
7ffee50e2ea0
/api/login
{'Content-Type': ['text/html; charset=UTF-8'], 'X-Powered-By': ['VEX/0.3.5'], 'Server': ['VEX'], 'Access-Control-Allow-Origin': ['http://localhost:3100'], 'Access-Control-Allow-Methods': ['POST'], 'Access-Control-Allow-Headers': ['x-requested-with, content-type']}
7ffee50e2ea0
/api/login
admin_login
No appropriate content type header for body found.
7ffee50e2ea0
/api/login
{'Content-Type': ['text/html; charset=UTF-8'], 'X-Powered-By': ['VEX/0.3.5'], 'Server': ['VEX']}
nedpals commented 3 years ago

I didn't know they have the same memory address for this. But if that's your concern, what are the implication for this?

xiusin commented 3 years ago

i write a middleware cors.v

the code as follows:

pub fn cors(mut req ctx.Req, mut res ctx.Resp) {
    res.headers["Access-Control-Allow-Origin"] = ["http://localhost:3100"]
    res.headers["Access-Control-Allow-Methods"] = ["POST"]
    res.headers["Access-Control-Allow-Headers"] = ["x-requested-with, content-type"]
        println(res.headers) // here
}

but only first request print info :

{'Content-Type': ['text/html; charset=UTF-8'], 'X-Powered-By': ['VEX/0.3.5'], 'Server': ['VEX'], 'Access-Control-Allow-Origin': ['http://localhost:3100'], 'Access-Control-Allow-Methods': ['POST'], 'Access-Control-Allow-Headers': ['x-requested-with, content-type']}

Other requests print info:

{'Content-Type': ['text/html; charset=UTF-8'], 'X-Powered-By': ['VEX/0.3.5'], 'Server': ['VEX']}

So, only the first request is effective.

ZihxS commented 8 months ago

Hi @xiusin, i have same case, how you handle for this case?

xiusin commented 8 months ago

@ZihxS I suggest using vweb, which seems to be no longer maintained.

I also wrote a simple framework, you can try it if you are interested.