Advertisement
Guest User

resonse

a guest
Dec 17th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using BIF.SWE1.Interfaces;
  7.  
  8. namespace MyWebServer
  9. {
  10. class Response : IResponse
  11. {
  12. private int _statusCode;
  13. private IDictionary<string, string> _headers;
  14. private byte[] _byteContent;
  15.  
  16.  
  17. public Response()
  18. {
  19. if (_headers == null) _headers = new Dictionary<string, string>();
  20. _headers.Add("Server", "BIF-SWE1-Server");
  21. }
  22.  
  23.  
  24. public IDictionary<string, string> Headers => _headers;
  25.  
  26. public int ContentLength
  27. {
  28. get
  29. {
  30. string val;
  31. if (_headers.TryGetValue("Content-Length", out val)) return int.Parse(val);
  32. else return 0;
  33. }
  34. }
  35.  
  36. public string ContentType
  37. {
  38. get
  39. {
  40. string val;
  41. if (_headers.TryGetValue("Content-Type", out val)) return val;
  42. else return null;
  43. }
  44. set
  45. {
  46. _headers["Content-Type"] = value;
  47. }
  48. }
  49. public int StatusCode
  50. {
  51. get
  52. {
  53. if (_statusCode == 0)
  54. throw new InvalidOperationException("no status code set yet!!!!!!!");
  55. return this._statusCode;
  56. }
  57. set
  58. {
  59. this._statusCode = value;
  60. }
  61. }
  62.  
  63. public string Status
  64. {
  65. get
  66. {
  67. if (_statusCode == 200) return "200 OK";
  68. if (_statusCode == 404) return "404 Not Found";
  69. if (_statusCode == 500) return "500 INTERNAL SERVER ERROR";
  70. else return "";
  71. }
  72. }
  73.  
  74. public string ServerHeader
  75. {
  76. get
  77. {
  78. return _headers["Server"];
  79. }
  80. set
  81. {
  82. _headers["Server"] = value.ToString();
  83. }
  84. }
  85.  
  86. public void AddHeader(string header, string value)
  87. {
  88. if (_headers == null)
  89. _headers = new Dictionary<string, string>();
  90.  
  91. if (_headers.ContainsKey(header))
  92. {
  93. _headers[header] = value;
  94. }
  95. else
  96. _headers.Add(header, value);
  97. }
  98.  
  99.  
  100. public void SetContent(string content)
  101. {
  102.  
  103. if (content.Length > 0)
  104. {
  105. _byteContent = new byte[Encoding.UTF8.GetByteCount(content)];
  106. _byteContent = Encoding.UTF8.GetBytes(content);
  107. _headers["Content-Length"] = _byteContent.Length.ToString();
  108.  
  109. }
  110. }
  111.  
  112. public void SetContent(byte[] content)
  113. {
  114. if (_byteContent != null) _byteContent = null;
  115. _byteContent = new byte[content.Length];
  116. _byteContent = content;
  117. _headers["Content-Length"] = _byteContent.Length.ToString();
  118. }
  119.  
  120. public void SetContent(Stream stream)
  121. {
  122.  
  123. if (_byteContent != null) _byteContent = null;
  124. if (stream.Length > 0)
  125. {
  126. _byteContent = new byte[stream.Length];
  127. stream.Read(_byteContent, 0, _byteContent.Length);
  128. _headers["Content-Length"] = _byteContent.Length.ToString();
  129. }
  130. }
  131.  
  132. public void Send(Stream network)
  133. {
  134. if (network.CanWrite)
  135. {
  136. StreamWriter sw = new StreamWriter(network);
  137.  
  138. sw.WriteLine("HTTP/1.1 " + Status);
  139.  
  140.  
  141.  
  142. foreach (KeyValuePair<string, string> entry in Headers)
  143. {
  144. sw.WriteLine(entry.Key + ": " + entry.Value);
  145. }
  146.  
  147. sw.WriteLine("");
  148.  
  149. if (ContentType != null && ContentLength == 0)
  150. {
  151. throw new System.ArgumentException("Content type is set, but no content was submitted!", "original");
  152. }
  153. else
  154. {
  155. if (_byteContent != null)
  156. {
  157. sw.Flush();
  158. UTF8Encoding encoding = new UTF8Encoding();
  159. string temp = encoding.GetString(_byteContent);
  160. network.Write(_byteContent, 0, _byteContent.Length);
  161. }
  162. }
  163.  
  164. sw.Flush();
  165.  
  166. }
  167.  
  168. }
  169.  
  170.  
  171.  
  172.  
  173.  
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement