Skip to content

Commit

Permalink
initial commit of DoD
Browse files Browse the repository at this point in the history
  • Loading branch information
secretgeek committed Aug 27, 2010
0 parents commit d983fc2
Show file tree
Hide file tree
Showing 21 changed files with 1,006 additions and 0 deletions.
Binary file added Content/Logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions Controllers/Home/Index.bat
@@ -0,0 +1,33 @@
@echo off
call ..\..\Views\Shared\_header DoD Congratulations
call ..\..\h\h1 Congratulations
echo [p]You've put [strong]DOS on DOPE[/strong][/p]
echo.
echo [p]Available controllers...[/p]
echo [blockquote]
echo [ul]
pushd %cd%
cd ..
FOR /F "delims=~" %%A IN (' DIR /B/a:d') DO (
echo [li][a href='%%A/']%%A[/a][/li]
)
echo [/ul]
echo [/blockquote]
echo [p]To Add a controller, navigate to:[/p]
echo [blockquote][pre]%cd%] [/pre][/blockquote]
popd

echo [p]and type:[/p]

echo [blockquote][pre]Dope Controller [em]ControllerName[/em][/pre][/blockquote]
echo.
echo [p]To change this default controller, edit:[/p]
echo [blockquote][pre]"%cd%\Index.bat"[/pre][/blockquote]

echo [p]To create new actions, add batchfiles to this folder, e.g. [/p]
echo [blockquote][pre]%cd%]Copy con About.bat
echo echo [[p]]Hello world![[/p]]
echo [[ctrl-Z]][/pre][/blockquote]


::echo [blockquote][pre]"C:\Temp\Blog\Controllers\Home\Index.bat"[/pre][/blockquote]
25 changes: 25 additions & 0 deletions Controllers/_actionMissing.bat
@@ -0,0 +1,25 @@
@echo off
call ..\Views\Shared\_header Action Missing
call ..\h\h1 Action Missing
echo [p]Controller = %1[/p]
echo [p]Action = %2[/p]
echo [p]Parameters = %*[/p]
echo [p]Available Actions to GET for this controller...[p]
echo [blockquote][pre]
echo [OL]
FOR /F "delims=~" %%A IN (' DIR %1\*.bat /B/a:-d') DO (
echo [A href='%%~nA']%%~nA[/A]
)
echo [/OL]
echo [/blockquote][/pre]

echo [p]Available Actions to POST for this controller...[p]
echo [blockquote][pre]
echo [OL]
FOR /F "delims=~" %%A IN (' DIR %1\*.cmd /B/a:-d') DO (
echo [A href='%%~nA']%%~nA[/A]
)
echo [/OL]
echo [/blockquote][/pre]

exit 44
16 changes: 16 additions & 0 deletions Controllers/_controllerMissing.bat
@@ -0,0 +1,16 @@
@echo off
call ..\Views\Shared\_header Controller Missing
call ..\h\h1 Controller Missing
echo [p]Controller = %1[/p]
echo [p]Action = %2[/p]
echo [p]Parameters = %*[/p]
echo [p]Available controllers...[/p]
echo [blockquote][pre]
echo [OL]
FOR /F "delims=~" %%A IN (' DIR /B/a:d') DO (
echo [A href='%%A/']%%A[/A]
)
echo [/OL]
echo [/blockquote][/pre]

exit 44
3 changes: 3 additions & 0 deletions Default.aspx
@@ -0,0 +1,3 @@
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DosOnDope._Default" %>

<%-- Please do not delete this file. It is used to ensure that ASP.NET MVC is activated by IIS when a user makes a "/" request to the server. --%>
16 changes: 16 additions & 0 deletions Default.aspx.cs
@@ -0,0 +1,16 @@
using System.Web;
using System.Web.Mvc;
using System.Web.UI;

namespace DosOnDope
{
public partial class _Default : Page
{
public void Page_Load(object sender, System.EventArgs e)
{
HttpContext.Current.RewritePath(Request.ApplicationPath, false);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);
}
}
}
113 changes: 113 additions & 0 deletions DosOnDope.csproj
@@ -0,0 +1,113 @@
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{80FA3DFC-363C-4B35-AAA5-0FD3C17B8ECB}</ProjectGuid>
<ProjectTypeGuids>{603c0e0b-db56-11dc-be95-000d561079b0};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DosOnDope</RootNamespace>
<AssemblyName>DosOnDope</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<MvcBuildViews>false</MvcBuildViews>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Web.Abstractions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\Program Files\Microsoft ASP.NET\ASP.NET MVC RC\Assemblies\System.Web.Routing.dll</HintPath>
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Web.Services" />
</ItemGroup>
<ItemGroup>
<Compile Include="Default.aspx.cs">
<DependentUpon>Default.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
</Compile>
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Content\Logo.png" />
<Content Include="Default.aspx" />
<Content Include="favicon.ico" />
<Content Include="Global.asax" />
<Content Include="Web.config" />
</ItemGroup>
<ItemGroup>
<None Include="Controllers\Blog\add.bat" />
<None Include="Controllers\Blog\add.cmd" />
<None Include="Controllers\Blog\Index.bat" />
<None Include="Controllers\Blog\view.bat" />
<Content Include="Controllers\Home\Index.bat" />
<Content Include="Controllers\_actionMissing.bat" />
<Content Include="Controllers\_controllerMissing.bat" />
<Content Include="dope.bat" />
<Content Include="h\a.bat" />
<Content Include="h\h1.bat" />
<Content Include="h\h2.bat" />
<Content Include="h\h3.bat" />
<Content Include="h\p.bat" />
<None Include="Models\Posts.csv" />
<Content Include="Models\split.vbs" />
<Content Include="Views\Shared\_header.bat" />
</ItemGroup>
<ItemGroup>
<Folder Include="Views\Home\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.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.
<Target Name="BeforeBuild">
</Target> -->
<Target Name="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)\..\$(ProjectName)" />
</Target>
<ProjectExtensions>
<VisualStudio>
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
<WebProjectProperties>
<UseIIS>False</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>13174</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>
</IISUrl>
<NTLMAuthentication>False</NTLMAuthentication>
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
</WebProjectProperties>
</FlavorProperties>
</VisualStudio>
</ProjectExtensions>
</Project>
1 change: 1 addition & 0 deletions Global.asax
@@ -0,0 +1 @@
<%@ Application Codebehind="Global.asax.cs" Inherits="DosOnDope.MvcApplication" Language="C#" %>
177 changes: 177 additions & 0 deletions Global.asax.cs
@@ -0,0 +1,177 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace DosOnDope
{
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("favicon.ico");
routes.IgnoreRoute("Content/{*pathInfo}");

routes.Add(new Route("{controller}/{action}/{*id}", new DosOnDopeRouteHandler())
{
Defaults = new RouteValueDictionary(new
{
controller = "Home",
action = "Index",
id = ""
})
});
}

protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}

public class DosOnDopeRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new DosOnDopeHttpHandler(requestContext);
}
}

public class DosOnDopeHttpHandler : IHttpHandler
{
public RequestContext requestContext { get; set; }
public DosOnDopeHttpHandler(RequestContext requestContext)
{
this.requestContext = requestContext;
}

public bool IsReusable {get { return false; } }

public void ProcessRequest(HttpContext context)
{
var controller = this.requestContext.RouteData.Values["controller"] as string;
var action = this.requestContext.RouteData.Values["action"] as string;
string[] args = { this.requestContext.RouteData.Values["id"] as string };
int httpStatus;
if (context.Request.RequestType == "POST")
{
var result = Execute(controller, action, context.Request.Form.ToString().Split('&'), out httpStatus, true);
if (httpStatus == 301)
context.Response.Redirect(result.Trim("\r\n".ToCharArray()));
else
context.Response.Write(result);
}
else
{
context.Response.Write(Execute(controller, action, args, out httpStatus, false ));
}
context.Response.StatusCode = httpStatus;
}

public string Execute(string controllerName, string actionName, string[] newArgs, out int httpStatus, bool isPost)
{
var controllerPath = Path.Combine(requestContext.HttpContext.Server.MapPath("~/Controllers"), controllerName);

if (!Directory.Exists(controllerPath))
{
return ControllerNotFound(controllerName, actionName, newArgs, out httpStatus, isPost);
}

var actionFileName = Path.Combine(controllerPath, actionName + ".bat");

if (!File.Exists(actionFileName))
{
return ActionNotFound(controllerName, actionName, newArgs, out httpStatus, isPost);
}

return Execute(controllerPath, actionName, Join(" ", newArgs), out httpStatus, isPost);
}

private string Execute(string path, string file, string args, out int httpStatus, bool isPost)
{
var errorLevel = 50;
var result = string.Empty;

using (var proc = new Process())
{
proc.EnableRaisingEvents = false;

proc.StartInfo.FileName = Path.Combine(path, file + (isPost? ".cmd" : ".bat"));
if (args != null)
{
proc.StartInfo.Arguments = args;
}
proc.StartInfo.WorkingDirectory = path;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;// false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
result = Dope(output);
proc.WaitForExit();
errorLevel = proc.ExitCode;
}
httpStatus = ConvertErrorlevelToHttpStatus(errorLevel);
return result;
}

private string ActionNotFound(string controllerName, string actionName, string[] newArgs, out int httpStatus, bool isPost)
{
var controllerPath = Path.Combine(requestContext.HttpContext.Server.MapPath("~/Controllers"), controllerName);

if (!File.Exists(Path.Combine(controllerPath, "_actionMissing" + ".bat")))
{
controllerPath = requestContext.HttpContext.Server.MapPath("~/Controllers");
}

return Execute(controllerPath, "_ActionMissing", controllerName + " " + actionName + " " + Join(" ", newArgs), out httpStatus, false);
}

private string ControllerNotFound(string controllerName, string actionName, string[] newArgs, out int httpStatus, bool isPost)
{
var controllerPath = requestContext.HttpContext.Server.MapPath("~/Controllers");
return Execute(controllerPath, "_ControllerMissing", controllerName + " " + actionName + " " + Join(" ", newArgs), out httpStatus, false);
}


private int ConvertErrorlevelToHttpStatus(int errorLevel)
{
if (errorLevel == 0) return 200;
return ((errorLevel / 10) * 100) + (errorLevel % 10);
}


private string Join(string separator, params string[] strings)
{
if (strings == null) return string.Empty;
return string.Join(separator, strings);
}

/// <summary>
/// Dopes the input.
/// i.e:
/// Turns square brackets into angle brackets
/// Turns pluses into spaces.
/// Escapes angle brackets
/// Turns escaped square brackets into regular square brackets
/// Turns url encoded CR/LF into spaces
/// </summary>
/// <param name="undoped">The undoped.</param>
/// <returns></returns>
private string Dope(string undoped)
{
if (undoped.IndexOf("[[") >= 0 || undoped.IndexOf("]]") >= 0)
{
var gOpen = Guid.NewGuid().ToString();
var gClose = Guid.NewGuid().ToString();
undoped = undoped.Replace("[[", gOpen).Replace("]]", gClose);
undoped = Dope(undoped);
return undoped.Replace(gOpen, "[").Replace(gClose, "]");
}
return undoped.Replace("<","&lt").Replace(">","&gt;").Replace("[", "<").Replace("]", ">").Replace("+"," ").Replace("%0d"," ").Replace("%0a"," ");
}
}
}

0 comments on commit d983fc2

Please sign in to comment.