2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-27 05:23:13 +00:00

HTTP+Entity

This commit is contained in:
2020-02-29 07:20:46 +03:00
parent fde1b1d8ad
commit 8bd9b3282c
9 changed files with 107 additions and 81 deletions

View File

@ -44,7 +44,7 @@ namespace Esyur.Misc
public static class Global
{
private static KeyList<string, object> variables = new KeyList<string, object>();
// private static Hashtable m_Cached = new Hashtable();
// private static Hashtable m_Cached = new Hashtable();
//internal static bool SystemIsWorking = false;
private static Random rand = new Random(System.Environment.TickCount);
@ -57,7 +57,12 @@ namespace Esyur.Misc
public static event LogEvent SystemLog;
static Random random = new Random();
public static string Version { get; }= FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion;
//FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
// string version = fvi.FileVersion;
/*
@ -419,7 +424,7 @@ namespace Esyur.Misc
//var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_-+=\\?/";
var result = new string(
Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)])
.Select(s => s[rand.Next(s.Length)])
.ToArray());
//if (result.Length < length)
// Console.WriteLine();

View File

@ -121,7 +121,7 @@ namespace Esyur.Net.HTTP
Response.Headers["Sec-WebSocket-Protocol"] = Request.Headers["Sec-WebSocket-Protocol"];
Response.Number = HTTPResponsePacket.ResponseCode.HTTP_SWITCHING;
Response.Number = HTTPResponsePacket.ResponseCode.Switching;
Response.Text = "Switching Protocols";
WSMode = true;
@ -253,7 +253,7 @@ namespace Esyur.Net.HTTP
if (!File.Exists(filename))
{
Response.Number = HTTPResponsePacket.ResponseCode.HTTP_NOTFOUND;
Response.Number = HTTPResponsePacket.ResponseCode.NotFound;
Send("File Not Found");
return true;
}
@ -267,8 +267,8 @@ namespace Esyur.Net.HTTP
var ims = DateTime.Parse(Request.Headers["if-modified-since"]);
if (Math.Abs((fileEditTime - ims).TotalSeconds) < 0)
{
Response.Number = HTTPResponsePacket.ResponseCode.HTTP_NOTMODIFIED;
Response.Text = "Not Modified";
Response.Number = HTTPResponsePacket.ResponseCode.NotModified;
//Response.Text = "Not Modified";
Send((byte[])null);
}
}
@ -280,7 +280,7 @@ namespace Esyur.Net.HTTP
Response.Number = HTTPResponsePacket.ResponseCode.HTTP_OK;
Response.Number = HTTPResponsePacket.ResponseCode.OK;
// Fri, 30 Oct 2007 14:19:41 GMT
Response.Headers["Last-Modified"] = fileEditTime.ToString("ddd, dd MMM yyyy HH:mm:ss");
FileInfo fi = new FileInfo(filename);

View File

@ -231,7 +231,7 @@ namespace Esyur.Net.HTTP
if (resource.Execute(sender))
return;
sender.Response.Number = HTTPResponsePacket.ResponseCode.HTTP_SERVERERROR;
sender.Response.Number = HTTPResponsePacket.ResponseCode.InternalServerError;
sender.Send("Bad Request");
sender.Close();
}

View File

@ -43,16 +43,29 @@ namespace Esyur.Net.Packets
public enum ResponseCode : int
{
HTTP_SWITCHING = 101,
HTTP_OK = 200,
HTTP_NOTFOUND = 404,
HTTP_SERVERERROR = 500,
HTTP_MOVED = 301,
HTTP_NOTMODIFIED = 304,
HTTP_REDIRECT = 307
Switching= 101,
OK = 200,
Created = 201,
Accepted = 202,
NoContent = 204,
MovedPermanently = 301,
Found = 302,
SeeOther = 303,
NotModified = 304,
TemporaryRedirect = 307,
BadRequest = 400,
Unauthorized = 401,
Forbidden = 403,
NotFound = 404,
MethodNotAllowed = 405,
NotAcceptable = 406,
PreconditionFailed = 412,
UnsupportedMediaType = 415,
InternalServerError = 500,
NotImplemented = 501,
}
public class HTTPCookie
public struct HTTPCookie
{
public string Name;
public string Value;
@ -61,46 +74,45 @@ namespace Esyur.Net.Packets
public bool HttpOnly;
public string Domain;
public HTTPCookie(string Name, string Value)
public HTTPCookie(string name, string value)
{
this.Name = Name;
this.Value = Value;
this.Name = name;
this.Value = value;
this.Path = null;
this.Expires = DateTime.MinValue;
this.HttpOnly = false;
this.Domain = null;
}
public HTTPCookie(string Name, string Value, DateTime Expires)
public HTTPCookie(string name, string value, DateTime expires)
{
this.Name = Name;
this.Value = Value;
this.Expires = Expires;
this.Name = name;
this.Value = value;
this.Expires = expires;
this.HttpOnly = false;
this.Domain = null;
this.Path = null;
}
public override string ToString()
{
//Set-Cookie: ckGeneric=CookieBody; expires=Sun, 30-Dec-2001 21:00:00 GMT; domain=.com.au; path=/
//Set-Cookie: SessionID=another; expires=Fri, 29 Jun 2006 20:47:11 UTC; path=/
string Cookie = Name + "=" + Value;
var cookie = Name + "=" + Value;
if (Expires.Ticks != 0)
{
Cookie += "; expires=" + Expires.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss") + " GMT";
}
cookie += "; expires=" + Expires.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss") + " GMT";
if (Domain != null)
{
Cookie += "; domain=" + Domain;
}
cookie += "; domain=" + Domain;
if (Path != null)
{
Cookie += "; path=" + Path;
}
cookie += "; path=" + Path;
if (HttpOnly)
{
Cookie += "; HttpOnly";
}
cookie += "; HttpOnly";
return Cookie;
return cookie;
}
}
@ -110,15 +122,9 @@ namespace Esyur.Net.Packets
public byte[] Message;
public ResponseCode Number;
public string Text;
//public DStringDictionary Cookies;
public List<HTTPCookie> Cookies = new List<HTTPCookie>();
public bool Handled;
//public bool ResponseHandled; //flag this as true if you are handling it yourself
//private bool createSession;
//private HTTPServer Server;
//public HTTPSession Session;
public override string ToString()
{
@ -129,31 +135,22 @@ namespace Esyur.Net.Packets
+ "\n\tMessage: " + (Message != null ? Message.Length.ToString() : "NULL");
}
private string MakeHeader(ComposeOptions Options)
private string MakeHeader(ComposeOptions options)
{
string header = Version + " " + (int)Number + " " + Text + "\r\n"
+ "Server: Delta Web Server\r\n"
//Fri, 30 Oct 2007 14:19:41 GMT"
+ "Date: " + DateTime.Now.ToUniversalTime().ToString("r") + "\r\n";
string header = $"{Version} {(int)Number} {Text}\r\nServer: Esyur {Global.Version}\r\nDate: {DateTime.Now.ToUniversalTime().ToString("r")}\r\n";
if (Options == ComposeOptions.AllCalculateLength && Message != null)
{
if (options == ComposeOptions.AllCalculateLength && Message != null)
Headers["Content-Length"] = Message.Length.ToString();
}
foreach (var kv in Headers)
{
header += kv.Key + ": " + kv.Value + "\r\n";
}
// Set-Cookie: ckGeneric=CookieBody; expires=Sun, 30-Dec-2007 21:00:00 GMT; path=/
// Set-Cookie: ASPSESSIONIDQABBDSQA=IPDPMMMALDGFLMICEJIOCIPM; path=/
foreach (var Cookie in Cookies)
{
header += "Set-Cookie: " + Cookie.ToString() + "\r\n";
}
header += "\r\n";