2
* Copyright 2011 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
// Author: sligocki@google.com (Shawn Ligocki)
19
#ifndef NET_INSTAWEB_REWRITER_PUBLIC_FILE_LOAD_POLICY_H_
20
#define NET_INSTAWEB_REWRITER_PUBLIC_FILE_LOAD_POLICY_H_
23
#include "net/instaweb/util/public/basictypes.h"
24
#include "net/instaweb/util/public/gtest_prod.h"
25
#include "net/instaweb/util/public/string.h"
26
#include "net/instaweb/util/public/string_util.h"
28
namespace net_instaweb {
31
class FileLoadMapping;
34
// Class for deciding which URLs get loaded from which files.
36
// Currently, you must explicitly set which directories to load directly
39
// Files with unknown extensions are never loaded from file because we wouldn't
40
// be able to set a content type.
41
class FileLoadPolicy {
44
virtual ~FileLoadPolicy();
46
// Note: This is O(N+M) for N calls to Associate and M calls to AddRule.
47
// TODO(sligocki): Set up a more efficient mapper.
48
virtual bool ShouldLoadFromFile(const GoogleUrl& url,
49
GoogleString* filename) const;
51
// Tells us to load all URLs with this prefix from filename_prefix directory.
52
// Both prefixes must specify directories, if they do not end in slashes,
55
// Tests against youngest association first in case of overlapping prefixes.
56
// Because we support regular expressions, checking for overlapping prefixes
58
virtual void Associate(const StringPiece& url_prefix,
59
const StringPiece& filename_prefix);
61
// A version of Associate supporting RE2-format regular expressions.
62
// Backreferences are supported, as in:
64
// AssociateRegexp("^https?://example.com/~([^/]*)/static/",
65
// "/var/static/\\1", &error);
67
// Which will map urls as:
69
// http://example.com/~pat/static/cat.jpg -> /var/static/pat/cat.jpg
70
// http://example.com/~sam/static/dog.jpg -> /var/static/sam/dog.jpg
71
// https://example.com/~al/static/css/ie -> /var/static/al/css/ie
73
// If the regular expression and substitution validate, returns true.
74
// Otherwise it writes a message to error and returns false.
75
virtual bool AssociateRegexp(const StringPiece& url_regexp,
76
const StringPiece& filename_prefix,
79
// By default Associate permits directly loading anything under the specified
80
// filesystem path prefix. So if we were given:
82
// Associate("http://example.com/", "/var/www/")
84
// we would use load-from-file for everything on the site. If some of those
85
// files actually need to be loaded through HTTP, for example because they
86
// need to be interpreted, we might need:
88
// AddRule("/var/www/cgi-bin/", false, false); // literal blacklist.
92
// // blacklist regexp
93
// AddRule("\\.php$", true, false); // regexp blacklist.
95
// In cases where it's easier to list what's allowed than what's prohibited,
98
// GoogleString e; // For regexp errors.
99
// Associate("http://example.com/", "/var/www/")
100
// AddRule(".*", true, false, &e) // regexp blacklist.
101
// AddRule("\\.html$", true, true, &e) // regexp whitelist.
102
// AddRule("/var/www/static/", false, true, &e) // literal whitelist.
103
// // regexp blacklist.
104
// AddRule("^/var/www/static/legacy/.*\\.php$", true, false, &e)
106
// AddRule will fail if RE2 can't compile the regular expression, and will
107
// write an error message to it's error string and return false if that
109
virtual bool AddRule(const GoogleString& rule, bool is_regexp, bool allowed,
110
GoogleString* error);
112
// Merge in other policies (needed for rewrite_options).
113
virtual void Merge(const FileLoadPolicy& other);
116
virtual bool ShouldLoadFromFileHelper(const GoogleUrl& url,
117
GoogleString* filename) const;
120
typedef std::list<FileLoadMapping*> FileLoadMappings;
121
FileLoadMappings file_load_mappings_;
122
typedef std::list<FileLoadRule*> FileLoadRules;
123
FileLoadRules file_load_rules_;
125
DISALLOW_COPY_AND_ASSIGN(FileLoadPolicy);
128
} // namespace net_instaweb
130
#endif // NET_INSTAWEB_REWRITER_PUBLIC_FILE_LOAD_POLICY_H_