Response.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Reflection;
  10. using System.Linq;
  11. using System.Text.RegularExpressions;
  12. namespace System.Net.ApiService {
  13. public class Response {
  14. public enum eSerialize {
  15. BlackPro_Binary = 0,
  16. Json,
  17. Xml
  18. }
  19. public enum eConnection {
  20. KeepAlive,
  21. Close
  22. }
  23. public enum eRedirectCode : int {
  24. SeeOther = 303,
  25. MovedPermanently = 301,
  26. Found = 302,
  27. TemporaryRedirect = 307,
  28. PermanentRedirect = 308
  29. }
  30. public enum eErrorCode : int {
  31. BadRequest = 400,
  32. Unauthorized = 401,
  33. PaymentRequired = 402,
  34. Forbidden = 403,
  35. NotFound = 404,
  36. MethodNotAllowed = 405,
  37. NotAcceptable = 406,
  38. ProxyAuthenticationRequired = 407,
  39. RequestTimeout = 408,
  40. Conflict = 409,
  41. Gone = 410,
  42. LengthRequired = 411,
  43. PreconditionFailed = 412,
  44. RequestEntityTooLarge = 413,
  45. URITooLong = 414,
  46. UnsupportedMediaType = 415,
  47. RequestedRangeNotSatisfiable = 416,
  48. ExpectationFailed = 417,
  49. PolicyNotFulfilled = 420,
  50. MisdirectedRequest = 421,
  51. UnprocessableEntity = 422,
  52. Locked = 423,
  53. FailedDependency = 424,
  54. UpgradeRequired = 426,
  55. PreconditionRequired = 428,
  56. TooManyRequests = 429,
  57. RequestHeaderFieldsTooLarge = 431,
  58. UnavailableForLegalReasons = 451,
  59. InternalServerError = 500,
  60. NotImplemented = 501,
  61. BadGateway = 502,
  62. ServiceUnavailable = 503,
  63. GatewayTimeout = 504,
  64. HTTPVersionNotSupported = 505,
  65. VariantAlsoNegotiates = 506,
  66. InsufficientStorage = 507,
  67. LoopDetected = 508,
  68. BandwidthLimitExceeded = 509,
  69. NotExtended = 510,
  70. NetworkAuthenticationRequired = 511
  71. }
  72. protected byte[] _Content;
  73. protected string _contentType;
  74. protected int _code = 100;
  75. protected long _len;
  76. protected string _file;
  77. protected string _location;
  78. protected bool _bIsSet = false;
  79. protected eConnection _connection = eConnection.Close;
  80. protected Response_CacheControl _cacheControl;
  81. public byte[] Content {
  82. get {
  83. return this._Content;
  84. }
  85. }
  86. public eConnection Connection {
  87. get {
  88. return this._connection;
  89. }
  90. set {
  91. this._connection = value;
  92. }
  93. }
  94. public string ContentType {
  95. get {
  96. return this._contentType;
  97. }
  98. }
  99. public int Code {
  100. get {
  101. return this._code;
  102. }
  103. }
  104. public string CodeName {
  105. get {
  106. return this.getCodeDescription(this._code);
  107. }
  108. }
  109. public long Length {
  110. get {
  111. return this._len;
  112. }
  113. }
  114. public string FilePath {
  115. get {
  116. return this._file;
  117. }
  118. }
  119. public string RedirectLocation {
  120. get {
  121. return this._location;
  122. }
  123. }
  124. public bool IsSet {
  125. get {
  126. return this._bIsSet;
  127. }
  128. }
  129. public Response_CacheControl CacheControl {
  130. get {
  131. return this._cacheControl;
  132. }
  133. }
  134. public void Error(eErrorCode code) {
  135. this._code = (int)code;
  136. this._Content = new byte[0];
  137. this._Content = this.getErrorContent(this._code);
  138. this._len = this._Content.Length;
  139. this._file = "";
  140. this._location = "";
  141. this._contentType = "text/html";
  142. }
  143. public void Error(int code) {
  144. this._code = (int)code;
  145. this._Content = new byte[0];
  146. this._Content = this.getErrorContent(this._code);
  147. this._len = this._Content.Length;
  148. this._file = "";
  149. this._location = "";
  150. this._contentType = "text/html";
  151. }
  152. protected byte[] getErrorContent(int code) {
  153. string PageTitle = "Error " + code;
  154. string HtmlTitle = "<div style='display:block;margin:auto;width:800px;border:1px solid #333;padding:30px;background-color:#eee;'><h1>An error appeard</h1><hr>";
  155. string HtmlMessage = "An error appeard when calling the site.<br>";
  156. HtmlMessage += "Code: " + code + " " + this.getCodeDescription(code) + "<br>";
  157. HtmlMessage += "Time: " + DateTime.Now.ToString("r") + "<br>";
  158. string header = "<title>" + PageTitle + "</title>";
  159. string body = "<div style='display:block;margin:auto;width:800px;border:1px solid #333;padding:30px;background-color:#eee;'><h1>" + HtmlTitle + "</h1><hr><div style='color:#999'>" + HtmlMessage + "</div></div>";
  160. string s = "<html><head>" + header + "</head><body>Error Code: " + code + "<br></body></head>";
  161. System.Text.UTF8Encoding enc = new UTF8Encoding();
  162. return enc.GetBytes(s);
  163. }
  164. protected string getCodeDescription(int code) {
  165. switch (code) {
  166. case 100:
  167. return "Continue";
  168. case 101:
  169. return "Switching Protocols";
  170. case 102:
  171. return "Processing";
  172. case 200:
  173. return "OK";
  174. case 201:
  175. return "Created";
  176. case 202:
  177. return "Accepted";
  178. case 203:
  179. return "Non-Authoritative Information";
  180. case 204:
  181. return "No Content";
  182. case 205:
  183. return "Reset Content";
  184. case 206:
  185. return "Partial Content";
  186. case 207:
  187. return "Multi-Status";
  188. case 208:
  189. return "Already Reported";
  190. case 226:
  191. return "IM Used";
  192. case 300:
  193. return "Multiple Choices";
  194. case 301:
  195. return "Moved Permanently";
  196. case 302:
  197. return "Found";
  198. case 303:
  199. return "See Other";
  200. case 304:
  201. return "Not Modified";
  202. case 305:
  203. return "Use Proxy";
  204. case 307:
  205. return "Temporary Redirect";
  206. case 308:
  207. return "Permanent Redirect";
  208. case 400:
  209. return "Bad Request";
  210. case 401:
  211. return "Unauthorized";
  212. case 402:
  213. return "Payment Required";
  214. case 403:
  215. return "Forbidden";
  216. case 404:
  217. return "Not Found";
  218. case 405:
  219. return "Method Not Allowed";
  220. case 406:
  221. return "Not Acceptable";
  222. case 407:
  223. return "Proxy Authentication Required";
  224. case 408:
  225. return "Request Timeout";
  226. case 409:
  227. return "Conflict";
  228. case 410:
  229. return "Gone";
  230. case 411:
  231. return "Length Required";
  232. case 412:
  233. return "Precondition Failed";
  234. case 413:
  235. return "Request Entity Too Large";
  236. case 414:
  237. return "URI Too Long";
  238. case 415:
  239. return "Unsupported Media Type";
  240. case 416:
  241. return "Requested range not satisfiable";
  242. case 417:
  243. return "Expectation Failed";
  244. case 420:
  245. return "Policy Not Fulfilled";
  246. case 421:
  247. return "Misdirected Request";
  248. case 422:
  249. return "Unprocessable Entity";
  250. case 423:
  251. return "Locked";
  252. case 424:
  253. return "Failed Dependency";
  254. case 426:
  255. return "Upgrade Required";
  256. case 428:
  257. return "Precondition Required";
  258. case 429:
  259. return "Too Many Requests";
  260. case 431:
  261. return "Request Header Fields Too Large";
  262. case 450:
  263. return "Unavailable For Legal Reasons";
  264. case 418:
  265. return "I’m a teapot";
  266. case 425:
  267. return "Unordered Collection";
  268. case 444:
  269. return "No Response";
  270. case 449:
  271. return "The request should be retried after doing the appropriate action";
  272. case 499:
  273. return "Client Closed Request";
  274. case 500:
  275. return "Internal Server Error";
  276. case 501:
  277. return "Not Implemented";
  278. case 502:
  279. return "Bad Gateway";
  280. case 503:
  281. return "Service Unavailable";
  282. case 504:
  283. return "Gateway Timeout";
  284. case 505:
  285. return "HTTP Version not supported";
  286. case 506:
  287. return "Variant Also Negotiates";
  288. case 507:
  289. return "Insufficient Storage";
  290. case 508:
  291. return "Loop Detected";
  292. case 509:
  293. return "Bandwidth Limit Exceeded";
  294. case 510:
  295. return "Not Extended";
  296. case 511:
  297. return "Network Authentication Required";
  298. default:
  299. return "Unknown";
  300. }
  301. }
  302. public void FromFile(string path) {
  303. this._Content = new byte[0];
  304. this._location = "";
  305. if (System.IO.File.Exists(path)) {
  306. this._len = (new System.IO.FileInfo(path)).Length;
  307. this._code = 200;
  308. string ext = path.Substring(path.LastIndexOf("."));
  309. this._contentType = System.Fire.Mime.getMimeType(ext);
  310. this._file = path;
  311. if (this._len < 500000) {
  312. switch (this._contentType) {
  313. case "text/html":
  314. case "text/css":
  315. case "text/stylesheet":
  316. case "text/javascript":
  317. case "text/xml":
  318. case "text/json":
  319. case "application/javascript":
  320. case "application/json":
  321. case "application/xml":
  322. this._file = "";
  323. this._Content = File.ReadAllBytes(path);
  324. break;
  325. }
  326. }
  327. this._bIsSet = true;
  328. } else {
  329. /*this._len = 0;
  330. this._code = 404;
  331. this._contentType = "text/html";*/
  332. this.Error(404);
  333. }
  334. }
  335. public void FromString(string Content) {
  336. this.FromString(200, Content, "text/html");
  337. this._bIsSet = true;
  338. }
  339. public void FromString(int Code, string Content) {
  340. this.FromString(Code, Content, "text/html");
  341. this._bIsSet = true;
  342. }
  343. public void FromString(string Content, string ContentType) {
  344. System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  345. this._code = 200;
  346. this._contentType = ContentType;
  347. this._Content = enc.GetBytes(Content);
  348. this._len = this._Content.Length;
  349. this._file = "";
  350. this._location = "";
  351. this._bIsSet = true;
  352. }
  353. public void FromString(int Code, string Content, string ContentType) {
  354. System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  355. this._code = Code;
  356. this._contentType = ContentType;
  357. this._Content = enc.GetBytes(Content);
  358. this._len = this._Content.Length;
  359. this._file = "";
  360. this._location = "";
  361. this._bIsSet = true;
  362. }
  363. public void FromBytes(byte[] Content) {
  364. this.FromBytes(200, Content, "text/html");
  365. }
  366. public void FromBytes(byte[] Content, string ContentType) {
  367. System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  368. this._code = 200;
  369. this._contentType = ContentType;
  370. this._Content = Content;
  371. this._len = this._Content.Length;
  372. this._file = "";
  373. this._location = "";
  374. this._bIsSet = true;
  375. }
  376. public void FromBytes(int Code, byte[] Content, string ContentType) {
  377. System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  378. this._code = Code;
  379. this._contentType = ContentType;
  380. this._Content = Content;
  381. this._len = this._Content.Length;
  382. this._file = "";
  383. this._location = "";
  384. this._bIsSet = true;
  385. }
  386. public void FromObject(object obj) {
  387. FromObject(200, obj, eSerialize.Json);
  388. }
  389. public void FromObject(int Code, object obj) {
  390. FromObject(Code, obj, eSerialize.Json);
  391. }
  392. public void FromObject(object obj, eSerialize Method) {
  393. FromObject(200, obj, Method);
  394. }
  395. public void FromObject(int Code, object obj, eSerialize Method) {
  396. this._file = "";
  397. this._location = "";
  398. this._Content = new byte[0];
  399. if (obj is null) {
  400. this.Error(404);
  401. return;
  402. }
  403. this._code = Code;
  404. if (Method == eSerialize.BlackPro_Binary) {
  405. MemoryStream mem = new MemoryStream();
  406. System.Fire.Serialize.Serializer.SerializeObject(mem, obj);
  407. string r = Convert.ToBase64String(mem.ToArray());
  408. mem.Close();
  409. this._Content = Encoding.ASCII.GetBytes(r);
  410. this._contentType = "application/binary-object";
  411. } else if (Method == eSerialize.Json) {
  412. System.Text.Json.JsonSerializerOptions opt = new System.Text.Json.JsonSerializerOptions();
  413. opt.IgnoreNullValues = false;
  414. this._contentType = "application/json";
  415. //this._Content = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(obj, opt);
  416. string str = System.Text.Json.JsonSerializer.Serialize(obj, obj.GetType(), opt);
  417. System.Diagnostics.Debug.WriteLine("str: " + str);
  418. this._Content = System.Text.Encoding.UTF8.GetBytes( str);
  419. } else if (Method == eSerialize.Xml) {
  420. System.Xml.Serialization.XmlSerializer xmlser = new System.Xml.Serialization.XmlSerializer(obj.GetType());
  421. MemoryStream mem = new MemoryStream();
  422. xmlser.Serialize(mem, obj);
  423. this._contentType = "text/xml";
  424. this._Content = mem.ToArray();
  425. }
  426. this._len = this._Content.Length;
  427. this._bIsSet = true;
  428. }
  429. public void Redirect(string Location) {
  430. this.Redirect(eRedirectCode.SeeOther, Location);
  431. this._bIsSet = true;
  432. }
  433. public void Redirect(eRedirectCode Code, string Location) {
  434. this._location = Location;
  435. this._code = (int)Code;
  436. this._file = "";
  437. this._Content = new byte[0];
  438. this._bIsSet = true;
  439. }
  440. }
  441. }