2019-02-14 09:14:50 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace ln.http
|
|
|
|
|
{
|
|
|
|
|
public class HttpResponse
|
|
|
|
|
{
|
|
|
|
|
public HttpRequest HttpRequest { get; }
|
|
|
|
|
|
|
|
|
|
public Stream ContentStream { get; private set; }
|
|
|
|
|
public TextWriter ContentWriter { get; private set; }
|
|
|
|
|
public bool HasCustomContentStream { get; private set; }
|
|
|
|
|
|
|
|
|
|
int statusCode;
|
|
|
|
|
string statusMessage;
|
|
|
|
|
Dictionary<string, List<String>> headers = new Dictionary<string, List<string>>();
|
2019-02-26 22:00:37 +01:00
|
|
|
|
List<HttpCookie> cookies = new List<HttpCookie>();
|
2019-02-14 09:14:50 +01:00
|
|
|
|
|
|
|
|
|
public HttpResponse(HttpRequest httpRequest)
|
|
|
|
|
{
|
|
|
|
|
HttpRequest = httpRequest;
|
|
|
|
|
ContentStream = new MemoryStream();
|
|
|
|
|
ContentWriter = new StreamWriter(ContentStream);
|
|
|
|
|
|
2019-02-26 22:00:37 +01:00
|
|
|
|
StatusCode = 200;
|
2019-02-14 09:14:50 +01:00
|
|
|
|
SetHeader("content-type", "text/html");
|
|
|
|
|
}
|
|
|
|
|
public HttpResponse(HttpRequest httpRequest,string contentType)
|
|
|
|
|
:this(httpRequest)
|
|
|
|
|
{
|
|
|
|
|
SetHeader("content-type", contentType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public HttpResponse(HttpRequest httpRequest, Stream contentStream)
|
|
|
|
|
{
|
|
|
|
|
HttpRequest = httpRequest;
|
|
|
|
|
ContentStream = contentStream;
|
|
|
|
|
ContentWriter = null;
|
|
|
|
|
HasCustomContentStream = true;
|
|
|
|
|
|
|
|
|
|
StatusCode = 200;
|
|
|
|
|
SetHeader("content-type", "text/html");
|
|
|
|
|
}
|
|
|
|
|
public HttpResponse(HttpRequest httpRequest, Stream contentStream,string contentType)
|
|
|
|
|
:this(httpRequest,contentStream)
|
|
|
|
|
{
|
|
|
|
|
SetHeader("content-type", contentType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String GetHeader(string name)
|
|
|
|
|
{
|
|
|
|
|
return String.Join(",", headers[name.ToUpper()]);
|
|
|
|
|
}
|
|
|
|
|
public String[] GetHeaderValues(string name)
|
|
|
|
|
{
|
|
|
|
|
return headers[name.ToUpper()].ToArray();
|
|
|
|
|
}
|
|
|
|
|
public String[] GetHeaderNames()
|
|
|
|
|
{
|
|
|
|
|
return headers.Keys.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetHeader(String name, String value)
|
|
|
|
|
{
|
|
|
|
|
name = name.ToUpper();
|
|
|
|
|
|
|
|
|
|
headers[name] = new List<string>();
|
|
|
|
|
headers[name].Add(value);
|
|
|
|
|
}
|
|
|
|
|
public void AddHeader(String name, String value)
|
|
|
|
|
{
|
|
|
|
|
name = name.ToUpper();
|
|
|
|
|
|
|
|
|
|
if (!headers.ContainsKey(name))
|
|
|
|
|
headers[name] = new List<string>();
|
|
|
|
|
|
|
|
|
|
headers[name].Add(value);
|
|
|
|
|
}
|
|
|
|
|
public void RemoveHeader(String name)
|
|
|
|
|
{
|
|
|
|
|
headers.Remove(name.ToUpper());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-02-26 22:00:37 +01:00
|
|
|
|
public void AddCookie(string name,string value)
|
|
|
|
|
{
|
|
|
|
|
AddCookie(new HttpCookie(name, value));
|
|
|
|
|
}
|
|
|
|
|
public void AddCookie(HttpCookie httpCookie)
|
|
|
|
|
{
|
|
|
|
|
cookies.Add(httpCookie);
|
|
|
|
|
}
|
|
|
|
|
public void RemoveCookie(HttpCookie httpCookie)
|
|
|
|
|
{
|
|
|
|
|
cookies.Remove(httpCookie);
|
|
|
|
|
}
|
|
|
|
|
public HttpCookie[] Cookies => cookies.ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-02-14 09:14:50 +01:00
|
|
|
|
public int StatusCode
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return statusCode;
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
statusCode = value;
|
|
|
|
|
statusMessage = HttpStatusCodes.GetStatusMessage(statusCode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public String StatusMessage
|
|
|
|
|
{
|
|
|
|
|
get {
|
|
|
|
|
return statusMessage;
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
statusMessage = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|