Chromium Code Reviews| Index: ios/web/web_state/js/resources/post_request.js |
| diff --git a/ios/web/web_state/js/resources/post_request.js b/ios/web/web_state/js/resources/post_request.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..194975a5d0e1e6d4d27fc0322ab1b34103b6aaf5 |
| --- /dev/null |
| +++ b/ios/web/web_state/js/resources/post_request.js |
| @@ -0,0 +1,85 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// Provides a way to implement POST requests via XMLHttpRequest. |
| +// Works around https://bugs.webkit.org/show_bug.cgi?id=145410 on WKWebView. |
| + |
| +'use strict'; |
| + |
| +goog.provide('__crPostRequestHack'); |
| + |
| +/** |
| + * Namespace for this file. |
| + */ |
| +__crPostRequestHack = {}; |
| + |
| +/** |
| + * Executes a POST request with given parameters and replaces document body with |
| + * the response. |
| + * @param {string} url The url of the request. |
| + * @param {Object<string,string>} headers Request headers to include in POST. |
| + * Each header value must be expressed as a key-value pair in this object. |
| + * @param {string} body Request body encoded with Base64. |
| + * @param {string} contentType Content-Type header value. |
| + */ |
| +__crPostRequestHack.runPostRequest = function(url, headers, body, |
| + contentType) { |
| + |
| + /** |
| + * Converts a Base64-encoded string to a blob. |
| + * @param {string} byteCharacters Base64-encoded data string. |
| + * @param {string} contentType Corresponding content type. |
| + * @return {Blob} Binary representation of byteCharacters. |
| + */ |
| + var base64ToBlob = function(byteCharacters, contentType) { |
| + contentType = contentType || ''; |
| + var sliceSize = 512; |
| + var byteArrays = []; |
| + for (var offset = 0; offset < byteCharacters.length; offset += sliceSize){ |
|
Eugene But (OOO till 7-30)
2015/12/02 17:06:16
Space before |{|, here and everywhere.
stkhapugin
2015/12/03 15:43:02
Done.
|
| + var slice = byteCharacters.slice(offset, offset + sliceSize); |
| + var byteNumbers = new Array(slice.length); |
| + for (var i = 0; i < slice.length; i++) { |
| + byteNumbers[i] = slice.charCodeAt(i); |
| + } |
| + var byteArray = new Uint8Array(byteNumbers); |
| + byteArrays.push(byteArray); |
| + } |
| + var blob = new Blob(byteArrays, {type: contentType}); |
|
Eugene But (OOO till 7-30)
2015/12/02 17:06:16
return new Blob(byteArrays, {type: contentType});
stkhapugin
2015/12/03 15:43:02
Done.
|
| + return blob; |
| + } |
| + |
| + /** |
| + * Creates and executes a POST request. |
| + * @param {string} url The url of the request. |
| + * @param {Object<string,string>} headers Request headers to include in POST. |
| + * Each header value must be expressed as a key-value pair in this object. |
| + * @param {string} body Request body encoded with Base64. |
| + * @param {string} contentType Content-Type header value. |
| + */ |
| + var createAndSendPostRequest = function(url, headers, body, contentType){ |
| + var request = new XMLHttpRequest(); |
| + request.open('POST', url, false); |
| + for (var key in headers) { |
| + if (headers.hasOwnProperty(key)) { |
| + request.setRequestHeader(key, headers[key]); |
| + } |
| + } |
| + var blob = base64ToBlob(atob(body), contentType); |
| + request.send(blob); |
| + if (request.status != 200) { |
| + throw request.status; |
| + } |
| + return request.responseText; |
| + } |
| + |
| + document.open(); |
| + try { |
| + document.write(createAndSendPostRequest(url, headers, body, |
| + contentType)); |
|
Eugene But (OOO till 7-30)
2015/12/02 17:06:16
This does not look like correct formatting
stkhapugin
2015/12/03 15:43:02
Done.
|
| + window.webkit.messageHandlers.POSTSuccess.postMessage(""); |
| + } catch(error) { |
| + window.webkit.messageHandlers.POSTErrorHandler.postMessage(error); |
| + } |
| + document.close(); |
| + } |