blob: d4c8b7ef540da0b6070e85a32993b0712339e9ff [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001diff -r -u libcgi-back-1.0/libcgi-1.0/src/cgi.c libcgi-1.0/src/cgi.c
2--- libcgi-back-1.0/libcgi-1.0/src/cgi.c 2014-10-28 15:16:27.000000000 +0800
3+++ libcgi-1.0/src/cgi.c 2015-05-11 16:19:55.980570364 +0800
4@@ -143,16 +143,18 @@
5 char *method;
6
7 method = getenv("REQUEST_METHOD");
8-
9+ system("echo \"cgi_process_form!\" > /tmp/my_debug");
10 // When METHOD has no contents, the default action is to process it as GET method
11- if (method == NULL || !strcasecmp("GET", method)) {
12+ if (method == NULL || !strcasecmp("GET", method) || !strcasecmp("POST", method)) {
13 char *dados;
14 dados = getenv("QUERY_STRING");
15-
16+
17 // Sometimes, GET comes with not any data
18- if (dados == NULL || strlen(dados) < 1)
19- return NULL;
20-
21+ if (dados == NULL || strlen(dados) < 1) {
22+ system("echo \"query string is null!\" > /tmp/my_debug");
23+ return NULL;
24+ }
25+ system("echo \"query string is not null!\" > /tmp/my_debug");
26 return process_data(dados, &formvars_start, &formvars_last, '=', '&');
27 }
28 else if (!strcasecmp("POST", method)) {
29diff -r -u libcgi-back-1.0/libcgi-1.0/src/cgi.h libcgi-1.0/src/cgi.h
30--- libcgi-back-1.0/libcgi-1.0/src/cgi.h 2014-10-28 15:16:27.000000000 +0800
31+++ libcgi-1.0/src/cgi.h 2015-05-08 16:56:51.865844100 +0800
32@@ -25,6 +25,14 @@
33 extern "C" {
34 #endif
35
36+#define LAST_ACCESS_TIME "last_time"
37+#define LOG_STATUS "log_status"
38+#define USER_ID "user_name"
39+#define USER_PSWD "user_pswd"
40+//second
41+#define MAX_SESSION_TIMEOUT 600
42+//tmp buffer size
43+#define TMP_BUF_MAX 256
44
45 // general purpose linked list. Actualy isn't very portable
46 // because uses only 'name' and 'value' variables to store data.
47@@ -36,6 +44,15 @@
48 struct formvarsA *next;
49 } formvars;
50
51+enum {
52+ SESS_VALID,
53+ SESS_NO_COOKIE,
54+ SESS_NO_ACCESS_TIME,
55+ SESS_NOT_FOUND,
56+ SESS_INVALID,
57+ SESS_TIMEOUT
58+};
59+
60 extern formvars *formvars_start;
61 extern formvars *formvars_last;
62 extern formvars *cookies_start;
63diff -r -u libcgi-back-1.0/libcgi-1.0/src/cookie.c libcgi-1.0/src/cookie.c
64--- libcgi-back-1.0/libcgi-1.0/src/cookie.c 2014-10-28 15:16:27.000000000 +0800
65+++ libcgi-1.0/src/cookie.c 2015-05-12 10:27:11.335231627 +0800
66@@ -92,14 +92,30 @@
67 aux = cookies;
68
69 while (cookies) {
70- position = 0;
71+start: position = 0;
72+
73 data = (formvars *)malloc(sizeof(formvars));
74 if (!data)
75 libcgi_error(E_MEMORY, "%s, line %s", __FILE__, __LINE__);
76
77- while (*aux++ != '=')
78- position++;
79
80+ if ((strchr(cookies, '=')) == NULL) {
81+ aux = NULL;
82+ free(data);
83+ cookies = aux;
84+ goto end;
85+ }
86+
87+ while (*aux++ != '=') {
88+ if (*(aux-1) != ';') {
89+ position++;
90+ } else {
91+ position = 0;
92+ free(data);
93+ cookies = aux;
94+ goto start;
95+ }
96+ }
97 data->name = (char *)malloc(position+1);
98 if (!data->name) {
99 libcgi_error(E_MEMORY, "%s, line %s", __FILE__, __LINE__);
100@@ -120,7 +136,8 @@
101 while (*aux++ != ';')
102 position++;
103 // Eliminate the blank space after ';'
104- aux++;
105+ while(*aux == ' ')
106+ aux++;
107 }
108
109 data->value = (char *)malloc(position + 1);
110@@ -134,7 +151,7 @@
111 slist_add(data, &cookies_start, &cookies_last);
112 cookies = aux;
113 }
114-
115+end:
116 return cookies_start;
117 }
118
119diff -r -u libcgi-back-1.0/libcgi-1.0/src/session.c libcgi-1.0/src/session.c
120--- libcgi-back-1.0/libcgi-1.0/src/session.c 2014-10-28 15:16:27.000000000 +0800
121+++ libcgi-1.0/src/session.c 2015-05-12 14:23:44.854940353 +0800
122@@ -124,6 +124,7 @@
123 SESS_OPEN_FILE
124 } sess_error;
125
126+
127 // This variables are used to control the linked list of all
128 // session objects. Most of time you don't need to use them
129 // directly
130@@ -214,8 +215,6 @@
131 {
132 formvars *data;
133
134- cgi_init_headers();
135-
136 // Rewrites all data to session file
137 sess_file = fopen(sess_fname, "w");
138
139@@ -547,10 +546,90 @@
140 fclose(fp);
141 sess_initialized = 1;
142 free(buf);
143-
144 return 1;
145 }
146
147+/**
148+* check the request validation.
149+* This function is responsible for reading a exist session file and
150+* validate the request. It no session file found will return session_invalid
151+* if file found but session timeout will return session_timeout, meanwhile
152+* destroy the session file anyway
153+* if seesion is valid will return session_valid and refresh the session access time
154+* @see session_destroy()
155+*/
156+int cgi_session_check()
157+{
158+ char *buf = NULL, *sid = NULL, *value = NULL;;
159+ struct stat st;
160+ FILE *fp;
161+ struct timeval cur_tv;
162+ time_t last_time;
163+ char tmp[TMP_BUF_MAX] = { 0 };
164+
165+ // Get the session ID
166+ sid = cgi_cookie_value(SESSION_COOKIE_NAME);
167+
168+ // If there isn't a session ID
169+ if (sid == NULL) {
170+ return SESS_NO_COOKIE;
171+ }
172+
173+ save_path_len = strlen(SESSION_SAVE_PATH) + strlen(SESSION_FILE_PREFIX);
174+
175+ sess_fname = (char *)malloc(save_path_len + SESS_ID_LEN + 1);
176+ if (!sess_fname)
177+ libcgi_error(E_MEMORY, "File %s, line %s", __FILE__, __LINE__);
178+
179+ snprintf(sess_fname, (SESS_ID_LEN + save_path_len + 1), "%s%s%s", SESSION_SAVE_PATH, SESSION_FILE_PREFIX, sid);
180+ sess_fname[SESS_ID_LEN + save_path_len] = '\0';
181+
182+ errno = 0;
183+ fp = fopen(sess_fname, "r");
184+ if (errno == ENOENT) {
185+ // The file doesn't exists
186+ return SESS_NOT_FOUND;
187+ }
188+
189+ strncpy(sess_id, sid, SESS_ID_LEN);
190+ sess_id[SESS_ID_LEN] = '\0';
191+
192+ stat(sess_fname, &st);
193+ buf = (char *)malloc(st.st_size + 2);
194+ if (!buf)
195+ libcgi_error(E_MEMORY, "File %s, line %s", __FILE__, __LINE__);
196+
197+ fgets(buf, st.st_size+1, fp);
198+ if (buf != NULL && strlen(buf) > 1)
199+ process_data(buf, &sess_list_start, &sess_list_last, '=', ';');
200+
201+ fclose(fp);
202+ sess_initialized = 1;
203+ free(buf);
204+
205+ gettimeofday(&cur_tv, NULL);
206+ value = cgi_session_var(LAST_ACCESS_TIME);
207+ if (!value) {
208+ return SESS_NO_ACCESS_TIME;
209+ }
210+
211+ last_time = (time_t)atol(value);
212+
213+ if (cur_tv.tv_sec - last_time > MAX_SESSION_TIMEOUT) {
214+ cgi_session_destroy();
215+ return SESS_TIMEOUT;
216+ }
217+
218+ //refresh last access time
219+ memset(tmp, 0, TMP_BUF_MAX);
220+ snprintf(tmp, TMP_BUF_MAX - 1, "%u", cur_tv.tv_sec );
221+ if ( cgi_session_alter_var(LAST_ACCESS_TIME, tmp)) {
222+ return SESS_VALID;
223+ }
224+ else {
225+ return SESS_INVALID;
226+ }
227+}
228 /**
229 * @}
230 */