daurnimator / lua-http

HTTP Library for Lua. Supports HTTP(S) 1.0, 1.1 and 2.0; client and server.
https://daurnimator.github.io/lua-http/
MIT License
779 stars 80 forks source link

Cookie Example? #149

Closed RussellHaley closed 5 years ago

RussellHaley commented 5 years ago

Hi @daurnimator,

I'm just approaching using cookies and was wondering if an example could be provided?

daurnimator commented 5 years ago

An example of accomplishing what?

RussellHaley commented 5 years ago

(Facepalm. My bad) We have an existing node.js "IoT" server that I am modifying to use as a demo application. The client passes login credentials as a post request (over a TLS connection) and the server returns a token as a cookie. I would then pass back that cookie on any subsequent requests to the server.

While I would be interested in seeing an example of a server that returns a token as a cookie, it's the client side that I wanted to code in Lua. Receiving, storing and returning the token is what I'm focused on at the moment.

daurnimator commented 5 years ago

it's the client side that I wanted to code in Lua. Receiving, storing and returning the token is what I'm focused on at the moment.

A request object has a cookie_store field. It should just "work out of the box": if a server returns a Set-Cookie header, then it will be added to the request's cookie store. When you make the next request, it will attach any relevant cookies. For manually editing cookeis, see the docs for the cookie store object

daurnimator commented 5 years ago

I would be interested in seeing an example of a server that returns a token as a cookie

local http_cookie = require "http.cookie"
local function my_onstream_handler(request_headers, stream)
    -- .....
    response_headers:append("set-cookie", http_cookie.bake("mycookiename", "mycookievalue"))
    -- .....
end
RussellHaley commented 5 years ago

Okay, I have a little more of my use case now: The first request I send is a 'login' POST request and if successful the 'token' is returned as a cookie. My request URI is like this: http://myurl.com/auth/signin?username=notarealboy&passwd=notarealpassword

The next request will be a GET request to update the status from the client (I didn't write this API): http://myurl.com/device/SomeDeviceName?status=okay Is there a way to reuse the prior request object (and cookie) or do I need to save the cookie to file and create a new request?

Also, I am unfortunately splitting my questions between github and lua-l. Please let me know where you'd prefer that I ask these questions?

daurnimator commented 5 years ago

Is there a way to reuse the prior request object (and cookie) or do I need to save the cookie to file and create a new request?

The default cookiejar will be reused automatically if you create a new request: it's opt-out; not opt-in.

Also, I am unfortunately splitting my questions between github and lua-l. Please let me know where you'd prefer that I ask these questions?

I'm fine with either.

RussellHaley commented 5 years ago

Awesome, works as advertised. Your DEBUG_SOCKET suggestion in #151 was what I needed to see what's going on.