2 * (C) Copyright 2013 John J. Foerch
4 * Use, modification, and distribution are subject to the terms specified in the
8 require("http-request-hook");
12 function user_agent_policy (name, user_agent, patterns) {
14 this.user_agent = user_agent;
17 for (var i = 0, n = patterns.length; i < n; ++i) {
19 if (p instanceof RegExp)
20 this.byregexp.push(p);
22 this.byhost[p] = true;
25 user_agent_policy.prototype = {
26 constructor: user_agent_policy,
33 function define_policy (name, user_agent) {
34 var patterns = Array.prototype.slice.call(arguments, 2);
35 var o = new user_agent_policy(name, user_agent, patterns);
40 function http_request_hook_function (channel) {
41 var uri = channel.URI;
46 outer: for each (let p in policies) {
47 if (host && p.byhost[host]) {
48 var user_agent = p.user_agent;
51 for (var i = 0, r; r = p.byregexp[i]; ++i) {
53 user_agent = p.user_agent;
58 if (user_agent != null) // allow empty-string ua
59 channel.setRequestHeader("User-Agent", user_agent, false);
61 add_hook("http_request_hook", http_request_hook_function);
63 provide("user-agent-policy");