tesses.webserver/Tesses.WebServer/ServerContext.cs

80 lines
2.7 KiB
C#
Raw Normal View History

2022-04-02 18:59:12 +00:00
using System.Collections.Generic;
using System.IO;
using System;
using System.Net;
namespace Tesses.WebServer
{
public class ServerContext
{
public string Method { get; set; }
public ServerContext(string method,Stream strm,string path,Dictionary<string,List<string>> headers)
{
Method = method;
NetworkStream = strm;
RequestHeaders = headers;
ResponseHeaders = new Dictionary<string, List<string>>();
QueryParams = new Dictionary<string, List<string>>();
2022-04-02 18:59:12 +00:00
StatusCode = 200;
// /joel/path/luigi?local=jim&john_surname=connor&demi_surname=lovato&local=tim
string[] splitUrl = path.Split(new char[] { '?' }, 2);
if (splitUrl.Length > 0)
{
UrlPath = splitUrl[0];
if (splitUrl.Length == 2)
{
//local=jim&john_surname=connor&demi_surname=lovato&local=tim
//we want to split on &
q_parm = splitUrl[1];
}
else
{
q_parm = "";
}
ResetQueryParms();
}
}
public void ResetQueryParms()
{
QueryParams.Clear();
foreach (var item in q_parm.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries))
{
//Console.WriteLine(item);
var itemSplit = item.Split(new char[] { '=' }, 2);
if (itemSplit.Length > 0)
{
string key = itemSplit[0];
string value = "";
if (itemSplit.Length == 2)
2022-04-02 18:59:12 +00:00
{
value = itemSplit[1];
2022-04-02 18:59:12 +00:00
}
QueryParams.Add(key, value);
2022-04-02 18:59:12 +00:00
}
}
2022-04-02 18:59:12 +00:00
}
private string get_host()
{
if(RequestHeaders.ContainsKey("Host"))
{
return RequestHeaders.GetFirst("Host");
}
return Server.Address.ToString();
2022-04-02 18:59:12 +00:00
}
string q_parm;
public IPEndPoint Server { get; set; }
public IPEndPoint Client { get; set; }
2022-04-02 18:59:12 +00:00
public string Host { get { return get_host(); } }
public string UrlPath { get; set; }
public Dictionary<string,List<string>> QueryParams { get; set; }
public Dictionary<string,List<string>> RequestHeaders { get; set; }
public Dictionary<string,List<string>> ResponseHeaders { get; set; }
public Stream NetworkStream { get; set; }
public int StatusCode { get; internal set; }
}
}