Example #1
0
        bool TryProcessAssemblyRequest(HttpSocket s, string uri)
        {
            if (Chiron.UrlPrefix == "")
            {
                return(false);
            }

            int slash = uri.LastIndexOf('/');

            if (slash == -1)
            {
                return(false);
            }

            // must start with URL prefix
            if (string.Compare(uri.Substring(0, slash + 1), Chiron.UrlPrefix, StringComparison.OrdinalIgnoreCase) != 0)
            {
                return(false);
            }

            uri = uri.Substring(slash + 1);

            // must end with DLL or PDB
            if (string.Compare(Path.GetExtension(uri), ".dll", StringComparison.OrdinalIgnoreCase) != 0 &&
                string.Compare(Path.GetExtension(uri), ".pdb", StringComparison.OrdinalIgnoreCase) != 0)
            {
                return(false);
            }

            // get mime type
            string mimeType = HttpSocket.GetMimeType(uri);

            if (string.IsNullOrEmpty(mimeType))
            {
                s.WriteErrorResponse(403);
                return(true);
            }

            // see if the file exists in the assembly reference path
            string path = Chiron.TryGetAssemblyPath(uri);

            if (path == null)
            {
                return(false);
            }

            // read the file
            byte[] body = null;
            try {
                body = File.ReadAllBytes(path);
            } catch (Exception ex) {
                s.WriteErrorResponse(500, ex.Message + "\r\n" + ex.StackTrace);
                return(true);
            }

            // write the response
            s.WriteResponse(200, string.Format("Content-type: {0}\r\n", mimeType), body, false);
            return(true);
        }
Example #2
0
        bool TryProcessDirectoryRequest(HttpSocket s, string uri, string path)
        {
            if (!Directory.Exists(path))
            {
                return(false);
            }

            string q = string.Empty;
            int    i = uri.IndexOf('?');

            if (i > 0)
            {
                q = uri.Substring(i); uri = uri.Substring(0, i);
            }

            // redirect to trailing '/' to fix-up relative links
            if (!uri.EndsWith("/"))
            {
                string newUri = uri + "/" + q;

                s.WriteResponse(302,
                                string.Format("Content-Type: text/html; charset=utf-8\r\nLocation: {0}\r\n", newUri),
                                Encoding.UTF8.GetBytes(
                                    string.Format(@"<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href=""{0}"">here</a>.</h2></body></html>", newUri)),
                                false);

                return(true);
            }

            // get all files and subdirs
            FileSystemInfo[] infos;
            try {
                infos = new DirectoryInfo(path).GetFileSystemInfos();
            }
            catch {
                infos = new FileSystemInfo[0];
            }

            // decode %XX
            uri = DecodeUri(uri);

            // determine if parent is appropriate
            string parent = null;

            if (uri.Length > 1)
            {
                i      = uri.LastIndexOf('/', uri.Length - 2);
                parent = (i > 0) ? uri.Substring(0, i) : "/";
            }

            // write the response
            s.WriteTextResponse(200, "html", HtmlFormatter.FormatDirectoryListing(uri, parent, infos), false);
            return(true);
        }
Example #3
0
        bool TryProcessFileRequest(HttpSocket s, string uri, string path)
        {
            // path shouldn't end with '\' (that's for XAP listing)
            if (path.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                return(false);
            }

            // file must exist
            if (!File.Exists(path))
            {
                return(false);
            }

            // check extension
            string mimeType = HttpSocket.GetMimeType(path);

            if (string.IsNullOrEmpty(mimeType))
            {
                s.WriteErrorResponse(403);
                return(true);
            }

            // read the file
            byte[] body = null;
            try {
                body = File.ReadAllBytes(path);
            }
            catch (Exception ex) {
                s.WriteErrorResponse(500, ex.Message + "\r\n" + ex.StackTrace);
                return(true);
            }

            // write the response
            s.WriteResponse(200, string.Format("Content-type: {0}\r\n", mimeType), body, false);
            return(true);
        }
Example #4
0
        bool TryProcessXapListingRequest(HttpSocket s, string uri, string path)
        {
            // path should end with '\' (for XAP listing)
            if (!path.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                return(false);
            }
            path = path.Substring(0, path.Length - 1);

            // must end with XAP
            if (string.Compare(Path.GetExtension(path), ".xap", StringComparison.OrdinalIgnoreCase) != 0)
            {
                return(false);
            }

            // file must exist
            if (!File.Exists(path))
            {
                return(false);
            }

            // see if need to serve file from XAP
            string filename = null;
            int    iq       = uri.IndexOf('?');

            if (iq >= 0)
            {
                filename = uri.Substring(iq + 1);
            }

            ZipArchive xap = null;

            try {
                // open XAP file
                xap = new ZipArchive(path, FileAccess.Read);

                if (string.IsNullOrEmpty(filename))
                {
                    // list contents
                    List <ZipArchiveFile> xapContents = new List <ZipArchiveFile>();
                    foreach (KeyValuePair <string, ZipArchiveFile> p in xap.entries)
                    {
                        xapContents.Add(p.Value);
                    }
                    s.WriteTextResponse(200, "html", HtmlFormatter.FormatXapListing(uri, xapContents), false);
                    return(true);
                }

                // server file from XAP
                ZipArchiveFile f = null;
                if (!xap.entries.TryGetValue(filename, out f))
                {
                    s.WriteErrorResponse(404, "Resource not found in XAP");
                    return(true);
                }

                // check mime type
                string mimeType = HttpSocket.GetMimeType(filename);
                if (string.IsNullOrEmpty(mimeType))
                {
                    s.WriteErrorResponse(403);
                    return(true);
                }

                // get the content
                byte[] body = new byte[(int)f.Length];
                if (body.Length > 0)
                {
                    using (Stream fs = f.OpenRead()) {
                        fs.Read(body, 0, body.Length);
                    }
                }

                // write the resposne
                s.WriteResponse(200, string.Format("Content-type: {0}\r\n", mimeType), body, false);
                return(true);
            }
            catch {
                s.WriteErrorResponse(500, "error reading XAP");
                return(true);
            }
            finally {
                if (xap != null)
                {
                    xap.Close();
                }
            }
        }
Example #5
0
        bool TryProcessAssemblyRequest(HttpSocket s, string uri) {
            if (Chiron.UrlPrefix == "")
                return false;

            int slash = uri.LastIndexOf('/');
            if (slash == -1)
                return false;

            // must start with URL prefix
            if (string.Compare(uri.Substring(0, slash + 1), Chiron.UrlPrefix, StringComparison.OrdinalIgnoreCase) != 0)
                return false;

            uri = uri.Substring(slash + 1);

            // must end with DLL or PDB
            if (string.Compare(Path.GetExtension(uri), ".dll", StringComparison.OrdinalIgnoreCase) != 0 &&
                string.Compare(Path.GetExtension(uri), ".pdb", StringComparison.OrdinalIgnoreCase) != 0)
                return false;

            // get mime type
            string mimeType = HttpSocket.GetMimeType(uri);
            if (string.IsNullOrEmpty(mimeType)) {
                s.WriteErrorResponse(403);
                return true;
            }

            // see if the file exists in the assembly reference path
            string path = Chiron.TryGetAssemblyPath(uri);
            if (path == null)
                return false;

            // read the file
            byte[] body = null;
            try {
                body = File.ReadAllBytes(path);
            } catch (Exception ex) {
                s.WriteErrorResponse(500, ex.Message + "\r\n" + ex.StackTrace);
                return true;
            }

            // write the response
            s.WriteResponse(200, string.Format("Content-type: {0}\r\n", mimeType), body, false);
            return true;
        }
Example #6
0
        bool TryProcessXapListingRequest(HttpSocket s, string uri, string path) {
            // path should end with '\' (for XAP listing)
            if (!path.EndsWith(Path.DirectorySeparatorChar.ToString())) return false;
            path = path.Substring(0, path.Length - 1);

            // must end with XAP
            if (string.Compare(Path.GetExtension(path), ".xap", StringComparison.OrdinalIgnoreCase) != 0)
                return false;

            // file must exist
            if (!File.Exists(path)) return false;

            // see if need to serve file from XAP
            string filename = null;
            int iq = uri.IndexOf('?');
            if (iq >= 0) filename = uri.Substring(iq + 1);

            ZipArchive xap = null;

            try {
                // open XAP file
                xap = new ZipArchive(path, FileAccess.Read);

                if (string.IsNullOrEmpty(filename)) {
                    // list contents
                    List<ZipArchiveFile> xapContents = new List<ZipArchiveFile>();
                    foreach (KeyValuePair<string, ZipArchiveFile> p in xap.entries) xapContents.Add(p.Value);
                    s.WriteTextResponse(200, "html", HtmlFormatter.FormatXapListing(uri, xapContents), false);
                    return true;
                }

                // server file from XAP
                ZipArchiveFile f = null;
                if (!xap.entries.TryGetValue(filename, out f)) {
                    s.WriteErrorResponse(404, "Resource not found in XAP");
                    return true;
                }

                // check mime type
                string mimeType = HttpSocket.GetMimeType(filename);
                if (string.IsNullOrEmpty(mimeType)) {
                    s.WriteErrorResponse(403);
                    return true;
                }

                // get the content
                byte[] body = new byte[(int)f.Length];
                if (body.Length > 0) {
                    using (Stream fs = f.OpenRead()) {
                        fs.Read(body, 0, body.Length);
                    }
                }

                // write the resposne
                s.WriteResponse(200, string.Format("Content-type: {0}\r\n", mimeType), body, false);
                return true;
            }
            catch {
                s.WriteErrorResponse(500, "error reading XAP");
                return true;
            }
            finally {
                if (xap != null) xap.Close();
            }
        }
Example #7
0
        bool TryProcessDirectoryRequest(HttpSocket s, string uri, string path) {
            if (!Directory.Exists(path)) return false;

            string q = string.Empty;
            int i = uri.IndexOf('?');
            if (i > 0) { q = uri.Substring(i); uri = uri.Substring(0, i); }

            // redirect to trailing '/' to fix-up relative links
            if (!uri.EndsWith("/")) {
                string newUri = uri + "/" + q;

                s.WriteResponse(302,
                    string.Format("Content-Type: text/html; charset=utf-8\r\nLocation: {0}\r\n", newUri),
                    Encoding.UTF8.GetBytes(
string.Format(@"<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href=""{0}"">here</a>.</h2></body></html>", newUri)),
                    false);

                return true;
            }

            // get all files and subdirs
            FileSystemInfo[] infos;
            try {
                infos = new DirectoryInfo(path).GetFileSystemInfos();
            }
            catch {
                infos = new FileSystemInfo[0];
            }

            // decode %XX
            uri = DecodeUri(uri);

            // determine if parent is appropriate
            string parent = null;
            if (uri.Length > 1) {
                i = uri.LastIndexOf('/', uri.Length-2);
                parent = (i > 0) ? uri.Substring(0, i) : "/";
            }

            // write the response
            s.WriteTextResponse(200, "html", HtmlFormatter.FormatDirectoryListing(uri, parent, infos), false);
            return true;
        }
Example #8
0
        bool TryProcessFileRequest(HttpSocket s, string uri, string path) {
            // path shouldn't end with '\' (that's for XAP listing)
            if (path.EndsWith(Path.DirectorySeparatorChar.ToString())) return false;

            // file must exist
            if (!File.Exists(path)) return false;

            // check extension
            string mimeType = HttpSocket.GetMimeType(path);
            if (string.IsNullOrEmpty(mimeType)) {
                s.WriteErrorResponse(403);
                return true;
            }

            // read the file
            byte[] body = null;
            try {
                body = File.ReadAllBytes(path);
            }
            catch (Exception ex) {
                s.WriteErrorResponse(500, ex.Message + "\r\n" + ex.StackTrace);
                return true;
            }

            // write the response
            s.WriteResponse(200, string.Format("Content-type: {0}\r\n", mimeType), body, false);
            return true;
        }