示例#1
0
        static ResourceResponse GetLocalResource(string url)
        {
            if (string.IsNullOrWhiteSpace(url))
            {
                var    encoding         = new UTF8Encoding();
                var    bytes            = encoding.GetBytes(Host.Html);
                IntPtr unmanagedPointer = Marshal.AllocHGlobal(bytes.Length);
                Marshal.Copy(bytes, 0, unmanagedPointer, bytes.Length);
                // Call unmanaged code
                try
                {
                    return(ResourceResponse.Create((uint)bytes.Length, unmanagedPointer, "text/html"));
                }
                finally
                {
                    Marshal.FreeHGlobal(unmanagedPointer);
                }
            }

            var resourceFileName = GetResourceFileName(url);

            if (!File.Exists(resourceFileName))
            {
                return(null);
            }

            return(ResourceResponse.Create(resourceFileName));
        }
        private static ResourceResponse BytesToResourceResponce(byte[] bytes, string mediaType)
        {
            var buffer = Marshal.AllocHGlobal(bytes.Length);

            Marshal.Copy(bytes, 0, buffer, bytes.Length);

            var response = ResourceResponse.Create((uint)bytes.Length, buffer, mediaType);

            Marshal.FreeHGlobal(buffer);
            return(response);
        }
        public ResourceResponse OnRequest(ResourceRequest request)
        {
            if (request.Url.Scheme != "asset")
            {
                return(null);
            }

            var arguments = request.Url.OriginalString.ToPageArguments();

            long messageId;

            if (arguments.ContainsKey("messageId") && long.TryParse(arguments["messageId"], out messageId))
            {
                if (arguments.ContainsKey("cid"))
                {
                    var attachment = GetAttachmentBytes(arguments["cid"], messageId);
                    if (attachment != null)
                    {
                        return(BytesToResourceResponce(attachment, "image"));
                    }
                }
            }

            //asset://tempImage/
            var image = request.Url.AbsolutePath.StartsWith("/") ? request.Url.AbsolutePath.Remove(0, 1) : request.Url.AbsolutePath;

            if (File.Exists(image))
            {
                return(ResourceResponse.Create(image));
            }

            Debug.WriteLine(request.Url.AbsolutePath);

            // TODO: add more resources if needed
            if (!request.Url.AbsolutePath.EndsWith(".js") && !request.Url.AbsolutePath.EndsWith(".css") &&
                !request.Url.AbsolutePath.EndsWith(".png"))
            {
                return(null);
            }

            var basePath = new FileInfo(Assembly.GetEntryAssembly().Location).DirectoryName;
            var file     = basePath + request.Url.AbsolutePath.Replace("/message", string.Empty);

            if (File.Exists(file))
            {
                return(ResourceResponse.Create(file));
            }
            throw new Exception("resource could not be resolved");
        }
示例#4
0
        public ResourceResponse OnRequest(ResourceRequest request)
        {
            if (!String.IsNullOrEmpty(request.Url.Host))
            {
                switch (request.Url.Host)
                {
                case "emotic.org":
                    return(ResourceResponse.Create(Path.Combine(Settings.AniEmoticPath, "org", request.Url.LocalPath.Substring(1))));

                case "emotic.ext":
                    return(ResourceResponse.Create(Path.Combine(Settings.AniEmoticPath, "ext", request.Url.LocalPath.Substring(1))));

                case "emotic.ui":
                    return(ResourceResponse.Create(Path.Combine(Settings.AniEmoticPath, "ui", request.Url.LocalPath.Substring(1))));

                case "emotic.emoji":
                    return(ResourceResponse.Create(Path.Combine(Settings.AppPath, "emoji", "at24", request.Url.LocalPath.Substring(1))));

                case "scribble.image":
                    return(ResourceResponse.Create(Path.Combine(Settings.ScribblePath, request.Url.LocalPath.Substring(1))));

                default:
                    String[] parts = request.Url.Host.Split(new String[] { "." }, StringSplitOptions.RemoveEmptyEntries);

                    if (parts.Length == 2)
                    {
                        if (parts[1] == "script")
                        {
                            String   script_name = parts[0] + ".js";
                            JSScript script      = ScriptManager.Scripts.Find(x => x.ScriptName == script_name);

                            if (script != null)
                            {
                                return(ResourceResponse.Create(Path.Combine(script.DataPath, request.Url.LocalPath.Substring(1))));
                            }
                        }
                    }

                    break;
                }
            }

            return(null);
        }
示例#5
0
        /// <summary>
        /// Creates a response using the contents of an embedded assembly resource.
        /// </summary>
        private ResourceResponse CreateResponseFromResource(ResourceRequest request)
        {
            string resourceName;
            string filePath;

            // this project embeds static HTML/JS/CSS/PNG files as resources
            // by translating the resource's relative file path like Resources\foo/bar.html
            // to a logical name like /www/foo/bar.html
            resourceName = String.Concat("www", request.Url.AbsolutePath);
            filePath     = Path.GetFullPath(Path.Combine(TempFolder, resourceName.Replace('/', Path.DirectorySeparatorChar)));

            // cache the resource to a temp file if
            if (!File.Exists(filePath))
            {
                ExtractResourceToFile(resourceName, filePath);
            }

            return(ResourceResponse.Create(filePath));
        }
示例#6
0
        private ResourceResponse CreateResponseFromLocalContent(ResourceRequest request)
        {
            //Uncomment this once above functionality is complete
            string resourceName;
            string filePath;

            // this project embeds static HTML/JS/CSS/PNG files as resources
            // by translating the resource's relative file path like Resources\foo/bar.html
            // to a logical name like /www/foo/bar.html
            //resourceName = String.Concat("Content", request.Url.AbsolutePath);
            resourceName = request.Url.AbsolutePath;
            resourceName = resourceName.Replace('/', Path.DirectorySeparatorChar);
            resourceName = (resourceName.StartsWith(Path.DirectorySeparatorChar.ToString())) ? resourceName.TrimStart(new char[] { Path.DirectorySeparatorChar }) : resourceName;
            filePath     = Path.GetFullPath(Path.Combine(TempFolder, resourceName));

            // cache the resource to a temp file if
            if (!File.Exists(filePath))
            {
                ExtractResourceToFile(resourceName, filePath);
            }

            return(ResourceResponse.Create(filePath));
        }
示例#7
0
        private ResourceResponse readWebResponse(HttpWebRequest webreq)
        {
            HttpWebRequest.DefaultMaximumErrorResponseLength = 1048576;
            HttpWebResponse webresp   = null;// = webreq.GetResponse() as HttpWebResponse;
            var             memStream = new MemoryStream();
            Stream          webStream;

            try
            {
                webresp   = (HttpWebResponse)webreq.GetResponse();
                webStream = webresp.GetResponseStream();
                byte[] readBuffer = new byte[4096];
                int    bytesRead;
                while ((bytesRead = webStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
                {
                    memStream.Write(readBuffer, 0, bytesRead);
                }
            }
            catch (WebException e)
            {
                var r = e.Response as HttpWebResponse;
                webStream = r.GetResponseStream();
                memStream = Read(webStream);
                var wrongLength = memStream.Length;
            }


            memStream.Position = 0;
            StreamReader sr = new StreamReader(memStream);

            //string webStreamContent = sr.ReadToEnd();


            /*////****loads to file and creates response ****
             * //string webStreamContent = sr.ReadToEnd();
             *
             * string resourceName = String.Concat("www", webreq.RequestUri.AbsolutePath + ".temp");
             * string filePath = Path.GetFullPath(Path.Combine(TempFolder, resourceName.Replace('/', Path.DirectorySeparatorChar)));
             *
             * string parentPath = Directory.GetParent(filePath).FullName;
             * if (!Directory.Exists(parentPath))
             * {
             *  Directory.CreateDirectory(parentPath);
             * }
             *
             * using (Stream inputStream = sr.BaseStream)
             * using (FileStream outputStream = new FileStream(filePath, FileMode.Create))
             * {
             *  inputStream.CopyTo(outputStream);
             *  outputStream.Close();
             * }
             *
             * return (ResourceResponse.Create(filePath));
             *
             *///**********


            byte[] responseBuffer = memStream.GetBuffer(); //Encoding.UTF8.GetBytes(webStreamContent);

            // Initialize unmanaged memory to hold the array.
            int    responseSize = Marshal.SizeOf(responseBuffer[0]) * responseBuffer.Length;
            IntPtr pointer      = Marshal.AllocHGlobal(responseSize);

            try
            {
                // Copy the array to unmanaged memory.
                Marshal.Copy(responseBuffer, 0, pointer, responseBuffer.Length);
                return(ResourceResponse.Create((uint)responseBuffer.Length, pointer, webresp.ContentType));
            }
            finally
            {
                // Data is not owned by the ResourceResponse. A copy is made
                // of the supplied buffer. We can safely free the unmanaged memory.
                //Marshal.FreeHGlobal(pointer);
                webStream.Close();
            }
        }