// define custom virtual page
        public override async Task<bool> rewrite(System.IO.Stream resp, HttpServerRequest req)
        {
            String tempPath = req.path;
            if (req.path.IndexOf("?") != -1)
                tempPath = tempPath.Substring(0, req.path.IndexOf("?"));
            switch (tempPath)
            {
                // generate virtual page for url /hello.png
                case "/hello.png":
                    //Download image from internet and return as local page
                    byte[] imageByte = await DownloadImageFromWebsiteAsync("https://assets.windowsphone.com/db658987-b7ca-43aa-885c-fd4426fb6962/Downloads-VS_InvariantCulture_Default.png");

                        String contentType = Util.getContentType("/hello.png");
                    //Write header
                    string header = String.Format("HTTP/1.1 200 OK\r\n" +
                                    "Content-Length: {0}\r\n" + contentType +
                                    "Cache-Control: no-cache, no-store, must-revalidate\r\nPragma: no-cache\r\nExpires: 0\r\n" +
                                    "Connection: close\r\n\r\n",
                                    imageByte.Length);

                    byte[] headerArray = Encoding.UTF8.GetBytes(header);
                    //Write response
                    await resp.WriteAsync(headerArray, 0, headerArray.Length);
                    await resp.WriteAsync(imageByte, 0, imageByte.Length);

                    return true;
                    break;

            }
            return false;
        }
 private async Task StreamContent(byte[] buffer, System.IO.Stream outputStream)
 {
     var pointer = 0;
     while (pointer < buffer.Length)
     {
         var bytesToStream = buffer.Skip(pointer).Take(65536).ToArray();
         await outputStream.WriteAsync(bytesToStream, 0, bytesToStream.Length);
         pointer += bytesToStream.Length;
     }
 }
Example #3
0
 public override async Task DownloadFileAsync(DriveFile driveFile, System.IO.Stream fileStream, CancellationToken token)
 {
     var url = ((GoogleFile)driveFile).File.DownloadUrl;
     if (url == null)
         throw new TaskException("Can't download this file");
     var service = await GetServiceAsync(token);
     var stream = await service.HttpClient.GetStreamAsync(url);
     if (stream == null)
     {
         throw new Exception("Can't download this file");
     }
     var buffer = new byte[1000];
     var n = await stream.ReadAsync(buffer, 0, buffer.Length, token);
     while (n > 0)
     {
         await fileStream.WriteAsync(buffer, 0, n, token);
         n = await stream.ReadAsync(buffer, 0, buffer.Length, token);
     }
 }