Skip to content

Instantly share code, notes, and snippets.

@Flygenring
Created January 27, 2016 13:31
Show Gist options
  • Save Flygenring/c3043fb9d9cece6ceb39 to your computer and use it in GitHub Desktop.
Save Flygenring/c3043fb9d9cece6ceb39 to your computer and use it in GitHub Desktop.
/**
* Reference and translate HTTP Status Codes
* Modified from: https://github.com/prettymuchbryce/node-http-status/blob/master/index.js
* Implemented according to:
* - http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
* - https://httpstatuses.com/
* - https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
* - http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpStatus.html
*
* @returns {Object} - HTTP Status Code constants and a method to translate status codes to a description
*/
function HttpStatusHelper() {
var rfcs, httpStatuses = {},
codes = {
getReason: function(statusCode) {
var code = guardInvalidCode(statusCode);
return httpStatuses[code].reason;
},
getFull: function(statusCode) {
var response, code = guardInvalidCode(statusCode);
response = {
reason: httpStatuses[code].reason,
protocol: rfcs[httpStatuses[code].rfc].protocol,
rfc: rfcs[httpStatuses[code].rfc]
};
response.rfc.name = "RFC " + httpStatuses[code].rfc;
delete response.rfc.protocol;
return response;
}
};
function guardInvalidCode(statusCode) {
var code = parseInt(statusCode, 10);
if(code != statusCode) { //eslint-disable-line eqeqeq
throw new Error("Status code \"" + code + "\" is invalid.");
}
if(code === 306) { //eslint-disable-line no-magic-numbers
throw new Error("Status code \"" + code + "\" is explicitly reserved but not used.");
}
if(!httpStatuses.hasOwnProperty(code)) {
throw new Error("Status code \"" + code + "\" is unassigned.");
}
return code;
}
/**
* Add translations from code to status
*/
/* 1xx: Informational - Request received, continuing process */
(function() {
httpStatuses[codes.CONTINUE = 100] = {
reason: "Continue",
rfc: 7231
};
httpStatuses[codes.SWITCHING_PROTOCOLS = 101] = {
reason: "Switching Protocols",
rfc: 7231
};
httpStatuses[codes.PROCESSING = 102] = {
reason: "Processing",
rfc: 2518
};
httpStatuses[codes.CHECKPOINT = 103] = {
reason: "Checkpoint",
rfc: "Proposal for supporting resumable POST/PUT HTTP requests in HTTP/1.0"
};
})();
/* 2xx: Success - The action was successfully received, understood, and accepted */
(function() {
httpStatuses[codes.OK = 200] = {
reason: "OK",
rfc: 1945
};
httpStatuses[codes.CREATED = 201] = {
reason: "Created",
rfc: 1945
};
httpStatuses[codes.ACCEPTED = 202] = {
reason: "Accepted",
rfc: 1945
};
httpStatuses[codes.NON_AUTHORITATIVE_INFORMATION = 203] = {
reason: "Non-Authoritative Information",
rfc: 7231
};
httpStatuses[codes.NO_CONTENT = 204] = {
reason: "No Content",
rfc: 1945
};
httpStatuses[codes.RESET_CONTENT = 205] = {
reason: "Reset Content",
rfc: 7231
};
httpStatuses[codes.PARTIAL_CONTENT = 206] = {
reason: "Partial Content",
rfc: 7233
};
httpStatuses[codes.MULTI_STATUS = 207] = {
reason: "Multi-Status",
rfc: 4918
};
httpStatuses[codes.PARTIAL_UPDATE_OK = 207] = {
reason: "Partial Update OK",
rfc: "draft-ietf-http-v11-spec-rev-01"
};
httpStatuses[codes.ALREADY_REPORTED = 208] = {
reason: "Already Reported",
rfc: 5842
};
// 209-225 Unassigned
httpStatuses[codes.IM_USED = 226] = {
reason: "Instance-Manipulation Used",
rfc: 3229
};
// 227-299 Unassigned
})();
/* 3xx: Redirection - Further action must be taken in order to complete the request */
(function() {
httpStatuses[codes.MULTIPLE_CHOICES = 300] = {
reason: "Multiple Choices",
rfc: 7231
};
httpStatuses[codes.MOVED_PERMANENTLY = 301] = {
reason: "Moved Permanently",
rfc: 1945
};
httpStatuses[codes.FOUND = 302] = {
reason: "Found (Moved Temporarily)",
rfc: 1945
};
httpStatuses[codes.SEE_OTHER = 303] = {
reason: "See Other",
rfc: 7232
};
httpStatuses[codes.NOT_MODIFIED = 302] = {
reason: "Not Modified",
rfc: 1945
};
httpStatuses[codes.USE_PROXY = 305] = {
reason: "Use Proxy",
rfc: 7231
};
// 306 (Unused)
httpStatuses[codes.TEMPORARY_REDIRECT = 307] = {
reason: "Temporary Redirect",
rfc: 7231
};
httpStatuses[codes.PERMANENT_REDIRECT = 308] = {
reason: "Permanent Redirect",
rfc: 7538
};
// 309-399 Unassigned
})();
/* 4xx: Client Error - The request contains bad syntax or cannot be fulfilled */
(function() {
httpStatuses[codes.BAD_REQUEST = 400] = {
reason: "Bad Request",
rfc: 7231
};
httpStatuses[codes.UNAUTHORIZED = 401] = {
reason: "Unauthorized",
rfc: 7235
};
httpStatuses[codes.PAYMENT_REQUIRED = 402] = {
reason: "Payment Required",
rfc: 7231
};
httpStatuses[codes.FORBIDDEN = 403] = {
reason: "Forbidden",
rfc: 1945
};
httpStatuses[codes.NOT_FOUND = 404] = {
reason: "Not Found",
rfc: 1945
};
httpStatuses[codes.METHOD_NOT_ALLOWED = 405] = {
reason: "Method Not Allowed",
rfc: 7231
};
httpStatuses[codes.NOT_ACCEPTABLE = 406] = {
reason: "Not Acceptable",
rfc: 7231
};
httpStatuses[codes.PROXY_AUTHENTICATION_REQUIRED = 407] = {
reason: "Proxy Authentication Required",
rfc: 7235
};
httpStatuses[codes.REQUEST_TIMEOUT = 408] = {
reason: "Request Timeout",
rfc: 7231
};
httpStatuses[codes.CONFLICT = 409] = {
reason: "Conflict",
rfc: 7231
};
httpStatuses[codes.GONE = 410] = {
reason: "Gone",
rfc: 7231
};
httpStatuses[codes.LENGTH_REQUIRED = 411] = {
reason: "Length Required",
rfc: 7231
};
httpStatuses[codes.PRECONDITION_FAILED = 412] = {
reason: "Precondition Failed",
rfc: 7232
};
httpStatuses[codes.PAYLOAD_TOO_LARGE = 413] = {
reason: "Payload Too Large",
rfc: 7231
};
httpStatuses[codes.URI_TOO_LONG = 414] = {
reason: "URI Too Long",
rfc: 7231
};
httpStatuses[codes.UNSUPPORTED_MEDIA_TYPE = 415] = {
reason: "Unsupported Media Type",
rfc: 7231
};
httpStatuses[codes.RANGE_NOT_SATISFIABLE = 416] = {
reason: "Range Not Satisfiable",
rfc: 7233
};
httpStatuses[codes.EXPECTATION_FAILED = 417] = {
reason: "Expectation Failed",
rfc: 7231
};
httpStatuses[codes.I_AM_A_TEAPOT = 418] = {
reason: "I'm A Teapot",
rfc: 2324
};
// 419-420 Unassigned
httpStatuses[codes.MISDIRECTED_REQUEST = 421] = {
reason: "Misdirected Request",
rfc: 7540
};
httpStatuses[codes.UNPROCESSABLE_ENTITY = 422] = {
reason: "Unprocessable Entity",
rfc: 4918
};
httpStatuses[codes.LOCKED = 423] = {
reason: "Locked",
rfc: 4918
};
httpStatuses[codes.FAILED_DEPENDENCY = 424] = {
reason: "Failed Dependency",
rfc: 4918
};
// 425 Unassigned
httpStatuses[codes.UPGRADE_REQUIRED = 426] = {
reason: "Upgrade Required",
rfc: 7231
};
// 427 Unassigned
httpStatuses[codes.PRECONDITION_REQUIRED = 428] = {
reason: "Precondition Required",
rfc: 6585
};
httpStatuses[codes.TOO_MANY_REQUESTS = 429] = {
reason: "Too Many Requests",
rfc: 6585
};
// 430 Unassigned
httpStatuses[codes.REQUEST_HEADER_FIELDS_TOO_LARGE = 431] = {
reason: "Request Header Fields Too Large",
rfc: 6585
};
// 432-450 Unassigned
httpStatuses[codes.UNAVAILABLE_FOR_LEGAL_REASONS = 451] = {
reason: "Unavailable for Legal Reasons",
rfc: "RFC-ietf-httpbis-legally-restricted-status-04"
};
// 452-499 Unassigned
})();
/* 5xx: Server Error - The server failed to fulfill an apparently valid request */
(function() {
httpStatuses[codes.INTERNAL_SERVER_ERROR = 500] = {
reason: "Internal Server Error",
rfc: 1945
};
httpStatuses[codes.NOT_IMPLEMENTED = 501] = {
reason: "Not Implemented",
rfc: 1945
};
httpStatuses[codes.BAD_GATEWAY = 502] = {
reason: "Bad Gateway",
rfc: 1945
};
httpStatuses[codes.SERVICE_UNAVAILABLE = 503] = {
reason: "Service Unavailable",
rfc: 1945
};
httpStatuses[codes.GATEWAY_TIMEOUT = 504] = {
reason: "Gateway Timeout",
rfc: 7231
};
httpStatuses[codes.HTTP_VERSION_NOT_SUPPORTED = 505] = {
reason: "HTTP Version Not Supported",
rfc: 7231
};
httpStatuses[codes.VARIANT_ALSO_NEGOTIATES = 506] = {
reason: "Variant Also Negotiates",
rfc: 2295
};
httpStatuses[codes.INSUFFICIENT_STORAGE = 507] = {
reason: "Insufficient Storage",
rfc: 4918
};
httpStatuses[codes.LOOP_DETECTED = 508] = {
reason: "Loop Detected",
rfc: 5842
};
httpStatuses[codes.BANDWIDTH_LIMIT_EXCEEDED = 509] = {
reason: "Bandwidth Limit Exceeded",
rfc: "Apache Web Server/cPanel"
};
httpStatuses[codes.NOT_EXTENDED = 510] = {
reason: "Not Extended",
rfc: 2774
};
httpStatuses[codes.NETWORK_AUTHENTICATION_REQUIRED = 511] = {
reason: "Network Authentication Required",
rfc: 6585
};
httpStatuses[codes.NETWORK_CONNECT_TIMEOUT_ERROR = 599] = {
reason: "Network Connect Timeout Error",
rfc: "None-599"
};
})();
/* RFC: The relevant standards defining the status codes */
rfcs = {
"1945": {
title: "",
protocol: "HTTP/1.0",
description: ""
},
"Proposal for supporting resumable POST/PUT HTTP requests in HTTP/1.0": {
title: "",
protocol: "HTTP/1.0",
description: ""
},
"2295": {
title: "",
protocol: "HTTP/1.1",
description: ""
},
"2774": {
title: "",
protocol: "HTTP/1.1",
description: ""
},
"3229": {
title: "",
protocol: "HTTP/1.1",
description: ""
},
"6585": {
title: "",
protocol: "HTTP/1.1",
description: ""
},
"7231": {
title: "",
protocol: "HTTP/1.1",
description: ""
},
"7232": {
title: "",
protocol: "HTTP/1.1",
description: ""
},
"7233": {
title: "",
protocol: "HTTP/1.1",
description: ""
},
"7235": {
title: "",
protocol: "HTTP/1.1",
description: ""
},
"7538": {
title: "",
protocol: "HTTP/1.1",
description: ""
},
"draft-ietf-http-v11-spec-rev-01": {
title: "",
protocol: "HTTP/1.1",
description: ""
},
"RFC-ietf-httpbis-legally-restricted-status-04": {
title: "",
protocol: "HTTP/1.1",
description: ""
},
"7540": {
title: "",
protocol: "HTTP/2",
description: ""
},
"2518": {
title: "",
protocol: "WebDAV",
description: ""
},
"4918": {
title: "",
protocol: "WebDAV",
description: ""
},
"5842": {
title: "",
protocol: "WebDAV",
description: ""
},
"Apache Web Server/cPanel": {
title: "",
protocol: "",
description: ""
},
"None-599": {
title: "",
protocol: "Proxy",
description: "https://github.com/citricsquid/httpstatuses/issues/22"
},
"2324": {
title: "",
protocol: "HTCPCP/1.0",
description: ""
}
};
return codes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment