sessions.inc 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. This chapter discusses how one should manage sessions, that is, share state between multiple
  2. HTTP requests from the same user. We use a simple example where the user submits multiple
  3. forms and the server is supposed to accumulate state from all of these forms. Naturally, as
  4. this is a network protocol, our session mechanism must support having many users with
  5. many concurrent sessions at the same time.
  6. In order to track users, we use a simple session cookie. A session cookie expires when the
  7. user closes the browser. Changing from session cookies to persistent cookies only requires
  8. adding an expiration time to the cookie. The server creates a fresh session cookie whenever
  9. a request without a cookie is received, or if the supplied session cookie is not known to
  10. the server.
  11. @heading Looking up the cookie
  12. Since MHD parses the HTTP cookie header for us, looking up an existing cookie
  13. is straightforward:
  14. @verbatim
  15. const char *value;
  16. value = MHD_lookup_connection_value (connection,
  17. MHD_COOKIE_KIND,
  18. "KEY");
  19. @end verbatim
  20. Here, "KEY" is the name we chose for our session cookie.
  21. @heading Setting the cookie header
  22. MHD requires the user to provide the full cookie format string in order to set
  23. cookies. In order to generate a unique cookie, our example creates a random
  24. 64-character text string to be used as the value of the cookie:
  25. @verbatim
  26. char value[128];
  27. char raw_value[65];
  28. for (unsigned int i=0;i<sizeof (raw_value);i++)
  29. raw_value = 'A' + (rand () % 26); /* bad PRNG! */
  30. raw_value[64] = '\0';
  31. snprintf (value, sizeof (value),
  32. "%s=%s",
  33. "KEY",
  34. raw_value);
  35. @end verbatim
  36. Given this cookie value, we can then set the cookie header in our HTTP response
  37. as follows:
  38. @verbatim
  39. assert (MHD_YES ==
  40. MHD_set_connection_value (connection,
  41. MHD_HEADER_KIND,
  42. MHD_HTTP_HEADER_SET_COOKIE,
  43. value));
  44. @end verbatim
  45. @heading Remark: Session expiration
  46. It is of course possible that clients stop their interaction with the
  47. server at any time. In order to avoid using too much storage, the
  48. server must thus discard inactive sessions at some point. Our example
  49. implements this by discarding inactive sessions after a certain amount
  50. of time. Alternatively, the implementation may limit the total number
  51. of active sessions. Which bounds are used for idle sessions or the
  52. total number of sessions obviously depends largely on the type of
  53. the application and available server resources.
  54. @heading Example code
  55. A sample application implementing a website with multiple
  56. forms (which are dynamically created using values from previous
  57. POST requests from the same session) is available
  58. as the example @code{sessions.c}.
  59. Note that the example uses a simple, $O(n)$ linked list traversal to
  60. look up sessions and to expire old sessions. Using a hash table and a
  61. heap would be more appropriate if a large number of concurrent
  62. sessions is expected.
  63. @heading Remarks
  64. Naturally, it is quite conceivable to store session data in a database
  65. instead of in memory. Still, having mechanisms to expire data
  66. associated with long-time idle sessions (where the business process
  67. has still not finished) is likely a good idea.