blob: 46c74e0be18930a3c2e0e9274943ec940da7a0c4 [file] [log] [blame]
jat@google.com134be542009-08-03 15:30:11 +00001#ifndef _H_AllowedConnections
2#define _H_AllowedConnections
3/*
4 * Copyright 2008 Google Inc.
jat@google.com5e86cbd2009-08-22 23:59:24 +00005 *
jat@google.com134be542009-08-03 15:30:11 +00006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7 * use this file except in compliance with the License. You may obtain a copy of
8 * the License at
jat@google.com5e86cbd2009-08-22 23:59:24 +00009 *
jat@google.com134be542009-08-03 15:30:11 +000010 * http://www.apache.org/licenses/LICENSE-2.0
jat@google.com5e86cbd2009-08-22 23:59:24 +000011 *
jat@google.com134be542009-08-03 15:30:11 +000012 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 * License for the specific language governing permissions and limitations under
16 * the License.
17 */
18
19#include <string>
jat@google.com23181962009-09-03 22:22:56 +000020#include <vector>
21#include <utility>
jat@google.com134be542009-08-03 15:30:11 +000022
23/**
jat@google.com5e86cbd2009-08-22 23:59:24 +000024 * Manages rules to control access to other sites from the plugin. This is
25 * important since arbitrary web pages could try and use the plugin to connect
26 * to hosts the browser's machine has access to, such as doing port scanning
27 * behind a firewall.
jat@google.com134be542009-08-03 15:30:11 +000028 */
29class AllowedConnections {
30public:
jat@google.com23181962009-09-03 22:22:56 +000031 /**
32 * Add a rule to match new requests against.
33 *
34 * @param pattern pattern to match
35 * @param exclude true if matches should be excluded instead of included
36 */
37 static void addRule(const std::string& pattern, bool exclude = false);
jat@google.com5e86cbd2009-08-22 23:59:24 +000038
jat@google.com23181962009-09-03 22:22:56 +000039 /**
40 * Clear all rules.
41 */
42 static void clearRules();
43
44 /**
45 * Get the host portion of the URL, not including the port.
46 *
47 * @return the host portion of the URL, or the unmodified URL if it does not
48 * appear to be valid
49 */
50 static std::string getHostFromUrl(const std::string& url);
51
52 /**
53 * Clear any existing rules and reinitialize from the supplied access list.
54 *
55 * This access list is of the form:
56 * [!]pattern,[!]pattern...
57 * where the optional exclamation indicates the following pattern is to be
58 * excluded, and an arbitrary number of patterns may be supplied with the
59 * first match being used. Each pattern currently is only an exact literal
60 * match against the host name, but will be extended to support simple
61 * wildcard patterns.
62 */
63 static void initFromAccessList(const std::string& accessList);
64
65 /**
66 * Returns true if the server for the requested URL matched any rule in
67 * our access list, and sets a flag based on whether that rule permits or
68 * denies the request. A host name of localhost or 127.0.0.1 is always
69 * allowed.
70 *
71 * @param url url of page initiating connection
72 * @param allowed pointer to return value indiciating that this URL should
73 * be allowed to initiate GWT development mode connections
74 * @return true if url matched a rule
75 */
76 static bool matchesRule(const std::string& url, bool* allowed);
77
78private:
79 AllowedConnections() {
jat@google.com134be542009-08-03 15:30:11 +000080 }
81
82 /**
jat@google.com23181962009-09-03 22:22:56 +000083 * Internal class used for representing a rule.
jat@google.com134be542009-08-03 15:30:11 +000084 */
jat@google.com23181962009-09-03 22:22:56 +000085 class Rule : std::pair<std::string, bool> {
86 public:
87 Rule(const std::string& pattern, bool exclude)
88 : std::pair<std::string, bool>(pattern, exclude) {}
jat@google.com134be542009-08-03 15:30:11 +000089
jat@google.com23181962009-09-03 22:22:56 +000090 const std::string& getPattern() const {
91 return first;
92 }
93
94 bool isExcluded() const {
95 return second;
96 }
97 };
98
99 static std::vector<Rule> rules;
jat@google.com134be542009-08-03 15:30:11 +0000100};
101
102#endif