type.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package mime implements parts of the MIME spec.
  5. package mime
  6. import (
  7. "fmt"
  8. "strings"
  9. "sync"
  10. )
  11. var (
  12. mimeLock sync.RWMutex
  13. mimeTypesLower = map[string]string{
  14. ".css": "text/css; charset=utf-8",
  15. ".gif": "image/gif",
  16. ".htm": "text/html; charset=utf-8",
  17. ".html": "text/html; charset=utf-8",
  18. ".jpg": "image/jpeg",
  19. ".js": "application/x-javascript",
  20. ".pdf": "application/pdf",
  21. ".png": "image/png",
  22. ".xml": "text/xml; charset=utf-8",
  23. }
  24. mimeTypes = clone(mimeTypesLower)
  25. )
  26. func clone(m map[string]string) map[string]string {
  27. m2 := make(map[string]string, len(m))
  28. for k, v := range m {
  29. m2[k] = v
  30. if strings.ToLower(k) != k {
  31. panic("keys in mimeTypesLower must be lowercase")
  32. }
  33. }
  34. return m2
  35. }
  36. var once sync.Once // guards initMime
  37. // TypeByExtension returns the MIME type associated with the file extension ext.
  38. // The extension ext should begin with a leading dot, as in ".html".
  39. // When ext has no associated type, TypeByExtension returns "".
  40. //
  41. // Extensions are looked up first case-sensitively, then case-insensitively.
  42. //
  43. // The built-in table is small but on unix it is augmented by the local
  44. // system's mime.types file(s) if available under one or more of these
  45. // names:
  46. //
  47. // /etc/mime.types
  48. // /etc/apache2/mime.types
  49. // /etc/apache/mime.types
  50. //
  51. // On Windows, MIME types are extracted from the registry.
  52. //
  53. // Text types have the charset parameter set to "utf-8" by default.
  54. func TypeByExtension(ext string) string {
  55. once.Do(initMime)
  56. mimeLock.RLock()
  57. defer mimeLock.RUnlock()
  58. // Case-sensitive lookup.
  59. v := mimeTypes[ext]
  60. if v != "" {
  61. return v
  62. }
  63. // Case-insensitive lookup.
  64. // Optimistically assume a short ASCII extension and be
  65. // allocation-free in that case.
  66. var buf [10]byte
  67. lower := buf[:0]
  68. const utf8RuneSelf = 0x80 // from utf8 package, but not importing it.
  69. for i := 0; i < len(ext); i++ {
  70. c := ext[i]
  71. if c >= utf8RuneSelf {
  72. // Slow path.
  73. return mimeTypesLower[strings.ToLower(ext)]
  74. }
  75. if 'A' <= c && c <= 'Z' {
  76. lower = append(lower, c+('a'-'A'))
  77. } else {
  78. lower = append(lower, c)
  79. }
  80. }
  81. // The conversion from []byte to string doesn't allocate in
  82. // a map lookup.
  83. return mimeTypesLower[string(lower)]
  84. }
  85. // AddExtensionType sets the MIME type associated with
  86. // the extension ext to typ. The extension should begin with
  87. // a leading dot, as in ".html".
  88. func AddExtensionType(ext, typ string) error {
  89. if !strings.HasPrefix(ext, ".") {
  90. return fmt.Errorf(`mime: extension %q misses dot`, ext)
  91. }
  92. once.Do(initMime)
  93. return setExtensionType(ext, typ)
  94. }
  95. func setExtensionType(extension, mimeType string) error {
  96. _, param, err := ParseMediaType(mimeType)
  97. if err != nil {
  98. return err
  99. }
  100. if strings.HasPrefix(mimeType, "text/") && param["charset"] == "" {
  101. param["charset"] = "utf-8"
  102. mimeType = FormatMediaType(mimeType, param)
  103. }
  104. extLower := strings.ToLower(extension)
  105. mimeLock.Lock()
  106. mimeTypes[extension] = mimeType
  107. mimeTypesLower[extLower] = mimeType
  108. mimeLock.Unlock()
  109. return nil
  110. }