Posts
47
Comments
55
Trackbacks
0
Serving up pages, MSH style
Well, blame this one on extreme boredom. I wish I could post all my Exchange 12 scripts… but now is apparently not the time. So I’ll post some of my Monad only tricks for now. If you think this is crazy / cool / useful / interesting… wait till we show you what you can do in Exchange using Monad. Stay tuned. The caveat on this script is: make sure to run this in a seperate MSH instance. Once you’re done serving up pages, close that instance. That way there isn’t a process thread lying around locking port 80. If you wrote an exe you could simply hit CTRL-C to make the listener thread go away, but in script, MSH.exe is hosting the thread, so it needs to go away for the port to be unblocked.
 
param($port=80)   
$webroot = “x:”
[reflection.Assembly]::LoadWithPartialName(“System.Net.Sockets”)  [reflection.Assembly]::LoadWithPartialName(“System.Text”
function SendResponse([string]$string, [net.sockets.socket]$sock) 
{
     if ($sock.Connected)
     {
         $bytesSent = $sock.Send( [text.Encoding]::Ascii.GetBytes($string)
     )
     if ( $bytesSent -eq -1 )       
     {
             “LOG: Send failed to ” + $sock.RemoteEndPoint
     } else
     {
             “LOG: Sent $bytesSent bytes to ” + $sock.RemoteEndPoint
     }
     }
 }
function SendHeader($httpVersion, $mimeHeader, $statusCode, [net.sockets.socket] $sock, $bytes)
{
     $response = ”$httpVersion $statusCode\r\nServer: Localhost\r\nContent-Type: $mimeHeader\r\nAccept-Ranges: bytes\r\nContent-Length: \r\n\r\n” 
    SendResponse $response $sock       “Header sent” 
}
$listener = [System.Net.Sockets.TcpListener]$port
$listener.Start()
$buffer = new-object byte[] 1024 
while($true)
{
     if($listener.Pending())
     {
             [net.sockets.socket] $socket = $listener.AcceptSocket()
     }
     if ( $socket.Connected )
     {
             “Connection at {0} from {1}.” -f (get-date), $socket.RemoteEndPoint         
             $bytesRecv = $socket.Receive($buffer, $buffer.Length, “0″)          
             $inStr = [Text.encoding]::Ascii.getString($buffer)
             if ($instr.SubString(0,3) -eq “GET”)
             {
             if ($inStr -match “(HTTP/.+)”)              
             {
                 $httpVersion = $matches[0]
             }
             if ($inStr -match “(/.*) ”)
             {
                 $outStr = get-childitem ($webroot + ($matches[0] - replace “%20″,“ ”)) | convert-html | out-string
                 $outStr.Length
                 SendHeader $httpVersion “text/html” “200 OK” $socket ($str.Length)                  SendResponse $outStr $socket
             }
      }
      $socket.Close()
} }
posted on Thursday, December 08, 2005 12:12 PM Print
Comments
No comments posted yet.

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 2 and 7 and type the answer here:
News
A little slow these days as I'm busy working on exchangelabs.com. I will try and post tidbits when I get some time. Enjoy the older posts till then!