Skip to content

Commit

Permalink
Merge pull request #2 from Garados007/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Garados007 authored Dec 18, 2020
2 parents 11ab664 + 1b8ac0c commit b8c402a
Show file tree
Hide file tree
Showing 47 changed files with 460 additions and 176 deletions.
2 changes: 1 addition & 1 deletion MaxLib.WebServer.Test/Testing/TestTestTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void TestGetAddedCookies()
{
var server = new TestWebServer();
var test = new TestTask(server);
test.Task.Document.RequestHeader.Cookie.AddedCookies.Add(
test.Task.Request.Cookie.AddedCookies.Add(
"test1",
new MaxLib.WebServer.HttpCookie.Cookie("test2", "test3"));
var added = test.GetAddedCookies().ToArray();
Expand Down
6 changes: 3 additions & 3 deletions MaxLib.WebServer/Api/ApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ public string[] Endpoint
public override bool CanWorkWith(WebProgressTask task)
{
_ = task ?? throw new ArgumentNullException(nameof(task));
return task.Document.RequestHeader.Location.StartsUrlWith(endpoint, IgnoreCase);
return task.Request.Location.StartsUrlWith(endpoint, IgnoreCase);
}

public override async Task ProgressTask(WebProgressTask task)
{
_ = task ?? throw new ArgumentNullException(nameof(task));
var tiles = task.Document.RequestHeader.Location.DocumentPathTiles;
var tiles = task.Request.Location.DocumentPathTiles;
var location = new string[tiles.Length - endpoint.Length];
Array.Copy(tiles, endpoint.Length, location, 0, location.Length);
var data = await HandleRequest(task, location);
if (data != null)
task.Document.DataSources.Add(data);
else task.Document.ResponseHeader.StatusCode = HttpStateCode.InternalServerError;
else task.Response.StatusCode = HttpStateCode.InternalServerError;
}
}
}
4 changes: 2 additions & 2 deletions MaxLib.WebServer/Api/Rest/RestApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ protected virtual RestQueryArgs GetQueryArgs(WebProgressTask task, string[] loca
{
_ = task ?? throw new ArgumentNullException(nameof(task));
_ = location ?? throw new ArgumentNullException(nameof(location));
return new RestQueryArgs(location, task.Document.RequestHeader.Location.GetParameter, task.Document.RequestHeader.Post);
return new RestQueryArgs(location, task.Request.Location.GetParameter, task.Request.Post);
}

protected virtual HttpDataSource NoEndpoint(WebProgressTask task, RestQueryArgs args)
{
_ = task ?? throw new ArgumentNullException(nameof(task));
_ = args ?? throw new ArgumentNullException(nameof(args));
task.Document.ResponseHeader.StatusCode = HttpStateCode.NotFound;
task.Response.StatusCode = HttpStateCode.NotFound;
return new HttpStringDataSource("no endpoint");
}
}
Expand Down
4 changes: 2 additions & 2 deletions MaxLib.WebServer/Chunked/ChunkedResponseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public override bool CanWorkWith(WebProgressTask task)

public override async Task ProgressTask(WebProgressTask task)
{
var request = task.Document.RequestHeader;
var response = task.Document.ResponseHeader;
var request = task.Request;
var response = task.Response;
response.FieldContentType = task.Document.PrimaryMime;
response.SetActualDate();
response.HttpProtocol = request.HttpProtocol;
Expand Down
4 changes: 2 additions & 2 deletions MaxLib.WebServer/Chunked/ChunkedSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override bool CanWorkWith(WebProgressTask task)

public override async Task ProgressTask(WebProgressTask task)
{
var header = task.Document.ResponseHeader;
var header = task.Response;
var stream = task.NetworkStream;
var writer = new StreamWriter(stream);
await writer.WriteAsync(header.HttpProtocol);
Expand All @@ -43,7 +43,7 @@ public override async Task ProgressTask(WebProgressTask task)
await writer.WriteAsync(": ");
await writer.WriteLineAsync(e.Value);
}
foreach (var cookie in task.Document.RequestHeader.Cookie.AddedCookies) //Cookies
foreach (var cookie in task.Request.Cookie.AddedCookies) //Cookies
{
await writer.WriteAsync("Set-Cookie: ");
await writer.WriteLineAsync(cookie.ToString());
Expand Down
18 changes: 6 additions & 12 deletions MaxLib.WebServer/HttpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,19 @@
using System.IO;
using System.Net.Sockets;

#nullable enable

namespace MaxLib.WebServer
{
[Serializable]
public class HttpConnection
{
public byte[] ConnectionKey { get; set; }

public string Ip { get; set; }

public TcpClient NetworkClient { get; set; }

public Stream NetworkStream { get; set; }
public string? Ip { get; set; }

public int LastWorkTime { get; set; }
public TcpClient? NetworkClient { get; set; }

public Dictionary<object, object> SessionInformation { get; private set; }
= new Dictionary<object, object>();
public Stream? NetworkStream { get; set; }

public void AlwaysSyncSessionInformation(Dictionary<object, object> information)
=> SessionInformation = information ?? throw new ArgumentNullException(nameof(information));
public int LastWorkTime { get; set; } = -1;
}
}
4 changes: 3 additions & 1 deletion MaxLib.WebServer/HttpCookie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Collections.ObjectModel;
using System.Text;

#nullable enable

namespace MaxLib.WebServer
{
[Serializable]
Expand Down Expand Up @@ -68,7 +70,7 @@ public override string ToString()
}
}

public string CompleteRequestCookie { get; private set; }
public string CompleteRequestCookie { get; private set; } = "";

public Dictionary<string, Cookie> AddedCookies { get; }

Expand Down
5 changes: 4 additions & 1 deletion MaxLib.WebServer/HttpDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.IO;
using System.Threading.Tasks;

#nullable enable

namespace MaxLib.WebServer
{
[Serializable]
Expand Down Expand Up @@ -43,7 +45,8 @@ public async Task<long> WriteStream(Stream stream, long start, long? stop)
{
_ = stream ?? throw new ArgumentNullException(nameof(stream));
if (start < 0) throw new ArgumentOutOfRangeException(nameof(start));
if (Length() != null && start >= Length().Value)
var length = Length();
if (length != null && start >= length.Value)
throw new ArgumentOutOfRangeException(nameof(start));
if (stop != null && stop < start) throw new ArgumentOutOfRangeException(nameof(stop));
return await WriteStreamInternal(stream, start, stop);
Expand Down
17 changes: 11 additions & 6 deletions MaxLib.WebServer/HttpDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,31 @@
using System.Collections.Generic;
using System.Linq;

#nullable enable

namespace MaxLib.WebServer
{
[Serializable]
public class HttpDocument : IDisposable
{
public List<HttpDataSource> DataSources { get; } = new List<HttpDataSource>();

public string PrimaryMime
public string? PrimaryMime
{
get { return DataSources.Count == 0 ? null : DataSources[0].MimeType; }
}

public string PrimaryEncoding { get; set; } = null;
public string? PrimaryEncoding { get; set; } = null;

[Obsolete("Use WebProgressTask.Request. this will be removed in a future release.")]
public HttpRequestHeader RequestHeader { get; set; } = new HttpRequestHeader();

public HttpRequestHeader RequestHeader { get; set; }
[Obsolete("Use WebProgressTask.Response. this will be removed in a future release.")]
public HttpResponseHeader ResponseHeader { get; set; } = new HttpResponseHeader();

public HttpResponseHeader ResponseHeader { get; set; }
public Dictionary<object, object> Information { get; } = new Dictionary<object, object>();
public Dictionary<object?, object?> Information { get; } = new Dictionary<object?, object?>();

public object this[object identifer]
public object? this[object? identifer]
{
get => Information[identifer];
set => Information[identifer] = value;
Expand Down
14 changes: 10 additions & 4 deletions MaxLib.WebServer/HttpFileDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
using System.IO;
using System.Threading.Tasks;

#nullable enable

namespace MaxLib.WebServer
{
[Serializable]
public class HttpFileDataSource : HttpDataSource
{
public FileStream File { get; private set; }
public FileStream? File { get; private set; }

private string path = null;
public virtual string Path
private string? path = null;
public virtual string? Path
{
get => path;
set
Expand All @@ -37,7 +39,7 @@ public virtual string Path

public override bool CanProvideData => true;

public HttpFileDataSource(string path, bool readOnly = true)
public HttpFileDataSource(string? path, bool readOnly = true)
{
ReadOnly = readOnly;
Path = path;
Expand All @@ -54,6 +56,8 @@ public override void Dispose()
protected override async Task<long> WriteStreamInternal(Stream stream, long start, long? stop)
{
await Task.CompletedTask;
if (File == null)
return 0;
File.Position = start;
using (var skip = new SkipableStream(File, 0))
{
Expand All @@ -75,6 +79,8 @@ protected override async Task<long> ReadStreamInternal(Stream stream, long? leng
await Task.CompletedTask;
if (ReadOnly)
throw new NotSupportedException();
if (File == null)
return 0;
File.Position = 0;
using (var skip = new SkipableStream(File, 0))
{
Expand Down
26 changes: 25 additions & 1 deletion MaxLib.WebServer/HttpHeader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;

#nullable enable

namespace MaxLib.WebServer
{
[Serializable]
Expand All @@ -20,11 +22,33 @@ public string HttpProtocol

public Dictionary<string, string> HeaderParameter { get; } = new Dictionary<string, string>();

public void SetHeader(IEnumerable<(string, string)> headers)
public string? GetHeader(string key)
{
_ = key ?? throw new ArgumentNullException(nameof(key));
return HeaderParameter.TryGetValue(key, out string value) ? value : null;
}

public void SetHeader(IEnumerable<(string, string?)> headers)
{
_ = headers ?? throw new ArgumentNullException(nameof(headers));
foreach (var (key, value) in headers)
if (value != null)
HeaderParameter[key] = value;
else HeaderParameter.Remove(key);
}

public void SetHeader(params (string, string?)[] header)
{
_ = header ?? throw new ArgumentNullException(nameof(header));
SetHeader(headers: header);
}

public void SetHeader(string key, string? value)
{
_ = key ?? throw new ArgumentNullException(nameof(key));
if (value != null)
HeaderParameter[key] = value;
else HeaderParameter.Remove(key);
}

private string protocolMethod = HttpProtocollMethod.Get;
Expand Down
7 changes: 6 additions & 1 deletion MaxLib.WebServer/HttpLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using System.Text.RegularExpressions;

#nullable enable

namespace MaxLib.WebServer
{
[Serializable]
Expand Down Expand Up @@ -56,8 +58,11 @@ public virtual void SetLocation(string url)

public HttpLocation(string url)
{
_ = url ?? throw new ArgumentNullException(nameof(url));
Url = url ?? throw new ArgumentNullException(nameof(url));
GetParameter = new Dictionary<string, string>();
DocumentPath = "";
DocumentPathTiles = new string[0];
CompleteGet = "";
SetLocation(url);
}

Expand Down
10 changes: 6 additions & 4 deletions MaxLib.WebServer/HttpPost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
using System.Collections.Generic;
using System.Text.RegularExpressions;

#nullable enable

namespace MaxLib.WebServer
{
[Serializable]
public class HttpPost
{
public string CompletePost { get; private set; }

public string MimeType { get; private set; }
public string? MimeType { get; private set; }

public Dictionary<string, string> PostParameter { get; }

public virtual void SetPost(string post, string mime)
public virtual void SetPost(string post, string? mime)
{
CompletePost = post ?? throw new ArgumentNullException("Post");

Expand Down Expand Up @@ -73,9 +75,9 @@ protected virtual void SetPostFormData(string post, string boundary)

}

public HttpPost(string post, string mime)
public HttpPost(string post, string? mime)
{
_ = post ?? throw new ArgumentNullException(nameof(post));
CompletePost = post ?? throw new ArgumentNullException(nameof(post));
PostParameter = new Dictionary<string, string>();
SetPost(post, mime);
}
Expand Down
2 changes: 2 additions & 0 deletions MaxLib.WebServer/HttpProtocollDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Linq;

#nullable enable

namespace MaxLib.WebServer
{
public static class HttpProtocollDefinition
Expand Down
8 changes: 5 additions & 3 deletions MaxLib.WebServer/HttpRequestHeader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;

#nullable enable

namespace MaxLib.WebServer
{
[Serializable]
Expand All @@ -27,10 +29,10 @@ public string Host
public HttpConnectionType FieldConnection { get; set; } = HttpConnectionType.Close;
public HttpCookie Cookie { get; } = new HttpCookie("");

public string FieldUserAgent
public string? FieldUserAgent
{
get => HeaderParameter.TryGetValue("User-Agent", out string value) ? value : null;
set => HeaderParameter["User-Agent"] = value;
get => GetHeader("User-Agent");
set => SetHeader("User-Agent", value);
}
}
}
Loading

0 comments on commit b8c402a

Please sign in to comment.