/** * Cookie-based session store */ #ifndef SESSION_STORE_H #define SESSION_STORE_H #include "esp_http_server.h" #define SESSION_EXPIRY_TIME_S 60*30 #define SESSION_COOKIE_NAME "SESSID" /** function that frees a session data object */ typedef void (*sess_data_free_fn_t)(void *); enum session_find_action { SESS_DROP, SESS_GET_DATA }; /** * Find session and either get data, or drop it. * * @param cookie * @param action * @return */ void *session_find_and(const char *cookie, enum session_find_action action); /** * Initialize the session store. * Safely empty it if initialized */ void session_store_init(void); // placeholder for when no data is stored #define SESSION_DUMMY ((void *) 1) /** * Create a new session. Data must not be NULL, because it wouldn't be possible * to distinguish between NULL value and session not found in return values. * It can be e.g. 1 if no data storage is needed. * * @param data - data object to attach to the session * @param free_fn - function that disposes of the data when the session expires * @return NULL on error, or the new session ID. This is a live pointer into the session structure, * must be copied if stored, as it can become invalid at any time */ const char *session_new(void *data, sess_data_free_fn_t free_fn); /** * Find a session by it's ID (from a cookie) * * @param cookie - session ID string * @return session data (void*), or NULL */ void *session_find(const char *cookie); /** * Loop through all sessions and drop these that expired. */ void session_drop_expired(void); /** * Drop a session by its ID. Does nothing if not found. * * @param cookie - session ID string */ void session_drop(const char *cookie); /** * Parse the Cookie header from a request, and do something with the corresponding session. * * To also delete the cookie, use req_delete_session_cookie(r) * * @param r - request * @param action - what to do with the session * @return session data, NULL if removed or not found */ void *httpd_req_find_session_and(httpd_req_t *r, enum session_find_action action); /** * Add a header that deletes the session cookie * * @param r - request */ void httpd_resp_delete_session_cookie(httpd_req_t *r); /** * Add a header that sets the session cookie. * * This must be called after creating a session (e.g. user logged in) to make it persistent. * * @attention NOT RE-ENTRANT, CAN'T BE USED AGAIN UNTIL THE REQUEST IT WAS CALLED FOR IS DISPATCHED. * * @param r - request * @param cookie - cookie ID */ void httpd_resp_set_session_cookie(httpd_req_t *r, const char *cookie); #endif //SESSION_STORE_H