]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - download.c
Ported r208 from branches/v2_2_7
[thirdparty/sarg.git] / download.c
index 399aa7ab6349189a207a20cf4423a35202cf20a4..6efba37e612f4d9d732d0a2fc2850aa465945442 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * AUTHOR: Pedro Lineu Orso                         pedro.orso@gmail.com
- *                                                            1998, 2009
+ *                                                            1998, 2010
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
  *
  * SARG donations:
 #include "include/conf.h"
 #include "include/defs.h"
 
+static char *DownloadSuffix=NULL;
+static char **DownloadSuffixIndex=NULL;
+static int NDownloadSuffix=0;
+
 void download_report(void)
 {
 
@@ -47,6 +51,7 @@ void download_report(void)
    int  z=0;
    int  count=0;
    int i;
+   struct getwordstruct gwarea;
 
    ouser[0]='\0';
    ouser2[0]='\0';
@@ -106,9 +111,10 @@ void download_report(void)
    fprintf(fp_ou,"<tr><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th></tr>\n",text[98],text[111],text[110],text[91]);
 
    while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
-      if (getword(data,sizeof(data),buf,'\t')<0 || getword(hora,sizeof(hora),buf,'\t')<0 ||
-          getword(user,sizeof(user),buf,'\t')<0 || getword(ip,sizeof(ip),buf,'\t')<0 ||
-          getword(url,sizeof(url),buf,'\t')<0) {
+      getword_start(&gwarea,buf);
+      if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 ||
+          getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0 ||
+          getword(url,sizeof(url),&gwarea,'\t')<0) {
          printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",report_in);
          exit(1);
       }
@@ -118,7 +124,7 @@ void download_report(void)
             fixip(user);
       }
 
-      if(strcmp(Ip2Name,"yes") == 0)
+      if(Ip2Name)
          ip2name(ip,sizeof(ip));
 
       if(!z) {
@@ -176,3 +182,105 @@ void download_report(void)
 
    return;
 }
+
+void free_download(void)
+{
+   if (DownloadSuffix) {
+      free(DownloadSuffix);
+      DownloadSuffix=NULL;
+   }
+   if (DownloadSuffixIndex) {
+      free(DownloadSuffixIndex);
+      DownloadSuffixIndex=NULL;
+   }
+   NDownloadSuffix=0;
+}
+
+void set_download_suffix(const char *list)
+{
+   char *str;
+   int i, j, k;
+   int cmp;
+
+   free_download();
+
+   DownloadSuffix=strdup(list);
+   if (!DownloadSuffix) {
+      fprintf(stderr,"SARG: Download suffix list too long\n");
+      exit(1);
+   }
+   j = 1;
+   for (i=0 ; list[i] ; i++)
+      if (list[i] == ',') j++;
+   DownloadSuffixIndex=malloc(j*sizeof(char *));
+   if (!DownloadSuffixIndex) {
+      fprintf(stderr,"SARG: Too many download suffixes\n");
+      exit(1);
+   }
+
+   str = DownloadSuffix;
+   for (i=0 ; DownloadSuffix[i] ; i++) {
+      if (DownloadSuffix[i] == ',') {
+         DownloadSuffix[i] = '\0';
+         if (*str) {
+            cmp = -1;
+            for (j=0 ; j<NDownloadSuffix && (cmp=strcasecmp(str,DownloadSuffixIndex[j]))>0 ; j++);
+            if (cmp != 0) {
+               for (k=NDownloadSuffix ; k>j ; k--)
+                  DownloadSuffixIndex[k]=DownloadSuffixIndex[k-1];
+               NDownloadSuffix++;
+               DownloadSuffixIndex[j]=str;
+            }
+         }
+         str=DownloadSuffix+i+1;
+      }
+   }
+
+   if (*str) {
+      cmp = -1;
+      for (j=0 ; j<NDownloadSuffix && (cmp=strcasecmp(str,DownloadSuffixIndex[j]))>0 ; j++);
+      if (cmp != 0) {
+         for (k=NDownloadSuffix ; k>j ; k--)
+            DownloadSuffixIndex[k]=DownloadSuffixIndex[k-1];
+         NDownloadSuffix++;
+         DownloadSuffixIndex[j]=str;
+      }
+   }
+}
+
+int is_download_suffix(const char *url)
+{
+   int urllen;
+   int i;
+   int down, up, center;
+   const char *suffix;
+   int cmp;
+   const int max_suffix=10;
+
+   if (DownloadSuffix == NULL || NDownloadSuffix == 0) return(0);
+
+   urllen=strlen(url)-1;
+   if (urllen<=0) return(0);
+   if (url[urllen] == '.') return(0); //reject a single trailing dot
+   for (i=0 ; i<urllen && (url[i]!='/' || url[i+1]=='/') && url[i]!='?' ; i++);
+   if (i>=urllen) return(0); // url is a hostname without any path or file to download
+
+   for (i=0 ; i<=max_suffix && i<urllen && url[urllen-i]!='.' ; i++)
+      if (url[urllen-i] == '/' || url[urllen-i] == '?') return(0);
+   if (i>max_suffix || i>=urllen) return(0);
+
+   suffix=url+urllen-i+1;
+   down=0;
+   up=NDownloadSuffix-1;
+   while (down<=up) {
+      center=(down+up)/2;
+      cmp=strcasecmp(suffix,DownloadSuffixIndex[center]);
+      if (cmp == 0) return(1);
+      if (cmp < 0)
+         up = center-1;
+      else
+         down = center+1;
+   }
+   return(0);
+}
+