Skip to content

Commit

Permalink
Release: 1.0.9 (#17)
Browse files Browse the repository at this point in the history
release: 1.0.9
  • Loading branch information
akon47 authored Sep 2, 2021
2 parents 3cd82c7 + 33b045f commit 42c6925
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 26 deletions.
2 changes: 1 addition & 1 deletion SimpleTcp/Server/BaseTcpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public IClient[] Clients
/// <param name="port">If you specify a valid port, the server starts immediately.</param>
public BaseTcpServer(int port = -1)
{
if (port > 0)
if (port >= 0)
{
Start(port);
}
Expand Down
77 changes: 73 additions & 4 deletions SimpleTcp/Server/Http/HttpResponse.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace SimpleTcp.Server.Http
Expand All @@ -13,17 +14,59 @@ public class HttpResponse : IHttpResponse
#region Properties
public int StatusCode { get; set; }
public string ReasonPhrase { get; set; }
public byte[] Content { get; set; }
public HttpHeaders Headers { get; private set; }

private byte[] _content;
public byte[] Content
{
get => _content;
set
{
if(_content == null && _contentStream == null )
{
_content = value;
}
else
{
throw new InvalidOperationException("content is already set");
}
}
}

public HttpHeaders Headers { get; private set; } = new HttpHeaders();
#endregion

#region Private Members
private Stream _contentStream;
private bool _leaveOpen;
#endregion

#region Public Methods
public HttpResponse()
{
Headers = new HttpHeaders();
_contentStream = null;
}

public HttpResponse(HttpStatusCode httpStatusCode) : this()
public HttpResponse(Stream contentStream, bool leaveOpen = false)
{
_contentStream = contentStream;
_leaveOpen = leaveOpen;
}

public HttpResponse(string filePath, string contentType) : this(new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
SetHttpStatusCode(HttpStatusCode.OK);
if(!string.IsNullOrWhiteSpace(contentType))
{
Headers["content-type"] = contentType;
}
}

public HttpResponse(HttpStatusCode httpStatusCode, Stream contentStream = null, bool leaveOpen = false) : this(contentStream, leaveOpen)
{
SetHttpStatusCode(httpStatusCode);
}

public void SetHttpStatusCode(HttpStatusCode httpStatusCode)
{
StatusCode = (int)httpStatusCode;
#region SetReasonPhrase
Expand Down Expand Up @@ -158,6 +201,32 @@ public HttpResponse(HttpStatusCode httpStatusCode) : this()
}
#endregion
}

public Stream GetContentStream()
{
if(_contentStream != null)
{
return _contentStream;
}
else if(Content != null)
{
_contentStream = new MemoryStream(Content);
_leaveOpen = false;
return _contentStream;
}
else
{
return null;
}
}

public void Dispose()
{
if(!_leaveOpen)
{
_contentStream?.Close();
}
}
#endregion
}

Expand Down
19 changes: 14 additions & 5 deletions SimpleTcp/Server/Http/HttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ protected override void OnDataReceived(IClient client, int receivedSize)
{
if(httpRequest.Process(client))
{
HttpResponse httpResponse = HttpRequest?.Invoke(this, new HttpRequestEventArgs(httpRequest));
IHttpResponse httpResponse = HttpRequest?.Invoke(this, new HttpRequestEventArgs(httpRequest));
if(httpResponse != null)
{
WriteHttpResponse(httpResponse, client);
httpResponse.Dispose();
}
client.Disconnect();
break;
Expand All @@ -85,21 +86,29 @@ protected override void OnClientDisconnected(IClient client)
#endregion

#region Private Methods
private void WriteHttpResponse(HttpResponse httpResponse, IClient client)
private void WriteHttpResponse(IHttpResponse httpResponse, IClient client)
{
if(!httpResponse.Headers.ContainsKey("content-type"))
{
httpResponse.Headers.Add("content-type", "text/html");
}
httpResponse.Headers["content-length"] = $"{httpResponse.Content?.Length ?? 0}";

Stream contentStream = httpResponse.GetContentStream();
httpResponse.Headers["content-length"] = $"{contentStream?.Length ?? 0}";

WriteText(client, $"HTTP/1.1 {httpResponse.StatusCode} {httpResponse.ReasonPhrase}\r\n");
WriteText(client, httpResponse.Headers.ToString());
WriteText(client, "\r\n\r\n"); // end

if(httpResponse.Content != null && httpResponse.Content.Length > 0)
if(contentStream?.Length > 0)
{
client.Write(httpResponse.Content, 0, httpResponse.Content.Length);
byte[] buffer = new byte[1024 * 4];

int readBytes;
while((readBytes = contentStream.Read(buffer, 0, buffer.Length)) > 0)
{
client.Write(buffer, 0, readBytes);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion SimpleTcp/Server/Http/HttpServerEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public HttpRequestEventArgs(IHttpRequest httpRequest)
Request = httpRequest;
}
}
public delegate HttpResponse HttpRequestEventHandler(object sender, HttpRequestEventArgs e);
public delegate IHttpResponse HttpRequestEventHandler(object sender, HttpRequestEventArgs e);
}
7 changes: 6 additions & 1 deletion SimpleTcp/Server/Http/IHttpResponse.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace SimpleTcp.Server.Http
{
public interface IHttpResponse
public interface IHttpResponse : IDisposable
{
int StatusCode { get; }
string ReasonPhrase { get; }
HttpHeaders Headers { get; }
Stream GetContentStream();
}
}
8 changes: 4 additions & 4 deletions SimpleTcp/SimpleTcp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
<PackageProjectUrl>https://github.com/akon47/SimpleTcp</PackageProjectUrl>
<RepositoryUrl>https://github.com/akon47/SimpleTcp</RepositoryUrl>
<PackageTags>SimpleTcp Tcp Client Server Socket</PackageTags>
<PackageReleaseNotes>Added Http Server function.</PackageReleaseNotes>
<AssemblyVersion>1.0.8.0</AssemblyVersion>
<FileVersion>1.0.8.0</FileVersion>
<Version>1.0.8</Version>
<PackageReleaseNotes>Improved the ability to return large files from Http servers.</PackageReleaseNotes>
<AssemblyVersion>1.0.9.0</AssemblyVersion>
<FileVersion>1.0.9.0</FileVersion>
<Version>1.0.9</Version>
<PackageIcon>logo.png</PackageIcon>
<PackageIconUrl />
<Authors>Kim Hwan</Authors>
Expand Down
6 changes: 3 additions & 3 deletions Tests/UnitTestProject/PacketTcpServerClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void ServerClientTest()
var packetTcpServer = new PacketTcpServer();
Assert.IsFalse(packetTcpServer.IsStarted);

packetTcpServer.Start(5005);
packetTcpServer.Start(0);
Assert.IsTrue(packetTcpServer.IsStarted, "PacketTcpServer is started by start function");

Assert.IsTrue(packetTcpServer.Clients.Length == 0);
Expand All @@ -26,7 +26,7 @@ public void ServerClientTest()
for(int i = 0; i < 25; i++)
{
var packetTcpClient = new PacketTcpClient();
packetTcpClient.Connect("localhost", 5005);
packetTcpClient.Connect("localhost", packetTcpServer.Port);
Assert.IsTrue(packetTcpClient.IsConnected);
packetTcpClients.Add(packetTcpClient);
}
Expand All @@ -37,7 +37,7 @@ public void ServerClientTest()

for (int i = 0; i < 25; i++)
{
var packetTcpClient = new PacketTcpClient("localhost", 5005);
var packetTcpClient = new PacketTcpClient("localhost", packetTcpServer.Port);
Assert.IsTrue(packetTcpClient.IsConnected);
packetTcpClients.Add(packetTcpClient);
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/UnitTestProject/PacketTcpServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void StartStopPacketTcpServer_StartWithStartFunctionAndStopWithStopFuncti
Assert.IsFalse(packetTcpServer.IsStarted);


packetTcpServer.Start(5000);
packetTcpServer.Start(0);
Assert.IsTrue(packetTcpServer.IsStarted, "PacketTcpServer is started by start function");

packetTcpServer.Stop();
Expand All @@ -26,7 +26,7 @@ public void StartStopPacketTcpServer_StartWithStartFunctionAndStopWithStopFuncti
[TestMethod]
public void StartStopPacketTcpServer_StartWithConstructorAndStopWithStopFunction()
{
var packetTcpServer = new RawTcpServer(5000);
var packetTcpServer = new RawTcpServer(0);
Assert.IsTrue(packetTcpServer.IsStarted, "PacketTcpServer is started by constructor");

packetTcpServer.Stop();
Expand Down
6 changes: 3 additions & 3 deletions Tests/UnitTestProject/RawTcpServerClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void ServerClientTest()
var rawTcpServer = new RawTcpServer();
Assert.IsFalse(rawTcpServer.IsStarted);

rawTcpServer.Start(5001);
rawTcpServer.Start(0);
Assert.IsTrue(rawTcpServer.IsStarted, "RawTcpServer is started by start function");

Assert.IsTrue(rawTcpServer.Clients.Length == 0);
Expand All @@ -26,7 +26,7 @@ public void ServerClientTest()
for(int i = 0; i < 25; i++)
{
var rawTcpClient = new RawTcpClient();
rawTcpClient.Connect("localhost", 5001);
rawTcpClient.Connect("localhost", rawTcpServer.Port);
Assert.IsTrue(rawTcpClient.IsConnected);
rawTcpClients.Add(rawTcpClient);
}
Expand All @@ -37,7 +37,7 @@ public void ServerClientTest()

for (int i = 0; i < 25; i++)
{
var rawTcpClient = new RawTcpClient("localhost", 5001);
var rawTcpClient = new RawTcpClient("localhost", rawTcpServer.Port);
Assert.IsTrue(rawTcpClient.IsConnected);
rawTcpClients.Add(rawTcpClient);
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/UnitTestProject/RawTcpServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void StartStopRawTcpServer_StartWithStartFunctionAndStopWithStopFunction(
Assert.IsFalse(rawTcpServer.IsStarted);


rawTcpServer.Start(5000);
rawTcpServer.Start(0);
Assert.IsTrue(rawTcpServer.IsStarted, "RawTcpServer is started by start function");

rawTcpServer.Stop();
Expand All @@ -26,7 +26,7 @@ public void StartStopRawTcpServer_StartWithStartFunctionAndStopWithStopFunction(
[TestMethod]
public void StartStopRawTcpServer_StartWithConstructorAndStopWithStopFunction()
{
var rawTcpServer = new RawTcpServer(5000);
var rawTcpServer = new RawTcpServer(0);
Assert.IsTrue(rawTcpServer.IsStarted, "RawTcpServer is started by constructor");

rawTcpServer.Stop();
Expand Down

0 comments on commit 42c6925

Please sign in to comment.