OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Provides a way to implement POST requests via XMLHttpRequest. | |
6 // Works around https://bugs.webkit.org/show_bug.cgi?id=145410 on WKWebView. | |
7 | |
8 'use strict'; | |
9 | |
10 goog.provide('__crPostRequestWorkaround'); | |
11 | |
12 /** | |
13 * Namespace for this file. | |
14 */ | |
15 __crPostRequestWorkaround = {}; | |
16 | |
17 /** | |
18 * Executes a POST request with given parameters and replaces document body with | |
19 * the response. | |
20 * @param {string} url The url of the request. | |
21 * @param {Object<string,string>} headers Request headers to include in POST. | |
22 * Each header value must be expressed as a key-value pair in this object. | |
23 * @param {string} body Request body encoded with Base64. | |
24 * @param {string} contentType Content-Type header value. | |
25 */ | |
26 __crPostRequestWorkaround.runPostRequest = function(url, headers, body, | |
27 contentType) { | |
noyau (Ping after 24h)
2015/12/04 12:19:18
Formatting is weird here.
stkhapugin
2015/12/04 16:28:10
Reformatted as per JS style guide:
// Arguments s
| |
28 | |
29 /** | |
noyau (Ping after 24h)
2015/12/04 12:19:18
Indent should be 2 here, not 4
stkhapugin
2015/12/04 16:28:10
Done.
| |
30 * Converts a Base64-encoded string to a blob. | |
31 * @param {string} byteCharacters Base64-encoded data string. | |
32 * @param {string} contentType Corresponding content type. | |
33 * @return {Blob} Binary representation of byteCharacters. | |
34 */ | |
35 var base64ToBlob = function(byteCharacters, contentType) { | |
36 contentType = contentType || ''; | |
37 var sliceSize = 512; | |
38 var byteArrays = []; | |
39 var byteCount = byteCharacters.length; | |
40 for (var offset = 0; offset < byteCount; offset += sliceSize) { | |
41 var slice = byteCharacters.slice(offset, offset + sliceSize); | |
42 var byteNumbers = new Array(slice.length); | |
43 for (var i = 0; i < slice.length; i++) { | |
44 byteNumbers[i] = slice.charCodeAt(i); | |
45 } | |
46 var byteArray = new Uint8Array(byteNumbers); | |
47 byteArrays.push(byteArray); | |
48 } | |
49 return new Blob(byteArrays, {type: contentType}); | |
50 } | |
51 | |
52 /** | |
53 * Creates and executes a POST request. | |
54 * @param {string} url The url of the request. | |
55 * @param {Object<string,string>} headers Request headers to include in POST. | |
56 * Each header value must be expressed as a key-value pair in this object. | |
57 * @param {string} body Request body encoded with Base64. | |
58 * @param {string} contentType Content-Type header value. | |
59 */ | |
60 var createAndSendPostRequest = function(url, headers, body, contentType) { | |
61 var request = new XMLHttpRequest(); | |
62 request.open('POST', url, false); | |
63 for (var key in headers) { | |
64 if (headers.hasOwnProperty(key)) { | |
65 request.setRequestHeader(key, headers[key]); | |
66 } | |
67 } | |
68 var blob = base64ToBlob(atob(body), contentType); | |
69 request.send(blob); | |
70 if (request.status != 200) { | |
71 throw request.status; | |
72 } | |
73 return request.responseText; | |
74 } | |
75 | |
76 document.open(); | |
77 try { | |
78 document.write(createAndSendPostRequest(url, headers, body, contentType)); | |
79 window.webkit.messageHandlers.POSTSuccessHandler.postMessage(""); | |
80 } catch(error) { | |
81 window.webkit.messageHandlers.POSTErrorHandler.postMessage(error); | |
82 } | |
83 document.close(); | |
noyau (Ping after 24h)
2015/12/04 12:19:18
Indent is wrong here.
stkhapugin
2015/12/04 16:28:10
Done.
| |
84 } | |
OLD | NEW |