2019-02-14 09:14:50 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using ln.http.exceptions;
|
2019-02-26 22:00:37 +01:00
|
|
|
|
using ln.http.resources.session;
|
2019-11-04 10:00:33 +01:00
|
|
|
|
using ln.types.net;
|
2019-02-14 09:14:50 +01:00
|
|
|
|
|
|
|
|
|
namespace ln.http
|
|
|
|
|
{
|
2019-09-11 09:29:05 +02:00
|
|
|
|
public class HttpRequest : IDisposable
|
2019-02-14 09:14:50 +01:00
|
|
|
|
{
|
|
|
|
|
Dictionary<String, String> requestHeaders;
|
2019-02-26 22:00:37 +01:00
|
|
|
|
Dictionary<String, String> requestCookies;
|
2019-02-14 09:14:50 +01:00
|
|
|
|
|
2019-11-04 10:00:33 +01:00
|
|
|
|
public HTTPServer HTTPServer { get; }
|
2019-10-18 12:31:41 +02:00
|
|
|
|
|
2019-11-04 10:00:33 +01:00
|
|
|
|
public Endpoint RemoteEndpoint { get; private set; }
|
|
|
|
|
public Endpoint LocalEndpoint { get; private set; }
|
2019-02-14 09:14:50 +01:00
|
|
|
|
|
|
|
|
|
public Uri BaseURI { get; set; }
|
|
|
|
|
public Uri URI { get; private set; }
|
|
|
|
|
|
|
|
|
|
public String Method { get; private set; }
|
|
|
|
|
public String RequestURL { get; private set; }
|
|
|
|
|
public String Protocol { get; private set; }
|
|
|
|
|
|
|
|
|
|
public String Hostname { get; private set; }
|
|
|
|
|
public int Port { get; private set; }
|
|
|
|
|
|
|
|
|
|
public QueryStringParameters Query { get; private set; }
|
|
|
|
|
|
2019-02-26 22:00:37 +01:00
|
|
|
|
public Session Session { get; private set; }
|
|
|
|
|
|
2019-03-14 08:35:54 +01:00
|
|
|
|
public MemoryStream ContentStream { get; }
|
|
|
|
|
public TextReader ContentReader
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (contentReader == null)
|
|
|
|
|
contentReader = new StreamReader(ContentStream);
|
|
|
|
|
return contentReader;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-03 12:56:33 +02:00
|
|
|
|
public Stream GetConnectionStream()
|
|
|
|
|
{
|
|
|
|
|
return connectionStream;
|
|
|
|
|
}
|
2019-03-14 08:35:54 +01:00
|
|
|
|
|
|
|
|
|
StreamReader contentReader;
|
|
|
|
|
byte[] requestBody;
|
2019-08-03 12:56:33 +02:00
|
|
|
|
Stream connectionStream;
|
2019-03-14 08:35:54 +01:00
|
|
|
|
|
2019-11-04 10:00:33 +01:00
|
|
|
|
public HttpRequest(HTTPServer httpServer, HttpReader httpReader, Endpoint localEndpoint)
|
2019-02-14 09:14:50 +01:00
|
|
|
|
{
|
2019-10-18 12:31:41 +02:00
|
|
|
|
HTTPServer = httpServer;
|
2019-08-03 12:56:33 +02:00
|
|
|
|
connectionStream = httpReader.Stream;
|
|
|
|
|
|
2019-02-14 09:14:50 +01:00
|
|
|
|
LocalEndpoint = localEndpoint;
|
|
|
|
|
RemoteEndpoint = httpReader.RemoteEndpoint;
|
|
|
|
|
Method = httpReader.Method;
|
|
|
|
|
Protocol = httpReader.Protocol;
|
|
|
|
|
RequestURL = httpReader.URL;
|
|
|
|
|
|
|
|
|
|
requestHeaders = new Dictionary<string, string>(httpReader.Headers);
|
2019-02-26 22:00:37 +01:00
|
|
|
|
requestCookies = new Dictionary<string, string>();
|
2019-02-14 09:14:50 +01:00
|
|
|
|
|
|
|
|
|
Setup();
|
2019-03-14 08:35:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int clength = int.Parse(GetRequestHeader("content-length", "0"));
|
|
|
|
|
requestBody = new byte[clength];
|
|
|
|
|
|
|
|
|
|
if (clength > 0)
|
|
|
|
|
{
|
|
|
|
|
int nread = httpReader.ReadRequestBody(requestBody, 0, clength);
|
|
|
|
|
if (nread != clength)
|
2019-10-18 12:31:41 +02:00
|
|
|
|
throw new HttpException(500, "failed to read request content");
|
2019-03-14 08:35:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ContentStream = new MemoryStream(requestBody);
|
2019-02-14 09:14:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Setup()
|
|
|
|
|
{
|
|
|
|
|
SetupResourceURI();
|
2019-02-26 22:00:37 +01:00
|
|
|
|
SetupCookies();
|
2019-02-14 09:14:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* SetupResourceURI()
|
|
|
|
|
*
|
|
|
|
|
* Setup the following fields:
|
|
|
|
|
*
|
|
|
|
|
* - Hostname
|
|
|
|
|
* - Port
|
|
|
|
|
* - BaseURI
|
|
|
|
|
* - URI
|
|
|
|
|
* - Query
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
private void SetupResourceURI()
|
|
|
|
|
{
|
|
|
|
|
String host = GetRequestHeader("HOST");
|
|
|
|
|
String[] hostTokens = host.Split(':');
|
|
|
|
|
|
|
|
|
|
Hostname = hostTokens[0];
|
|
|
|
|
Port = (hostTokens.Length > 1) ? int.Parse(hostTokens[1]) : LocalEndpoint.Port;
|
|
|
|
|
BaseURI = new UriBuilder("http", Hostname, Port).Uri;
|
|
|
|
|
URI = new Uri(BaseURI, RequestURL);
|
|
|
|
|
Query = new QueryStringParameters(URI.Query);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-26 22:00:37 +01:00
|
|
|
|
private void SetupCookies()
|
|
|
|
|
{
|
|
|
|
|
string cookies = GetRequestHeader("COOKIE");
|
|
|
|
|
foreach (String cookie in cookies.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
|
|
|
|
|
{
|
|
|
|
|
string[] c = cookie.Split(new char[] { '=' }, 2);
|
|
|
|
|
string cn, cv;
|
|
|
|
|
cn = c[0].Trim();
|
|
|
|
|
if (c.Length > 1)
|
|
|
|
|
cv = c[1].Trim();
|
|
|
|
|
else
|
|
|
|
|
cv = "";
|
|
|
|
|
|
|
|
|
|
if (!this.requestCookies.ContainsKey(cn))
|
|
|
|
|
this.requestCookies.Add(cn, cv);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ApplySession(SessionCache sessionCache)
|
|
|
|
|
{
|
|
|
|
|
Session session = sessionCache.ApplySession(this);
|
|
|
|
|
Session = session;
|
|
|
|
|
if (Session == null)
|
|
|
|
|
{
|
|
|
|
|
requestHeaders.Add("X-LNH-Session-Failed", "true");
|
|
|
|
|
Session = new Session();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-13 08:24:24 +01:00
|
|
|
|
public HttpUser CurrentUser => Session.CurrentUser;
|
|
|
|
|
|
2019-02-14 09:14:50 +01:00
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
//return string.Format("[HttpRequest: RemoteEndpoint={0}, Hostname={1} Port={2} URI={4}, Method={4}, RequestURL={5}, Protocol={6} Query={7}]", RemoteEndpoint, URI, Method, RequestURL, Protocol, Hostname, Port,Query);
|
|
|
|
|
return base.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-14 16:43:38 +01:00
|
|
|
|
public String GetRequestHeader(String name)
|
|
|
|
|
{
|
|
|
|
|
return GetRequestHeader(name, "");
|
|
|
|
|
}
|
|
|
|
|
public String GetRequestHeader(String name, String def)
|
2019-02-14 09:14:50 +01:00
|
|
|
|
{
|
|
|
|
|
name = name.ToUpper();
|
|
|
|
|
|
|
|
|
|
if (requestHeaders.ContainsKey(name))
|
|
|
|
|
return requestHeaders[name];
|
|
|
|
|
|
|
|
|
|
return def;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-14 16:43:38 +01:00
|
|
|
|
public String[] RequestHeaderNames => requestHeaders.Keys.ToArray();
|
|
|
|
|
|
2019-02-26 22:00:37 +01:00
|
|
|
|
public String[] CookieNames => requestCookies.Keys.ToArray();
|
|
|
|
|
public bool ContainsCookie(String name)
|
|
|
|
|
{
|
|
|
|
|
return this.requestCookies.ContainsKey(name);
|
|
|
|
|
}
|
|
|
|
|
public String GetCookie(String name)
|
|
|
|
|
{
|
|
|
|
|
return requestCookies[name];
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-14 08:35:54 +01:00
|
|
|
|
public string self()
|
|
|
|
|
{
|
|
|
|
|
return BaseURI.ToString();
|
|
|
|
|
}
|
2019-02-26 22:00:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-02 01:25:41 +02:00
|
|
|
|
public HttpResponse Redirect(string location,int status = 307)
|
2019-02-14 09:14:50 +01:00
|
|
|
|
{
|
|
|
|
|
HttpResponse httpResponse = new HttpResponse(this);
|
|
|
|
|
httpResponse.AddHeader("location", location);
|
|
|
|
|
httpResponse.StatusCode = status;
|
|
|
|
|
httpResponse.AddHeader("content-type", "text/plain");
|
|
|
|
|
httpResponse.ContentWriter.WriteLine("Redirect: {0}", location);
|
|
|
|
|
return httpResponse;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-11 09:29:05 +02:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
contentReader?.Dispose();
|
|
|
|
|
ContentStream?.Dispose();
|
2019-02-14 09:14:50 +01:00
|
|
|
|
|
2019-09-11 09:29:05 +02:00
|
|
|
|
Session = null;
|
|
|
|
|
}
|
2019-02-14 09:14:50 +01:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|