-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsessions.c
More file actions
107 lines (100 loc) · 2.18 KB
/
sessions.c
File metadata and controls
107 lines (100 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "easyhpc.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#define COOKIE_NAME "session"
SESSION_PTR sessions;
SESSION_PTR
get_session (struct MHD_Connection *connection)
{
SESSION_PTR ret;
const char *cookie;
cookie = MHD_lookup_connection_value (connection,
MHD_COOKIE_KIND,
COOKIE_NAME);
if (cookie != NULL)
{
/* find existing session */
ret = sessions;
while (NULL != ret)
{
if (0 == strcmp (cookie, ret->sid))
break;
ret = ret->next;
}
if (NULL != ret)
{
ret->rc++;
return ret;
}
}
/* create fresh session */
ret = calloc (1, sizeof ( SESSION ));
if (NULL == ret)
{
fprintf (stderr, "calloc error: %s\n", strerror (errno));
return NULL;
}
/* not a super-secure way to generate a random session ID,
but should do for a simple example... */
snprintf (ret->sid,
sizeof (ret->sid),
"%X%X%X%X",
(unsigned int) rand (),
(unsigned int) rand (),
(unsigned int) rand (),
(unsigned int) rand ());
ret->rc++;
ret->start = time (NULL);
ret->next = sessions;
sessions = ret;
return ret;
}
void
add_session_cookie ( SESSION_PTR session,
struct MHD_Response *response)
{
char cstr[256];
snprintf (cstr,
sizeof (cstr),
"%s=%s",
COOKIE_NAME,
session->sid);
if (MHD_NO ==
MHD_add_response_header (response,
MHD_HTTP_HEADER_SET_COOKIE,
cstr))
{
fprintf (stderr,
"Failed to set session cookie header!\n");
}
}
void
expire_sessions ()
{
SESSION_PTR pos;
SESSION_PTR prev;
SESSION_PTR next;
time_t now;
now = time (NULL);
prev = NULL;
pos = sessions;
while (NULL != pos)
{
next = pos->next;
if (now - pos->start > 60 * 60)
{
/* expire sessions after 1h */
if (NULL == prev)
sessions = pos->next;
else
prev->next = next;
free (pos);
}
else
prev = pos;
pos = next;
}
}