Exemple #1
0
        public async Task Any(SiteInvoke request)
        {
            var site = AssertSite(request.Slug);

            if (request.Request == null)
            {
                throw new ArgumentNullException(nameof(request.Request));
            }

            var url        = CreateSiteRequestUrl(site, request.Request);
            var qs         = ToUrlEncoded(request.Args);
            var sendInBody = HttpUtils.HasRequestBody(Request.Verb);

            if (!string.IsNullOrEmpty(qs) && !sendInBody)
            {
                url += "?" + qs;
            }

            var webReq = CreateSiteWebRequest(site, url);

            ProxyFeatureHandler.InitWebRequest(Request as IHttpRequest, webReq);

            if (!string.IsNullOrEmpty(qs) && sendInBody)
            {
                webReq.ContentType            = MimeTypes.FormUrlEncoded;
                await using var requestStream = await webReq.GetRequestStreamAsync();

                await requestStream.WriteAsync(MemoryProvider.Instance.ToUtf8(qs));
            }

            var proxy = new ProxyFeatureHandler();
            await proxy.ProxyToResponse((IHttpResponse)Response, webReq);
        }
        public async Task Any(DesktopDownloadUrl request)
        {
            DesktopFileService.AssertFile(request.File);

            var url = request.Url.IndexOf("://", StringComparison.Ordinal) >= 0
                ? request.Url.UrlDecode()
                : Request.GetBaseUrl().CombineWith(request.Url.UrlDecode());
            var webReq  = (HttpWebRequest)WebRequest.Create(url);
            var httpReq = (IHttpRequest)base.Request;

            ProxyFeatureHandler.InitWebRequest(httpReq, webReq);
            if (httpReq.ContentLength > 0)
            {
                using (request.RequestStream)
                    using (var requestStream = await webReq.GetRequestStreamAsync())
                    {
                        await request.RequestStream.WriteToAsync(requestStream);
                    }
            }
            var downloadFile = Path.Combine(KnownFolders.GetPath(KnownFolders.Downloads), request.File);

            try { File.Delete(downloadFile); } catch {}

            using (var webRes = await webReq.GetResponseAsync())
                using (var resStream = webRes.ResponseStream())
                    using (var fs = new FileInfo(downloadFile).Open(FileMode.OpenOrCreate))
                    {
                        await resStream.CopyToAsync(fs);
                    }
            await Response.EndRequestAsync();

            if (request.Open)
            {
                var p = new Process {
                    StartInfo = request.Start != null
                        ? new ProcessStartInfo(request.Start, downloadFile)
                    {
                        UseShellExecute = true
                    }
                        : new ProcessStartInfo(downloadFile)
                    {
                        UseShellExecute = true
                    }
                };
                p.Start();
            }
        }