git.schokokeks.org
Repositories
Help
Report an Issue
fs-words.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
99941cf
Branches
Tags
develop-client_server
master
typescript
fs-words.git
server
lib
plankton
http
logic-impl.js
[upd] server:lib:plankton
Christian Fraß
commited
99941cf
at 2021-03-12 00:46:54
logic-impl.js
Blame
History
Raw
/* This file is part of »bacterio-plankton:http«. Copyright 2016-2021 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:http« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:http« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:http«. If not, see <http://www.gnu.org/licenses/>. */ var lib_http; (function (lib_http) { /** * @author fenris <frass@greenscale.de> */ let enum_method; (function (enum_method) { enum_method["get"] = "get"; enum_method["post"] = "post"; enum_method["options"] = "options"; // put = "put", // delete = "delete", // head = "head", })(enum_method = lib_http.enum_method || (lib_http.enum_method = {})); })(lib_http || (lib_http = {})); /* This file is part of »bacterio-plankton:http«. Copyright 2016-2021 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' <info@greenscale.de> »bacterio-plankton:http« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »bacterio-plankton:http« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »bacterio-plankton:http«. If not, see <http://www.gnu.org/licenses/>. */ var lib_http; (function (lib_http) { /** * @author fenris <frass@greenscale.de> */ const linebreak = "\r\n"; /** * @author fenris <frass@greenscale.de> */ function encode_method(method) { switch (method) { case lib_http.enum_method.get: return "GET"; case lib_http.enum_method.post: return "POST"; case lib_http.enum_method.options: return "OPTIONS"; default: throw (new Error("impossible")); } } /** * @author fenris <frass@greenscale.de> */ function decode_method(method_raw) { switch (method_raw) { case "GET": return lib_http.enum_method.get; case "POST": return lib_http.enum_method.post; case "OPTIONS": return lib_http.enum_method.options; default: throw (new Error("unhandled method: " + method_raw)); } } /** * @author fenris <frass@greenscale.de> */ function get_statustext(statuscode) { switch (statuscode) { case 100: return "Continue"; case 101: return "Switching Protocols"; case 103: return "Early Hints"; case 200: return "OK"; case 201: return "Created"; case 202: return "Accepted"; case 203: return "Non-Authoritative Information"; case 204: return "No Content"; case 205: return "Reset Content"; case 206: return "Partial Content"; case 300: return "Multiple Choices"; case 301: return "Moved Permanently"; case 302: return "Found"; case 303: return "See Other"; case 304: return "Not Modified"; case 307: return "Temporary Redirect"; case 308: return "Permanent Redirect"; case 400: return "Bad Request"; case 401: return "Unauthorized"; case 402: return "Payment Required"; case 403: return "Forbidden"; case 404: return "Not Found"; case 405: return "Method Not Allowed"; case 406: return "Not Acceptable"; case 407: return "Proxy Authentication Required"; case 408: return "Request Timeout"; case 409: return "Conflict"; case 410: return "Gone"; case 411: return "Length Required"; case 412: return "Precondition Failed"; case 413: return "Payload Too Large"; case 414: return "URI Too Long"; case 415: return "Unsupported Media Type"; case 416: return "Range Not Satisfiable"; case 417: return "Expectation Failed"; case 418: return "I'm a teapot"; case 422: return "Unprocessable Entity"; case 425: return "Too Early"; case 426: return "Upgrade Required"; case 428: return "Precondition Required"; case 429: return "Too Many Requests"; case 431: return "Request Header Fields Too Large"; case 451: return "Unavailable For Legal Reasons"; case 500: return "Internal Server Error"; case 501: return "Not Implemented"; case 502: return "Bad Gateway"; case 503: return "Service Unavailable"; case 504: return "Gateway Timeout"; case 505: return "HTTP Version Not Supported"; case 506: return "Variant Also Negotiates"; case 507: return "Insufficient Storage"; case 508: return "Loop Detected"; case 510: return "Not Extended"; case 511: return "Network Authentication"; default: throw (new Error("unhandled statuscode: " + statuscode.toFixed(0))); } } /** * @author fenris <frass@greenscale.de> */ function encode_request(request) { let request_raw = ""; request_raw += (encode_method(request.method) + " " + request.query + " " + "HTTP/1.1" + linebreak); request_raw += ("Host: " + request.host + linebreak); for (const [key, value] of Object.entries(request.headers)) { request_raw += (key + ": " + value + linebreak); } request_raw += linebreak; request_raw += request.body; return request_raw; } lib_http.encode_request = encode_request; /** * @author fenris <frass@greenscale.de> */ function decode_request(request_raw) { const lines = request_raw.split(linebreak); const first = lines.shift(); const [method_raw, query, version] = first.split(" "); let headers = {}; while (true) { const line = lines.shift(); if (line === "") { break; } else { const [key, value] = line.split(": ", 2); headers[key] = value; } } const body = lines.join(linebreak); const request = { "host": headers["Host"], "query": query, "method": decode_method(method_raw), "headers": headers, "body": body, }; return request; } lib_http.decode_request = decode_request; /** * @author fenris <frass@greenscale.de> */ function encode_response(response) { let response_raw = ""; response_raw += ("HTTP/1.1" + " " + response.statuscode + " " + get_statustext(response.statuscode) + linebreak); for (const [key, value] of Object.entries(response.headers)) { response_raw += (key + ": " + value + linebreak); } response_raw += linebreak; response_raw += response.body; return response_raw; } lib_http.encode_response = encode_response; /** * @author fenris <frass@greenscale.de> */ function decode_response(response_raw) { const lines = response_raw.split(linebreak); const first = lines.shift(); const statuscode = parseInt(first.split(" ")[1]); let headers = {}; while (true) { const line = lines.shift(); if (line === "") { break; } else { const [key, value] = line.split(": ", 2); headers[key] = value; } } const body = lines.join(linebreak); const response = { "statuscode": statuscode, "headers": headers, "body": body, }; return response; } lib_http.decode_response = decode_response; })(lib_http || (lib_http = {}));