Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(305)

Unified Diff: ios/web/web_state/js/resources/post_request.js

Issue 1375023002: Adds support for POST request with bodies on WKWebView. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rest of the commetns Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..5d13a50c2586c499601650279ce4c1d78a0dc17d
--- /dev/null
+++ b/ios/web/web_state/js/resources/post_request.js
@@ -0,0 +1,78 @@
+// 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');
Eugene But (OOO till 7-30) 2015/10/08 17:46:32 s/__crPOSTRequestHack/__crWeb.postRequestHack
stkhapugin 2015/12/03 15:43:01 Done.
+
+/**
+ * Namespace for this file.
+ */
+__crPOSTRequestHack = {};
Eugene But (OOO till 7-30) 2015/10/08 17:46:32 __crPostRequestHack According to JS Style Guide a
stkhapugin 2015/12/03 15:43:01 Done.
+
+/**
+ * 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} base64Data Request body encoded with Base64.
+ * @param {string} contentType Content-Type header value.
+ */
+__crPOSTRequestHack.runPOSTRequest = function(url, headers, base64Data,
Eugene But (OOO till 7-30) 2015/10/08 17:46:32 NIT: s/base64Data/body this way we can keep consis
stkhapugin 2015/12/03 15:43:01 Done.
+ 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){
+ 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});
+ 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} b64Data Request body encoded with Base64.
+ * @param {string} contentType Content-Type header value.
+ */
+ var createAndSendPOSTRequest = function(url, headers, b64Data, contentType){
Eugene But (OOO till 7-30) 2015/10/08 17:46:32 createAndSendPostRequest
Eugene But (OOO till 7-30) 2015/10/08 17:46:32 NIT: s/b64Data/body
stkhapugin 2015/12/03 15:43:01 Done.
stkhapugin 2015/12/03 15:43:01 Done.
+ var request = new XMLHttpRequest();
+ request.open('POST', url, false);
+ for (var key in headers) {
+ request.setRequestHeader(key, headers[key]);
Eugene But (OOO till 7-30) 2015/10/08 17:46:32 You want to make hasOwnProperty check, otherwise y
stkhapugin 2015/12/03 15:43:01 Done.
+ }
+ var blob = base64ToBlob(atob(b64Data), contentType);
+ request.send(blob);
+ if (request.status != 200) {
+ throw request.status;
stuartmorgan 2015/10/08 19:11:29 Why are we throwing a JS exception for non-200 sta
stkhapugin 2015/12/03 15:43:01 I added an error handler that catches any non-200
+ }
+ return request.responseText;
+ }
+
+ document.open();
+ document.write(createAndSendPOSTRequest(url, headers, base64Data,
Eugene But (OOO till 7-30) 2015/10/08 17:46:32 document.write reinjects our scripts w/o giving us
stkhapugin 2015/12/03 15:43:01 Done.
+ contentType));
+ document.close();
+ }

Powered by Google App Engine
This is Rietveld 408576698