kphp
kphp copied to clipboard
Sessions
Sessions
This PR adds a simple implementation of php sessions in KPHP.
Implemented functions
session_start()session_abort()session_commit()session_write_close()session_gc()session_status()session_encode()session_decode()session_get_cookie_params()session_id()session_reset()session_unset()
Supported options
save_pathnamegc_probabilitygc_divisorgc_maxlifetimecookie_lifetimecookie_pathcookie_domaincookie_securecookie_httponlycookie_samesiteuse_strict_modesid_lengthlazy_write
Problems
- Storing open session variables within a single worker.
A simple implementation should avoid handling interprocessor states. This means that the standard ways of storing variables in a cpp file cannot be used. - Blocking session files.
It is possible situation of simultaneous attempt to write/read one session from different requests. This means that it is necessary to queue such workers in some way, as php also does. - Deleting session files.
The difficulty of deleting files follows from the problem above, since it is important to avoid locking workers.
Solutions and methods
- Memory
Using superglobals to store session states within a single worker. Variables such as v$_COOKIE have their own memory per worker, so an additional array created using the same rules as v$_SESSION is used to avoid inter-processor states. - Blocking sessions
Thelockf()function with exclusive blockingF_LOCKis used to block workers. - Extra attributes and file deletion
Еo avoid unnecessary reading of certain data from files, tags (getxattr,setxattr) from<sys/xattr.h>are used that store frequently used information in the form of metadata to the file. Tags are used to store the life duration of the session (gc_maxlifetime) and the status of the confirmation that the document is a session (to distinguish the session from other documents). - Session id generation
To generate a reliable sequence of a given lengthrandom_bytes()is used together withbin2hex().
Tests
- Interprocessor conflicts
To test the blocking of workers as processes with multiple requests to a single file, the php project JobWorkers from kphp-snippets is used. The test results are located on a separate branch in the form of github action test. Confirmation of the blocking of workers can be seen in the sectionsSend a request to the serverandRead logs. - cpp tests
Tests were written to demonstrate the correct operation of individual functions.
TO-DO
- add a check to see if headers have already been sent before the session starts: requires
headers_sent(https://www.php.net/manual/en/function.headers-sent.php) - add implementations:
session_destroy(), session_sek_cookie_params(), session_register_shutdown(), session_regenerate_id(), session_create_id() - change the way session options are stored, implement an analog of a php.ini file: currently, options are stored in runtime, so they need to be passed every time
session_start()is called