net/request.js

  1. /**
  2. * @fileoverview Simple implementation of
  3. * <code>javax.servlet.ServletRequest.</code> and
  4. * <code>javax.servlet.http.HttpServletRequest</code>.
  5. *
  6. * @see https://google.github.io/styleguide/javascriptguide.xml
  7. * @see https://developers.google.com/closure/compiler/docs/js-for-compiler
  8. * @see https://docs.oracle.com/javaee/7/api/javax/servlet/ServletRequest.html
  9. * @see https://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServletRequest.html
  10. * @module glize/net/requests
  11. * @requires glize/dom/cookies
  12. * @requires glize/utils/number
  13. */
  14. import { cookies } from '../dom/index.js';
  15. import { uint32 } from '../utils/number.js';
  16. /**
  17. * Returns the query string that is contained in the request URL after the
  18. * path.
  19. * @param {?Element|?Location|?URL|string=} opt_location Optional location object.
  20. * @return {string} Returns the query string that is contained in the request
  21. * URL after the path.
  22. * @method
  23. */
  24. export const getQueryString = (opt_location) => {
  25. opt_location = getLocation_(opt_location);
  26. return opt_location.search && opt_location.search.substr(1);
  27. };
  28. /**
  29. * Returns an <code>Array</code> of all the cookies included
  30. * with this request.
  31. * @return {!Array<!Object>} Returns an <code>Array</code> of all the cookies
  32. * included with this request.
  33. * @method
  34. */
  35. export const getCookies = function() {
  36. const keys = cookies.keys();
  37. const length = uint32(keys.length);
  38. const result = [];
  39. for (let i = 0; i < length;) {
  40. const name = keys[i++];
  41. result[i] = {'name': name, 'value': cookies.get(name)};
  42. }
  43. return result;
  44. };
  45. /**
  46. * Returns the value of a request parameter as a <code>string</code>, or empty
  47. * <code>string</code> if the parameter does not exist.
  48. * @param {string} name A <code>string</code> specifying the name of the
  49. * parameter.
  50. * @param {?Element|?Location|string=} opt_location Optional location object.
  51. * @return {string} Returns a <code>string</code> representing the single
  52. * value of the parameter.
  53. * @method
  54. */
  55. export const getParameter = (name, opt_location) => {
  56. const map = getParameterMap(opt_location);
  57. return map[name] || '';
  58. };
  59. /**
  60. * @param {?Element|?Location|string=} opt_location Optional location object.
  61. * @return {!Array<string>} Returns an <code>Array</code> of
  62. * <code>string</code> objects, each <code>string</code> containing the name
  63. * of a request parameter; or an empty <code>Array</code> if the request has
  64. * no parameters.
  65. * @method
  66. */
  67. export const getParameterNames = (opt_location) => {
  68. const map = getParameterMap(opt_location);
  69. return Object.keys(map);
  70. };
  71. /**
  72. * Returns a map of the parameters of this request including parameters from
  73. * parsed from query string and hash.
  74. * @param {?Element|?Location|?URL|string=} opt_location Optional location.
  75. * @return {!Object<string, string>} Map containing parameter names as keys
  76. * and parameter values as map values.
  77. * @method
  78. */
  79. export const getParameterMap = (opt_location) => {
  80. opt_location = getLocation_(opt_location);
  81. const url = opt_location.toString();
  82. const map = {};
  83. if (!(url in maps_)) {
  84. const pairs = opt_location.search.substr(1).split('&').concat(
  85. opt_location.hash.substr(1).split('&'));
  86. let index = uint32(pairs.length);
  87. while (index--) {
  88. const pair = pairs[index].split('=');
  89. const key = pair[0];
  90. if (key) map[key] = decodeURIComponent(pair[1]);
  91. }
  92. maps_[url] = map;
  93. }
  94. return maps_[url];
  95. };
  96. /**
  97. * @param {?Element|?Location|?URL|string=} opt_location Optional location.
  98. * @return {!URL}
  99. * @method
  100. * @private
  101. */
  102. const getLocation_ = (opt_location) => {
  103. opt_location = opt_location || location;
  104. if ('string' === typeof opt_location) {
  105. opt_location = new URL(opt_location);
  106. }
  107. return /** @type {!URL} */ (opt_location);
  108. };
  109. /**
  110. * Cache for parsed parameters maps.
  111. * @type {!Object<string, !Object<string,string>>}
  112. * @private
  113. */
  114. const maps_ = {};