File: referrer.c

package info (click to toggle)
gatling 0.13-6
  • links: PTS
  • area: main
  • in suites: sid, stretch
  • size: 1,196 kB
  • ctags: 1,115
  • sloc: ansic: 23,805; makefile: 143; sh: 71; perl: 30
file content (194 lines) | stat: -rw-r--r-- 4,165 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

size_t hash(const char* word) {
  size_t x;
  for (x=0; *word; ++word) {
    x = x*5^*word;
  }
  return x;
}

#define HASHTABSIZE 65536

struct node {
  char* word;
  size_t count;
  struct node* next;
  struct node* pl;
}* hashtab[HASHTABSIZE];

void* allocassert(void* x) {
  if (!x) {
    fprintf(stderr,"out of memory!\n");
    exit(1);
  }
  return x;
}

struct node** lookup(char* word) {
  struct node** x;
  for (x=&hashtab[hash(word)%HASHTABSIZE]; *x; x=&(*x)->next)
    if (!strcmp(word,(*x)->word))
      break;
  return x;
}

void addlist(struct node** N,char* word) {
  while (*N) {
    if (!strcmp((*N)->word,word)) break;
    N=&(*N)->next;
  }
  if (*N) {
    ++(*N)->count;
    return;
  }
  *N=malloc(sizeof(**N));
  (*N)->next=0;
  (*N)->word=strdup(word);
  (*N)->count=1;
}

int compar(const void* a,const void* b) {
  int i = (*(struct node**)b)->count - (*(struct node**)a)->count;
  return (i?i:strcmp((*(struct node**)a)->word,(*(struct node**)b)->word));
}

void sortbycount(struct node** N) {
  size_t i,count;
  struct node* n;
  struct node** x;
  for (count=0, n=*N; n; ++count, n=n->next);
  x=allocassert(malloc(count*sizeof(*x)));
  for (i=0, n=*N; i<count; ++i, n=n->next) x[i]=n;
  qsort(x,count,sizeof(*x),compar);
  for (i=0; i+1<count; ++i)
    x[i]->next=x[i+1];
  x[i]->next=0;
  *N=x[0];
  free(x);
}

int main() {
  char line[8192];
  char* dat;
//  char* timestamp;
  while (fgets(line,sizeof(line),stdin)) {
    int tslen;
    /* chomp */
    {
      int i;
      for (i=0; i<sizeof(line) && line[i]; ++i)
	if (line[i]=='\n') break;
      line[i]=0;
    }
    /* find out what kind of time stamp there is */
    tslen=0;
    if (line[0]=='@') {
      /* multilog timestamp */
      char* x=strchr(line,' ');
      if (x) {
	tslen=x-line;
	if (tslen!=25) tslen=0;
      }
    } else if (isdigit(line[0])) {
      char* x=strchr(line,' ');
      if (x && x==line+10) {
	x=strchr(x+1,' ');
	if (x && x==line+29) tslen=29;
      }
    }
    if (tslen) {
      dat=line+tslen+1;
      line[tslen]=0;
//      timestamp=line;
    } else {
      dat=line;
//      timestamp="";
    }
    /* element two is the unique key */
    {
      char* fields[20];
      char* x=dat;
      int i;
      /* split into fields */
      for (i=0; i<20; ++i) {
	char* y=strchr(x,' ');
	if (!y) break;
	*y=0;
	fields[i]=x;
	x=y+1;
      }
      fields[i]=x; ++i;
      /* now process */
      if (i>=6) {
	char* referrer=fields[5];
	char* url=fields[2];
	char* x;
	struct node** N;
//	printf("%s => %s\n",referrer,url);
	/* not interested in access without referrer */
#ifndef ALL
	if (!strcmp(referrer,"[no_referrer]")) continue;
#endif
	/* not interested in empty referrer (early versions of gatling) */
	if (!referrer[0]) continue;
	/* skip method and http://host/ */
	if (!strncmp(url,"http",4)) {
	  x=url+4;
	  if (*x=='s') ++x;
	  if (*x!=':') continue; ++x;
	  if (*x!='/') continue; ++x;
	  if (*x!='/') continue; ++x;
	  x=strchr(x,'/');
	  if (!x) continue;

	  /* now we know how long the http://host part is: x-url */
	  /* if it's the same in url and referrer, we aren't interested */
	  if (!memcmp(referrer,url,x-url)) continue;
	} else continue;
	/* now we're talking! */
	N=lookup(url);
	if (!*N) {
	  *N=calloc(sizeof(**N),1);
	  (*N)->word=allocassert(strdup(url));
	}
	(*N)->count++;
	addlist(&(*N)->pl,referrer);
      } else continue;
    }
  }

  /* now sort urls by count */
  {
    size_t count;
    size_t i;
    struct node** x;
    for (i=count=0; i<HASHTABSIZE; ++i) {
      struct node* n;
      for (n=hashtab[i]; n; n=n->next)
	++count;
    }
    x=malloc(count*sizeof(*x));
    for (i=count=0; i<HASHTABSIZE; ++i) {
      struct node* n;
      for (n=hashtab[i]; n; n=n->next) {
	x[count]=n;
	++count;
      }
    }
    qsort(x,count,sizeof(*x),compar);
    for (i=0; i<count; ++i) {
      struct node* n;
      printf("[%zu] %s\n",x[i]->count,x[i]->word);
      sortbycount(&x[i]->pl);
      for (n=x[i]->pl; n; n=n->next)
	printf("  %4zu => %s\n",n->count,n->word);
      printf("\n");
    }
  }
  return 0;
}