-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathParser.java
More file actions
289 lines (247 loc) · 9.81 KB
/
Parser.java
File metadata and controls
289 lines (247 loc) · 9.81 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
* Copyright 2012-2013 Snowplow Analytics Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.snowplowanalytics.refererparser;
// Java
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Collections;
// SnakeYAML
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
// Apache URLEncodedUtils
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
/**
* Java implementation of <a href="https://github.com/snowplow/referer-parser">Referer Parser</a>
*
* @author Alex Dean (@alexatkeplar)
*/
public class Parser {
private static final String REFERERS_YAML_PATH = "/referers.yml";
private Map<String,RefererLookup> referers;
/**
* Holds the structure of each referer
* in our lookup Map.
*/
private static class RefererLookup {
public Medium medium;
public String source;
public List<String> parameters;
public RefererLookup(Medium medium, String source, List<String> parameters) {
this.medium = medium;
this.source = source;
this.parameters = parameters;
}
}
/**
* Construct our Parser object using the
* bundled referers.yml
*/
public Parser() throws CorruptYamlException {
this(Parser.class.getResourceAsStream(REFERERS_YAML_PATH));
}
/**
* Construct our Parser object using a
* InputStream (in YAML format)
*
* @param referersStream The referers YAML
* to load into our Parser, in
* InputStream format
*/
public Parser(InputStream referersStream) throws CorruptYamlException {
referers = loadReferers(referersStream);
}
/**
* Construct our Parser object using a
* custom resource String
*
* @param referersResource The resource pointing
* to the referers YAML file to load
*/
public Parser(String referersResource) throws CorruptYamlException {
this(Parser.class.getResourceAsStream(referersResource));
}
public Referer parse(URI refererUri, URI pageUri) {
return parse(refererUri, pageUri.getHost());
}
public Referer parse(String refererUri, URI pageUri) throws URISyntaxException {
return parse(refererUri, pageUri.getHost());
}
public Referer parse(String refererUri, String pageHost) throws URISyntaxException {
if (refererUri == null || "".equals(refererUri)) return null;
final URI uri = new URI(refererUri);
return parse(uri, pageHost);
}
public Referer parse(URI refererUri, String pageHost) {
return parse(refererUri, pageHost, Collections.emptyList());
}
public Referer parse(URI refererUri, String pageHost, List<String> internalDomains) {
if (refererUri == null) { return null; }
return parse(refererUri.getScheme(), refererUri.getHost(), refererUri.getPath(), refererUri.getRawQuery(), pageHost, internalDomains);
}
public Referer parse(URL refererUrl, String pageHost){
if (refererUrl == null) { return null; }
return parse(refererUrl.getProtocol(), refererUrl.getHost(), refererUrl.getPath(), refererUrl.getQuery(), pageHost);
}
private Referer parse(String scheme, String host, String path, String query, String pageHost){
return parse(scheme, host, path, query, pageHost, Collections.emptyList());
}
private Referer parse(String scheme, String host, String path, String query, String pageHost, List<String> internalDomains){
if (scheme == null || (!scheme.equals("http") && !scheme.equals("https"))) return null;
// Internal link if hosts match exactly
// TODO: would also be nice to:
// 1. Support a list of other hosts which count as internal
// 2. Have an algo for stripping subdomains before checking match
if (host == null) return null; // Not a valid URL
if (host.equals(pageHost)) return new Referer(Medium.INTERNAL, null, null);
if (stripSubDomains(host).equals(pageHost)) return new Referer(Medium.INTERNAL, null, null);
for (String s : internalDomains) {
if (s.trim().equals(host))
return new Referer(Medium.INTERNAL, null, null);
}
// Try to lookup our referer. First check with paths, then without.
// This is the safest way of handling lookups
RefererLookup referer = lookupReferer(host, path, true);
if (referer == null) {
referer = lookupReferer(host, path, false);
}
if (referer == null) {
return new Referer(Medium.UNKNOWN, null, null); // Unknown referer, nothing more to do
} else {
// Potentially add a search term
final String term = (referer.medium == Medium.SEARCH) ? extractSearchTerm(query, referer.parameters) : null;
return new Referer(referer.medium, referer.source, term);
}
}
private String stripSubDomains(String host) {
String[] urlSegments = host.split("\\.");
int length = urlSegments.length;
if (length > 3){
return String.format("%s.%s.%s",urlSegments[0], urlSegments[length-2], urlSegments[length-1]);
}
return host;
}
/**
* Recursive function to lookup a host (or partial host)
* in our referers map.
*
* First check the host, then the host+full path, then the host+
* one-level path.
*
* If not found, remove one subdomain-level off the front
* of the host and try again.
*
* @param refererHost The host of the current page
* @param refererPath The path to the current page
* @param includePath Whether to include the path in the lookup
*
* @return a RefererLookup object populated with the given
* referer, or null if not found
*/
private RefererLookup lookupReferer(String refererHost, String refererPath, Boolean includePath) {
// Check if domain+full path matches, e.g. for apollo.lv/portal/search/
RefererLookup referer = (includePath) ? referers.get(refererHost + refererPath) : referers.get(refererHost);
// Check if domain+one-level path matches, e.g. for orange.fr/webmail/fr_FR/read.html (in our YAML it's orange.fr/webmail)
if (includePath && referer == null) {
final String[] pathElements = refererPath.split("/");
if (pathElements.length > 1) {
referer = referers.get(refererHost + "/" + pathElements[1]);
}
}
if (referer == null) {
final int idx = refererHost.indexOf('.');
if (idx == -1) {
return null; // No "."? Let's quit.
} else {
return lookupReferer(refererHost.substring(idx + 1), refererPath, includePath); // Recurse
}
} else {
return referer;
}
}
private String extractSearchTerm(String query, List<String> possibleParameters) {
List<NameValuePair> params;
try {
params = URLEncodedUtils.parse(new URI("http://localhost?" + query), "UTF-8");
// params = URLEncodedUtils.parse(query, Charset.forName("UTF-8")); because https://github.com/snowplow/referer-parser/issues/76
} catch (IllegalArgumentException | URISyntaxException iae) {
return null;
}
for (NameValuePair pair : params) {
final String name = pair.getName();
final String value = pair.getValue();
if (possibleParameters.contains(name)) {
return value;
}
}
return null;
}
/**
* Builds the map of hosts to referers from the
* input YAML file.
*
* @param referersYaml An InputStream containing the
* referers database in YAML format.
*
* @return a Map where the key is the hostname of each
* referer and the value (RefererLookup)
* contains all known info about this referer
*/
private Map<String,RefererLookup> loadReferers(InputStream referersYaml) throws CorruptYamlException {
Yaml yaml = new Yaml(new SafeConstructor());
Map<String,Map<String,Map>> rawReferers = yaml.load(referersYaml);
// This will store all of our referers
Map<String,RefererLookup> referers = new HashMap<>();
// Outer loop is all referers under a given medium
for (Map.Entry<String,Map<String,Map>> mediumReferers : rawReferers.entrySet()) {
Medium medium = Medium.fromString(mediumReferers.getKey());
// Inner loop is individual referers
for (Map.Entry<String,Map> referer : mediumReferers.getValue().entrySet()) {
String sourceName = referer.getKey();
Map<String,List<String>> refererMap = referer.getValue();
// Validate
List<String> parameters = refererMap.get("parameters");
if (medium == Medium.SEARCH) {
if (parameters == null) {
throw new CorruptYamlException("No parameters found for search referer '" + sourceName + "'");
}
} else {
if (parameters != null) {
throw new CorruptYamlException("Parameters not supported for non-search referer '" + sourceName + "'");
}
}
List<String> domains = refererMap.get("domains");
if (domains == null) {
throw new CorruptYamlException("No domains found for referer '" + sourceName + "'");
}
// Our hash needs referer domain as the
// key, so let's expand
for (String domain : domains) {
if (referers.containsValue(domain)) {
throw new CorruptYamlException("Duplicate of domain '" + domain + "' found");
}
referers.put(domain, new RefererLookup(medium, sourceName, parameters));
}
}
}
return referers;
}
}