using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text.RegularExpressions;
namespace System.Net.ApiService {
public class Response {
public enum eSerialize {
BlackPro_Binary = 0,
Json,
Xml
}
public enum eConnection {
KeepAlive,
Close
}
public enum eRedirectCode : int {
SeeOther = 303,
MovedPermanently = 301,
Found = 302,
TemporaryRedirect = 307,
PermanentRedirect = 308
}
public enum eErrorCode : int {
BadRequest = 400,
Unauthorized = 401,
PaymentRequired = 402,
Forbidden = 403,
NotFound = 404,
MethodNotAllowed = 405,
NotAcceptable = 406,
ProxyAuthenticationRequired = 407,
RequestTimeout = 408,
Conflict = 409,
Gone = 410,
LengthRequired = 411,
PreconditionFailed = 412,
RequestEntityTooLarge = 413,
URITooLong = 414,
UnsupportedMediaType = 415,
RequestedRangeNotSatisfiable = 416,
ExpectationFailed = 417,
PolicyNotFulfilled = 420,
MisdirectedRequest = 421,
UnprocessableEntity = 422,
Locked = 423,
FailedDependency = 424,
UpgradeRequired = 426,
PreconditionRequired = 428,
TooManyRequests = 429,
RequestHeaderFieldsTooLarge = 431,
UnavailableForLegalReasons = 451,
InternalServerError = 500,
NotImplemented = 501,
BadGateway = 502,
ServiceUnavailable = 503,
GatewayTimeout = 504,
HTTPVersionNotSupported = 505,
VariantAlsoNegotiates = 506,
InsufficientStorage = 507,
LoopDetected = 508,
BandwidthLimitExceeded = 509,
NotExtended = 510,
NetworkAuthenticationRequired = 511
}
protected byte[] _Content;
protected string _contentType;
protected int _code = 100;
protected long _len;
protected string _file;
protected string _location;
protected bool _bIsSet = false;
protected eConnection _connection = eConnection.Close;
protected Response_CacheControl _cacheControl;
public byte[] Content {
get {
return this._Content;
}
}
public eConnection Connection {
get {
return this._connection;
}
set {
this._connection = value;
}
}
public string ContentType {
get {
return this._contentType;
}
}
public int Code {
get {
return this._code;
}
}
public string CodeName {
get {
return this.getCodeDescription(this._code);
}
}
public long Length {
get {
return this._len;
}
}
public string FilePath {
get {
return this._file;
}
}
public string RedirectLocation {
get {
return this._location;
}
}
public bool IsSet {
get {
return this._bIsSet;
}
}
public Response_CacheControl CacheControl {
get {
return this._cacheControl;
}
}
public void Error(eErrorCode code) {
this._code = (int)code;
this._Content = new byte[0];
this._Content = this.getErrorContent(this._code);
this._len = this._Content.Length;
this._file = "";
this._location = "";
this._contentType = "text/html";
}
public void Error(int code) {
this._code = (int)code;
this._Content = new byte[0];
this._Content = this.getErrorContent(this._code);
this._len = this._Content.Length;
this._file = "";
this._location = "";
this._contentType = "text/html";
}
protected byte[] getErrorContent(int code) {
string PageTitle = "Error " + code;
string HtmlTitle = "
An error appeard
";
string HtmlMessage = "An error appeard when calling the site.
";
HtmlMessage += "Code: " + code + " " + this.getCodeDescription(code) + "
";
HtmlMessage += "Time: " + DateTime.Now.ToString("r") + "
";
string header = "
" + PageTitle + "";
string body = "
" + HtmlTitle + "
" + HtmlMessage + "
";
string s = "" + header + "Error Code: " + code + "
";
System.Text.UTF8Encoding enc = new UTF8Encoding();
return enc.GetBytes(s);
}
protected string getCodeDescription(int code) {
switch (code) {
case 100:
return "Continue";
case 101:
return "Switching Protocols";
case 102:
return "Processing";
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 207:
return "Multi-Status";
case 208:
return "Already Reported";
case 226:
return "IM Used";
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 305:
return "Use Proxy";
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 "Request Entity Too Large";
case 414:
return "URI Too Long";
case 415:
return "Unsupported Media Type";
case 416:
return "Requested range not satisfiable";
case 417:
return "Expectation Failed";
case 420:
return "Policy Not Fulfilled";
case 421:
return "Misdirected Request";
case 422:
return "Unprocessable Entity";
case 423:
return "Locked";
case 424:
return "Failed Dependency";
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 450:
return "Unavailable For Legal Reasons";
case 418:
return "I’m a teapot";
case 425:
return "Unordered Collection";
case 444:
return "No Response";
case 449:
return "The request should be retried after doing the appropriate action";
case 499:
return "Client Closed Request";
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 509:
return "Bandwidth Limit Exceeded";
case 510:
return "Not Extended";
case 511:
return "Network Authentication Required";
default:
return "Unknown";
}
}
public void FromFile(string path) {
this._Content = new byte[0];
this._location = "";
if (System.IO.File.Exists(path)) {
this._len = (new System.IO.FileInfo(path)).Length;
this._code = 200;
string ext = path.Substring(path.LastIndexOf("."));
this._contentType = System.Fire.Mime.getMimeType(ext);
this._file = path;
if (this._len < 500000) {
switch (this._contentType) {
case "text/html":
case "text/css":
case "text/stylesheet":
case "text/javascript":
case "text/xml":
case "text/json":
case "application/javascript":
case "application/json":
case "application/xml":
this._file = "";
this._Content = File.ReadAllBytes(path);
break;
}
}
this._bIsSet = true;
} else {
/*this._len = 0;
this._code = 404;
this._contentType = "text/html";*/
this.Error(404);
}
}
public void FromString(string Content) {
this.FromString(200, Content, "text/html");
this._bIsSet = true;
}
public void FromString(int Code, string Content) {
this.FromString(Code, Content, "text/html");
this._bIsSet = true;
}
public void FromString(string Content, string ContentType) {
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
this._code = 200;
this._contentType = ContentType;
this._Content = enc.GetBytes(Content);
this._len = this._Content.Length;
this._file = "";
this._location = "";
this._bIsSet = true;
}
public void FromString(int Code, string Content, string ContentType) {
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
this._code = Code;
this._contentType = ContentType;
this._Content = enc.GetBytes(Content);
this._len = this._Content.Length;
this._file = "";
this._location = "";
this._bIsSet = true;
}
public void FromBytes(byte[] Content) {
this.FromBytes(200, Content, "text/html");
}
public void FromBytes(byte[] Content, string ContentType) {
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
this._code = 200;
this._contentType = ContentType;
this._Content = Content;
this._len = this._Content.Length;
this._file = "";
this._location = "";
this._bIsSet = true;
}
public void FromBytes(int Code, byte[] Content, string ContentType) {
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
this._code = Code;
this._contentType = ContentType;
this._Content = Content;
this._len = this._Content.Length;
this._file = "";
this._location = "";
this._bIsSet = true;
}
public void FromObject(object obj) {
FromObject(200, obj, eSerialize.Json);
}
public void FromObject(int Code, object obj) {
FromObject(Code, obj, eSerialize.Json);
}
public void FromObject(object obj, eSerialize Method) {
FromObject(200, obj, Method);
}
public void FromObject(int Code, object obj, eSerialize Method) {
this._file = "";
this._location = "";
this._Content = new byte[0];
if (obj is null) {
this.Error(404);
return;
}
this._code = Code;
if (Method == eSerialize.BlackPro_Binary) {
MemoryStream mem = new MemoryStream();
System.Fire.Serialize.Serializer.SerializeObject(mem, obj);
string r = Convert.ToBase64String(mem.ToArray());
mem.Close();
this._Content = Encoding.ASCII.GetBytes(r);
this._contentType = "application/binary-object";
} else if (Method == eSerialize.Json) {
System.Text.Json.JsonSerializerOptions opt = new System.Text.Json.JsonSerializerOptions();
opt.IgnoreNullValues = false;
this._contentType = "application/json";
//this._Content = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(obj, opt);
string str = System.Text.Json.JsonSerializer.Serialize(obj, obj.GetType(), opt);
System.Diagnostics.Debug.WriteLine("str: " + str);
this._Content = System.Text.Encoding.UTF8.GetBytes( str);
} else if (Method == eSerialize.Xml) {
System.Xml.Serialization.XmlSerializer xmlser = new System.Xml.Serialization.XmlSerializer(obj.GetType());
MemoryStream mem = new MemoryStream();
xmlser.Serialize(mem, obj);
this._contentType = "text/xml";
this._Content = mem.ToArray();
}
this._len = this._Content.Length;
this._bIsSet = true;
}
public void Redirect(string Location) {
this.Redirect(eRedirectCode.SeeOther, Location);
this._bIsSet = true;
}
public void Redirect(eRedirectCode Code, string Location) {
this._location = Location;
this._code = (int)Code;
this._file = "";
this._Content = new byte[0];
this._bIsSet = true;
}
}
}