Powershell on Rails -- MonadRail!
Okay -- i've been going on about powershell a lot lately. And you want to hear about other things. I can accept that. Even the people on the bus seem to pull faces when i start spontaneously talking about powershell. (An old lady on the bus this morning, for example, just didn't get it. --A universal parser! I said, but did she even smile? Not a smirk.) Well, the reason for the powershell obsession is that I'm currently reading "Powershell in Action" by Bruce Payette which is a cracker! It goes deep into the language, as deep as can be. And I've been thinking about using Powershell for writing websites. Rails-style, no less.
Conveniently, one of the examples Bruce provides is a webserver implemented using powershell (download source code from the Manning website and see 'Invoke-Webserver.ps1' from chapter 11)(or for a similar example, see Vivek Sharma or this very dangerous example from soapy frog )
What got me thinking about a rail-style powershell server were Two things. One: in response to the hanselminutes on monoRail, and two because Mike Schinkel mentioned the concept of a powershell web programming in his hunt for a new language.
Mike S:
"[Jeffrey Snover said] that PowerShell can do web, and will be able to do it more easily in the future. [Mike feels that] webified PowerShell should be a url-based object-selector-and-invoker like Django or Rudy on Rails."
Which is a nice idea. Seemingly, the major limitation with using Powershell as a webserver, at the moment, is our old friend threading. A powershell webserver is just a polling loop that handles all requests synchronously. Hence, every client has to wait in line to have their request processed. Not good. Ideally it would instead receive request-events and handle each of them on new threads. This (multi-threading) is promised as a future feature.
Anyway -- looking at Bruce's example of a webserver -- it would be pretty easy to see how it could be given rails-like behaviour.
Note that I do *NOT* have the time to be thinking about this... so i just wanted to sketch the basic idea here and see if anyone is interested in taking over.
Basically Bruce's script waits for get requests and then looks for simple mathematical expressions, such a "2+2" -- which is evaluates and sends back to the browser. This is the relevant bit of code:
$received = @($received -match "GET")[0]
if ($received)
{
$expression = $received -replace "GET */" -replace
'HTTP.*$' -replace '%20',' '
if ($expression -match '[0-9.]+ *[-+*/%] *[0-9.]+')
{
Well, instead of expecting a mathematical expression like that, we could look for a path that has:
Zero or more 'areas', followed by exactly one 'controller name', followed by exacly one 'action', optionally followed by one or more 'parameters'.
In honour of John Backus (r.i.p.), i'd love to express this in EBNF... but alas, the brain forgets.
Anyway, i'll just write it in DWIM: {/area{/area}}/controller/action{?paramname{=value}{+paramname{=value}}}
So once we've tokenized the path into areas, controller, action, params...
...we check that the relevant 'controller' exists in the specified 'area'.
Where a 'controller' is just a cmdLet or maybe a script, with specific parameters (action, params).
And an 'area' is just a sub-folder. Nothing more, nothing less.
We pass the action and the params to the relevant controller, and then.... either the controller takes over from there, or maybe it just returns some html for us to return to the client.
Go for it. You have one hour.
Oh, and the name I've got for this one is 'MonadRail' as a play on 'monorail' and 'monad' (the original code name for Powershell)
'Jeffrey Snover' on Sat, 24 Mar 2007 01:27:59 GMT, sez: Dude! You are going to absolutely freak out when you see what we are working on for the next version. Even that old lady on the bus is going to be talking about it! I'm bursting to give the details but can't. We'll probably have something public this fall.
Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
'Mitch Denny' on Sun, 25 Mar 2007 19:31:37 GMT, sez: I've been thinking along exactly the same lines :)
'Serge van den Oever [Macaw]' on Wed, 28 Mar 2007 21:31:35 GMT, sez: Same here! Would be great to have PowerShell as dynamic language for web site development. All this power at your fingertips... Rally looknig forward on what you are working on Jeffrey, any possibilities to get is the beta program for that?
'lb' on Tue, 10 Apr 2007 09:55:34 GMT, sez: even brucey's sample web-server is very dangerous -- it's open to 'powershell injection!'
i just used it to create a directory on my machine (an example of a dangerous side effect... lucky i didn't try delete...)
i passed this to it:
http://127.0.0.1/get-date;dir;md f;1*5
and it executed all the commands (not just the 1*5)....
to fix this i changed the line with regular expression in it from:
if ($expression -match '[0-9.]+ *[-+*/%] *[0-9.]+')
to:
if ($expression.trim() -match '^[0-9.]+ *[-+*/%] *[0-9.]+$')
(three changes: added the trim(), added a ^ to the front of the regex, added a '$' to the end of the regex.)
much safer ;-)
'Richard C Haven' on Tue, 10 Apr 2007 16:30:24 GMT, sez: Is this JAP (Just Another Perl) ?
And look what crimes against humanity people are doing with Perl...
'Mike Schinkel' on Wed, 02 May 2007 15:30:15 GMT, sez: Very cool. I attended a Powershell ISV training event in Redmond a few weeks ago, and now I'm reading Bruce's book. I am actually getting more and more psyched about Powershell, and am seriously considering working on adapting Powershell to the web. I'd probably focus on IIS though, as that's make it easier to host the app at a web hosting service.
'Mike Schinkel' on Wed, 02 May 2007 15:32:13 GMT, sez: BTW, why do you call the SoapyFrog example "dangerous?"
'lb' on Wed, 02 May 2007 22:37:18 GMT, sez: @Mike
soapy frog's example is potentially dangerous because if you posted it up on a public-facing internet site, then anyone could come along and use it to run a command such as Delete *.* on your server.
regarding "adapting Powershell to the web" i'm waiting to see what the powershell do next -- because i think they're gonna do the hard work for us.
'Mike Schinkel' on Thu, 03 May 2007 01:16:32 GMT, sez: lb> anyone could come along and use it to
lb> run a command such as Delete *.* on
lb> your server.
Ah...Doh! For some reason I asked that after reading several others and I got it confused with [1]. What do you think of [1] as a framework for calling "Powershell Server Pages" using the Powershell SDK? Would it scale? Bart wrote "Ignore the naive approach to threading in Main" but he's turned off comments so I can't easily ask him what he means.
And from your comments on [2]:
lb> i think that the powershell team are doing some tricky stuff
lb> for the next release to let powershell be webified more easily.
I asked Jeffrey when I was in Redmond a few weeks ago and he said it would be nice if someone did something Powershell for the web but they were instead focused on system managability for several years. :-(
lb> they ought to be involved in the DLR -- but weren't mentioned
lb> there as far as i could see.
DLR?
[1] http://community.bartdesmet.net/blogs/bart/archive/2007/02/22/httplistener-for-dummies-a-simple-http-request-reflector.aspx
[2] http://www.mikeschinkel.com/blog/commentview,guid,57a0f01e-167c-4d1d-8430-553e11cc79ef/
'Mike Schinkel' on Thu, 03 May 2007 01:17:19 GMT, sez: Oh, and...
lb> regarding "adapting Powershell to the
lb> web" i'm waiting to see what the
lb> powershell do next -- because i think
lb> they're gonna do the hard work for us.
What do you think is needed?
'wow gold' on Tue, 04 Mar 2008 13:00:08 GMT, sez: Cool, the post.
Thanks for the information.
|