comparison components/nttMultipartFormData.js @ 0:dada0ac40a8f

initial import
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Tue, 02 Dec 2008 20:31:01 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:dada0ac40a8f
1 const Ci = Components.interfaces;
2 const Cc = Components.classes;
3 const Cr = Components.results;
4
5 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
6
7 function NTT_MakeStream(data)
8 {
9 var stream = Cc["@mozilla.org/io/string-input-stream;1"]
10 .createInstance(Ci.nsIStringInputStream);
11 stream.setData(data, data.length);
12 return stream;
13 }
14
15 const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
16
17 function NTT_decode64(input)
18 {
19 var output = "";
20 var chr1, chr2, chr3;
21 var enc1, enc2, enc3, enc4;
22 var i = 0;
23
24 // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
25 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
26
27 do
28 {
29 enc1 = keyStr.indexOf(input.charAt(i++));
30 enc2 = keyStr.indexOf(input.charAt(i++));
31 enc3 = keyStr.indexOf(input.charAt(i++));
32 enc4 = keyStr.indexOf(input.charAt(i++));
33
34 chr1 = (enc1 << 2) | (enc2 >> 4);
35 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
36 chr3 = ((enc3 & 3) << 6) | enc4;
37
38 output = output + String.fromCharCode(chr1);
39
40 if (enc3 != 64)
41 {
42 output = output + String.fromCharCode(chr2);
43 }
44 if (enc4 != 64)
45 {
46 output = output + String.fromCharCode(chr3);
47 }
48 }
49 while (i < input.length);
50
51 return output;
52 }
53
54 function MultipartFormData()
55 {
56 this.boundary = "hsdluicmwos";
57 this.controls = [];
58 this.files = [];
59 this.postdata = "";
60 }
61
62 MultipartFormData.prototype = {
63
64 boundary: null,
65 controls: null,
66 files: null,
67 length: null,
68 postdata: null,
69
70 getPostData: function()
71 {
72 if (this.postdata)
73 return this.postdata;
74
75 var data = "";
76
77 for (var name in this.controls)
78 {
79 data+="\r\n--"+this.boundary+"\r\n";
80 data+="Content-Disposition: form-data; name=\""+name+"\"\r\n\r\n";
81 data+=this.controls[name];
82 }
83
84 for (var name in this.files)
85 {
86 var filedata = this.files[name];
87 data+="\r\n--"+this.boundary+"\r\n";
88 data+="Content-Disposition: form-data; name=\""+name+"\"; filename=\""+filedata.filename+"\"\r\n";
89 data+="Content-Type: "+filedata.contenttype+"\r\n";
90 if (filedata.source)
91 {
92 data+="Content-Transfer-Encoding: base64\r\n\r\n";
93
94 var fis = Cc["@mozilla.org/network/file-input-stream;1"]
95 .createInstance(Ci.nsIFileInputStream);
96 fis.init(filedata.source, 1, 384, Ci.nsIFileInputStream.CLOSE_ON_EOF);
97
98 var bis = Cc["@mozilla.org/binaryinputstream;1"]
99 .createInstance(Ci.nsIBinaryInputStream);
100 bis.setInputStream(fis);
101
102 //TODO this isnt needed as yet
103 }
104 else
105 {
106 data+="Content-Transfer-Encoding: binary\r\n\r\n";
107 if (filedata.encoding == "base64")
108 {
109 data+=NTT_decode64(filedata.data);
110 }
111 else if (filedata.encoding == "binary")
112 {
113 data+=filedata.data;
114 }
115 }
116 }
117 data+="\r\n--"+this.boundary+"--\r\n";
118
119 this.length = data.length-2;
120 this.postdata = data;
121
122 return data;
123 },
124
125 getPostDataStream: function()
126 {
127 return NTT_MakeStream(this.getPostData());
128 },
129
130 getHeaders: function()
131 {
132 if (!this.length)
133 this.getPostData();
134
135 var headers = "";
136 headers+="Content-Type: "+this.getContentType()+"\r\n";
137 headers+="Content-Length: "+this.length+"\r\n";
138 return headers;
139 },
140
141 getHeaderStream: function()
142 {
143 return NTT_MakeStream(this.getHeaders());
144 },
145
146 getContentType: function()
147 {
148 return "multipart/form-data; boundary=\""+this.boundary+"\"";
149 },
150
151 addControl: function(name, value)
152 {
153 this.controls[name]=value;
154 this.postdata = null;
155 this.length = null;
156 },
157
158 addFile: function(name, contenttype, file)
159 {
160 throw Components.results.NS_NOT_IMPLEMENTED;
161 var filedata = {
162 filename: file.leafName,
163 contenttype: contenttype,
164 source: file
165 };
166 this.files[name] = filedata;
167 this.postdata = null;
168 this.length = null;
169 },
170
171 addFileData: function(name, filename, contenttype, encoding, data)
172 {
173 var filedata = {
174 filename: filename,
175 contenttype: contenttype,
176 encoding: encoding,
177 data: data
178 };
179 this.files[name] = filedata;
180 this.postdata = null;
181 this.length = null;
182 },
183
184 classDescription: "Nightly Tester Multipart Form Data",
185 contractID: "@blueprintit.co.uk/multipartformdata;1",
186 classID: Components.ID("{46c8b0c6-216c-41e8-ace2-03d61783e278}"),
187 QueryInterface: XPCOMUtils.generateQI([Ci.nttIMultipartFormData])
188 }
189
190 function NSGetModule(compMgr, fileSpec)
191 XPCOMUtils.generateModule([MultipartFormData]);