Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using BIF.SWE1.Interfaces;
- namespace MyWebServer
- {
- class Response : IResponse
- {
- private int _statusCode;
- private IDictionary<string, string> _headers;
- private byte[] _byteContent;
- public Response()
- {
- if (_headers == null) _headers = new Dictionary<string, string>();
- _headers.Add("Server", "BIF-SWE1-Server");
- }
- public IDictionary<string, string> Headers => _headers;
- public int ContentLength
- {
- get
- {
- string val;
- if (_headers.TryGetValue("Content-Length", out val)) return int.Parse(val);
- else return 0;
- }
- }
- public string ContentType
- {
- get
- {
- string val;
- if (_headers.TryGetValue("Content-Type", out val)) return val;
- else return null;
- }
- set
- {
- _headers["Content-Type"] = value;
- }
- }
- public int StatusCode
- {
- get
- {
- if (_statusCode == 0)
- throw new InvalidOperationException("no status code set yet!!!!!!!");
- return this._statusCode;
- }
- set
- {
- this._statusCode = value;
- }
- }
- public string Status
- {
- get
- {
- if (_statusCode == 200) return "200 OK";
- if (_statusCode == 404) return "404 Not Found";
- if (_statusCode == 500) return "500 INTERNAL SERVER ERROR";
- else return "";
- }
- }
- public string ServerHeader
- {
- get
- {
- return _headers["Server"];
- }
- set
- {
- _headers["Server"] = value.ToString();
- }
- }
- public void AddHeader(string header, string value)
- {
- if (_headers == null)
- _headers = new Dictionary<string, string>();
- if (_headers.ContainsKey(header))
- {
- _headers[header] = value;
- }
- else
- _headers.Add(header, value);
- }
- public void SetContent(string content)
- {
- if (content.Length > 0)
- {
- _byteContent = new byte[Encoding.UTF8.GetByteCount(content)];
- _byteContent = Encoding.UTF8.GetBytes(content);
- _headers["Content-Length"] = _byteContent.Length.ToString();
- }
- }
- public void SetContent(byte[] content)
- {
- if (_byteContent != null) _byteContent = null;
- _byteContent = new byte[content.Length];
- _byteContent = content;
- _headers["Content-Length"] = _byteContent.Length.ToString();
- }
- public void SetContent(Stream stream)
- {
- if (_byteContent != null) _byteContent = null;
- if (stream.Length > 0)
- {
- _byteContent = new byte[stream.Length];
- stream.Read(_byteContent, 0, _byteContent.Length);
- _headers["Content-Length"] = _byteContent.Length.ToString();
- }
- }
- public void Send(Stream network)
- {
- if (network.CanWrite)
- {
- StreamWriter sw = new StreamWriter(network);
- sw.WriteLine("HTTP/1.1 " + Status);
- foreach (KeyValuePair<string, string> entry in Headers)
- {
- sw.WriteLine(entry.Key + ": " + entry.Value);
- }
- sw.WriteLine("");
- if (ContentType != null && ContentLength == 0)
- {
- throw new System.ArgumentException("Content type is set, but no content was submitted!", "original");
- }
- else
- {
- if (_byteContent != null)
- {
- sw.Flush();
- UTF8Encoding encoding = new UTF8Encoding();
- string temp = encoding.GetString(_byteContent);
- network.Write(_byteContent, 0, _byteContent.Length);
- }
- }
- sw.Flush();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement