Skip to content

Commit

Permalink
Merge pull request #5 from Garados007/version-3
Browse files Browse the repository at this point in the history
Version 3
  • Loading branch information
Garados007 authored Nov 9, 2021
2 parents 2a35878 + 7a05520 commit 34c8b87
Show file tree
Hide file tree
Showing 33 changed files with 474 additions and 830 deletions.
4 changes: 3 additions & 1 deletion MaxLib.WebServer.Test/Api/Rest/TestRestApiFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public void Init()
{ "foo", "bar" },
{ "baz", "7" },
},
new HttpPost(Encoding.UTF8.GetBytes("a=1&b=2"),
new HttpPost(
new WebProgressTask(),
Encoding.UTF8.GetBytes("a=1&b=2"),
MimeType.ApplicationXWwwFromUrlencoded),
null
);
Expand Down
71 changes: 0 additions & 71 deletions MaxLib.WebServer.Test/Services/TestHttpHeaderParser.cs

This file was deleted.

4 changes: 2 additions & 2 deletions MaxLib.WebServer.Test/Services/TestHttpHeaderSpecialAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public void Init()
[TestMethod]
public async Task TestHead()
{
test.Request.ProtocolMethod = HttpProtocollMethod.Head;
test.Request.ProtocolMethod = HttpProtocolMethod.Head;
await new HttpHeaderSpecialAction().ProgressTask(test.Task).ConfigureAwait(false);
Assert.AreEqual(true, test.GetInfoObject("Only Header"));
}

[TestMethod]
public async Task TestOptions()
{
test.Request.ProtocolMethod = HttpProtocollMethod.Options;
test.Request.ProtocolMethod = HttpProtocolMethod.Options;
await new HttpHeaderSpecialAction().ProgressTask(test.Task).ConfigureAwait(false);
Assert.AreEqual(1, test.GetDataSources().Count);
Assert.IsTrue(test.GetDataSources()[0] is HttpStringDataSource);
Expand Down
16 changes: 9 additions & 7 deletions MaxLib.WebServer.Test/Services/TestHttpRequestParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public async Task TestRequestParser_SimpleGet()
using (var output = test.SetStream(sb.ToString()))
{
await new HttpRequestParser().ProgressTask(test.Task).ConfigureAwait(false);
Assert.AreEqual(HttpProtocollMethod.Get, test.Request.ProtocolMethod);
Assert.AreEqual(HttpProtocolMethod.Get, test.Request.ProtocolMethod);
Assert.AreEqual("/test.html", test.Request.Location.DocumentPath);
Assert.AreEqual(HttpProtocollDefinition.HttpVersion1_1, test.Request.HttpProtocol);
Assert.AreEqual(HttpProtocolDefinition.HttpVersion1_1, test.Request.HttpProtocol);
Assert.AreEqual("testdomain.local", test.GetRequestHeader("Host"));
}
}
Expand All @@ -56,9 +56,9 @@ public async Task TestRequestParser_SimplePost()
using (var output = test.SetStream(sb.ToString()))
{
await new HttpRequestParser().ProgressTask(test.Task).ConfigureAwait(false);
Assert.AreEqual(HttpProtocollMethod.Post, test.Request.ProtocolMethod);
Assert.AreEqual(HttpProtocolMethod.Post, test.Request.ProtocolMethod);
Assert.AreEqual("/test.html", test.Request.Location.DocumentPath);
Assert.AreEqual(HttpProtocollDefinition.HttpVersion1_1, test.Request.HttpProtocol);
Assert.AreEqual(HttpProtocolDefinition.HttpVersion1_1, test.Request.HttpProtocol);
Assert.AreEqual("testdomain.local", test.GetRequestHeader("Host"));
Assert.AreEqual(content.Length.ToString(), test.GetRequestHeader("Content-Length"));
Assert.AreEqual(MimeType.ApplicationXWwwFromUrlencoded, test.GetRequestHeader("Content-Type"));
Expand Down Expand Up @@ -90,9 +90,9 @@ public async Task TestRequestParser_MultipartPost()
using (var output = test.SetStream(sb.ToString()))
{
await new HttpRequestParser().ProgressTask(test.Task).ConfigureAwait(false);
Assert.AreEqual(HttpProtocollMethod.Post, test.Request.ProtocolMethod);
Assert.AreEqual(HttpProtocolMethod.Post, test.Request.ProtocolMethod);
Assert.AreEqual("/test.html", test.Request.Location.DocumentPath);
Assert.AreEqual(HttpProtocollDefinition.HttpVersion1_1, test.Request.HttpProtocol);
Assert.AreEqual(HttpProtocolDefinition.HttpVersion1_1, test.Request.HttpProtocol);
Assert.AreEqual("testdomain.local", test.GetRequestHeader("Host"));
Assert.AreEqual(content.Length.ToString(), test.GetRequestHeader("Content-Length"));
Assert.AreEqual("multipart/form-data; boundary=---1234", test.GetRequestHeader("Content-Type"));
Expand All @@ -102,8 +102,10 @@ public async Task TestRequestParser_MultipartPost()
Assert.AreEqual(1, data.Entries.Count);
Assert.AreEqual(1, data.Entries[0].Header.Count);
Assert.AreEqual("text/plain", data.Entries[0].Header["Content-Type"]);
Assert.IsNull(data.Entries[0].TempFile);
Assert.IsTrue(data.Entries[0].Content.HasValue);
Assert.AreEqual("Hello World",
Encoding.UTF8.GetString(data.Entries[0].Content.ToArray())
Encoding.UTF8.GetString(data.Entries[0].Content.Value.ToArray())
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions MaxLib.WebServer.Test/Services/TestHttpResponseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public async Task TestHeader()
TextEncoding = "utf-8"
});
test.Task.Document.PrimaryEncoding = "utf-8";
test.Request.HttpProtocol = HttpProtocollDefinition.HttpVersion1_1;
test.Request.HttpProtocol = HttpProtocolDefinition.HttpVersion1_1;
await new HttpResponseCreator().ProgressTask(test.Task).ConfigureAwait(false);
Assert.AreEqual($"{MimeType.TextPlain}; charset=utf-8", test.Response.FieldContentType);
Assert.AreNotEqual(null, test.Response.FieldDate);
Assert.AreEqual(HttpProtocollDefinition.HttpVersion1_1, test.Response.HttpProtocol);
Assert.AreEqual(HttpProtocolDefinition.HttpVersion1_1, test.Response.HttpProtocol);
Assert.AreEqual("keep-alive", test.GetResponseHeader("Connection"));
Assert.AreEqual("IE=Edge", test.GetResponseHeader("X-UA-Compatible"));
Assert.AreEqual("4", test.GetResponseHeader("Content-Length"));
Expand Down
2 changes: 1 addition & 1 deletion MaxLib.WebServer.Test/Services/TestHttpSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void Init()
[TestMethod]
public async Task TestSending()
{
test.Response.HttpProtocol = HttpProtocollDefinition.HttpVersion1_1;
test.Response.HttpProtocol = HttpProtocolDefinition.HttpVersion1_1;
test.Response.StatusCode = HttpStateCode.OK;
test.Response.FieldContentType = MimeType.TextPlain;
test.Request.Cookie.AddedCookies.Add("test",
Expand Down
6 changes: 0 additions & 6 deletions MaxLib.WebServer/HttpDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ public string? PrimaryMime

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();

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

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

public object? this[object? identifer]
Expand Down
4 changes: 2 additions & 2 deletions MaxLib.WebServer/HttpHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace MaxLib.WebServer
[Serializable]
public abstract class HttpHeader
{
private string httpProtocol = HttpProtocollDefinition.HttpVersion1_1;
private string httpProtocol = HttpProtocolDefinition.HttpVersion1_1;
public string HttpProtocol
{
get => httpProtocol;
Expand Down Expand Up @@ -52,7 +52,7 @@ public void SetHeader(string key, string? value)
else HeaderParameter.Remove(key);
}

private string protocolMethod = HttpProtocollMethod.Get;
private string protocolMethod = HttpProtocolMethod.Get;
public string ProtocolMethod
{
get => protocolMethod;
Expand Down
97 changes: 44 additions & 53 deletions MaxLib.WebServer/HttpPost.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MaxLib.WebServer.Post;

#nullable enable

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

public string? MimeType { get; private set; }

internal IO.ContentStream? Content { get; private set; }

protected Lazy<IPostData>? LazyData { get; set; }
public IPostData? Data => LazyData?.Value;

[Obsolete("this will be removed in a future release. Use HttpPost.Data instead.")]
public Dictionary<string, string> PostParameter
=> Data is UrlEncodedData data ? data.Parameter : new Dictionary<string, string>();
private Lazy<Task<IPostData>>? LazyData;
public Task<IPostData>? DataAsync => LazyData?.Value;
public IPostData? Data => DataAsync?.Result;

public static Dictionary<string, Func<IPostData>> DataHandler { get; }
= new Dictionary<string, Func<IPostData>>();
Expand All @@ -33,8 +29,9 @@ static HttpPost()
() => new MultipartFormData();
}

public virtual void SetPost(ReadOnlyMemory<byte> post, string? mime)
public virtual void SetPost(WebProgressTask task, IO.ContentStream content, string? mime)
{
Content = content;
string args = "";
if (mime != null)
{
Expand All @@ -49,64 +46,58 @@ public virtual void SetPost(ReadOnlyMemory<byte> post, string? mime)
if ((MimeType = mime) != null &&
DataHandler.TryGetValue(mime!, out Func<IPostData> constructor)
)
LazyData = new Lazy<IPostData>(() =>
LazyData = new Lazy<Task<IPostData>>(() =>
{
var data = constructor();
data.Set(post, args);
return data;
return Task.Run(async () =>
{
var data = constructor();
await data.SetAsync(task, content, args).ConfigureAwait(false);
return data;
});
}, System.Threading.LazyThreadSafetyMode.ExecutionAndPublication);
else LazyData = new Lazy<IPostData>(new UnknownPostData(post, mime));
else LazyData = new Lazy<Task<IPostData>>(
Task.FromResult<IPostData>(new UnknownPostData(content, mime))
);
}

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

string args = "";
if (mime != null)
{
var ind = mime.IndexOf(';');
if (ind >= 0)
{
args = mime.Substring(ind + 1);
mime = mime.Remove(ind);
}
}

if ((MimeType = mime) != null &&
DataHandler.TryGetValue(mime!, out Func<IPostData> constructor)
)
LazyData = new Lazy<IPostData>(() =>
{
var data = constructor();
data.Set(post, args);
return data;
}, System.Threading.LazyThreadSafetyMode.ExecutionAndPublication);
else LazyData = null;
}

public HttpPost()
public HttpPost(WebProgressTask task, ReadOnlyMemory<byte> content, string? mime)
: this(
task,
new IO.ContentStream(
new IO.NetworkReader(new IO.SpanStream(content)),
content.Length
),
mime
)
{
#pragma warning disable CS0612 // 'HttpPost.CompletePost' is obsolete
CompletePost = "";
#pragma warning restore CS0612

}

public HttpPost(ReadOnlyMemory<byte> post, string? mime)
public HttpPost(WebProgressTask task, IO.ContentStream content, string? mime)
: this()
=> SetPost(post, mime);
=> SetPost(task, content, mime);

[Obsolete]
public HttpPost(string post, string? mime)
{
CompletePost = post ?? throw new ArgumentNullException(nameof(post));
SetPost(post, mime);
}

public override string ToString()
{
return $"{MimeType}: {Data}";
}

public void Dispose()
{
if (LazyData != null && LazyData.IsValueCreated)
{
Task.Run(async () =>
{
var data = await LazyData.Value.ConfigureAwait(false);
data.Dispose();
});
}
Content?.Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace MaxLib.WebServer
{
public static class HttpProtocollDefinition
public static class HttpProtocolDefinition
{
public const string HttpVersion1_0 = "HTTP/1.0";
public const string HttpVersion1_1 = "HTTP/1.1";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace MaxLib.WebServer
{
public static class HttpProtocollMethod
public static class HttpProtocolMethod
{
public const string Get = "GET";

Expand Down
Loading

0 comments on commit 34c8b87

Please sign in to comment.