Adjust the mail address
[infodrom/cgilib] / cookies.c
1 /*
2     cookies.c - Cookie support for CGI library
3     Copyright (C) 1999 by Martin Schulze <joey@infodrom.org>
4      
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.
9     
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.
14     
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.
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <cgi.h>
24
25 extern int cgiDebugLevel, cgiDebugStderr;
26
27 s_cookie **cgiReadCookies()
28 {
29     char *http_cookie;
30     char *curpos, *n0, *n1, *v0, *v1, *cp;
31     s_cookie **res, *pivot = NULL;
32     int count;
33     int len;
34
35     if ((curpos = http_cookie = getenv ("HTTP_COOKIE")) == NULL)
36         return NULL;
37     count = 0;
38     if ((res = (s_cookie **)malloc (sizeof (s_cookie *))) == NULL)
39         return NULL;
40     res[0] = NULL;
41
42     while (*curpos) {
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++);
46         if (*v0 == '"') {
47             v1=++v0;
48             for (;*v1 && *v1!='"';v1++);
49         } else {
50             v1=v0;
51             for (;*v1 && *v1!=',' && *v1!=';';v1++);
52         }
53
54         if (n0 != n1) {
55             if (*n0 == '$') {
56                 if (strncasecmp (n0, "$version", 8) && strncasecmp (n0, "$path", 5)
57                     && strncasecmp (n0, "$domain", 7)) {
58                     curpos = v1+1;
59                     continue;
60                 }
61             } else
62                 if (pivot && pivot->name) {
63                     count++;
64                     if ((res = (s_cookie **)realloc (res, sizeof (s_cookie *) * (count+1))) == NULL)
65                         return NULL;
66                     res[count-1] = pivot;
67                     res[count] = pivot = NULL;
68                 }
69
70             if (!pivot) {
71                 if ((pivot = (s_cookie *)malloc (sizeof (s_cookie))) == NULL)
72                     return res;
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 */
76             }
77             if (*n0 == '$') {
78                 n0++;
79                 len = sizeof (char) * (v1-v0);
80                 if ((cp = (char *)malloc (len)) == NULL)
81                     return res;
82                 memset (cp, 0, len);
83                 strncpy (cp, v0, v1-v0);
84
85                 if (!strncasecmp (n0, "version", 7))
86                     pivot->version = cp;
87                 else if (!strncasecmp (n0, "path", 4))
88                     pivot->path = cp;
89                 else if (!strncasecmp (n0, "domain", 6))
90                     pivot->domain = cp;
91                 else
92                     free (cp);
93             } else {
94                 len = sizeof (char) * (n1-n0+1);
95                 if ((pivot->name = (char *)malloc (len)) == NULL)
96                     return res;
97                 memset (pivot->name, 0, len);
98                 strncpy (pivot->name, n0, n1-n0);
99
100                 if (*v0 == '"')
101                     v0++;
102                 len = sizeof (char) * (v1-v0+1);
103                 if ((pivot->value = (char *)malloc (len)) == NULL)
104                     return res;
105                 memset (pivot->value, 0, len);
106                 strncpy (pivot->value, v0, v1-v0);
107             }
108         } else {
109             curpos = n0+1;
110         }
111         curpos = v1;
112         if (*curpos) curpos++;
113     }
114
115     count++;
116     if ((res = (s_cookie **)realloc (res, sizeof (s_cookie *) * (count+1))) == NULL)
117         return NULL;
118     res[count-1] = pivot;
119     res[count] = NULL;
120
121     return res;
122 }
123
124 s_cookie *cgiGetCookie (s_cgi *parms, const char *name)
125 {
126     int i;
127
128     if (!parms || !parms->cookies)
129         return NULL;
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) {
133                 if (cgiDebugStderr)
134                     fprintf (stderr, "%s found as %s\n", name, parms->cookies[i]->value);
135                 else
136                     printf ("%s found as %s<br>\n", name, parms->cookies[i]->value);
137             }
138             return parms->cookies[i];
139         }
140     if (cgiDebugLevel) {
141         if (cgiDebugStderr)
142             fprintf (stderr, "%s not found\n", name);
143         else
144             printf ("%s not found<br>\n", name);
145     }
146     return NULL;
147 }
148
149 char **cgiGetCookies (s_cgi *parms)
150 {
151     int i, k;
152     char **res = NULL;
153     int len;
154
155     if (!parms || !parms->cookies)
156         return NULL;
157     for (i=k=0;parms->cookies[i];i++)
158         if (parms->cookies[i]->name && parms->cookies[i]->value)
159             k++;
160     len = sizeof (char *) * ++k;
161     if ((res = (char **)malloc (len)) == NULL)
162         return 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)
168                 return NULL;
169             memset (res[i], 0, len);
170             strcpy (res[i], parms->cookies[i]->name);
171         }
172     }
173     return res;
174 }
175
176 /*
177  * Local variables:
178  *  c-indent-level: 4
179  *  c-basic-offset: 4
180  *  tab-width: 8
181  * End:
182  */