2 cookies.c - Cookie support for CGI library
3 Copyright (C) 1999 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.
25 extern int cgiDebugLevel, cgiDebugStderr;
27 s_cookie **cgiReadCookies()
30 char *curpos, *n0, *n1, *v0, *v1, *cp;
31 s_cookie **res, *pivot = NULL;
35 if ((curpos = http_cookie = getenv ("HTTP_COOKIE")) == NULL)
38 if ((res = (s_cookie **)malloc (sizeof (s_cookie *))) == NULL)
43 for (n0=curpos; *n0 && *n0==' '; n0++);
44 for (n1=n0; *n1 && *n1!=' ' && *n1!='=' && *n1!=';' && *n1!=',';n1++);
45 for (v0=n1; *v0 && (*v0==' ' || *v0=='=' || *v0==' ');v0++);
48 for (;*v1 && *v1!='"';v1++);
51 for (;*v1 && *v1!=',' && *v1!=';';v1++);
56 if (strncasecmp (n0, "$version", 8) && strncasecmp (n0, "$path", 5)
57 && strncasecmp (n0, "$domain", 7)) {
62 if (pivot && pivot->name) {
64 if ((res = (s_cookie **)realloc (res, sizeof (s_cookie *) * (count+1))) == NULL)
67 res[count] = pivot = NULL;
71 if ((pivot = (s_cookie *)malloc (sizeof (s_cookie))) == NULL)
73 memset (pivot, 0, sizeof (s_cookie));
74 if (res && res[0] && res[0]->version)
75 pivot->version = res[0]->version; /* Warning: *Never* free() res[n!=0]->version */
79 len = sizeof (char) * (v1-v0);
80 if ((cp = (char *)malloc (len)) == NULL)
83 strncpy (cp, v0, v1-v0);
85 if (!strncasecmp (n0, "version", 7))
87 else if (!strncasecmp (n0, "path", 4))
89 else if (!strncasecmp (n0, "domain", 6))
94 len = sizeof (char) * (n1-n0+1);
95 if ((pivot->name = (char *)malloc (len)) == NULL)
97 memset (pivot->name, 0, len);
98 strncpy (pivot->name, n0, n1-n0);
102 len = sizeof (char) * (v1-v0+1);
103 if ((pivot->value = (char *)malloc (len)) == NULL)
105 memset (pivot->value, 0, len);
106 strncpy (pivot->value, v0, v1-v0);
112 if (*curpos) curpos++;
116 if ((res = (s_cookie **)realloc (res, sizeof (s_cookie *) * (count+1))) == NULL)
118 res[count-1] = pivot;
124 s_cookie *cgiGetCookie (s_cgi *parms, const char *name)
128 if (!parms || !parms->cookies)
130 for (i=0;parms->cookies[i]; i++)
131 if (parms->cookies[i]->name && parms->cookies[i]->value && !strcmp(name,parms->cookies[i]->name)) {
132 if (cgiDebugLevel > 0) {
134 fprintf (stderr, "%s found as %s\n", name, parms->cookies[i]->value);
136 printf ("%s found as %s<br>\n", name, parms->cookies[i]->value);
138 return parms->cookies[i];
142 fprintf (stderr, "%s not found\n", name);
144 printf ("%s not found<br>\n", name);
149 char **cgiGetCookies (s_cgi *parms)
155 if (!parms || !parms->cookies)
157 for (i=k=0;parms->cookies[i];i++)
158 if (parms->cookies[i]->name && parms->cookies[i]->value)
160 len = sizeof (char *) * ++k;
161 if ((res = (char **)malloc (len)) == NULL)
163 memset (res, 0, len);
164 for (i=k=0;parms->cookies[i]; i++) {
165 if (parms->cookies[i]->name && parms->cookies[i]->value) {
166 len = strlen (parms->cookies[i]->name) +1;
167 if ((res[i] = (char *)malloc (len)) == NULL)
169 memset (res[i], 0, len);
170 strcpy (res[i], parms->cookies[i]->name);