electricimp/Rocky

Support for cookies

ppetrosh opened this issue · 0 comments

It would be nice to have cookies supported in Rocky (to automatically set the cookies path to that of the agent to properly scope it):

// If we had http.agentpath(), this would be less error-prone.
local URL = http.agenturl();
local AGENT_ID = split(URL, "/")[2];
// And note that it needs to be the agent *path*, rather than *id*
// because (thought experiment)
// what if we changed agents to live under the 
// '<agent_id>.agent.electricimp.io' domain?

http.onrequest(function(req, res) {
    // This issues cookies with path=/, which means that the browser
    // sends them to any page on agent.electricimp.com;
    // this means that other agents can read it.

    // DON'T:
    //res.header("set-cookie",
    //            "ID=6853b162f152; max-age=3600");

    // DO: Scope the cookie to the agent path.
    res.header("set-cookie",
                "ID=6853b162f152; max-age=3600; path=/" + AGENT_ID);

    res.send(200, "OK");
});