kaven-utils
Version:
Utils for Node.js.
44 lines (43 loc) • 1.57 kB
JavaScript
/********************************************************************
* @author: Kaven
* @email: kaven@wuwenkai.com
* @website: http://blog.kaven.xyz
* @file: [Kaven-Utils] /src/net/http/HttpResponseMessage.ts
* @create: 2022-04-19 14:23:15.399
* @modify: 2023-11-25 22:32:53.191
* @version: 5.0.7
* @times: 8
* @lines: 57
* @copyright: Copyright © 2022-2023 Kaven. All Rights Reserved.
* @description: [description]
* @license: [license]
********************************************************************/
import { HttpStatusCode, Strings_CR_LF } from "kaven-basic";
import { HttpResponseHeader } from "./HttpResponseHeader.js";
import { HttpResponseStatusLine } from "./HttpResponseStatusLine.js";
export class HttpResponseMessage {
constructor(statusLine) {
this.StatusLine = statusLine ?? new HttpResponseStatusLine(HttpStatusCode.NotFound);
}
StatusLine;
Headers = [];
Body;
GetHeaders() {
const obj = {};
for (const header of this.Headers) {
obj[header.Name] = header.Value;
}
return obj;
}
AddHeader(name, value) {
this.Headers.push(new HttpResponseHeader(name, value));
}
ToBuffer() {
const lines = [this.StatusLine.ToString(), ...this.Headers.map(p => p.ToString()), Strings_CR_LF];
const data = Buffer.from(lines.join(Strings_CR_LF));
if (this.Body === undefined) {
return data;
}
return Buffer.concat([data, this.Body.Data]);
}
}