mime_util.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (c) 2012 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. #ifndef NET_BASE_MIME_UTIL_H__
  5. #define NET_BASE_MIME_UTIL_H__
  6. // This file defines MIME utility functions. All of them assume the MIME type
  7. // to be of the format specified by rfc2045. According to it, MIME types are
  8. // case strongly insensitive except parameter values, which may or may not be
  9. // case sensitive.
  10. //
  11. // These utilities perform a *case-sensitive* matching for parameter values,
  12. // which may produce some false negatives. Except that, matching is
  13. // case-insensitive.
  14. //
  15. // All constants in mime_util.cc must be written in lower case, except parameter
  16. // values, which can be any case.
  17. #include <stddef.h>
  18. #include <string>
  19. #include <vector>
  20. #include "base/files/file_path.h"
  21. #include "base/strings/string_piece.h"
  22. #include "base/strings/string_split.h"
  23. #include "net/base/net_export.h"
  24. namespace net {
  25. // Gets the mime type (if any) that is associated with the given file extension.
  26. // Returns true if a corresponding mime type exists.
  27. NET_EXPORT bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext,
  28. std::string* mime_type);
  29. // Gets the mime type (if any) that is associated with the given file extension.
  30. // Returns true if a corresponding mime type exists. In this method,
  31. // the search for a mime type is constrained to a limited set of
  32. // types known to the net library, the OS/registry is not consulted.
  33. NET_EXPORT bool GetWellKnownMimeTypeFromExtension(
  34. const base::FilePath::StringType& ext,
  35. std::string* mime_type);
  36. // Gets the mime type (if any) that is associated with the given file. Returns
  37. // true if a corresponding mime type exists.
  38. NET_EXPORT bool GetMimeTypeFromFile(const base::FilePath& file_path,
  39. std::string* mime_type);
  40. // Gets the preferred extension (if any) associated with the given mime type.
  41. // Returns true if a corresponding file extension exists. The extension is
  42. // returned without a prefixed dot, ex "html".
  43. NET_EXPORT bool GetPreferredExtensionForMimeType(
  44. const std::string& mime_type,
  45. base::FilePath::StringType* extension);
  46. // Returns true if this the mime_type_pattern matches a given mime-type.
  47. // Checks for absolute matching and wildcards. MIME types are case insensitive.
  48. NET_EXPORT bool MatchesMimeType(const std::string& mime_type_pattern,
  49. const std::string& mime_type);
  50. // Parses |type_str| for |mime_type| and any |params|. Returns false if mime
  51. // cannot be parsed, and does not modify |mime_type| or |params|.
  52. //
  53. // Returns true when mime can be parsed and:
  54. // If |mime_type| is non-NULL, sets it to parsed mime string.
  55. // If |params| is non-NULL, clears it and sets it with name-value pairs of
  56. // parsed parameters. Parsing of parameters is lenient, and invalid params are
  57. // ignored.
  58. NET_EXPORT bool ParseMimeType(const std::string& type_str,
  59. std::string* mime_type,
  60. base::StringPairs* params);
  61. // Returns true if the |type_string| is a correctly-formed mime type specifier
  62. // with no parameter, i.e. string that matches the following ABNF (see the
  63. // definition of content ABNF in RFC2045 and media-type ABNF httpbis p2
  64. // semantics).
  65. //
  66. // token "/" token
  67. //
  68. // If |top_level_type| is non-NULL, sets it to parsed top-level type string.
  69. // If |subtype| is non-NULL, sets it to parsed subtype string.
  70. //
  71. // This function strips leading and trailing whitespace from the MIME type.
  72. // TODO: investigate if we should strip strictly HTTP whitespace.
  73. NET_EXPORT bool ParseMimeTypeWithoutParameter(base::StringPiece type_string,
  74. std::string* top_level_type,
  75. std::string* subtype);
  76. // Returns true if the |type_string| is a top-level type of any media type
  77. // registered with IANA media types registry at
  78. // http://www.iana.org/assignments/media-types/media-types.xhtml or an
  79. // experimental type (type with x- prefix).
  80. //
  81. // This method doesn't check that the input conforms to token ABNF, so if input
  82. // is experimental type strings, you need to check check that before using
  83. // this method.
  84. NET_EXPORT bool IsValidTopLevelMimeType(const std::string& type_string);
  85. // Get the extensions associated with the given mime type.
  86. //
  87. // There could be multiple extensions for a given mime type, like "html,htm" for
  88. // "text/html", or "txt,text,html,..." for "text/*". Note that we do not erase
  89. // the existing elements in the the provided vector. Instead, we append the
  90. // result to it. The new extensions are returned in no particular order.
  91. NET_EXPORT void GetExtensionsForMimeType(
  92. const std::string& mime_type,
  93. std::vector<base::FilePath::StringType>* extensions);
  94. // Generates a random MIME multipart boundary.
  95. // The returned string is guaranteed to be at most 70 characters long.
  96. NET_EXPORT std::string GenerateMimeMultipartBoundary();
  97. // Prepares one value as part of a multi-part upload request.
  98. NET_EXPORT void AddMultipartValueForUpload(const std::string& value_name,
  99. const std::string& value,
  100. const std::string& mime_boundary,
  101. const std::string& content_type,
  102. std::string* post_data);
  103. // Prepares one value as part of a multi-part upload request, with file name as
  104. // an additional parameter.
  105. NET_EXPORT void AddMultipartValueForUploadWithFileName(
  106. const std::string& value_name,
  107. const std::string& file_name,
  108. const std::string& value,
  109. const std::string& mime_boundary,
  110. const std::string& content_type,
  111. std::string* post_data);
  112. // Adds the final delimiter to a multi-part upload request.
  113. NET_EXPORT void AddMultipartFinalDelimiterForUpload(
  114. const std::string& mime_boundary,
  115. std::string* post_data);
  116. } // namespace net
  117. #endif // NET_BASE_MIME_UTIL_H__