Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
/packages/*
*/bin/*
*/obj/*
*.psess
*.vsp
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Usage :

More modifications will be made to make it more "user friendly" out of the box :

* [RESTful](http://en.wikipedia.org/wiki/Representational_state_transfer) controllers
* ~~[RESTful](http://en.wikipedia.org/wiki/Representational_state_transfer) controllers~~ (Done)
* Caching support
* NuGet package
* ~~Ssl Support~~ (Done!)
Expand Down
25 changes: 25 additions & 0 deletions uhttpsharp-demo/Handlers/ExceptionHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Threading.Tasks;
using uhttpsharp;

namespace uhttpsharpdemo.Handlers
{
public class ExceptionHandler : IHttpRequestHandler
{
public async Task Handle(IHttpContext context, Func<Task> next)
{
try
{
await next();
}
catch (HttpException e)
{
context.Response = new HttpResponse(e.ResponseCode, "Error while handling your request. " + e.Message, false);
}
catch (Exception e)
{
context.Response = new HttpResponse(HttpResponseCode.InternalServerError, "Error while handling your request. " + e, false);
}
}
}
}
24 changes: 24 additions & 0 deletions uhttpsharp-demo/HttpException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using uhttpsharp;

namespace uhttpsharpdemo
{
public class HttpException : Exception
{
private readonly HttpResponseCode _responseCode;

public HttpResponseCode ResponseCode
{
get { return _responseCode; }
}

public HttpException(HttpResponseCode responseCode)
{
_responseCode = responseCode;
}
public HttpException(HttpResponseCode responseCode, string message) : base(message)
{
_responseCode = responseCode;
}
}
}
8 changes: 5 additions & 3 deletions uhttpsharp-demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@ private static void Main()
{
log4net.Config.XmlConfigurator.Configure();

var serverCertificate = X509Certificate.CreateFromCertFile(@"TempCert.cer");
//var serverCertificate = X509Certificate.CreateFromCertFile(@"TempCert.cer");

using (var httpServer = new HttpServer(new HttpRequestProvider()))
{
httpServer.Use(new TcpListenerAdapter(new TcpListener(IPAddress.Loopback, 80)));
httpServer.Use(new ListenerSslDecorator(new TcpListenerAdapter(new TcpListener(IPAddress.Loopback, 443)), serverCertificate));
//httpServer.Use(new ListenerSslDecorator(new TcpListenerAdapter(new TcpListener(IPAddress.Loopback, 443)), serverCertificate));

httpServer.Use(new ExceptionHandler());
httpServer.Use(new TimingHandler());

httpServer.Use(new HttpRouter().With(string.Empty, new IndexHandler())
.With("about", new AboutHandler()));
.With("about", new AboutHandler())
.With("strings", new RestHandler<string>(new StringsRestController(), new JsonResponseProvider())));

httpServer.Use(new FileHandler());
httpServer.Use(new ErrorHandler());
Expand Down
47 changes: 47 additions & 0 deletions uhttpsharp-demo/SomeRestController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using uhttpsharp;

namespace uhttpsharpdemo
{
class SomeRestController
{

IDictionary<int, string> _strings = new Dictionary<int, string>() { { 1 , "Hahaha"}};

public Task<uhttpsharp.HttpResponse> Get(uhttpsharp.IHttpRequest request)
{
var memoryStream = new MemoryStream();
Newtonsoft.Json.JsonWriter writer = new JsonTextWriter(new StreamWriter( memoryStream));

JsonSerializer.Create().Serialize(writer, _strings);
writer.Flush();
return Task.FromResult(new HttpResponse(HttpResponseCode.Ok, "application/json; charset=utf-8", memoryStream, true));
}

public Task<uhttpsharp.HttpResponse> GetItem(uhttpsharp.IHttpRequest request)
{
throw new NotImplementedException();
}

public Task<uhttpsharp.HttpResponse> Create(uhttpsharp.IHttpRequest request)
{
throw new NotImplementedException();
}

public Task<uhttpsharp.HttpResponse> Upsert(uhttpsharp.IHttpRequest request)
{
throw new NotImplementedException();
}

public Task<uhttpsharp.HttpResponse> Delete(uhttpsharp.IHttpRequest request)
{
throw new NotImplementedException();
}
}
}
64 changes: 64 additions & 0 deletions uhttpsharp-demo/StringsRestController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using uhttpsharp;
using uhttpsharp.Handlers;

namespace uhttpsharpdemo
{
class StringsRestController : IRestController<string>
{
private readonly ICollection<string> _collection = new HashSet<string>();

public Task<IEnumerable<string>> Get(IHttpRequest request)
{
return Task.FromResult<IEnumerable<string>>(_collection);
}
public Task<string> GetItem(IHttpRequest request)
{
var id = GetId(request);

if (_collection.Contains(id))
{
return Task.FromResult(id);
}

throw GetNotFoundException();
}
private static string GetId(IHttpRequest request)
{
var id = request.RequestParameters[1];

return id;
}
public Task<string> Create(IHttpRequest request)
{
var id = GetId(request);

_collection.Add(id);

return Task.FromResult(id);
}
public Task<string> Upsert(IHttpRequest request)
{
return Create(request);
}
public Task<string> Delete(IHttpRequest request)
{
var id = GetId(request);

if (_collection.Remove(id))
{
return Task.FromResult(id);
}

throw GetNotFoundException();
}
private static Exception GetNotFoundException()
{
return new HttpException(HttpResponseCode.NotFound, "The resource you've looked for is not found");
}
}
}
1 change: 1 addition & 0 deletions uhttpsharp-demo/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="log4net" version="2.0.3" targetFramework="net45" />
<package id="Newtonsoft.Json" version="5.0.8" targetFramework="net45" />
</packages>
8 changes: 8 additions & 0 deletions uhttpsharp-demo/uhttpsharp.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@
<Reference Include="log4net">
<HintPath>..\packages\log4net.2.0.3\lib\net40-full\log4net.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.5.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -56,10 +60,14 @@
</Compile>
<Compile Include="Handlers\AboutHandler.cs" />
<Compile Include="Handlers\ErrorHandler.cs" />
<Compile Include="Handlers\ExceptionHandler.cs" />
<Compile Include="Handlers\IndexHandler.cs" />
<Compile Include="HttpException.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Handlers\TimingHandler.cs" />
<Compile Include="SomeRestController.cs" />
<Compile Include="StringsRestController.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\uhttpsharp\uhttpsharp.csproj">
Expand Down
3 changes: 1 addition & 2 deletions uhttpsharp/Handlers/FileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ public async Task Handle(IHttpContext context, System.Func<Task> next)

if (!File.Exists(path))
{
await next();
await next().ConfigureAwait(false);

return;
}


context.Response = new HttpResponse(GetContentType(path), File.OpenRead(path), context.Request.Headers.KeepAliveConnection());
}
}
Expand Down
11 changes: 11 additions & 0 deletions uhttpsharp/Handlers/IResponseProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading.Tasks;

namespace uhttpsharp.Handlers
{
public interface IResponseProvider
{

Task<IHttpResponse> Provide(object value);

}
}
43 changes: 43 additions & 0 deletions uhttpsharp/Handlers/IRestController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace uhttpsharp.Handlers
{
public interface IRestController<T>
{
/// <summary>
/// Returns a list of object that found in the collection
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
Task<IEnumerable<T>> Get(IHttpRequest request);

/// <summary>
/// Returns an item from the collection
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
Task<T> GetItem(IHttpRequest request);

/// <summary>
/// Creates a new entry in the collection - new uri is returned
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
Task<T> Create(IHttpRequest request);

/// <summary>
/// Updates an entry in the collection
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
Task<T> Upsert(IHttpRequest request);

/// <summary>
/// Removes an entry from the collection
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
Task<T> Delete(IHttpRequest request);
}
}
19 changes: 19 additions & 0 deletions uhttpsharp/Handlers/JsonResponseProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace uhttpsharp.Handlers
{
public class JsonResponseProvider : IResponseProvider
{
public Task<IHttpResponse> Provide(object value)
{
var memoryStream = new MemoryStream();
var writer = new JsonTextWriter(new StreamWriter(memoryStream));
var serializer = new JsonSerializer();
serializer.Serialize(writer, value);
writer.Flush();
return Task.FromResult<IHttpResponse>(new HttpResponse(HttpResponseCode.Ok, "application/json; charset=utf-8", memoryStream, true));
}
}
}
Loading