1
// Copyright 2010 Google Inc.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
15
// Author: jmarantz@google.com (Joshua Marantz)
17
#ifndef NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_NAMER_H_
18
#define NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_NAMER_H_
20
#include "base/logging.h"
21
#include "net/instaweb/util/public/basictypes.h"
22
#include "net/instaweb/util/public/string.h"
23
#include "net/instaweb/util/public/string_util.h"
25
namespace net_instaweb {
29
// Encapsulates the naming of resource URL leafs. The class holds the context
30
// of a single resource, and is not intended for re-use. We could, of course,
31
// add a Clear(), but it is a stateful class.
34
// This determines the overhead imposed on each URL by the ResourceNamer
35
// syntax, such as separators.
36
static const int kOverhead;
41
// Encoding and decoding in various formats.
43
// Decodes an entire resource name (NAME.pagespeed[.EXPT].ID.HASH[|SIG].EXT),
44
// placing the result in the fields in this encoder.
45
bool Decode(const StringPiece& encoded_string, int hash_length,
46
int signature_length);
48
// Calls Decode() passing values of -1, -1 for hash and signature lengths.
49
// Hash and signature outputs from this must not be used.
50
bool DecodeIgnoreHashAndSignature(StringPiece encoded_string);
52
// Encodes the fields in this encoder into an absolute url, with the
53
// trailing portion "NAME.pagespeed[.(EXPT|PsolOpts)].ID.HASH[.SIG].EXT".
54
GoogleString Encode() const;
56
// Encode a key that can used to do a lookup based on an id
57
// and the name. This key can be used to find the hash-code for a
58
// resource within the origin TTL.
60
// The 'id' is a short code indicating which Instaweb rewriter was
61
// used to generate the resource.
62
GoogleString EncodeIdName() const;
64
// Note: there is no need at this time to decode the name key.
66
// Eventual length of name. Gets eventual hash length from passed in hasher
67
// and signature_length.
68
// Needed by RewriteDriver to check that filenames aren't too long.
69
int EventualSize(const Hasher& hasher, int signature_length) const;
72
StringPiece id() const { return id_; }
73
StringPiece options() const { return options_; }
74
StringPiece name() const { return name_; }
75
StringPiece hash() const { return hash_; }
76
StringPiece ext() const { return ext_; }
77
StringPiece experiment() const { return experiment_; }
78
StringPiece signature() const { return signature_; }
80
bool has_experiment() const { return !experiment_.empty(); }
81
bool has_options() const { return !options_.empty(); }
84
void set_id(const StringPiece& p) { p.CopyToString(&id_); }
85
void set_options(const StringPiece& opts) { opts.CopyToString(&options_); }
86
void set_name(const StringPiece& n) { n.CopyToString(&name_); }
87
void set_hash(const StringPiece& h) { h.CopyToString(&hash_); }
88
void set_ext(const StringPiece& e) {
89
// TODO(jmaessen): Remove check after transitioning to undotted extensions
91
CHECK(e.empty() || e[0] != '.');
92
e.CopyToString(&ext_);
94
void set_experiment(const StringPiece& e) { e.CopyToString(&experiment_); }
95
void set_signature(const StringPiece& s) { s.CopyToString(&signature_); }
97
// Other setter-like operations
98
void ClearHash() { hash_.clear(); }
99
void CopyFrom(const ResourceNamer& other);
103
// Name suitable for debugging and logging
104
GoogleString PrettyName() const {return InternalEncode(); }
107
GoogleString InternalEncode() const;
108
bool LegacyDecode(const StringPiece& encoded_string);
111
GoogleString options_;
115
GoogleString experiment_;
116
GoogleString signature_;
118
DISALLOW_COPY_AND_ASSIGN(ResourceNamer);
121
} // namespace net_instaweb
123
#endif // NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_NAMER_H_