Example #1
0
        public override void handle(Request req)
        {
            FileStream fs;
            string     uri = "";

            byte[] buffer = new byte[BUFFER_SIZE];


            try {
                // get file
                if (isFile)
                {
                    fs  = new FileStream(rootPath, FileMode.Open);
                    uri = rootPath;
                }
                else
                {
                    uri = (prefix != "") ? req.URL.Substring(prefix.Length): req.URI;
                    uri = WebUtility.UrlDecode(uri);
                    uri = uri.Replace('/', '\\');
                    Console.WriteLine("PATH: {0}", rootPath + uri);
                    if (File.Exists(rootPath + uri))
                    {
                        fs = new FileStream(rootPath + uri, FileMode.Open);
                    }
                    else
                    {
                        Console.WriteLine("Error: {0}", rootPath + uri);
                        req.StatusCode        = 400;
                        req.StatusDescription = "File " + uri + " not found.";
                        req.ContentType       = MIME.TXT;
                        req.WriteString("400 File " + uri + " not found.");
                        return;
                    }
                }
                // write header
                //req.AcceptRange = true;
                req.ContentType       = MIME.GetTypeFromFilen(uri);
                req.SendChunked       = true;
                req.StatusCode        = 200;
                req.StatusDescription = "OK";
                int len = (int)fs.Length;
                req.ContentLength = len;
                int start = req.Range();
                fs.Seek(start, SeekOrigin.Begin);
                // write data
                int i;
                while (len > 0)
                {
                    i = fs.Read(buffer, 0, BUFFER_SIZE);
                    req.Write(buffer, i);
                    len -= i;
                }
                fs.Close();
            } catch (Exception e) {
                Console.WriteLine(e);
                req.StatusCode        = 400;
                req.StatusDescription = "File " + uri + " not found.";
            }
        }
Example #2
0
 // use file input foe a file
 public Template fromFile(string name, string filen)
 {
     try {
         return(fromString(name, MIME.GetTypeFromFilen(filen), File.ReadAllText(filen)));
     } catch (Exception e)    {
         Console.WriteLine("ERROR: {0}.", e.ToString());
     }
     return(null);
 }