utility.c

Go to the documentation of this file.
00001 /* utility.c
00002  *
00003  * This file is part of httpd
00004  *
00005  * 02/17/1996                   Michael Temari <Michael@TemWare.Com>
00006  * 07/07/1996 Initial Release   Michael Temari <Michael@TemWare.Com>
00007  * 12/29/2002 Initial Release   Michael Temari <Michael@TemWare.Com>
00008  *
00009  */
00010 #include <sys/types.h>
00011 #include <time.h>
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 #include <ctype.h>
00016 
00017 #include "utility.h"
00018 #include "config.h"
00019 
00020 const char *days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
00021 const char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
00022                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
00023 
00024 char *logdate(t)
00025 time_t *t;
00026 {
00027 time_t worktime;
00028 struct tm *tm;
00029 static char datebuffer[80];
00030 
00031   if(t == (time_t *)NULL)
00032         (void) time(&worktime);
00033   else
00034         worktime = *t;
00035 
00036    tm = localtime(&worktime);
00037 
00038    sprintf(datebuffer, "%4d%02d%02d%02d%02d%02d",
00039                 1900+tm->tm_year,
00040                 tm->tm_mon + 1,
00041                 tm->tm_mday,
00042                 tm->tm_hour, tm->tm_min, tm->tm_sec);
00043 
00044    return(datebuffer);
00045 }
00046 
00047 char *httpdate(t)
00048 time_t *t;
00049 {
00050 time_t worktime;
00051 struct tm *tm;
00052 static char datebuffer[80];
00053 
00054   if(t == (time_t *)NULL)
00055         (void) time(&worktime);
00056   else
00057         worktime = *t;
00058 
00059    tm = gmtime(&worktime);
00060 
00061    sprintf(datebuffer, "%s, %02d %s %4d %02d:%02d:%02d GMT",
00062                 days[tm->tm_wday],
00063                 tm->tm_mday, months[tm->tm_mon], 1900+tm->tm_year,
00064                 tm->tm_hour, tm->tm_min, tm->tm_sec);
00065 
00066    return(datebuffer);
00067 }
00068 
00069 time_t httptime(p)
00070 char *p;
00071 {
00072 time_t worktime, gtime, ltime;
00073 struct tm tm;
00074 struct tm *tm2;
00075 int i;
00076 
00077    worktime = (time_t) -1;
00078 
00079    tm.tm_yday = 0;
00080    tm.tm_isdst = -1;
00081 
00082    /* day of week */
00083    for(i = 0; i < 7; i++)
00084         if(!strncmp(p, days[i], 3)) break;
00085    if(i < 7)
00086         tm.tm_wday = i;
00087    else
00088         return(worktime);
00089    while(*p && *p != ' ') p++;
00090    if(!*p) return(worktime);
00091    while(*p && *p == ' ') p++;
00092    if(!*p) return(worktime);
00093 
00094    if(*p >= '0' && *p <= '9') {
00095         /* day */
00096         if(*(p+1) >= '0' && *(p+1) <= '9')
00097                 tm.tm_mday = 10 * (*p - '0') + (*(p+1) - '0');
00098         else
00099                 return(worktime);
00100         p += 3;
00101         /* month */
00102         for(i = 0; i < 12; i++)
00103                 if(!strncmp(p, months[i], 3)) break;
00104         if(i < 12)
00105                 tm.tm_mon = i;
00106         else
00107                 return(worktime);
00108         p += 3;
00109         if(!*p++) return(worktime);
00110         /* year */
00111         tm.tm_year = atoi(p);
00112         while(*p && *p != ' ') p++;
00113         if(*p) p++;
00114    } else {
00115         /* day */
00116         tm.tm_mday = atoi(p);
00117         while(*p && *p != ' ') p++;
00118         while(*p && *p == ' ') p++;
00119         if(!*p) return(worktime);
00120    }
00121 
00122    /* hour */
00123    if(*p < '0' || *p > '9' || *(p+1) < '0' || *(p+1) > '9' || *(p+2) != ':') return(worktime);
00124    tm.tm_hour = 10 * (*p - '0') + (*(p+1) - '0');
00125    p += 3;
00126 
00127    /* minute */
00128    if(*p < '0' || *p > '9' || *(p+1) < '0' || *(p+1) > '9' || *(p+2) != ':') return(worktime);
00129    tm.tm_min  = 10 * (*p - '0') + (*(p+1) - '0');
00130    p += 3;
00131 
00132    /* second */
00133    if(*p < '0' || *p > '9' || *(p+1) < '0' || *(p+1) > '9' || *(p+2) != ' ') return(worktime);
00134    tm.tm_sec  = 10 * (*p - '0') + (*(p+1) - '0');
00135    p += 3;
00136    while(*p && *p == ' ') p++;
00137    if(!*p) return(worktime);
00138 
00139    if(*p >= '0' && *p <= '9')
00140         tm.tm_year = atoi(p);
00141    else
00142         if(*p++ != 'G' || *p++ != 'M' || *p++ != 'T')
00143                 return(worktime);
00144 
00145    if(tm.tm_year == 0)
00146         return(worktime);
00147 
00148    if(tm.tm_year > 1900)
00149         tm.tm_year -= 1900;
00150 
00151    worktime = mktime(&tm);
00152 
00153    gtime = mktime(gmtime(&worktime));
00154    tm2 = localtime(&worktime);
00155    tm2->tm_isdst = 0;
00156    ltime = mktime(tm2);
00157 
00158    worktime = worktime - (gtime - ltime);
00159 
00160    return(worktime);
00161 }
00162 
00163 char *mimetype(url)
00164 char *url;
00165 {
00166 char *p;
00167 struct msufx *ps;
00168 char *dmt;
00169 
00170    dmt = (char *) NULL;
00171    p = url;
00172    while(*p) {
00173         if(*p != '.') {
00174                 p++;
00175                 continue;
00176         }
00177         for(ps = msufx; ps != NULL; ps = ps->snext)
00178                 if(!strcmp(ps->suffix, "") && dmt == (char *) NULL)
00179                         dmt = ps->mtype->mimetype;
00180                 else
00181                         if(!strcmp(p, ps->suffix))
00182                                 return(ps->mtype->mimetype);
00183         p++;
00184    }
00185 
00186    if(dmt == (char *) NULL)
00187         dmt = "application/octet-stream";
00188 
00189    return(dmt);
00190 }
00191 
00192 char *decode64(p)
00193 char *p;
00194 {
00195 static char decode[80];
00196 char c[4];
00197 int i;
00198 int d;
00199 
00200    i = 0;
00201    d = 0;
00202 
00203    while(*p) {
00204         if(*p >= 'A' && *p <= 'Z') c[i++] = *p++ - 'A'; else
00205         if(*p >= 'a' && *p <= 'z') c[i++] = *p++ - 'a' + 26; else
00206         if(*p >= '0' && *p <= '9') c[i++] = *p++ - '0' + 52; else
00207         if(*p == '+') c[i++] = *p++ - '+' + 62; else
00208         if(*p == '/') c[i++] = *p++ - '/' + 63; else
00209         if(*p == '=') c[i++] = *p++ - '='; else
00210                 return("");
00211         if(i < 4) continue;
00212         decode[d++] = ((c[0] << 2) | (c[1] >> 4));
00213         decode[d++] = ((c[1] << 4) | (c[2] >> 2));
00214         decode[d++] = ((c[2] << 6) |  c[3]);
00215         decode[d] = '\0';
00216         i = 0;
00217    }
00218 
00219    return(decode);
00220 }
00221 
00222 int getparms(p, parms, maxparms)
00223 char *p;
00224 char *parms[];
00225 int maxparms;
00226 {
00227 int np;
00228 
00229    np = 0;
00230 
00231    if(LWS(*p)) {
00232         while(*p && LWS(*p)) p++;
00233         if(!*p) return(0);
00234         parms[np++] = (char *)NULL;
00235    } else
00236         np = 0;
00237 
00238    while(np < maxparms && *p) {
00239         parms[np++] = p;
00240         while(*p && !LWS(*p)) p++;
00241         if(*p) *p++ = '\0';
00242         while(*p && LWS(*p)) p++;
00243    }
00244 
00245    return(np);
00246 }
00247 
00248 int mkurlaccess(p)
00249 char *p;
00250 {
00251 int ua;
00252 
00253    ua = 0;
00254 
00255    while(*p) {
00256         if(toupper(*p) == 'R') ua |= URLA_READ; else
00257         if(toupper(*p) == 'W') ua |= URLA_WRITE; else
00258         if(toupper(*p) == 'X') ua |= URLA_EXEC; else
00259         if(toupper(*p) == 'H') ua |= URLA_HEADERS; else
00260                 return(0);
00261         p++;
00262    }
00263 
00264    return(ua);
00265 }

Generated on Fri Apr 14 22:56:39 2006 for minix by  doxygen 1.4.6