httpcors.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. * @fileoverview Provides CORS support for HTTP based RPC requests.
  16. *
  17. * As part of net.rpc package, CORS features provided by this class
  18. * depend on the server support. Please check related specs to decide how
  19. * to enable any of the features provided by this class.
  20. *
  21. */
  22. goog.module('goog.net.rpc.HttpCors');
  23. var GoogUri = goog.require('goog.Uri');
  24. var googObject = goog.require('goog.object');
  25. var googString = goog.require('goog.string');
  26. var googUriUtils = goog.require('goog.uri.utils');
  27. /**
  28. * The default URL parameter name to overwrite http headers with a URL param
  29. * to avoid CORS preflight.
  30. *
  31. * See https://github.com/whatwg/fetch/issues/210#issue-129531743 for the spec.
  32. *
  33. * @type {string}
  34. */
  35. exports.HTTP_HEADERS_PARAM_NAME = '$httpHeaders';
  36. /**
  37. * Generates the URL parameter value with custom headers encoded as
  38. * HTTP/1.1 headers block.
  39. *
  40. * @param {!Object<string, string>} headers The custom headers.
  41. * @return {string} The URL param to overwrite custom HTTP headers.
  42. */
  43. exports.generateHttpHeadersOverwriteParam = function(headers) {
  44. var result = '';
  45. googObject.forEach(headers, function(value, key) {
  46. result += key;
  47. result += ':';
  48. result += value;
  49. result += '\r\n';
  50. });
  51. return result;
  52. };
  53. /**
  54. * Generates the URL-encoded URL parameter value with custom headers encoded as
  55. * HTTP/1.1 headers block.
  56. *
  57. * @param {!Object<string, string>} headers The custom headers.
  58. * @return {string} The URL param to overwrite custom HTTP headers.
  59. */
  60. exports.generateEncodedHttpHeadersOverwriteParam = function(headers) {
  61. return googString.urlEncode(
  62. exports.generateHttpHeadersOverwriteParam(headers));
  63. };
  64. /**
  65. * Sets custom HTTP headers via an overwrite URL param.
  66. *
  67. * @param {!GoogUri|string} url The URI object or a string path.
  68. * @param {string} urlParam The URL param name.
  69. * @param {!Object<string, string>} extraHeaders The HTTP headers.
  70. * @return {!GoogUri|string} The URI object or a string path with headers
  71. * encoded as a url param.
  72. */
  73. exports.setHttpHeadersWithOverwriteParam = function(
  74. url, urlParam, extraHeaders) {
  75. if (googObject.isEmpty(extraHeaders)) {
  76. return url;
  77. }
  78. var httpHeaders = exports.generateHttpHeadersOverwriteParam(extraHeaders);
  79. if (goog.isString(url)) {
  80. return googUriUtils.appendParam(
  81. url, googString.urlEncode(urlParam), httpHeaders);
  82. } else {
  83. url.setParameterValue(urlParam, httpHeaders); // duplicate removed!
  84. return url;
  85. }
  86. };