lib/apacheLog.c

Go to the documentation of this file.
00001 /* apacheLog - stuff to parse out apache web server logs, currently
00002  * just the access log. */
00003 
00004 #include "common.h"
00005 #include "obscure.h"
00006 #include "apacheLog.h"
00007 
00008 static char const rcsid[] = "$Id: apacheLog.c,v 1.1 2005/09/03 02:07:40 kent Exp $";
00009 
00010 void apacheAccessLogFree(struct apacheAccessLog **pLl)
00011 /* Free up apacheAccessLog. */
00012 {
00013 struct apacheAccessLog *ll = *pLl;
00014 if (ll != NULL)
00015     {
00016     freeMem(ll->buf);
00017     freez(pLl);
00018     }
00019 }
00020 
00021 
00022 static void badFormat(struct apacheAccessLog **pLl, char *line, char *fileName, 
00023         int lineIx, char *message)
00024 /* Complain about format if verbose flag is on.  Free up
00025  * *pLl */
00026 {
00027 if (verboseLevel()  > 1)
00028     {
00029     if (fileName != NULL)
00030         warn("%s line %d: %s", fileName, lineIx, message);
00031     else
00032         warn("%s", message);
00033     }
00034 }
00035 
00036 static void unterminatedQuote(struct apacheAccessLog **pLl, char *line, 
00037         char *fileName, int lineIx)
00038 /* Complain about unterminated quote. */
00039 {
00040 badFormat(pLl, line, fileName, lineIx, 
00041         "missing closing quote");
00042 }
00043 
00044 static void shortLine(struct apacheAccessLog **pLl, char *line, 
00045         char *fileName, int lineIx)
00046 /* Complain about short line. */
00047 {
00048 badFormat(pLl, line, fileName, lineIx, 
00049         "short line");
00050 }
00051 
00052 static void badTimeStamp(struct apacheAccessLog **pLl, char *line, 
00053         char *fileName, int lineIx)
00054 /* Complain about bad time stamp. */
00055 {
00056 badFormat(pLl, line, fileName, lineIx, 
00057         "bad time stamp");
00058 }
00059 
00060 struct apacheAccessLog *apacheAccessLogParse(char *line, 
00061         char *fileName, int lineIx)
00062 /* Return a apacheAccessLog from line.  Return NULL if there's a parsing 
00063  * problem, but don't abort. */
00064 {
00065 struct apacheAccessLog *ll;
00066 char *buf, *s, *e;
00067 AllocVar(ll);
00068 ll->buf = buf = cloneString(line);
00069 ll->ip = nextWord(&buf);
00070 ll->dash1 = nextWord(&buf);
00071 ll->dash2 = nextWord(&buf);
00072 if (buf == NULL)
00073     {
00074     shortLine(&ll, line, fileName, lineIx);
00075     return NULL;
00076     }
00077 s = strchr(buf, '[');
00078 if (s == NULL)
00079     {
00080     badTimeStamp(&ll, line, fileName, lineIx);
00081     return NULL;
00082     }
00083 s += 1;
00084 e = strchr(s, ']');
00085 if (e == NULL)
00086     {
00087     badTimeStamp(&ll, line, fileName, lineIx);
00088     return NULL;
00089     }
00090 *e = 0;
00091 ll->timeStamp = nextWord(&s);
00092 if (!isdigit(ll->timeStamp[0]))
00093     {
00094     badTimeStamp(&ll, line, fileName, lineIx);
00095     return NULL;
00096     }
00097 ll->timeZone = nextWord(&s);
00098 buf = e+2;
00099 if (buf[0] != '"')
00100     {
00101     badFormat(&ll, line, fileName, lineIx, "Missing quote after time stamp");
00102     return NULL;
00103     }
00104 if (!parseQuotedString(buf, buf, &e))
00105     {
00106     unterminatedQuote(&ll, line, fileName, lineIx);
00107     return NULL;
00108     }
00109 ll->method = nextWord(&buf);
00110 ll->url = nextWord(&buf);
00111 ll->httpVersion = nextWord(&buf);
00112 if (ll->url == NULL)
00113     {
00114     badFormat(&ll, line, fileName, lineIx, "Missing URL");
00115     return NULL;
00116     }
00117 buf = e;
00118 s = nextWord(&buf);
00119 if (!isdigit(s[0]))
00120     {
00121     badFormat(&ll, line, fileName, lineIx, "Non-numerical status code");
00122     return NULL;
00123     }
00124 ll->status = atoi(s);
00125 ll->num1 = nextWord(&buf);
00126 if (buf == NULL)
00127     {
00128     shortLine(&ll, line, fileName, lineIx);
00129     return NULL;
00130     }
00131 if (buf[0] != '"')
00132     {
00133     badFormat(&ll, line, fileName, lineIx, "Missing quote after request");
00134     return NULL;
00135     }
00136 if (!parseQuotedString(buf, buf, &e))
00137     {
00138     unterminatedQuote(&ll, line, fileName, lineIx);
00139     return NULL;
00140     }
00141 if (!sameString(buf, "-"))
00142     ll->referrer = buf;
00143 buf = e + 1;
00144 if (buf[0] != '"')
00145     {
00146     badFormat(&ll, line, fileName, lineIx, "Missing quote after referrer");
00147     return NULL;
00148     }
00149 if (!parseQuotedString(buf, buf, &e))
00150     {
00151     unterminatedQuote(&ll, line, fileName, lineIx);
00152     return NULL;
00153     }
00154 ll->program = buf;
00155 return ll;
00156 }
00157 

Generated on Tue Dec 25 18:39:30 2007 for blat by  doxygen 1.5.2