You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

634 lines
19 KiB

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>mime - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package mime</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "mime"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. <dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd>
  58. <dd><a href="index.html#pkg-subdirectories">Subdirectories</a></dd>
  59. </dl>
  60. </div>
  61. <!-- The package's Name is printed as title by the top-level template -->
  62. <div id="pkg-overview" class="toggleVisible">
  63. <div class="collapsed">
  64. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  65. </div>
  66. <div class="expanded">
  67. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  68. <p>
  69. Package mime implements parts of the MIME spec.
  70. </p>
  71. </div>
  72. </div>
  73. <div id="pkg-index" class="toggleVisible">
  74. <div class="collapsed">
  75. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  76. </div>
  77. <div class="expanded">
  78. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  79. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  80. <div id="manual-nav">
  81. <dl>
  82. <dd><a href="index.html#pkg-constants">Constants</a></dd>
  83. <dd><a href="index.html#AddExtensionType">func AddExtensionType(ext, typ string) error</a></dd>
  84. <dd><a href="index.html#ExtensionsByType">func ExtensionsByType(typ string) ([]string, error)</a></dd>
  85. <dd><a href="index.html#FormatMediaType">func FormatMediaType(t string, param map[string]string) string</a></dd>
  86. <dd><a href="index.html#ParseMediaType">func ParseMediaType(v string) (mediatype string, params map[string]string, err error)</a></dd>
  87. <dd><a href="index.html#TypeByExtension">func TypeByExtension(ext string) string</a></dd>
  88. <dd><a href="index.html#WordDecoder">type WordDecoder</a></dd>
  89. <dd>&nbsp; &nbsp; <a href="index.html#WordDecoder.Decode">func (d *WordDecoder) Decode(word string) (string, error)</a></dd>
  90. <dd>&nbsp; &nbsp; <a href="index.html#WordDecoder.DecodeHeader">func (d *WordDecoder) DecodeHeader(header string) (string, error)</a></dd>
  91. <dd><a href="index.html#WordEncoder">type WordEncoder</a></dd>
  92. <dd>&nbsp; &nbsp; <a href="index.html#WordEncoder.Encode">func (e WordEncoder) Encode(charset, s string) string</a></dd>
  93. </dl>
  94. </div><!-- #manual-nav -->
  95. <div id="pkg-examples">
  96. <h4>Examples</h4>
  97. <dl>
  98. <dd><a class="exampleLink" href="index.html#example_WordDecoder_Decode">WordDecoder.Decode</a></dd>
  99. <dd><a class="exampleLink" href="index.html#example_WordDecoder_DecodeHeader">WordDecoder.DecodeHeader</a></dd>
  100. <dd><a class="exampleLink" href="index.html#example_WordEncoder_Encode">WordEncoder.Encode</a></dd>
  101. </dl>
  102. </div>
  103. <h4>Package files</h4>
  104. <p>
  105. <span style="font-size:90%">
  106. <a href="http://localhost:6060/src/mime/encodedword.go">encodedword.go</a>
  107. <a href="http://localhost:6060/src/mime/grammar.go">grammar.go</a>
  108. <a href="http://localhost:6060/src/mime/mediatype.go">mediatype.go</a>
  109. <a href="http://localhost:6060/src/mime/type.go">type.go</a>
  110. <a href="http://localhost:6060/src/mime/type_unix.go">type_unix.go</a>
  111. </span>
  112. </p>
  113. </div><!-- .expanded -->
  114. </div><!-- #pkg-index -->
  115. <div id="pkg-callgraph" class="toggle" style="display: none">
  116. <div class="collapsed">
  117. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  118. </div> <!-- .expanded -->
  119. <div class="expanded">
  120. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  121. <p>
  122. In the call graph viewer below, each node
  123. is a function belonging to this package
  124. and its children are the functions it
  125. calls&mdash;perhaps dynamically.
  126. </p>
  127. <p>
  128. The root nodes are the entry points of the
  129. package: functions that may be called from
  130. outside the package.
  131. There may be non-exported or anonymous
  132. functions among them if they are called
  133. dynamically from another package.
  134. </p>
  135. <p>
  136. Click a node to visit that function's source code.
  137. From there you can visit its callers by
  138. clicking its declaring <code>func</code>
  139. token.
  140. </p>
  141. <p>
  142. Functions may be omitted if they were
  143. determined to be unreachable in the
  144. particular programs or tests that were
  145. analyzed.
  146. </p>
  147. <!-- Zero means show all package entry points. -->
  148. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  149. </div>
  150. </div> <!-- #pkg-callgraph -->
  151. <h2 id="pkg-constants">Constants</h2>
  152. <pre>const (
  153. <span class="comment">// BEncoding represents Base64 encoding scheme as defined by RFC 2045.</span>
  154. <span id="BEncoding">BEncoding</span> = <a href="index.html#WordEncoder">WordEncoder</a>(&#39;b&#39;)
  155. <span class="comment">// QEncoding represents the Q-encoding scheme as defined by RFC 2047.</span>
  156. <span id="QEncoding">QEncoding</span> = <a href="index.html#WordEncoder">WordEncoder</a>(&#39;q&#39;)
  157. )</pre>
  158. <h2 id="AddExtensionType">func <a href="http://localhost:6060/src/mime/type.go?s=4150:4194#L147">AddExtensionType</a></h2>
  159. <pre>func AddExtensionType(ext, typ <a href="../builtin/index.html#string">string</a>) <a href="../builtin/index.html#error">error</a></pre>
  160. <p>
  161. AddExtensionType sets the MIME type associated with
  162. the extension ext to typ. The extension should begin with
  163. a leading dot, as in &#34;.html&#34;.
  164. </p>
  165. <h2 id="ExtensionsByType">func <a href="http://localhost:6060/src/mime/type.go?s=3701:3752#L128">ExtensionsByType</a></h2>
  166. <pre>func ExtensionsByType(typ <a href="../builtin/index.html#string">string</a>) ([]<a href="../builtin/index.html#string">string</a>, <a href="../builtin/index.html#error">error</a>)</pre>
  167. <p>
  168. ExtensionsByType returns the extensions known to be associated with the MIME
  169. type typ. The returned extensions will each begin with a leading dot, as in
  170. &#34;.html&#34;. When typ has no associated extensions, ExtensionsByType returns an
  171. nil slice.
  172. </p>
  173. <h2 id="FormatMediaType">func <a href="http://localhost:6060/src/mime/mediatype.go?s=534:596#L11">FormatMediaType</a></h2>
  174. <pre>func FormatMediaType(t <a href="../builtin/index.html#string">string</a>, param map[<a href="../builtin/index.html#string">string</a>]<a href="../builtin/index.html#string">string</a>) <a href="../builtin/index.html#string">string</a></pre>
  175. <p>
  176. FormatMediaType serializes mediatype t and the parameters
  177. param as a media type conforming to RFC 2045 and RFC 2616.
  178. The type and parameter names are written in lower-case.
  179. When any of the arguments result in a standard violation then
  180. FormatMediaType returns the empty string.
  181. </p>
  182. <h2 id="ParseMediaType">func <a href="http://localhost:6060/src/mime/mediatype.go?s=2584:2669#L94">ParseMediaType</a></h2>
  183. <pre>func ParseMediaType(v <a href="../builtin/index.html#string">string</a>) (mediatype <a href="../builtin/index.html#string">string</a>, params map[<a href="../builtin/index.html#string">string</a>]<a href="../builtin/index.html#string">string</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  184. <p>
  185. ParseMediaType parses a media type value and any optional
  186. parameters, per RFC 1521. Media types are the values in
  187. Content-Type and Content-Disposition headers (RFC 2183).
  188. On success, ParseMediaType returns the media type converted
  189. to lowercase and trimmed of white space and a non-nil map.
  190. The returned map, params, maps from the lowercase
  191. attribute to the attribute value with its case preserved.
  192. </p>
  193. <h2 id="TypeByExtension">func <a href="http://localhost:6060/src/mime/type.go?s=2667:2706#L91">TypeByExtension</a></h2>
  194. <pre>func TypeByExtension(ext <a href="../builtin/index.html#string">string</a>) <a href="../builtin/index.html#string">string</a></pre>
  195. <p>
  196. TypeByExtension returns the MIME type associated with the file extension ext.
  197. The extension ext should begin with a leading dot, as in &#34;.html&#34;.
  198. When ext has no associated type, TypeByExtension returns &#34;&#34;.
  199. </p>
  200. <p>
  201. Extensions are looked up first case-sensitively, then case-insensitively.
  202. </p>
  203. <p>
  204. The built-in table is small but on unix it is augmented by the local
  205. system&#39;s mime.types file(s) if available under one or more of these
  206. names:
  207. </p>
  208. <pre>/etc/mime.types
  209. /etc/apache2/mime.types
  210. /etc/apache/mime.types
  211. </pre>
  212. <p>
  213. On Windows, MIME types are extracted from the registry.
  214. </p>
  215. <p>
  216. Text types have the charset parameter set to &#34;utf-8&#34; by default.
  217. </p>
  218. <h2 id="WordDecoder">type <a href="http://localhost:6060/src/mime/encodedword.go?s=4642:5058#L175">WordDecoder</a></h2>
  219. <pre>type WordDecoder struct {
  220. <span class="comment">// CharsetReader, if non-nil, defines a function to generate</span>
  221. <span class="comment">// charset-conversion readers, converting from the provided</span>
  222. <span class="comment">// charset into UTF-8.</span>
  223. <span class="comment">// Charsets are always lower-case. utf-8, iso-8859-1 and us-ascii charsets</span>
  224. <span class="comment">// are handled by default.</span>
  225. <span class="comment">// One of the the CharsetReader&#39;s result values must be non-nil.</span>
  226. CharsetReader func(charset <a href="../builtin/index.html#string">string</a>, input <a href="../io/index.html">io</a>.<a href="../io/index.html#Reader">Reader</a>) (<a href="../io/index.html">io</a>.<a href="../io/index.html#Reader">Reader</a>, <a href="../builtin/index.html#error">error</a>)
  227. }</pre>
  228. <p>
  229. A WordDecoder decodes MIME headers containing RFC 2047 encoded-words.
  230. </p>
  231. <h3 id="WordDecoder.Decode">func (*WordDecoder) <a href="http://localhost:6060/src/mime/encodedword.go?s=5104:5161#L186">Decode</a></h3>
  232. <pre>func (d *<a href="index.html#WordDecoder">WordDecoder</a>) Decode(word <a href="../builtin/index.html#string">string</a>) (<a href="../builtin/index.html#string">string</a>, <a href="../builtin/index.html#error">error</a>)</pre>
  233. <p>
  234. Decode decodes an RFC 2047 encoded-word.
  235. </p>
  236. <div id="example_WordDecoder_Decode" class="toggle">
  237. <div class="collapsed">
  238. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  239. </div>
  240. <div class="expanded">
  241. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  242. <p>Code:</p>
  243. <pre class="code">dec := new(mime.WordDecoder)
  244. header, err := dec.Decode(&#34;=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=&#34;)
  245. if err != nil {
  246. panic(err)
  247. }
  248. fmt.Println(header)
  249. dec.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
  250. switch charset {
  251. case &#34;x-case&#34;:
  252. <span class="comment">// Fake character set for example.</span>
  253. <span class="comment">// Real use would integrate with packages such</span>
  254. <span class="comment">// as code.google.com/p/go-charset</span>
  255. content, err := ioutil.ReadAll(input)
  256. if err != nil {
  257. return nil, err
  258. }
  259. return bytes.NewReader(bytes.ToUpper(content)), nil
  260. default:
  261. return nil, fmt.Errorf(&#34;unhandled charset %q&#34;, charset)
  262. }
  263. }
  264. header, err = dec.Decode(&#34;=?x-case?q?hello!?=&#34;)
  265. if err != nil {
  266. panic(err)
  267. }
  268. fmt.Println(header)
  269. <span class="comment"></pre>
  270. <p>Output:</p>
  271. <pre class="output">¡Hola, señor!
  272. HELLO!
  273. </pre>
  274. </div>
  275. </div>
  276. <h3 id="WordDecoder.DecodeHeader">func (*WordDecoder) <a href="http://localhost:6060/src/mime/encodedword.go?s=6020:6085#L221">DecodeHeader</a></h3>
  277. <pre>func (d *<a href="index.html#WordDecoder">WordDecoder</a>) DecodeHeader(header <a href="../builtin/index.html#string">string</a>) (<a href="../builtin/index.html#string">string</a>, <a href="../builtin/index.html#error">error</a>)</pre>
  278. <p>
  279. DecodeHeader decodes all encoded-words of the given string. It returns an
  280. error if and only if CharsetReader of d returns an error.
  281. </p>
  282. <div id="example_WordDecoder_DecodeHeader" class="toggle">
  283. <div class="collapsed">
  284. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  285. </div>
  286. <div class="expanded">
  287. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  288. <p>Code:</p>
  289. <pre class="code">dec := new(mime.WordDecoder)
  290. header, err := dec.DecodeHeader(&#34;=?utf-8?q?=C3=89ric?= &lt;eric@example.org&gt;, =?utf-8?q?Ana=C3=AFs?= &lt;anais@example.org&gt;&#34;)
  291. if err != nil {
  292. panic(err)
  293. }
  294. fmt.Println(header)
  295. header, err = dec.DecodeHeader(&#34;=?utf-8?q?=C2=A1Hola,?= =?utf-8?q?_se=C3=B1or!?=&#34;)
  296. if err != nil {
  297. panic(err)
  298. }
  299. fmt.Println(header)
  300. dec.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
  301. switch charset {
  302. case &#34;x-case&#34;:
  303. <span class="comment">// Fake character set for example.</span>
  304. <span class="comment">// Real use would integrate with packages such</span>
  305. <span class="comment">// as code.google.com/p/go-charset</span>
  306. content, err := ioutil.ReadAll(input)
  307. if err != nil {
  308. return nil, err
  309. }
  310. return bytes.NewReader(bytes.ToUpper(content)), nil
  311. default:
  312. return nil, fmt.Errorf(&#34;unhandled charset %q&#34;, charset)
  313. }
  314. }
  315. header, err = dec.DecodeHeader(&#34;=?x-case?q?hello_?= =?x-case?q?world!?=&#34;)
  316. if err != nil {
  317. panic(err)
  318. }
  319. fmt.Println(header)
  320. <span class="comment"></pre>
  321. <p>Output:</p>
  322. <pre class="output">Éric &lt;eric@example.org&gt;, Anaïs &lt;anais@example.org&gt;
  323. ¡Hola, señor!
  324. HELLO WORLD!
  325. </pre>
  326. </div>
  327. </div>
  328. <h2 id="WordEncoder">type <a href="http://localhost:6060/src/mime/encodedword.go?s=336:357#L10">WordEncoder</a></h2>
  329. <pre>type WordEncoder <a href="../builtin/index.html#byte">byte</a></pre>
  330. <p>
  331. A WordEncoder is a RFC 2047 encoded-word encoder.
  332. </p>
  333. <h3 id="WordEncoder.Encode">func (WordEncoder) <a href="http://localhost:6060/src/mime/encodedword.go?s=846:899#L26">Encode</a></h3>
  334. <pre>func (e <a href="index.html#WordEncoder">WordEncoder</a>) Encode(charset, s <a href="../builtin/index.html#string">string</a>) <a href="../builtin/index.html#string">string</a></pre>
  335. <p>
  336. Encode returns the encoded-word form of s. If s is ASCII without special
  337. characters, it is returned unchanged. The provided charset is the IANA
  338. charset name of s. It is case insensitive.
  339. </p>
  340. <div id="example_WordEncoder_Encode" class="toggle">
  341. <div class="collapsed">
  342. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  343. </div>
  344. <div class="expanded">
  345. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  346. <p>Code:</p>
  347. <pre class="code">fmt.Println(mime.QEncoding.Encode(&#34;utf-8&#34;, &#34;¡Hola, señor!&#34;))
  348. fmt.Println(mime.QEncoding.Encode(&#34;utf-8&#34;, &#34;Hello!&#34;))
  349. fmt.Println(mime.BEncoding.Encode(&#34;UTF-8&#34;, &#34;¡Hola, señor!&#34;))
  350. fmt.Println(mime.QEncoding.Encode(&#34;ISO-8859-1&#34;, &#34;Caf\xE9&#34;))
  351. <span class="comment"></pre>
  352. <p>Output:</p>
  353. <pre class="output">=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=
  354. Hello!
  355. =?UTF-8?b?wqFIb2xhLCBzZcOxb3Ih?=
  356. =?ISO-8859-1?q?Caf=E9?=
  357. </pre>
  358. </div>
  359. </div>
  360. <h2 id="pkg-subdirectories">Subdirectories</h2>
  361. <div class="pkg-dir">
  362. <table>
  363. <tr>
  364. <th class="pkg-name">Name</th>
  365. <th class="pkg-synopsis">Synopsis</th>
  366. </tr>
  367. <tr>
  368. <td colspan="2"><a href="http://localhost:6060/pkg/">..</a></td>
  369. </tr>
  370. <tr>
  371. <td class="pkg-name" style="padding-left: 0px;">
  372. <a href="multipart/index.html">multipart</a>
  373. </td>
  374. <td class="pkg-synopsis">
  375. Package multipart implements MIME multipart parsing, as defined in RFC 2046.
  376. </td>
  377. </tr>
  378. <tr>
  379. <td class="pkg-name" style="padding-left: 0px;">
  380. <a href="quotedprintable/index.html">quotedprintable</a>
  381. </td>
  382. <td class="pkg-synopsis">
  383. Package quotedprintable implements quoted-printable encoding as specified by RFC 2045.
  384. </td>
  385. </tr>
  386. </table>
  387. </div>
  388. <div id="footer">
  389. Build version go1.6.<br>
  390. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  391. the content of this page is licensed under the
  392. Creative Commons Attribution 3.0 License,
  393. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  394. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  395. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  396. </div>
  397. </div><!-- .container -->
  398. </div><!-- #page -->
  399. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  400. <script type="text/javascript" src="../../lib/godoc/jquery.js"></script>
  401. <script type="text/javascript" src="../../lib/godoc/jquery.treeview.js"></script>
  402. <script type="text/javascript" src="../../lib/godoc/jquery.treeview.edit.js"></script>
  403. <script type="text/javascript" src="../../lib/godoc/godocs.js"></script>
  404. </body>
  405. </html>