Exemple #1
0
        internal void AddUrlSegment(string key, string value)
        {
            string trueKey = "{" + key + "}";
            var    url     = RequestUri.ToString();

            url = url.Replace(trueKey, value);
            var uri = new Uri(url, UriKind.Relative);

            RequestUri = uri;
        }
        /// <summary>
        /// Create a pre-sign string formed from the following items and concatenating them together
        /// </summary>
        /// <param name="timestamp"></param>
        /// <param name="url"></param>
        /// <param name="method"></param>
        /// <param name="contentHash"></param>
        /// <param name="subaccountId"></param>
        /// <returns></returns>
        private string GetSignature(string timestamp, string url, string method, string contentHash, string subaccountId = "")
        {
            var sign   = string.Join(string.Empty, timestamp, RequestUri.ToString(), method, contentHash, subaccountId);
            var secret = Encoding.UTF8.GetBytes(_secret);

            using (HMACSHA512 hmac = new HMACSHA512(secret))
            {
                var bytes  = Encoding.UTF8.GetBytes(sign);
                var signed = hmac.ComputeHash(bytes);
                return(BitConverter.ToString(signed).ToLower().Replace("-", string.Empty));
            }
        }
        public IClientResponse Send()
        {
            _request.ContentType = _entity.ContentType.ToString();
            RaiseStatusChanged("Connecting to {0}", RequestUri.ToString());
            if (_entity.Stream != null &&
                ((_entity.Stream.CanSeek && _entity.Stream.Length > 0) ||
                 _entity.Stream != _emptyStream))
            {
                SendRequestStream();
            }

            return(new HttpWebResponseBasedResponse(this, _request));
        }
        /// <summary>
        /// Generate headers for authenticated request.
        /// </summary>
        private void ConstructAuthenticationHeaders()
        {
            var timestamp   = GetTimestamp();
            var contentHash = GetContentHash();
            var url         = RequestUri.ToString();
            var method      = Method.Method;
            var signature   = GetSignature(timestamp, url, method, contentHash);


            Headers.Add("Api-Key", _key);
            Headers.Add("Api-Timestamp", timestamp);
            Headers.Add("Api-Content-Hash", contentHash);
            Headers.Add("Api-Signature", signature);
        }
Exemple #5
0
        public void Connect()
        {
            if (connection != null)
            {
                throw new InvalidOperationException("Already connection exists");
            }
            connection            = new WebSocket(RequestUri.ToString());
            connection.OnMessage += OnMessage;
            connection.OnOpen    += OnOpen;;
            connection.OnClose   += OnClose;
            connection.OnError   += OnError;
            OnConnectionCreate(connection);

            connection.Connect();
        }
Exemple #6
0
        internal void AddQueryParameter(string key, string value)
        {
            var currentUri = RequestUri.ToString();

            if (!currentUri.Contains("?"))
            {
                currentUri += "?";
            }
            else
            {
                currentUri += "&";
            }

            RequestUri = new Uri(string.Format("{0}{1}={2}", currentUri, key, value), UriKind.Relative);
        }
Exemple #7
0
        private HttpWebResponse RecordRequestAndResponse(Func <HttpWebResponse> getResponse)
        {
            // record the request
            var recordedRequest = new RecordedRequest
            {
                Url    = RequestUri.ToString(),
                Method = Method,
                RequestCookieContainer = CookieContainer,
                RequestHeaders         = Headers,
                RequestPayload         =
                    null == _shadowCopyRequestStream
                    ? new RecordedStream()
                    : new RecordedStream(
                        _shadowCopyRequestStream.ShadowCopy.ToArray(),
                        this)
            };

            RecordedRequests.Add(recordedRequest);

            try
            {
                var response = getResponse();

                RecordResponse(response, recordedRequest);

                return(response);
            }
            catch (Exception e)
            {
                // record exception, exception's response and
                recordedRequest.ResponseException = new RecordedResponseException
                {
                    Message = e.Message,
                    Type    = e.GetType()
                };

                if (e is WebException webException)
                {
                    recordedRequest.ResponseException.WebExceptionStatus = webException.Status;

                    // if WebException - try and record the response
                    RecordResponse((HttpWebResponse)webException.Response, recordedRequest);
                }

                // re-throw
                throw;
            }
        }
Exemple #8
0
        public override string ToString()
        {
            var sb = new StringBuilder();

            sb.Append("Method: ").Append(method);
            sb.Append(", RequestUri: '").Append(RequestUri != null ? RequestUri.ToString() : "<null>");
            sb.Append("', Version: ").Append(Version);
            sb.Append(", Content: ").Append(Content != null ? Content.ToString() : "<null>");
            sb.Append(", Headers:\r\n{\r\n").Append(Headers);
            if (Content != null)
            {
                sb.Append(Content.Headers);
            }
            sb.Append("}");

            return(sb.ToString());
        }
Exemple #9
0
        public ListBucketRequest(string host, string bucketName, ListBucketOptions options)
            : base(HttpMethod.Get, host, bucketName, null)
        {
            options.QueryList.Add("list-type", "2");

            if (options.QueryList.Count > 0)
            {
                RequestUri = new Uri(RequestUri.ToString() + options.QueryList.ToQueryString());
            }

            CompletionOption = HttpCompletionOption.ResponseContentRead;

            /*
             *          GET ?prefix=photos/2006/&delimiter=/ HTTP/1.1
             *          Host: quotes.s3.amazonaws.com
             *          Date: Wed, 01 Mar  2009 12:00:00 GMT
             *          Authorization: AWS 15B4D3461F177624206A:xQE0diMbLRepdf3YB+FIEXAMPLE=
             */
        }
Exemple #10
0
        /// <summary>
        /// extracts the link to the created topic
        /// </summary>
        /// <param name="responseString">the html code of the codestriker response</param>
        /// <returns>the specific link</returns>
        private string ExtractUriFromResponse(string responseString)
        {
            // last item is the link to the topic in Codestriker
            try
            {
                LinkItem topicLinkString = LinkFinder.Find(responseString.ToLower()).Last();
                string   originalLink    = topicLinkString.Text;
                if (RequestUri.ToString().Contains(":"))
                {
                    originalLink = m_Regex.Replace(originalLink, $"{RequestUri.OriginalString}?");
                }

                string linkToProperties = originalLink.Replace("view", "view_topic_properties");
                var    topicUri         = linkToProperties;
                return(topicUri);
            }
            catch (InvalidOperationException e)
            {
                throw new ArgumentException($"Invalid Argument {nameof(responseString)}", e);
            }
        }
        public AzureRequestStatus CheckRequestStatus(RequestUri requestUri)
        {
            var response = _http.Get(requestUri.ToString());
            var match = Regex.Match(response.Content, "<Status>(.*?)</Status>");
            FailFast.Unless(match.Success, "Expected regex match in response content: " + match + ".  Request URI:" + requestUri + ", Response content:" + response.Content);

            switch (match.Groups[1].Value)
            {
                case "InProgress":
                    return AzureRequestStatus.InProgress;
                case "Succeeded":
                    return AzureRequestStatus.Succeeded;
                case "Failed":
                    OurTrace.TraceError("CheckRequestStatus gave us a failure: " + response.Content);
                    var is400BadRequest = Regex.Match(response.Content, "<HttpStatusCode>400</HttpStatusCode><Error><Code>BadRequest</Code>");
                    if (is400BadRequest.Success)
                        throw new BadRequestException(requestUri, response.Content);
                    return AzureRequestStatus.Failed;
                default:
                    FailFast.WithMessage("Unexpected operation status: " + match.Groups[1].Value + ", for operation: " + requestUri);
                    // ReSharper disable HeuristicUnreachableCode
                    throw new InvalidOperationException("Shouldn't ever get here!");
                    // ReSharper restore HeuristicUnreachableCode
            }
        }
Exemple #12
0
        public byte[] Perform(IPAddress address)
        {
            //Redirect if not login
            if (_currentUser == null && RequestUri.Host != _configuration["ApiBase"])
            {
                _logger.LogInformation($"Connection to {RequestUri.Host} blocked because not login.");
                return(Generate302(_configuration["LoginPage"]));
            }

            //Redirect if blocked
            if (_currentUser.BlockList.Any(blocked => new Regex(blocked)
                                           .IsMatch(RequestUri.ToString()) &&
                                           !_currentUser.AllowList.Any(a =>
                                                                       new Regex(a).IsMatch(RequestUri.ToString()))))
            {
                _logger.LogInformation($"Connection to {RequestUri} blocked because it was on blocklist.");
                return(Generate302(_configuration["BlockedPage"]));
            }

            //Quick CRLF
            Span <byte> crlf = stackalloc byte[2];

            crlf[0]             = (byte)'\r';
            crlf[1]             = (byte)'\n';
            using var cacheHash = SHA256.Create();
            var key = BitConverter.ToString(cacheHash.ComputeHash(Encoding.UTF8.GetBytes(RequestUri.ToString())))
                      .Replace("-", string.Empty);
            var ms      = new MemoryStream();
            var message = new HttpRequestMessage(Method, RequestUri)
            {
                Content = new ByteArrayContent(Body)
            };

            message.Content.Headers.Clear();
            message.Headers.Clear();
            foreach (var(k, v) in Header)
            {
                if (k.StartsWith("Content"))
                {
                    message.Content.Headers.Add(k, v);
                }
                else
                {
                    message.Headers.Add(k, v);
                }
            }
            //Add X-Forwarded-For
            message.Headers.Add("X-Forwarded-For", address.ToString());
            //Check if no cache allowed
            var cached =
                from cache
                in Context.Caches
                where cache.CacheId == key && cache.ExpireTime > DateTime.Now
                select cache;

            if (message.Headers.CacheControl?.NoStore != true)
            {
                _logger.LogInformation("Cache allowed, now verifying fresh.");

                if (cached.Any())
                {
                    //Verify first
                    message.Headers.IfModifiedSince = cached.First().CachedTime;
                }
            }

            //No cache used
            Response = Client.Send(message);
            //304, cache allowed
            if (Response.StatusCode == HttpStatusCode.NotModified)
            {
                _logger.LogInformation("Cache verified successful, using cache.");
                //Use cache
                if (cached.Any())
                {
                    ms.Write(cached.First().Content);
                    return(ms.ToArray());
                }
            }

            var responseHeader = Encoding.ASCII.GetBytes(Response.Headers.ToString());
            var contentHeader  = Encoding.ASCII.GetBytes(Response.Content.Headers.ToString());
            //Write status
            var status =
                Encoding.ASCII.GetBytes(
                    $"HTTP/{Response.Version.ToString(2)} {(int) Response.StatusCode} {Response.ReasonPhrase}");

            ms.Write(status);
            ms.Write(crlf);
            //Write header
            ms.Write(responseHeader);
            ms.Write(contentHeader);
            ms.Write(crlf);
            //Write body
            Response.Content.CopyTo(ms, null, default);

            //Check if cache allowed and ContentLength
            if (Response.Headers.CacheControl?.NoStore != true &&
                Response.Content.Headers.ContentLength < 40960)
            {
                //Only cache 200, 404 and 301
                if (Response.StatusCode == HttpStatusCode.OK ||
                    Response.StatusCode == HttpStatusCode.NotFound ||
                    Response.StatusCode == HttpStatusCode.MovedPermanently)
                {
                    //Create a new thread to write cache
                    Task.Run(async() =>
                    {
                        var caches =
                            from c
                            in Context.Caches
                            where c.CacheId == key
                            select c;
                        //Check and remove old expired
                        if (caches.Any())
                        {
                            Context.Caches.Remove(caches.First());
                        }
                        var cache = new Cache
                        {
                            CacheId    = key,
                            CachedTime = DateTime.Now,
                            Content    = ms.ToArray()
                        };
                        cache.ExpireTime = Response.Headers.CacheControl?.MaxAge != null
                            ? cache.CachedTime.Add((TimeSpan)Response.Headers.CacheControl.MaxAge)
                            : cache.CachedTime.AddDays(7);

                        await Context.AddAsync(cache);
                        await Context.SaveChangesAsync();
                    });
                }
            }
            return(ms.ToArray());
        }
Exemple #13
0
 /// <summary>
 /// Process.Startを用いて、RequestUriを表示します。
 /// </summary>
 public void ShowWithBrowser()
 {
     Process.Start(RequestUri.ToString());
 }