2 cookies.c - Cookie support for CGI library
3 Copyright (C) 1999,2007 by Martin Schulze <joey@infodrom.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 s_cookie **cgiReadCookies()
29 char *curpos, *n0, *n1, *v0, *v1, *cp;
30 s_cookie **res, *pivot = NULL;
34 if ((curpos = http_cookie = getenv ("HTTP_COOKIE")) == NULL)
37 if ((res = (s_cookie **)malloc (sizeof (s_cookie *))) == NULL)
42 for (n0=curpos; *n0 && *n0==' '; n0++);
43 for (n1=n0; *n1 && *n1!=' ' && *n1!='=' && *n1!=';' && *n1!=',';n1++);
44 for (v0=n1; *v0 && (*v0==' ' || *v0=='=' || *v0==' ');v0++);
47 for (;*v1 && *v1!='"';v1++);
50 for (;*v1 && *v1!=',' && *v1!=';';v1++);
55 if (strncasecmp (n0, "$version", 8) && strncasecmp (n0, "$path", 5)
56 && strncasecmp (n0, "$domain", 7)) {
61 if (pivot && pivot->name) {
63 if ((res = (s_cookie **)realloc (res, sizeof (s_cookie *) * (count+1))) == NULL)
66 res[count] = pivot = NULL;
70 if ((pivot = (s_cookie *)malloc (sizeof (s_cookie))) == NULL)
72 memset (pivot, 0, sizeof (s_cookie));
73 if (res && res[0] && res[0]->version)
74 pivot->version = res[0]->version; /* Warning: *Never* free() res[n!=0]->version */
78 len = sizeof (char) * (v1-v0);
79 if ((cp = (char *)malloc (len)) == NULL)
82 strncpy (cp, v0, v1-v0);
84 if (!strncasecmp (n0, "version", 7))
86 else if (!strncasecmp (n0, "path", 4))
88 else if (!strncasecmp (n0, "domain", 6))
93 len = sizeof (char) * (n1-n0+1);
94 if ((pivot->name = (char *)malloc (len)) == NULL)
96 memset (pivot->name, 0, len);
97 strncpy (pivot->name, n0, n1-n0);
101 len = sizeof (char) * (v1-v0+1);
102 if ((pivot->value = (char *)malloc (len)) == NULL)
104 memset (pivot->value, 0, len);
105 strncpy (pivot->value, v0, v1-v0);
111 if (*curpos) curpos++;
115 if ((res = (s_cookie **)realloc (res, sizeof (s_cookie *) * (count+1))) == NULL)
117 res[count-1] = pivot;
123 s_cookie *cgiGetCookie (s_cgi *parms, const char *name)
127 if (!parms || !parms->cookies)
129 for (i=0;parms->cookies[i]; i++)
130 if (parms->cookies[i]->name && parms->cookies[i]->value && !strcmp(name,parms->cookies[i]->name)) {
131 cgiDebugOutput (1, "%s found as %s\n", name, parms->cookies[i]->value);
132 return parms->cookies[i];
134 cgiDebugOutput (1, "%s not found\n", name);
138 char **cgiGetCookies (s_cgi *parms)
144 if (!parms || !parms->cookies)
146 for (i=k=0;parms->cookies[i];i++)
147 if (parms->cookies[i]->name && parms->cookies[i]->value)
149 len = sizeof (char *) * ++k;
150 if ((res = (char **)malloc (len)) == NULL)
152 memset (res, 0, len);
153 for (i=k=0;parms->cookies[i]; i++) {
154 if (parms->cookies[i]->name && parms->cookies[i]->value) {
155 len = strlen (parms->cookies[i]->name) +1;
156 if ((res[i] = (char *)malloc (len)) == NULL)
158 memset (res[i], 0, len);
159 strcpy (res[i], parms->cookies[i]->name);