/
multipartFormData.js
186 lines (155 loc) · 4.1 KB
/
multipartFormData.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var Ci = Components.interfaces;
var Cc = Components.classes;
var Cr = Components.results;
function NTT_MakeStream(data)
{
var stream = Cc["@mozilla.org/io/string-input-stream;1"]
.createInstance(Ci.nsIStringInputStream);
stream.setData(data, data.length);
return stream;
}
const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
function NTT_decode64(input)
{
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
do
{
enc1 = keyStr.indexOf(input.charAt(i++));
enc2 = keyStr.indexOf(input.charAt(i++));
enc3 = keyStr.indexOf(input.charAt(i++));
enc4 = keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64)
{
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64)
{
output = output + String.fromCharCode(chr3);
}
}
while (i < input.length);
return output;
}
function MultipartFormData()
{
this.boundary = "hsdluicmwos";
this.controls = [];
this.files = [];
this.postdata = "";
}
MultipartFormData.prototype = {
boundary: null,
controls: null,
files: null,
length: null,
postdata: null,
getPostData: function()
{
if (this.postdata)
return this.postdata;
var data = "";
for (var name in this.controls)
{
data+="\r\n--"+this.boundary+"\r\n";
data+="Content-Disposition: form-data; name=\""+name+"\"\r\n\r\n";
data+=this.controls[name];
}
for (var name in this.files)
{
var filedata = this.files[name];
data+="\r\n--"+this.boundary+"\r\n";
data+="Content-Disposition: form-data; name=\""+name+"\"; filename=\""+filedata.filename+"\"\r\n";
data+="Content-Type: "+filedata.contenttype+"\r\n";
if (filedata.source)
{
data+="Content-Transfer-Encoding: base64\r\n\r\n";
var fis = Cc["@mozilla.org/network/file-input-stream;1"]
.createInstance(Ci.nsIFileInputStream);
fis.init(filedata.source, 1, 384, Ci.nsIFileInputStream.CLOSE_ON_EOF);
var bis = Cc["@mozilla.org/binaryinputstream;1"]
.createInstance(Ci.nsIBinaryInputStream);
bis.setInputStream(fis);
// TODO this isnt needed as yet
}
else
{
data+="Content-Transfer-Encoding: binary\r\n\r\n";
if (filedata.encoding == "base64")
{
data+=NTT_decode64(filedata.data);
}
else if (filedata.encoding == "binary")
{
data+=filedata.data;
}
}
}
data+="\r\n--"+this.boundary+"--\r\n";
this.length = data.length-2;
this.postdata = data;
return data;
},
getPostDataStream: function()
{
return NTT_MakeStream(this.getPostData());
},
getHeaders: function()
{
if (!this.length)
this.getPostData();
var headers = "";
headers+="Content-Type: "+this.getContentType()+"\r\n";
headers+="Content-Length: "+this.length+"\r\n";
return headers;
},
getHeaderStream: function()
{
return NTT_MakeStream(this.getHeaders());
},
getContentType: function()
{
return "multipart/form-data; boundary=\""+this.boundary+"\"";
},
addControl: function(name, value)
{
this.controls[name]=value;
this.postdata = null;
this.length = null;
},
addFile: function(name, contenttype, file)
{
throw Components.results.NS_NOT_IMPLEMENTED;
var filedata = {
filename: file.leafName,
contenttype: contenttype,
source: file
};
this.files[name] = filedata;
this.postdata = null;
this.length = null;
},
addFileData: function(name, filename, contenttype, encoding, data)
{
var filedata = {
filename: filename,
contenttype: contenttype,
encoding: encoding,
data: data
};
this.files[name] = filedata;
this.postdata = null;
this.length = null;
},
}