示例#1
0
        protected override void AugmentHeaders(SerialilzableWebRequest request, HttpWebRequest remoteRequest)
        {
            var p          = request.Path.Split('/');
            var service    = p.Length > 0 ? p[0].ToLower() : "";
            var copyHeader = request.Headers.Get("x-ms-copy-source");

            if (copyHeader != null)
            {
                var copyPath = copyHeader.Split('/');
                if (copyPath.Length > 4 && copyPath[3] == copyPath[5])
                {
                    copyPath    = copyPath.Skip(5).ToArray();
                    copyPath[0] = "";
                }
                copyPath[1] = ManagedAccount;
                remoteRequest.Headers.Set("x-ms-copy-source", String.Join("/", copyPath));
            }
            var authHeader = request.Headers.Get("Authorization");

            // Only sign a request that contains a verified signature.
            if (authHeader != null)
            {
                remoteRequest.Headers.Set("Authorization", SharedKeyAuthorizationHeader(authHeader.StartsWith("SharedKeyLite"), ManagedAccount, ManagedKey, remoteRequest.Method, remoteRequest.Headers, remoteRequest.RequestUri, remoteRequest.ContentLength, service == "table"));
            }
        }
示例#2
0
        protected override Uri BuildUri(SerialilzableWebRequest request)
        {
            var ub = new UriBuilder(ResourceManaged);

            ub.Path  = ub.Path + "/" + request.Path;
            ub.Query = request.Query;
            return(ub.Uri);
        }
 public override Task <HttpWebResponse> GetResponse(SerialilzableWebRequest request)
 {
     if (request.Method.Equals("get", StringComparison.OrdinalIgnoreCase) || request.Method.Equals("head", StringComparison.OrdinalIgnoreCase))
     {
         return(base.GetResponse(request));
     }
     else
     {
         return(stateMachine.SubmitAsync(request));
     }
 }
示例#4
0
        protected override Uri BuildUri(SerialilzableWebRequest request)
        {
            var p       = request.Path.Split('/');
            var service = p.Length > 0 ? p[0].ToLower() : "";
            var host    = String.Format("{0}.{1}.core.windows.net", ManagedAccount, service);
            var path    = String.Join("/", p.Skip(1));

            var ub = new UriBuilder()
            {
                Host = host, Path = path, Query = request.Query
            };

            return(ub.Uri);
        }
        private IEnumerable <Task> _getResponse(SerialilzableWebRequest request, TaskCompletionSource <HttpWebResponse> result)
        {
            //Trace.TraceInformation("ReverseProxy._getResponse Enter");
            Uri            uri = BuildUri(request);
            HttpWebRequest remoteRequest;

            remoteRequest             = (HttpWebRequest)HttpWebRequest.Create(uri);
            remoteRequest.Method      = request.Method;
            remoteRequest.ContentType = request.ContentType;
            remoteRequest.Headers.AddUnrestricted(request.Headers);
            remoteRequest.ContentLength = request.Content.Length;
            AugmentHeaders(request, remoteRequest);

            if (!HasNoContent(request.Method))
            {
                //Trace.TraceInformation("ReverseProxy._getResponse Has Content");
                using (var stream = remoteRequest.GetRequestStream())
                    using (var task = Streams.WriteAsync(stream, request.Content, 0, request.Content.Length))
                    {
                        yield return(task);

                        task.Wait();
                    }
            }

            //Trace.TraceInformation("ReverseProxy._getResponse Calling GetResponseAsync");
            using (var task = remoteRequest.GetResponseAsync(TimeSpan.FromSeconds(15)))
            {
                yield return(task);

                //Trace.TraceInformation("ReverseProxy._getResponse GetResponseAsync Returned");
                switch (task.Status)
                {
                case TaskStatus.RanToCompletion:
                    result.SetResult((HttpWebResponse)task.Result);
                    yield break;

                case TaskStatus.Canceled:
                    result.SetCanceled();
                    yield break;

                case TaskStatus.Faulted:
                    result.SetException(task.Exception.InnerExceptions);
                    yield break;
                }
                throw new InvalidOperationException("The task was not completed.");

                //Trace.TraceInformation("ReverseProxy._getResponse result set");
            }
        }
        public override Task <HttpWebResponse> GetResponse(SerialilzableWebRequest request)
        {
            //Trace.TraceInformation("AzureStorageProxy.GetResponse");
            bool read        = request.Method.Equals("get", StringComparison.OrdinalIgnoreCase) || request.Method.Equals("head", StringComparison.OrdinalIgnoreCase);
            var  cacheHeader = request.Headers["Cache-Control"];
            var  noCache     = cacheHeader != null && cacheHeader.Equals("no-cache", StringComparison.InvariantCultureIgnoreCase);
            var  cacheParam  = request.Query.Contains("$nocache=true");

            noCache |= cacheParam;
            if (read && !noCache)
            {
                return(base.GetResponse(request));
            }
            else
            {
                return(stateMachine.SubmitAsync(request));
            }
        }
        protected override void AugmentHeaders(SerialilzableWebRequest request, HttpWebRequest remoteRequest)
        {
            // This should not be neccessary, but one server occasionally throws on the set and this fixes it.
            request.Headers = new System.Collections.Specialized.NameValueCollection(request.Headers);

            var if_match = request.Headers.Get("If-Match");

            if (if_match != null)
            {
                remoteRequest.Headers.Set("If-Match", "*");
                request.Headers.Set("If-Match", "*");
            }
            if (request.Headers.AllKeys.ContainsInvariantIgnoreCase("Date"))
            {
                request.Headers.Remove("Date");
            }
            var now = DateTime.UtcNow.ToString("R", System.Globalization.CultureInfo.InvariantCulture);

            request.Headers.Set("x-ms-date", now);
            remoteRequest.Headers.Set("x-ms-date", now);
            base.AugmentHeaders(request, remoteRequest);
        }
        private IEnumerable <Task> _makeSerializableWebRequest(HttpRequest request, TaskCompletionSource <SerialilzableWebRequest> result)
        {
            string path   = ExtractPath(request.Url.AbsolutePath);
            var    remote = new SerialilzableWebRequest
            {
                Path        = path,
                Query       = request.Url.Query.TrimStart('?'),
                Method      = request.HttpMethod,
                ContentType = request.ContentType,
                Headers     = request.Headers
            };

            remote.Content = new byte[request.InputStream.Length];
            using (var stream = new MemoryStream(remote.Content, 0, remote.Content.Length, true, true))
            {
                var task = Streams.CopyStreamAsync(request.InputStream, stream);
                yield return(task);

                task.Wait();
            }
            result.SetResult(remote);
        }
 protected abstract Uri BuildUri(SerialilzableWebRequest request);
 protected virtual void AugmentHeaders(SerialilzableWebRequest request, HttpWebRequest remoteRequest)
 {
 }
 public virtual Task <HttpWebResponse> GetResponse(SerialilzableWebRequest request)
 {
     return(Concurrency.Iterate <HttpWebResponse>(r => _getResponse(request, r)));
 }