Debian Codegraph @

closure-library/closure/goog/net/rpc/httpcors.js

raw
1 // Copyright 2017 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Provides CORS support for HTTP based RPC requests.
17 *
18 * As part of net.rpc package, CORS features provided by this class
19 * depend on the server support. Please check related specs to decide how
20 * to enable any of the features provided by this class.
21 *
22 */
23
24 goog.module('goog.net.rpc.HttpCors');
25
26 var GoogUri = goog.require('goog.Uri');
27 var googObject = goog.require('goog.object');
28 var googString = goog.require('goog.string');
29 var googUriUtils = goog.require('goog.uri.utils');
30
31
32 /**
33 * The default URL parameter name to overwrite http headers with a URL param
34 * to avoid CORS preflight.
35 *
36 * See https://github.com/whatwg/fetch/issues/210#issue-129531743 for the spec.
37 *
38 * @type {string}
39 */
40 exports.HTTP_HEADERS_PARAM_NAME = '$httpHeaders';
41
42
43 /**
44 * The default URL parameter name to overwrite http method with a URL param
45 * to avoid CORS preflight.
46 *
47 * See https://github.com/whatwg/fetch/issues/210#issue-129531743 for the spec.
48 *
49 * @type {string}
50 */
51 exports.HTTP_METHOD_PARAM_NAME = '$httpMethod';
52
53
54 /**
55 * Generates the URL parameter value with custom headers encoded as
56 * HTTP/1.1 headers block.
57 *
58 * @param {!Object<string, string>} headers The custom headers.
59 * @return {string} The URL param to overwrite custom HTTP headers.
60 */
61 exports.generateHttpHeadersOverwriteParam = function(headers) {
62 var result = '';
63 googObject.forEach(headers, function(value, key) {
64 result += key;
65 result += ':';
66 result += value;
67 result += '\r\n';
68 });
69 return result;
70 };
71
72
73 /**
74 * Generates the URL-encoded URL parameter value with custom headers encoded as
75 * HTTP/1.1 headers block.
76 *
77 * @param {!Object<string, string>} headers The custom headers.
78 * @return {string} The URL param to overwrite custom HTTP headers.
79 */
80 exports.generateEncodedHttpHeadersOverwriteParam = function(headers) {
81 return googString.urlEncode(
82 exports.generateHttpHeadersOverwriteParam(headers));
83 };
84
85
86 /**
87 * Sets custom HTTP headers via an overwrite URL param.
88 *
89 * @param {!GoogUri|string} url The URI object or a string path.
90 * @param {string} urlParam The URL param name.
91 * @param {!Object<string, string>} extraHeaders The HTTP headers.
92 * @return {!GoogUri|string} The URI object or a string path with headers
93 * encoded as a url param.
94 */
95 exports.setHttpHeadersWithOverwriteParam = function(
96 url, urlParam, extraHeaders) {
97 if (googObject.isEmpty(extraHeaders)) {
98 return url;
99 }
100 var httpHeaders = exports.generateHttpHeadersOverwriteParam(extraHeaders);
101 if (goog.isString(url)) {
102 return googUriUtils.appendParam(
103 url, googString.urlEncode(urlParam), httpHeaders);
104 } else {
105 url.setParameterValue(urlParam, httpHeaders); // duplicate removed!
106 return url;
107 }
108 };