Created
May 17, 2013 03:30
-
-
Save jsakamoto/5596729 to your computer and use it in GitHub Desktop.
Small http daemon by F# script. run "> fsi.exe httpd.fsx", then you can access http://localhost:8080/anycontents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open System.IO | |
open System.Net | |
let root = @"c:\inetpub\wwwroot" | |
let listener = new HttpListener() | |
listener.Prefixes.Add "http://*:8080/" | |
listener.Start () | |
let rec procreq ():(unit->unit) = | |
let context = listener.GetContext () | |
let req = context.Request | |
let res = context.Response | |
let path = Path.Combine (root, (req.RawUrl.TrimStart('/').Replace("/","\\"))) | |
match (File.Exists path) with | |
| true -> path |> File.ReadAllBytes |> fun content -> res.OutputStream.Write(content, 0, content.Length) | |
| false -> res.StatusCode <- 404 | |
res.Close () | |
procreq () | |
procreq () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment