directory_listing.cc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "net/base/directory_listing.h"
  5. #include "base/i18n/time_formatting.h"
  6. #include "base/json/string_escape.h"
  7. #include "base/logging.h"
  8. #include "base/memory/ref_counted_memory.h"
  9. #include "base/strings/escape.h"
  10. #include "base/strings/string_util.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. #include "base/time/time.h"
  13. #include "net/base/net_module.h"
  14. #include "net/grit/net_resources.h"
  15. namespace net {
  16. std::string GetDirectoryListingHeader(const std::u16string& title) {
  17. scoped_refptr<base::RefCountedMemory> header(
  18. NetModule::GetResource(IDR_DIR_HEADER_HTML));
  19. // This can be null in unit tests.
  20. DLOG_IF(WARNING, !header) << "Missing resource: directory listing header";
  21. std::string result;
  22. if (header)
  23. result.assign(header->front_as<char>(), header->size());
  24. result.append("<script>start(");
  25. base::EscapeJSONString(title, true, &result);
  26. result.append(");</script>\n");
  27. return result;
  28. }
  29. std::string GetDirectoryListingEntry(const std::u16string& name,
  30. const std::string& raw_bytes,
  31. bool is_dir,
  32. int64_t size,
  33. base::Time modified) {
  34. std::string result;
  35. result.append("<script>addRow(");
  36. base::EscapeJSONString(name, true, &result);
  37. result.append(",");
  38. if (raw_bytes.empty()) {
  39. base::EscapeJSONString(base::EscapePath(base::UTF16ToUTF8(name)), true,
  40. &result);
  41. } else {
  42. base::EscapeJSONString(base::EscapePath(raw_bytes), true, &result);
  43. }
  44. if (is_dir) {
  45. result.append(",1,");
  46. } else {
  47. result.append(",0,");
  48. }
  49. // Negative size means unknown or not applicable (e.g. directory).
  50. std::stringstream raw_size_string_stream;
  51. raw_size_string_stream << size << ",";
  52. result.append(raw_size_string_stream.str());
  53. std::u16string size_string;
  54. if (size >= 0)
  55. size_string = base::FormatBytesUnlocalized(size);
  56. base::EscapeJSONString(size_string, true, &result);
  57. result.append(",");
  58. // |modified| can be NULL in FTP listings.
  59. std::u16string modified_str;
  60. if (modified.is_null()) {
  61. result.append("0,");
  62. } else {
  63. std::stringstream raw_time_string_stream;
  64. // Certain access paths can only get up to seconds resolution, so here we
  65. // output the raw time value in seconds for consistency.
  66. raw_time_string_stream << modified.ToJavaTime() /
  67. base::Time::kMillisecondsPerSecond
  68. << ",";
  69. result.append(raw_time_string_stream.str());
  70. modified_str = base::TimeFormatShortDateAndTime(modified);
  71. }
  72. base::EscapeJSONString(modified_str, true, &result);
  73. result.append(");</script>\n");
  74. return result;
  75. }
  76. std::string GetParentDirectoryLink() {
  77. return std::string("<script>onHasParentDirectory();</script>\n");
  78. }
  79. } // namespace net