~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/net/instaweb/rewriter/public/file_load_policy.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2011 Google Inc.
3
 
 *
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
7
 
 *
8
 
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
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.
15
 
 */
16
 
 
17
 
// Author: sligocki@google.com (Shawn Ligocki)
18
 
 
19
 
#ifndef NET_INSTAWEB_REWRITER_PUBLIC_FILE_LOAD_POLICY_H_
20
 
#define NET_INSTAWEB_REWRITER_PUBLIC_FILE_LOAD_POLICY_H_
21
 
 
22
 
#include <list>
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"
27
 
 
28
 
namespace net_instaweb {
29
 
 
30
 
class GoogleUrl;
31
 
class FileLoadMapping;
32
 
class FileLoadRule;
33
 
 
34
 
// Class for deciding which URLs get loaded from which files.
35
 
//
36
 
// Currently, you must explicitly set which directories to load directly
37
 
// from filesystem.
38
 
//
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 {
42
 
 public:
43
 
  FileLoadPolicy() {}
44
 
  virtual ~FileLoadPolicy();
45
 
 
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;
50
 
 
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,
53
 
  // we add them.
54
 
  //
55
 
  // Tests against youngest association first in case of overlapping prefixes.
56
 
  // Because we support regular expressions, checking for overlapping prefixes
57
 
  // isn't practical.
58
 
  virtual void Associate(const StringPiece& url_prefix,
59
 
                         const StringPiece& filename_prefix);
60
 
 
61
 
  // A version of Associate supporting RE2-format regular expressions.
62
 
  // Backreferences are supported, as in:
63
 
  //
64
 
  //   AssociateRegexp("^https?://example.com/~([^/]*)/static/",
65
 
  //                   "/var/static/\\1", &error);
66
 
  //
67
 
  // Which will map urls as:
68
 
  //
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
72
 
  //
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,
77
 
                               GoogleString* error);
78
 
 
79
 
  // By default Associate permits directly loading anything under the specified
80
 
  // filesystem path prefix.  So if we were given:
81
 
  //
82
 
  //   Associate("http://example.com/", "/var/www/")
83
 
  //
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:
87
 
  //
88
 
  //   AddRule("/var/www/cgi-bin/", false, false);  // literal blacklist.
89
 
  //
90
 
  // or:
91
 
  //
92
 
  //   // blacklist regexp
93
 
  //   AddRule("\\.php$", true, false);  // regexp blacklist.
94
 
  //
95
 
  // In cases where it's easier to list what's allowed than what's prohibited,
96
 
  // you can whitelist:
97
 
  //
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)
105
 
  //
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
108
 
  // happens.
109
 
  virtual bool AddRule(const GoogleString& rule, bool is_regexp, bool allowed,
110
 
                       GoogleString* error);
111
 
 
112
 
  // Merge in other policies (needed for rewrite_options).
113
 
  virtual void Merge(const FileLoadPolicy& other);
114
 
 
115
 
 protected:
116
 
  virtual bool ShouldLoadFromFileHelper(const GoogleUrl& url,
117
 
                                        GoogleString* filename) const;
118
 
 
119
 
 private:
120
 
  typedef std::list<FileLoadMapping*> FileLoadMappings;
121
 
  FileLoadMappings file_load_mappings_;
122
 
  typedef std::list<FileLoadRule*> FileLoadRules;
123
 
  FileLoadRules file_load_rules_;
124
 
 
125
 
  DISALLOW_COPY_AND_ASSIGN(FileLoadPolicy);
126
 
};
127
 
 
128
 
}  // namespace net_instaweb
129
 
 
130
 
#endif  // NET_INSTAWEB_REWRITER_PUBLIC_FILE_LOAD_POLICY_H_