Skip to content

Commit

Permalink
Handlers introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayIT committed Oct 13, 2015
1 parent 833e313 commit a755175
Show file tree
Hide file tree
Showing 14 changed files with 375 additions and 24 deletions.
226 changes: 226 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/

# Visual Studio 2015 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/

# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*

# NUNIT
*.VisualState.xml
TestResult.xml

# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c

# DNX
project.lock.json
artifacts/

*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc

# Chutzpah Test files
_Chutzpah*

# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf
*.cachefile

# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap

# TFS 2012 Local Workspace
$tf/

# Guidance Automation Toolkit
*.gpState

# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user

# JustCode is a .NET coding add-in
.JustCode

# TeamCity is a build add-in
_TeamCity*

# DotCover is a Code Coverage Tool
*.dotCover

# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*

# MightyMoose
*.mm.*
AutoTest.Net/

# Web workbench (sass)
.sass-cache/

# Installshield output folder
[Ee]xpress/

# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html

# Click-Once directory
publish/

# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj

# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config

# Windows Azure Build Output
csx/
*.build.csdef

# Windows Store app package directory
AppPackages/

# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/

# Others
ClientBin/
[Ss]tyle[Cc]op.*
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.pfx
*.publishsettings
node_modules/
orleans.codegen.cs

# RIA/Silverlight projects
Generated_Code/

# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm

# SQL Server files
*.mdf
*.ldf

# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings

# Microsoft Fakes
FakesAssemblies/

# GhostDoc plugin setting file
*.GhostDoc.xml

# Node.js Tools for Visual Studio
.ntvs_analysis.dat

# Visual Studio 6 build log
*.plg

# Visual Studio 6 workspace options file
*.opt

# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions

# Paket dependency manager
.paket/paket.exe

# FAKE - F# Make
.fake/
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Models\" />
<Folder Include="Views\" />
</ItemGroup>
<ItemGroup>
<Content Include="Files\data.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
4 changes: 4 additions & 0 deletions ConsoleWebServer/ConsoleWebServer.Application/Files/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Za tebe imam
fail pulen s taini
Ela i si kopni
Ela i si kopni
32 changes: 15 additions & 17 deletions ConsoleWebServer/ConsoleWebServer.Application/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;

using ConsoleWebServer.Framework;
using ConsoleWebServer.Framework.Handlers;

public static class Startup
{
Expand All @@ -17,7 +18,8 @@ public static void Main()
var inputLine = Console.ReadLine();
if (string.IsNullOrWhiteSpace(inputLine))
{
HandleRequest(requestBuilder.ToString());
var response = GetResponse(requestBuilder.ToString());
Console.WriteLine(response);
requestBuilder.Clear();
continue;
}
Expand All @@ -26,30 +28,26 @@ public static void Main()
}
}

private static void HandleRequest(string requestAsString)
private static HttpResponse GetResponse(string requestAsString)
{
var requestParser = new RequestParser();
var request = requestParser.Parse(requestAsString);
var fileHandler = new StaticFileHandler();
var controllerHandler = new ControllerHandler();

HttpResponse response;
fileHandler.SetSuccessor(controllerHandler);

HttpRequest request;
try
{
var controllerFactory = new ControllerFactory();
var controller = controllerFactory.CreateController(request);
var actionInvoker = new ActionInvoker();
var actionResult = actionInvoker.InvokeAction(controller, request.Action);
response = actionResult.GetResponse();
}
catch (HttpNotFoundException exception)
{
response = new HttpResponse(request.ProtocolVersion, HttpStatusCode.NotFound, exception.Message);
var requestParser = new RequestParser();
request = requestParser.Parse(requestAsString);
}
catch (Exception exception)
catch (Exception ex)
{
response = new HttpResponse(request.ProtocolVersion, HttpStatusCode.InternalServerError, exception.Message);
return new HttpResponse(new Version(1, 1), HttpStatusCode.BadRequest, ex.Message);
}

Console.WriteLine(response.ToString());
var response = fileHandler.HandleRequest(request);
return response;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@
<Compile Include="ActionInvoker.cs" />
<Compile Include="ActionResults\ViewActionResult.cs" />
<Compile Include="ControllerFactory.cs" />
<Compile Include="Handlers\ControllerHandler.cs" />
<Compile Include="Handlers\Handler.cs" />
<Compile Include="Handlers\StaticFileHandler.cs" />
<Compile Include="HttpMessage.cs" />
<Compile Include="HttpNotFoundException.cs" />
<Compile Include="HttpRequest.cs" />
<Compile Include="Controller.cs" />
<Compile Include="HttpResponse.cs" />
<Compile Include="ActionResults\IActionResult.cs" />
<Compile Include="ParserException.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RequestParser.cs" />
<Compile Include="ActionDescriptor.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ControllerFactory
public Controller CreateController(HttpRequest request)
{
var controllerClassName = request.Action.ControllerName + "Controller";
var type = Assembly.GetCallingAssembly().GetTypes().FirstOrDefault(x => x.Name == controllerClassName);
var type = Assembly.GetEntryAssembly().GetTypes().FirstOrDefault(x => x.Name == controllerClassName);
if (type == null || !typeof(Controller).IsAssignableFrom(type))
{
throw new HttpNotFoundException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace ConsoleWebServer.Framework.Handlers
{
using System;
using System.Net;

public class ControllerHandler : Handler
{
protected override bool CanHandle(HttpRequest request)
{
return request.ProtocolVersion.Major < 3;
}

protected override HttpResponse Handle(HttpRequest request)
{
HttpResponse response;
try
{
var controllerFactory = new ControllerFactory();
var controller = controllerFactory.CreateController(request);
var actionInvoker = new ActionInvoker();
var actionResult = actionInvoker.InvokeAction(controller, request.Action);
response = actionResult.GetResponse();
}
catch (HttpNotFoundException exception)
{
response = new HttpResponse(request.ProtocolVersion, HttpStatusCode.NotFound, exception.Message);
}
catch (Exception exception)
{
response = new HttpResponse(request.ProtocolVersion, HttpStatusCode.InternalServerError, exception.Message);
}

return response;
}
}
}
Loading

0 comments on commit a755175

Please sign in to comment.