-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMimeString.as
140 lines (117 loc) · 3.01 KB
/
MimeString.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* MimeString Class
*
* (C) David Verhasselt 2007
* Licensed under the MIT license
*/
package crowdway.http
{
import crowdway.http.MimePart;
import flash.utils.ByteArray;
import flash.events.*;
import mx.utils.StringUtil;
import flash.net.*;
/**
* Implements any input-field from a form that can be represented as a string
*
* @see MimePart
* @see MimeFile
* @see HTTPForm
*/
public class MimeString implements MimePart
{
private const crlf= "\r\n";
// Content parameters
private var name:String;
private var contentType:String;
private var charset:String;
private var content:String;
/**
* Factory-Method: Creates a new MimeString with the supplied name, content, and optional charset
*/
public static function fromString(name:String,content:String,charset:String = null)
{
var result:MimeString = new MimeString();
result.name = name;
result.setContent(content,charset);
return result;
}
/**
* Set the content of this field.
*
* If charset is set, content-type and charset are populated and the content is written with an extra linebreak at the end, as in this example:
* From: Nathaniel Borenstein <nsb@bellcore.com>
* To: Ned Freed <ned@innosoft.com>
* Subject: Sample message
* MIME-Version: 1.0
* Content-type: multipart/mixed; boundary="simple boundary"
* This is the preamble. It is to be ignored, though it
* is a handy place for mail composers to include an
* explanatory note to non-MIME compliant readers.
* --simple boundary
*
* This is implicitly typed plain ASCII text.
* It does NOT end with a linebreak.
* --simple boundary
* Content-type: text/plain; charset=us-ascii
*
* This is explicitly typed plain ASCII text.
* It DOES end with a linebreak.
*
* --simple boundary--
* This is the epilogue. It is also to be ignored
*
* @param content The new content of the field
* @param charset The optional charset of the string, if none set, uses default us-ascii
*/
private function setContent(content:String, charset:String = null)
{
if (charset != null)
{
this.contentType = "text/plain";
this.charset = charset;
//this.content.writeMultiByte(content + crlf,charset);
this.content = content;
}
else
{
this.contentType = null;
this.charset = null;
//this.content.writeMultiByte(content,"us-ascii");
this.content = content;
}
}
public function getName():String
{
return this.name;
}
public function getFilename():String
{
return null;
}
public function getHead():String
{
if (this.contentType == null)
{
return null;
}
else
{
return "Content-Type: " + this.contentType + "; charset=" + this.charset;
}
}
public function getBody():ByteArray
{
var result:ByteArray = new ByteArray();
if (this.contentType != null)
{
result.writeMultiByte(this.content, this.charset);
}
else
{
result.writeUTFBytes(this.content);
}
return result;
}
}
}