remove traces of benchmarks from test/.
[minix.git] / commands / httpd / utility.c
blob543a9d0537dafedaab091255e4a6b42692a654e5
1 /* utility.c
3 * This file is part of httpd
5 * 02/17/1996 Michael Temari <Michael@TemWare.Com>
6 * 07/07/1996 Initial Release Michael Temari <Michael@TemWare.Com>
7 * 12/29/2002 Initial Release Michael Temari <Michael@TemWare.Com>
9 */
10 #include <sys/types.h>
11 #include <time.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
17 #include "utility.h"
18 #include "config.h"
20 const char *days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
21 const char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
22 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
24 char *logdate(t)
25 time_t *t;
27 time_t worktime;
28 struct tm *tm;
29 static char datebuffer[80];
31 if(t == (time_t *)NULL)
32 (void) time(&worktime);
33 else
34 worktime = *t;
36 tm = localtime(&worktime);
38 sprintf(datebuffer, "%4d%02d%02d%02d%02d%02d",
39 1900+tm->tm_year,
40 tm->tm_mon + 1,
41 tm->tm_mday,
42 tm->tm_hour, tm->tm_min, tm->tm_sec);
44 return(datebuffer);
47 char *httpdate(t)
48 time_t *t;
50 time_t worktime;
51 struct tm *tm;
52 static char datebuffer[80];
54 if(t == (time_t *)NULL)
55 (void) time(&worktime);
56 else
57 worktime = *t;
59 tm = gmtime(&worktime);
61 sprintf(datebuffer, "%s, %02d %s %4d %02d:%02d:%02d GMT",
62 days[tm->tm_wday],
63 tm->tm_mday, months[tm->tm_mon], 1900+tm->tm_year,
64 tm->tm_hour, tm->tm_min, tm->tm_sec);
66 return(datebuffer);
69 time_t httptime(p)
70 char *p;
72 time_t worktime, gtime, ltime;
73 struct tm tm;
74 struct tm *tm2;
75 int i;
77 worktime = (time_t) -1;
79 tm.tm_yday = 0;
80 tm.tm_isdst = -1;
82 /* day of week */
83 for(i = 0; i < 7; i++)
84 if(!strncmp(p, days[i], 3)) break;
85 if(i < 7)
86 tm.tm_wday = i;
87 else
88 return(worktime);
89 while(*p && *p != ' ') p++;
90 if(!*p) return(worktime);
91 while(*p && *p == ' ') p++;
92 if(!*p) return(worktime);
94 if(*p >= '0' && *p <= '9') {
95 /* day */
96 if(*(p+1) >= '0' && *(p+1) <= '9')
97 tm.tm_mday = 10 * (*p - '0') + (*(p+1) - '0');
98 else
99 return(worktime);
100 p += 3;
101 /* month */
102 for(i = 0; i < 12; i++)
103 if(!strncmp(p, months[i], 3)) break;
104 if(i < 12)
105 tm.tm_mon = i;
106 else
107 return(worktime);
108 p += 3;
109 if(!*p++) return(worktime);
110 /* year */
111 tm.tm_year = atoi(p);
112 while(*p && *p != ' ') p++;
113 if(*p) p++;
114 } else {
115 /* day */
116 tm.tm_mday = atoi(p);
117 while(*p && *p != ' ') p++;
118 while(*p && *p == ' ') p++;
119 if(!*p) return(worktime);
122 /* hour */
123 if(*p < '0' || *p > '9' || *(p+1) < '0' || *(p+1) > '9' || *(p+2) != ':') return(worktime);
124 tm.tm_hour = 10 * (*p - '0') + (*(p+1) - '0');
125 p += 3;
127 /* minute */
128 if(*p < '0' || *p > '9' || *(p+1) < '0' || *(p+1) > '9' || *(p+2) != ':') return(worktime);
129 tm.tm_min = 10 * (*p - '0') + (*(p+1) - '0');
130 p += 3;
132 /* second */
133 if(*p < '0' || *p > '9' || *(p+1) < '0' || *(p+1) > '9' || *(p+2) != ' ') return(worktime);
134 tm.tm_sec = 10 * (*p - '0') + (*(p+1) - '0');
135 p += 3;
136 while(*p && *p == ' ') p++;
137 if(!*p) return(worktime);
139 if(*p >= '0' && *p <= '9')
140 tm.tm_year = atoi(p);
141 else
142 if(*p++ != 'G' || *p++ != 'M' || *p++ != 'T')
143 return(worktime);
145 if(tm.tm_year == 0)
146 return(worktime);
148 if(tm.tm_year > 1900)
149 tm.tm_year -= 1900;
151 worktime = mktime(&tm);
153 gtime = mktime(gmtime(&worktime));
154 tm2 = localtime(&worktime);
155 tm2->tm_isdst = 0;
156 ltime = mktime(tm2);
158 worktime = worktime - (gtime - ltime);
160 return(worktime);
163 char *mimetype(url)
164 char *url;
166 char *p;
167 struct msufx *ps;
168 char *dmt;
170 dmt = (char *) NULL;
171 p = url;
172 while(*p) {
173 if(*p != '.') {
174 p++;
175 continue;
177 for(ps = msufx; ps != NULL; ps = ps->snext)
178 if(!strcmp(ps->suffix, "") && dmt == (char *) NULL)
179 dmt = ps->mtype->mimetype;
180 else
181 if(!strcmp(p, ps->suffix))
182 return(ps->mtype->mimetype);
183 p++;
186 if(dmt == (char *) NULL)
187 dmt = "application/octet-stream";
189 return(dmt);
192 char *decode64(p)
193 char *p;
195 static char decode[80];
196 char c[4];
197 int i;
198 int d;
200 i = 0;
201 d = 0;
203 while(*p) {
204 if(*p >= 'A' && *p <= 'Z') c[i++] = *p++ - 'A'; else
205 if(*p >= 'a' && *p <= 'z') c[i++] = *p++ - 'a' + 26; else
206 if(*p >= '0' && *p <= '9') c[i++] = *p++ - '0' + 52; else
207 if(*p == '+') c[i++] = *p++ - '+' + 62; else
208 if(*p == '/') c[i++] = *p++ - '/' + 63; else
209 if(*p == '=') c[i++] = *p++ - '='; else
210 return("");
211 if(i < 4) continue;
212 decode[d++] = ((c[0] << 2) | (c[1] >> 4));
213 decode[d++] = ((c[1] << 4) | (c[2] >> 2));
214 decode[d++] = ((c[2] << 6) | c[3]);
215 decode[d] = '\0';
216 i = 0;
219 return(decode);
222 int getparms(p, parms, maxparms)
223 char *p;
224 char *parms[];
225 int maxparms;
227 int np;
229 np = 0;
231 if(LWS(*p)) {
232 while(*p && LWS(*p)) p++;
233 if(!*p) return(0);
234 parms[np++] = (char *)NULL;
235 } else
236 np = 0;
238 while(np < maxparms && *p) {
239 parms[np++] = p;
240 while(*p && !LWS(*p)) p++;
241 if(*p) *p++ = '\0';
242 while(*p && LWS(*p)) p++;
245 return(np);
248 int mkurlaccess(p)
249 char *p;
251 int ua;
253 ua = 0;
255 while(*p) {
256 if(toupper(*p) == 'R') ua |= URLA_READ; else
257 if(toupper(*p) == 'W') ua |= URLA_WRITE; else
258 if(toupper(*p) == 'X') ua |= URLA_EXEC; else
259 if(toupper(*p) == 'H') ua |= URLA_HEADERS; else
260 return(0);
261 p++;
264 return(ua);