00001
00002
00003 #include <string>
00004 #include <ctype.h>
00005 #include "logrecord.h"
00006 #include "configuration.h"
00007 #include "date.h"
00008
00009
00010 bool
00011 LogRecord::parse_line(const string& line,const Configuration& conf) {
00012
00013
00014
00015
00016 const set<Configuration::CRITERIUM>& criteria(conf.criteria());
00017
00018 if (criteria.count(Configuration::DATE))
00019 if (!parse_date(line))
00020 return false;
00021
00022 if (criteria.count(Configuration::PATH))
00023 if (!parse_path(line))
00024 return false;
00025
00026 if (criteria.count(Configuration::DOMAIN))
00027 if (!parse_domain(line))
00028 return false;
00029
00030 return true;
00031 }
00032
00033
00034 bool
00035 LogRecord::parse_date(const string& line) {
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 string::size_type date_start(line.find_first_of('['));
00046 if (date_start==string::npos)
00047 return false;
00048 string::size_type date_end(line.find_first_of(' ',date_start));
00049 if (date_end==string::npos)
00050 return false;
00051 string date_string(line,date_start+1,date_end - date_start-1);
00052 if (date_string.size()==0)
00053 return false;
00054
00055
00056
00057
00058
00059
00060 for (string::size_type i=0; i<date_string.size(); ++i)
00061 if (date_string[i]=='/')
00062 date_string[i] = '-';
00063
00064
00065
00066
00067
00068 date_string.replace(date_string.find_first_of(':'),1," ");
00069
00070
00071
00072
00073
00074 try {
00075 Date d(date_string);
00076 date_.set(d.year(),d.month(),d.day(),d.hours());
00077 }
00078 catch (exception& e) {
00079 cerr << "LogRecord::parse_date failed: " << e.what() << endl;
00080 return false;
00081 }
00082 return true;
00083 }
00084
00086
00087
00088 static inline int
00089 hexdigit(char c) {
00090 if (isdigit(c))
00091 return c - '0';
00092 else if (isupper(c))
00093 return c - 'A' + 10;
00094 else
00095 return c - 'a' +10;
00096 }
00097
00098
00099 void
00100 www_decode(string& s) {
00101 unsigned int i(0);
00102 unsigned int j(0);
00103 unsigned int len(s.size());
00104 for (i=0; (i<len); ++i,++j)
00105 switch (s[i]) {
00106
00107 case '%':
00108 if ((i+2<len) && isxdigit(s[i+1]) && isxdigit(s[i+2])) {
00109 s[j] = hexdigit(s[i+1])* 16 + hexdigit(s[i+2]);
00110 i += 2;
00111 }
00112 else
00113 s[j] = s[i];
00114 break;
00115 default: s[j] = s[i]; break;
00116 }
00117 s.resize(j);
00118 }
00120
00121
00122 bool
00123 LogRecord::parse_path(const string& line) {
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 string::size_type path_start(line.find_first_of('\"'));
00134 if (path_start==string::npos)
00135 return false;
00136
00137
00138
00139
00140
00141 path_start = line.find_first_of(' ',path_start);
00142 if (path_start==string::npos)
00143 return false;
00144 ++path_start;
00145
00146
00147
00148
00149
00150
00151 string::size_type path_end = line.find_first_of("#? ",path_start);
00152 if (path_end==string::npos)
00153 return false;
00154
00155 if (path_end==path_start)
00156 return false;
00157
00158 string path_string(line,path_start,path_end - path_start);
00159
00160
00161 www_decode(path_string);
00162
00163 return path_.parse(path_string);
00164 }
00165
00166
00167 bool
00168 LogRecord::parse_domain(const string& line) {
00169
00170
00171
00172
00173
00174
00175 string::size_type domain_start(0);
00176 string::size_type domain_end(line.find_first_of(' '));
00177 if (domain_end==string::npos)
00178 return false;
00179 string domain_string(line,domain_start,domain_end);
00180
00181 return domain_.parse(domain_string);
00182 }