Example #1
0
        public override void handle(Request req)
        {
            //get handler
            Handler h    = null;
            string  path = req.URL;

            foreach (var pair in routes)
            {
                if (Match(pair.Key, path))
                {
                    h = pair.Value;
                    break;
                }
            }

            if (h == null)
            {
                req.StatusCode = 400;
                byte[] buf = Encoding.UTF8.GetBytes("Bad Request.");
                req.ContentLength = buf.Length;
                req.Write(buf, buf.Length);
                return;
            }
            h.handle(req);
        }
Example #2
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.";
            }
        }