public override void OnGetDataNow(ContentPackage package)
        {
            // Main thread only:
            Callback.MainThread(delegate(){
                // Getting a files text content from resources.
                byte[] data = null;

                string path = GetPath(package.location);

                TextAsset asset = Resources.Load(path) as TextAsset;

                if (asset == null)
                {
                    // Not found
                    package.Failed(404);
                }
                else
                {
                    // Ok
                    data = asset.bytes;
                    package.ReceivedHeaders(data.Length);
                    package.ReceivedData(data, 0, data.Length);
                }
            });
        }
Exemplo n.º 2
0
        // Raw binary data
        public override void OnGetDataNow(ContentPackage package)
        {
            // Content type header is segments[0]:
            string contentType = package.location.Segments[0];

            // Trim it:
            contentType = contentType.Trim();

            byte[] data;

            if (contentType.EndsWith(";base64"))
            {
                // Split it off:
                contentType = contentType.Substring(0, contentType.Length - 7);

                // The data is at location.Segments[1] as base64:
                data = System.Convert.FromBase64String(package.location.Segments[1]);
            }
            else
            {
                // The data is at location.Segments[1], simply url encoded:
                string unescaped = System.Uri.UnescapeDataString(package.location.Segments[1]);

                data = System.Text.Encoding.UTF8.GetBytes(unescaped);
            }

            // Apply headers:
            package.responseHeaders["content-type"] = contentType;
            package.ReceivedHeaders(data.Length);

            // Apply data:
            package.ReceivedData(data, 0, data.Length);
        }
Exemplo n.º 3
0
        /// <summary>Get the raw file data.</summary>
        public override void OnGetDataNow(ContentPackage package)
        {
            // Path without the protocol:
            string rawPath = package.location.Path;

            // If it contains a : and starts with / then chop it off:
            if (rawPath != null && rawPath.IndexOf(':') != -1 && rawPath[0] == '/')
            {
                rawPath = rawPath.Substring(1);
            }

            if (File.Exists(rawPath))
            {
                // Partial?
                int partialStart;
                int partialEnd;

                byte[] data;

                if (package.GetRange(out partialStart, out partialEnd))
                {
                    // Partial request.
                    FileStream fs = new FileStream(rawPath, FileMode.Open, FileAccess.Read);

                    // Seek there:
                    fs.Seek(partialStart, SeekOrigin.Begin);

                    // Setup Content-Range:
                    package.SetPartialResponse(partialStart, partialEnd, (int)fs.Length);

                    int contentLength = (partialEnd + 1) - partialStart;

                    // Read that many bytes into our buffer:
                    data = new byte[contentLength];

                    // Read:
                    fs.Read(data, 0, data.Length);

                    // Got headers:
                    package.ReceivedHeaders(contentLength);

                    // Ok!
                    fs.Close();
                }
                else
                {
                    // Get the bytes:
                    data = File.ReadAllBytes(rawPath);

                    // Got headers:
                    package.ReceivedHeaders(data.Length);
                }

                // Got data:
                package.ReceivedData(data, 0, data.Length);
            }
            else
            {
                // Let the package know:
                package.Failed(404);
            }
        }