httpHeaders.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License. See License.txt in the project root for license information.
  3. /**
  4. * A collection of HttpHeaders that can be sent with a HTTP request.
  5. */
  6. function getHeaderKey(headerName: string) {
  7. return headerName.toLowerCase();
  8. }
  9. /**
  10. * An individual header within a HttpHeaders collection.
  11. */
  12. export interface HttpHeader {
  13. /**
  14. * The name of the header.
  15. */
  16. name: string;
  17. /**
  18. * The value of the header.
  19. */
  20. value: string;
  21. }
  22. /**
  23. * A HttpHeaders collection represented as a simple JSON object.
  24. */
  25. export type RawHttpHeaders = { [headerName: string]: string };
  26. /**
  27. * A collection of HTTP header key/value pairs.
  28. */
  29. export class HttpHeaders {
  30. private readonly _headersMap: { [headerKey: string]: HttpHeader };
  31. constructor(rawHeaders?: RawHttpHeaders) {
  32. this._headersMap = {};
  33. if (rawHeaders) {
  34. for (const headerName in rawHeaders) {
  35. this.set(headerName, rawHeaders[headerName]);
  36. }
  37. }
  38. }
  39. /**
  40. * Set a header in this collection with the provided name and value. The name is
  41. * case-insensitive.
  42. * @param headerName The name of the header to set. This value is case-insensitive.
  43. * @param headerValue The value of the header to set.
  44. */
  45. public set(headerName: string, headerValue: string | number): void {
  46. this._headersMap[getHeaderKey(headerName)] = { name: headerName, value: headerValue.toString() };
  47. }
  48. /**
  49. * Get the header value for the provided header name, or undefined if no header exists in this
  50. * collection with the provided name.
  51. * @param headerName The name of the header.
  52. */
  53. public get(headerName: string): string | undefined {
  54. const header: HttpHeader = this._headersMap[getHeaderKey(headerName)];
  55. return !header ? undefined : header.value;
  56. }
  57. /**
  58. * Get whether or not this header collection contains a header entry for the provided header name.
  59. */
  60. public contains(headerName: string): boolean {
  61. return !!this._headersMap[getHeaderKey(headerName)];
  62. }
  63. /**
  64. * Remove the header with the provided headerName. Return whether or not the header existed and
  65. * was removed.
  66. * @param headerName The name of the header to remove.
  67. */
  68. public remove(headerName: string): boolean {
  69. const result: boolean = this.contains(headerName);
  70. delete this._headersMap[getHeaderKey(headerName)];
  71. return result;
  72. }
  73. /**
  74. * Get the headers that are contained this collection as an object.
  75. */
  76. public rawHeaders(): RawHttpHeaders {
  77. const result: RawHttpHeaders = {};
  78. for (const headerKey in this._headersMap) {
  79. const header: HttpHeader = this._headersMap[headerKey];
  80. result[header.name.toLowerCase()] = header.value;
  81. }
  82. return result;
  83. }
  84. /**
  85. * Get the headers that are contained in this collection as an array.
  86. */
  87. public headersArray(): HttpHeader[] {
  88. const headers: HttpHeader[] = [];
  89. for (const headerKey in this._headersMap) {
  90. headers.push(this._headersMap[headerKey]);
  91. }
  92. return headers;
  93. }
  94. /**
  95. * Get the header names that are contained in this collection.
  96. */
  97. public headerNames(): string[] {
  98. const headerNames: string[] = [];
  99. const headers: HttpHeader[] = this.headersArray();
  100. for (let i = 0; i < headers.length; ++i) {
  101. headerNames.push(headers[i].name);
  102. }
  103. return headerNames;
  104. }
  105. /**
  106. * Get the header names that are contained in this collection.
  107. */
  108. public headerValues(): string[] {
  109. const headerValues: string[] = [];
  110. const headers: HttpHeader[] = this.headersArray();
  111. for (let i = 0; i < headers.length; ++i) {
  112. headerValues.push(headers[i].value);
  113. }
  114. return headerValues;
  115. }
  116. /**
  117. * Get the JSON object representation of this HTTP header collection.
  118. */
  119. public toJson(): RawHttpHeaders {
  120. return this.rawHeaders();
  121. }
  122. /**
  123. * Get the string representation of this HTTP header collection.
  124. */
  125. public toString(): string {
  126. return JSON.stringify(this.toJson());
  127. }
  128. /**
  129. * Create a deep clone/copy of this HttpHeaders collection.
  130. */
  131. public clone(): HttpHeaders {
  132. return new HttpHeaders(this.rawHeaders());
  133. }
  134. }