Commit 9ad70d39 9ad70d393ad07d00f834611b3492ec7e4aa6e3dc by Nicolas Perriault

Merge remote-tracking branch 'jasonlfunk/master' into jasonlfunk-integration

2 parents 5319067c 8641c3de
...@@ -120,13 +120,15 @@ ...@@ -120,13 +120,15 @@
120 * 120 *
121 * NOTE: we cannot use window.btoa() for some strange reasons here. 121 * NOTE: we cannot use window.btoa() for some strange reasons here.
122 * 122 *
123 * @param String url The url to download 123 * @param String url The url to download
124 * @return string Base64 encoded result 124 * @param String method The method to use, optional: default GET
125 * @param String data The data to send, optional
126 * @return string Base64 encoded result
125 */ 127 */
126 base64encode: function(url) { 128 base64encode: function(url,method,data) {
127 return this.evaluate(function(url) { 129 return this.evaluate(function(url,method,data) {
128 return __utils__.getBase64(url); 130 return __utils__.getBase64(url,method,data);
129 }, { url: url }); 131 }, { url: url, method: method, data: data });
130 }, 132 },
131 133
132 /** 134 /**
......
...@@ -224,10 +224,12 @@ ...@@ -224,10 +224,12 @@
224 * contents. 224 * contents.
225 * 225 *
226 * @param String url The resource url 226 * @param String url The resource url
227 * @param String method The request method, optional
228 * @param Object data The request data, optional
227 * @return String Base64 contents string 229 * @return String Base64 contents string
228 */ 230 */
229 this.getBase64 = function(url) { 231 this.getBase64 = function(url,method,data) {
230 return this.encode(this.getBinary(url)); 232 return this.encode(this.getBinary(url,method,data));
231 }; 233 };
232 234
233 /** 235 /**
...@@ -235,14 +237,38 @@ ...@@ -235,14 +237,38 @@
235 * fails but log errors. 237 * fails but log errors.
236 * 238 *
237 * @param String url 239 * @param String url
240 * @param String method
241 * @param Object data
238 * @return string 242 * @return string
239 */ 243 */
240 this.getBinary = function(url) { 244 this.getBinary = function(url, method, data) {
241 try { 245 try {
242 var xhr = new XMLHttpRequest(); 246 var xhr = new XMLHttpRequest();
243 xhr.open("GET", url, false); 247 if (method === undefined || ["GET","get","POST","post"].indexOf(method) == -1) {
248 method = "GET";
249 } else {
250 method = method.toUpperCase();
251 }
252
253 xhr.open(method, url, false);
254 this.log("using HTTP method: '" + method + "'", "debug");
244 xhr.overrideMimeType("text/plain; charset=x-user-defined"); 255 xhr.overrideMimeType("text/plain; charset=x-user-defined");
245 xhr.send(null); 256 if (method == "POST") {
257 if(data === undefined) {
258 data_str = "";
259 } else {
260 data_str = "";
261 for (k in data) {
262 if (typeof(k) == "string" && typeof(data[k]) == "string") {
263 data_str += "&" + escape(k) + "=" + escape(data[k]);
264 }
265 }
266 data_str = data_str.substring(1);
267 }
268 xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
269 }
270 this.log("using request data: '" + data_str + "'", "debug");
271 xhr.send(method == "POST" ? data_str : null);
246 return xhr.responseText; 272 return xhr.responseText;
247 } catch (e) { 273 } catch (e) {
248 if (e.name === "NETWORK_ERR" && e.code === 101) { 274 if (e.name === "NETWORK_ERR" && e.code === 101) {
...@@ -358,4 +384,4 @@ ...@@ -358,4 +384,4 @@
358 return out; 384 return out;
359 }; 385 };
360 }; 386 };
361 })(phantom);
...\ No newline at end of file ...\ No newline at end of file
387 })(phantom);
......