mime.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html>
  4. <head>
  5. <meta name="description" content="LuaSocket: MIME support">
  6. <meta name="keywords" content="Lua, LuaSocket, MIME, Library, Support">
  7. <title>LuaSocket: MIME module</title>
  8. <link rel="stylesheet" href="reference.css" type="text/css">
  9. </head>
  10. <body>
  11. <!-- header +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  12. <div class=header>
  13. <hr>
  14. <center>
  15. <table summary="LuaSocket logo">
  16. <tr><td align=center><a href="http://www.lua.org">
  17. <img width=128 height=128 border=0 alt="LuaSocket" src="luasocket.png">
  18. </a></td></tr>
  19. <tr><td align=center valign=top>Network support for the Lua language
  20. </td></tr>
  21. </table>
  22. <p class=bar>
  23. <a href="index.html">home</a> &middot;
  24. <a href="index.html#download">download</a> &middot;
  25. <a href="installation.html">installation</a> &middot;
  26. <a href="introduction.html">introduction</a> &middot;
  27. <a href="reference.html">reference</a>
  28. </p>
  29. </center>
  30. <hr>
  31. </div>
  32. <!-- mime +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  33. <h2 id=mime>MIME</h2>
  34. <p>
  35. The <tt>mime</tt> namespace offers filters that apply and remove common
  36. content transfer encodings, such as Base64 and Quoted-Printable.
  37. It also provides functions to break text into lines and change
  38. the end-of-line convention.
  39. MIME is described mainly in
  40. <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>,
  41. <a href="http://www.ietf.org/rfc/rfc2046.txt">2046</a>,
  42. <a href="http://www.ietf.org/rfc/rfc2047.txt">2047</a>,
  43. <a href="http://www.ietf.org/rfc/rfc2047.txt">2048</a>, and
  44. <a href="http://www.ietf.org/rfc/rfc2048.txt">2049</a>.
  45. </p>
  46. <p>
  47. All functionality provided by the MIME module
  48. follows the ideas presented in
  49. <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">
  50. LTN012, Filters sources and sinks</a>.
  51. </p>
  52. <p>
  53. To obtain the <tt>mime</tt> namespace, run:
  54. </p>
  55. <pre class=example>
  56. -- loads the MIME module and everything it requires
  57. local mime = require("mime")
  58. </pre>
  59. <!-- High-level +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  60. <h3 id=high>High-level filters</h3>
  61. <!-- decode +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  62. <p class=name id="decode">
  63. mime.<b>decode(</b>"base64"<b>)</b><br>
  64. mime.<b>decode(</b>"quoted-printable"<b>)</b>
  65. </p>
  66. <p class=description>
  67. Returns a filter that decodes data from a given transfer content
  68. encoding.
  69. </p>
  70. <!-- encode +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  71. <p class=name id="encode">
  72. mime.<b>encode(</b>"base64"<b>)</b><br>
  73. mime.<b>encode(</b>"quoted-printable" [, mode]<b>)</b>
  74. </p>
  75. <p class=description>
  76. Returns a filter that encodes data according to a given transfer content
  77. encoding.
  78. </p>
  79. <p class=parameters>
  80. In the Quoted-Printable case, the user can specify whether the data is
  81. textual or binary, by passing the <tt>mode</tt> strings "<tt>text</tt>" or
  82. "<tt>binary</tt>". <tt>Mode</tt> defaults to "<tt>text</tt>".
  83. </p>
  84. <p class=note>
  85. Although both transfer content encodings specify a limit for the line
  86. length, the encoding filters do <em>not</em> break text into lines (for
  87. added flexibility).
  88. Below is a filter that converts binary data to the Base64 transfer content
  89. encoding and breaks it into lines of the correct size.
  90. </p>
  91. <pre class=example>
  92. base64 = ltn12.filter.chain(
  93. mime.encode("base64"),
  94. mime.wrap("base64")
  95. )
  96. </pre>
  97. <p class=note>
  98. Note: Text data <em>has</em> to be converted to canonic form
  99. <em>before</em> being encoded.
  100. </p>
  101. <pre class=example>
  102. base64 = ltn12.filter.chain(
  103. mime.normalize(),
  104. mime.encode("base64"),
  105. mime.wrap("base64")
  106. )
  107. </pre>
  108. <!-- normalize ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  109. <p class=name id="normalize">
  110. mime.<b>normalize(</b>[marker]<b>)</b>
  111. </p>
  112. <p class=description>
  113. Converts most common end-of-line markers to a specific given marker.
  114. </p>
  115. <p class=parameters>
  116. <tt>Marker</tt> is the new marker. It defaults to CRLF, the canonic
  117. end-of-line marker defined by the MIME standard.
  118. </p>
  119. <p class=return>
  120. The function returns a filter that performs the conversion.
  121. </p>
  122. <p class=note>
  123. Note: There is no perfect solution to this problem. Different end-of-line
  124. markers are an evil that will probably plague developers forever.
  125. This function, however, will work perfectly for text created with any of
  126. the most common end-of-line markers, i.e. the Mac OS (CR), the Unix (LF),
  127. or the DOS (CRLF) conventions. Even if the data has mixed end-of-line
  128. markers, the function will still work well, although it doesn't
  129. guarantee that the number of empty lines will be correct.
  130. </p>
  131. <!-- stuff +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  132. <p class=name id="stuff">
  133. mime.<b>stuff()</b><br>
  134. </p>
  135. <p class=description>
  136. Creates and returns a filter that performs stuffing of SMTP messages.
  137. </p>
  138. <p class=note>
  139. Note: The <a href=smtp.html#send><tt>smtp.send</tt></a> function
  140. uses this filter automatically. You don't need to chain it with your
  141. source, or apply it to your message body.
  142. </p>
  143. <!-- wrap +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  144. <p class=name id="wrap">
  145. mime.<b>wrap(</b>"text" [, length]<b>)</b><br>
  146. mime.<b>wrap(</b>"base64"<b>)</b><br>
  147. mime.<b>wrap(</b>"quoted-printable"<b>)</b>
  148. </p>
  149. <p class=description>
  150. Returns a filter that breaks data into lines.
  151. </p>
  152. <p class=parameters>
  153. The "<tt>text</tt>" line-wrap filter simply breaks text into lines by
  154. inserting CRLF end-of-line markers at appropriate positions.
  155. <tt>Length</tt> defaults 76.
  156. The "<tt>base64</tt>" line-wrap filter works just like the default
  157. "<tt>text</tt>" line-wrap filter with default length.
  158. The function can also wrap "<tt>quoted-printable</tt>" lines, taking care
  159. not to break lines in the middle of an escaped character. In that case, the
  160. line length is fixed at 76.
  161. </p>
  162. <p class=note>
  163. For example, to create an encoding filter for the Quoted-Printable transfer content encoding of text data, do the following:
  164. </p>
  165. <pre class=example>
  166. qp = ltn12.filter.chain(
  167. mime.normalize(),
  168. mime.encode("quoted-printable"),
  169. mime.wrap("quoted-printable")
  170. )
  171. </pre>
  172. <p class=note>
  173. Note: To break into lines with a different end-of-line convention, apply
  174. a normalization filter after the line break filter.
  175. </p>
  176. <!-- Low-level ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  177. <h3 id=low>Low-level filters</h3>
  178. <!-- b64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  179. <p class=name id="b64">
  180. A, B = mime.<b>b64(</b>C [, D]<b>)</b>
  181. </p>
  182. <p class=description>
  183. Low-level filter to perform Base64 encoding.
  184. </p>
  185. <p class=description>
  186. <tt>A</tt> is the encoded version of the largest prefix of
  187. <tt>C..D</tt>
  188. that can be encoded unambiguously. <tt>B</tt> has the remaining bytes of
  189. <tt>C..D</tt>, <em>before</em> encoding.
  190. If <tt>D</tt> is <tt><b>nil</b></tt>, <tt>A</tt> is padded with
  191. the encoding of the remaining bytes of <tt>C</tt>.
  192. </p>
  193. <p class=note>
  194. Note: The simplest use of this function is to encode a string into it's
  195. Base64 transfer content encoding. Notice the extra parenthesis around the
  196. call to <tt>mime.b64</tt>, to discard the second return value.
  197. </p>
  198. <pre class=example>
  199. print((mime.b64("diego:password")))
  200. --&gt; ZGllZ286cGFzc3dvcmQ=
  201. </pre>
  202. <!-- dot +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  203. <p class=name id="dot">
  204. A, n = mime.<b>dot(</b>m [, B]<b>)</b>
  205. </p>
  206. <p class=description>
  207. Low-level filter to perform SMTP stuffing and enable transmission of
  208. messages containing the sequence "CRLF.CRLF".
  209. </p>
  210. <p class=parameters>
  211. <tt>A</tt> is the stuffed version of <tt>B</tt>. '<tt>n</tt>' gives the
  212. number of characters from the sequence CRLF seen in the end of <tt>B</tt>.
  213. '<tt>m</tt>' should tell the same, but for the previous chunk.
  214. </p>
  215. <p class=note>Note: The message body is defined to begin with
  216. an implicit CRLF. Therefore, to stuff a message correctly, the
  217. first <tt>m</tt> should have the value 2.
  218. </p>
  219. <pre class=example>
  220. print((string.gsub(mime.dot(2, ".\r\nStuffing the message.\r\n.\r\n."), "\r\n", "\\n")))
  221. --&gt; ..\nStuffing the message.\n..\n..
  222. </pre>
  223. <p class=note>
  224. Note: The <a href=smtp.html#send><tt>smtp.send</tt></a> function
  225. uses this filter automatically. You don't need to
  226. apply it again.
  227. </p>
  228. <!-- eol ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  229. <p class=name id="eol">
  230. A, B = mime.<b>eol(</b>C [, D, marker]<b>)</b>
  231. </p>
  232. <p class=description>
  233. Low-level filter to perform end-of-line marker translation.
  234. For each chunk, the function needs to know if the last character of the
  235. previous chunk could be part of an end-of-line marker or not. This is the
  236. context the function receives besides the chunk. An updated version of
  237. the context is returned after each new chunk.
  238. </p>
  239. <p class=parameters>
  240. <tt>A</tt> is the translated version of <tt>D</tt>. <tt>C</tt> is the
  241. ASCII value of the last character of the previous chunk, if it was a
  242. candidate for line break, or 0 otherwise.
  243. <tt>B</tt> is the same as <tt>C</tt>, but for the current
  244. chunk. <tt>Marker</tt> gives the new end-of-line marker and defaults to CRLF.
  245. </p>
  246. <pre class=example>
  247. -- translates the end-of-line marker to UNIX
  248. unix = mime.eol(0, dos, "\n")
  249. </pre>
  250. <!-- qp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  251. <p class=name id="qp">
  252. A, B = mime.<b>qp(</b>C [, D, marker]<b>)</b>
  253. </p>
  254. <p class=description>
  255. Low-level filter to perform Quoted-Printable encoding.
  256. </p>
  257. <p class=parameters>
  258. <tt>A</tt> is the encoded version of the largest prefix of
  259. <tt>C..D</tt>
  260. that can be encoded unambiguously. <tt>B</tt> has the remaining bytes of
  261. <tt>C..D</tt>, <em>before</em> encoding.
  262. If <tt>D</tt> is <tt><b>nil</b></tt>, <tt>A</tt> is padded with
  263. the encoding of the remaining bytes of <tt>C</tt>.
  264. Throughout encoding, occurrences of CRLF are replaced by the
  265. <tt>marker</tt>, which itself defaults to CRLF.
  266. </p>
  267. <p class=note>
  268. Note: The simplest use of this function is to encode a string into it's
  269. Quoted-Printable transfer content encoding.
  270. Notice the extra parenthesis around the call to <tt>mime.qp</tt>, to discard the second return value.
  271. </p>
  272. <pre class=example>
  273. print((mime.qp("maçã")))
  274. --&gt; ma=E7=E3=
  275. </pre>
  276. <!-- qpwrp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  277. <p class=name id="qpwrp">
  278. A, m = mime.<b>qpwrp(</b>n [, B, length]<b>)</b>
  279. </p>
  280. <p class=description>
  281. Low-level filter to break Quoted-Printable text into lines.
  282. </p>
  283. <p class=parameters>
  284. <tt>A</tt> is a copy of <tt>B</tt>, broken into lines of at most
  285. <tt>length</tt> bytes (defaults to 76).
  286. '<tt>n</tt>' should tell how many bytes are left for the first
  287. line of <tt>B</tt> and '<tt>m</tt>' returns the number of bytes
  288. left in the last line of <tt>A</tt>.
  289. </p>
  290. <p class=note>
  291. Note: Besides breaking text into lines, this function makes sure the line
  292. breaks don't fall in the middle of an escaped character combination. Also,
  293. this function only breaks lines that are bigger than <tt>length</tt> bytes.
  294. </p>
  295. <!-- unb64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  296. <p class=name id="unb64">
  297. A, B = mime.<b>unb64(</b>C [, D]<b>)</b>
  298. </p>
  299. <p class=description>
  300. Low-level filter to perform Base64 decoding.
  301. </p>
  302. <p class=parameters>
  303. <tt>A</tt> is the decoded version of the largest prefix of
  304. <tt>C..D</tt>
  305. that can be decoded unambiguously. <tt>B</tt> has the remaining bytes of
  306. <tt>C..D</tt>, <em>before</em> decoding.
  307. If <tt>D</tt> is <tt><b>nil</b></tt>, <tt>A</tt> is the empty string
  308. and <tt>B</tt> returns whatever couldn't be decoded.
  309. </p>
  310. <p class=note>
  311. Note: The simplest use of this function is to decode a string from it's
  312. Base64 transfer content encoding.
  313. Notice the extra parenthesis around the call to <tt>mime.unqp</tt>, to discard the second return value.
  314. </p>
  315. <pre class=example>
  316. print((mime.unb64("ZGllZ286cGFzc3dvcmQ=")))
  317. --&gt; diego:password
  318. </pre>
  319. <!-- unqp +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  320. <p class=name id="unqp">
  321. A, B = mime.<b>unqp(</b>C [, D]<b>)</b>
  322. </p>
  323. <p class=description>
  324. Low-level filter to remove the Quoted-Printable transfer content encoding
  325. from data.
  326. </p>
  327. <p class=parameters>
  328. <tt>A</tt> is the decoded version of the largest prefix of
  329. <tt>C..D</tt>
  330. that can be decoded unambiguously. <tt>B</tt> has the remaining bytes of
  331. <tt>C..D</tt>, <em>before</em> decoding.
  332. If <tt>D</tt> is <tt><b>nil</b></tt>, <tt>A</tt> is augmented with
  333. the encoding of the remaining bytes of <tt>C</tt>.
  334. </p>
  335. <p class=note>
  336. Note: The simplest use of this function is to decode a string from it's
  337. Quoted-Printable transfer content encoding.
  338. Notice the extra parenthesis around the call to <tt>mime.unqp</tt>, to discard the second return value.
  339. </p>
  340. <pre class=example>
  341. print((mime.qp("ma=E7=E3=")))
  342. --&gt; maçã
  343. </pre>
  344. <!-- wrp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  345. <p class=name id="wrp">
  346. A, m = mime.<b>wrp(</b>n [, B, length]<b>)</b>
  347. </p>
  348. <p class=description>
  349. Low-level filter to break text into lines with CRLF marker.
  350. Text is assumed to be in the <a href=#normalize><tt>normalize</tt></a> form.
  351. </p>
  352. <p class=parameters>
  353. <tt>A</tt> is a copy of <tt>B</tt>, broken into lines of at most
  354. <tt>length</tt> bytes (defaults to 76).
  355. '<tt>n</tt>' should tell how many bytes are left for the first
  356. line of <tt>B</tt> and '<tt>m</tt>' returns the number of bytes
  357. left in the last line of <tt>A</tt>.
  358. </p>
  359. <p class=note>
  360. Note: This function only breaks lines that are bigger than
  361. <tt>length</tt> bytes. The resulting line length does not include the CRLF
  362. marker.
  363. </p>
  364. <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  365. <div class=footer>
  366. <hr>
  367. <center>
  368. <p class=bar>
  369. <a href="index.html">home</a> &middot;
  370. <a href="index.html#down">download</a> &middot;
  371. <a href="installation.html">installation</a> &middot;
  372. <a href="introduction.html">introduction</a> &middot;
  373. <a href="reference.html">reference</a>
  374. </p>
  375. <p>
  376. <small>
  377. Last modified by Diego Nehab on <br>
  378. Fri Mar 4 15:19:17 BRT 2016
  379. </small>
  380. </p>
  381. </center>
  382. </div>
  383. </body>
  384. </html>