Page Speed Optimization Libraries  1.3.25.1
net/instaweb/util/public/url_to_filename_encoder.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http:///www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00089 
00091 
00092 #ifndef NET_INSTAWEB_UTIL_PUBLIC_URL_TO_FILENAME_ENCODER_H_
00093 #define NET_INSTAWEB_UTIL_PUBLIC_URL_TO_FILENAME_ENCODER_H_
00094 
00095 #include <cstddef>
00096 
00097 #include "net/instaweb/util/public/string.h"
00098 
00099 namespace net_instaweb {
00100 
00102 class UrlToFilenameEncoder {
00103  public:
00109   static GoogleString Encode(const GoogleString& url, GoogleString base_path,
00110                              bool legacy_escape) {
00111     GoogleString filename;
00112     if (!legacy_escape) {
00113       GoogleString url_no_scheme = GetUrlHostPath(url);
00114       EncodeSegment(base_path, url_no_scheme, '/', &filename);
00115 #ifdef WIN32
00116       ReplaceAll(&filename, "/", "\\");
00117 #endif
00118     } else {
00119       GoogleString clean_url(url);
00120       if (clean_url.length() && clean_url[clean_url.length()-1] == '/')
00121         clean_url.append("index.html");
00122 
00123       GoogleString host = GetUrlHost(clean_url);
00124       filename.append(base_path);
00125       filename.append(host);
00126 #ifdef WIN32
00127       filename.append("\\");
00128 #else
00129       filename.append("/");
00130 #endif
00131 
00132       GoogleString url_filename = GetUrlPath(clean_url);
00134       if (url_filename[0] == '/')
00135         url_filename = url_filename.substr(1);
00136 
00138       ConvertToSlashes(&url_filename);
00139 
00141       StripDoubleSlashes(&url_filename);
00142 
00144       url_filename = LegacyEscape(url_filename);
00145       filename.append(url_filename);
00146 
00147 #ifndef WIN32
00148 
00149       const GoogleString slash("/");
00150       const GoogleString backslash("\\");
00151       ReplaceAll(&filename, backslash, slash);
00152 #endif
00153     }
00154 
00155     return filename;
00156   }
00157 
00165   static void EncodeSegment(
00166       const GoogleString& filename_prefix,
00167       const GoogleString& escaped_ending,
00168       char dir_separator,
00169       GoogleString* encoded_filename);
00170 
00173   static bool Decode(const GoogleString& encoded_filename,
00174                      char dir_separator,
00175                      GoogleString* decoded_url);
00176 
00181   static GoogleString Unescape(const GoogleString& escaped_url);
00182 
00183   static const char kEscapeChar;
00184   static const char kTruncationChar;
00185   static const size_t kMaximumSubdirectoryLength;
00186 
00187   friend class UrlToFilenameEncoderTest;
00188 
00189  private:
00197   static void AppendSegment(
00198       GoogleString* segment,
00199       GoogleString* dest);
00200 
00202   static GoogleString LegacyEscape(const GoogleString& path);
00203 
00205   static void ReplaceAll(GoogleString* str, const GoogleString& from,
00206                          const GoogleString& to) {
00207     GoogleString::size_type pos(0);
00208     while ((pos = str->find(from, pos)) != GoogleString::npos) {
00209       str->replace(pos, from.size(), to);
00210       pos += from.size();
00211     }
00212   }
00213 
00215   static void ConvertToSlashes(GoogleString* path) {
00216     const GoogleString slash("/");
00217     const GoogleString backslash("\\");
00218     ReplaceAll(path, slash, backslash);
00219   }
00220 
00222   static void StripDoubleSlashes(GoogleString* path) {
00223     const GoogleString doubleslash("\\\\");
00224     const GoogleString escaped_doubleslash("%5C%5C");
00225     ReplaceAll(path, doubleslash, escaped_doubleslash);
00226   }
00227 
00231   static GoogleString GetUrlHost(const GoogleString& url);
00232 
00236   static GoogleString GetUrlHostPath(const GoogleString& url);
00237 
00241   static GoogleString GetUrlPath(const GoogleString& url);
00242 };
00243 
00244 }  
00245 
00246 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_URL_TO_FILENAME_ENCODER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines