Exemple #1
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();
                }
            }
        }