Commit 1780d49c authored by Shani Elharrar's avatar Shani Elharrar

Added IHttpContext.

parent 425e8dfe
......@@ -16,6 +16,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Threading.Tasks;
using uhttpsharp;
......@@ -24,9 +25,11 @@ namespace uhttpsharpdemo.Handlers
public class AboutHandler : IHttpRequestHandler
{
public Task<IHttpResponse> Handle(IHttpRequest httpRequest, System.Func<Task<IHttpResponse>> next)
public Task Handle(IHttpContext context, Func<Task> next)
{
return Task.FromResult<IHttpResponse>(HttpResponse.CreateWithMessage(HttpResponseCode.Ok, "Sample http-request-handler"));
context.Response = HttpResponse.CreateWithMessage(HttpResponseCode.Ok, "Sample http-request-handler");
return Task.Factory.GetCompleted();
}
}
}
\ No newline at end of file
......@@ -23,9 +23,10 @@ namespace uhttpsharpdemo.Handlers
{
public class ErrorHandler : IHttpRequestHandler
{
public Task<IHttpResponse> Handle(IHttpRequest httpRequest, System.Func<System.Threading.Tasks.Task<IHttpResponse>> next)
public Task Handle(IHttpContext context, System.Func<Task> next)
{
return Task.FromResult<IHttpResponse>(new HttpResponse(HttpResponseCode.NotFound, "These are not the droids you are looking for."));
context.Response = new HttpResponse(HttpResponseCode.NotFound, "These are not the droids you are looking for.");
return Task.Factory.GetCompleted();
}
}
}
\ No newline at end of file
......@@ -17,6 +17,7 @@
*/
using System;
using System.Net.Cache;
using System.Text;
using System.Threading.Tasks;
using uhttpsharp;
......@@ -25,16 +26,18 @@ namespace uhttpsharpdemo.Handlers
{
public class IndexHandler : IHttpRequestHandler
{
private byte[] _contents;
private readonly HttpResponse _httpResponse;
public IndexHandler()
{
_contents = Encoding.UTF8.GetBytes("Welcome to the Index. ☺");
byte[] contents = Encoding.UTF8.GetBytes("Welcome to the Index. ☺");
_httpResponse = new HttpResponse(HttpResponseCode.Ok, contents);
}
public Task<IHttpResponse> Handle(IHttpRequest httpRequest, Func<Task<IHttpResponse>> next)
public Task Handle(IHttpContext context, Func<Task> next)
{
return Task.FromResult<IHttpResponse>(new HttpResponse(HttpResponseCode.Ok, _contents));
context.Response = _httpResponse;
return Task.Factory.GetCompleted();
}
}
}
\ No newline at end of file
......@@ -11,14 +11,13 @@ namespace uhttpsharpdemo.Handlers
{
private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public async Task<IHttpResponse> Handle(IHttpRequest httpRequest, Func<Task<IHttpResponse>> next)
public async Task Handle(IHttpContext context, Func<Task> next)
{
var stopWatch = Stopwatch.StartNew();
var retVal = await next();
await next();
Logger.InfoFormat("request {0} took {1}", httpRequest.Uri, stopWatch.Elapsed);
Logger.InfoFormat("request {0} took {1}", context.Request.Uri, stopWatch.Elapsed);
return retVal;
}
}
}
\ No newline at end of file
......@@ -51,17 +51,17 @@ namespace uhttpsharp.Handlers
return MimeTypes[extension];
return DefaultMimeType;
}
public async Task<IHttpResponse> Handle(IHttpRequest httpRequest, System.Func<Task<IHttpResponse>> next)
public async Task Handle(IHttpContext context, System.Func<Task> next)
{
var requestPath = httpRequest.Uri.OriginalString.TrimStart('/');
var requestPath = context.Request.Uri.OriginalString.TrimStart('/');
var httpRoot = Path.GetFullPath(HttpRootDirectory ?? ".");
var path = Path.GetFullPath(Path.Combine(httpRoot, requestPath));
if (!File.Exists(path))
return await next();
await next();
return new HttpResponse(GetContentType(path), File.OpenRead(path));
context.Response = new HttpResponse(GetContentType(path), File.OpenRead(path));
}
}
}
\ No newline at end of file
......@@ -33,19 +33,19 @@ namespace uhttpsharp.Handlers
return this;
}
public Task<IHttpResponse> Handle(IHttpRequest request, Func<Task<IHttpResponse>> nextHandler)
public Task Handle(IHttpContext context, Func<Task> nextHandler)
{
string function = string.Empty;
if (request.RequestParameters.Length > 0)
if (context.Request.RequestParameters.Length > 0)
{
function = request.RequestParameters[0];
function = context.Request.RequestParameters[0];
}
IHttpRequestHandler value;
if (_handlers.TryGetValue(function, out value))
{
return value.Handle(request, nextHandler);
return value.Handle(context, nextHandler);
}
......
......@@ -63,11 +63,15 @@ namespace uhttpsharp
if (request != null)
{
var context = new HttpContext(request);
Logger.InfoFormat("{1} : Got request {0}", request.Uri, _client.Client.RemoteEndPoint);
var getResponse = BuildHandlers(request)();
var getResponse = BuildHandlers(context)();
await getResponse.ConfigureAwait(false);
var response = await getResponse.ConfigureAwait(false);
var response = context.Response;
if (response != null)
{
......@@ -96,14 +100,35 @@ namespace uhttpsharp
}
private Func<Task<IHttpResponse>> BuildHandlers(IHttpRequest request, int index = 0)
private Func<Task> BuildHandlers(IHttpContext context, int index = 0)
{
if (index > _requestHandlers.Count)
{
return null;
}
return () => _requestHandlers[index].Handle(request, BuildHandlers(request, index + 1));
return () => _requestHandlers[index].Handle(context, BuildHandlers(context, index + 1));
}
}
internal class HttpContext : IHttpContext
{
private readonly IHttpRequest _request;
private IHttpResponse _response;
public HttpContext(IHttpRequest request)
{
_request = request;
}
public IHttpRequest Request
{
get { return _request; }
}
public IHttpResponse Response
{
get { return _response; }
set { _response = value; }
}
}
}
\ No newline at end of file
......@@ -23,6 +23,6 @@ namespace uhttpsharp
{
public interface IHttpRequestHandler
{
Task<IHttpResponse> Handle(IHttpRequest httpRequest, Func<Task<IHttpResponse>> next);
Task Handle(IHttpContext context, Func<Task> next);
}
}
\ No newline at end of file
......@@ -94,12 +94,13 @@ namespace uhttpsharp
using (var tempWriter = new StreamWriter(memoryStream))
{
WriteHeaders(tempWriter);
memoryStream.Position = 0;
await memoryStream.CopyToAsync(writer.BaseStream).ConfigureAwait(false);
}
ContentStream.Position = 0;
await ContentStream.CopyToAsync(writer.BaseStream).ConfigureAwait(false);
await writer.BaseStream.FlushAsync();
}
private void WriteHeaders(StreamWriter tempWriter)
......@@ -110,6 +111,7 @@ namespace uhttpsharp
tempWriter.WriteLine("Content-Type: {0}", ContentType);
tempWriter.WriteLine("Content-Length: {0}", ContentStream.Length);
tempWriter.WriteLine();
tempWriter.Flush();
}
}
}
\ No newline at end of file
......@@ -9,7 +9,7 @@ namespace uhttpsharp
public static class HttpServerExtensions
{
public static void Use(this HttpServer server, Func<IHttpRequest, Func<Task<IHttpResponse>>, Task<IHttpResponse>> method)
public static void Use(this HttpServer server, Func<IHttpContext, Func<Task>, Task> method)
{
server.Use(new AnonymousHttpRequestHandler(method));
}
......@@ -18,16 +18,17 @@ namespace uhttpsharp
public class AnonymousHttpRequestHandler : IHttpRequestHandler
{
private readonly Func<IHttpRequest, Func<Task<IHttpResponse>>, Task<IHttpResponse>> _method;
private readonly Func<IHttpContext, Func<Task>, Task> _method;
public AnonymousHttpRequestHandler(Func<IHttpRequest, Func<Task<IHttpResponse>>, Task<IHttpResponse>> method)
public AnonymousHttpRequestHandler(Func<IHttpContext, Func<Task>, Task> method)
{
_method = method;
}
public Task<IHttpResponse> Handle(IHttpRequest httpRequest, Func<Task<IHttpResponse>> next)
public Task Handle(IHttpContext context, Func<Task> next)
{
return _method(httpRequest, next);
return _method(context, next);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace uhttpsharp
{
public interface IHttpContext
{
IHttpRequest Request { get; }
IHttpResponse Response { get; set; }
}
}
......@@ -62,9 +62,11 @@
<Compile Include="HttpResponse.cs" />
<Compile Include="HttpServer.cs" />
<Compile Include="HttpServerExtensions.cs" />
<Compile Include="IHttpContext.cs" />
<Compile Include="IHttpMethodProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="HttpResponseCode.cs" />
<Compile Include="TaskFactoryExtensions.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment