public IWebResponse Invoke(Ds3Request request) { int redirectCount = 0; using (var content = request.GetContentStream()) { do { HttpWebRequest httpRequest = CreateRequest(request, content); try { var response = new WebResponse((HttpWebResponse)httpRequest.GetResponse()); if (Is307(response)) { redirectCount++; Trace.Write(string.Format(Resources.Encountered307NTimes, redirectCount), "Ds3Network"); } else { return(response); } } catch (WebException e) { if (e.Response == null) { throw e; } return(new WebResponse((HttpWebResponse)e.Response)); } } while (redirectCount < _maxRedirects); } throw new Ds3RedirectLimitException(Resources.TooManyRedirectsException); }
public IWebResponse Invoke(Ds3Request request) { Assert.AreEqual(_verb, request.Verb); Assert.AreEqual(_path, request.Path); CollectionAssert.AreEquivalent(_queryParams, request.QueryParams); Assert.AreEqual(_requestContent, HelpersForTest.StringFromStream(request.GetContentStream())); return(new MockWebResponse(_responseContent, _statusCode, _responseHeaders)); }
private HttpWebRequest CreateRequest(Ds3Request request, Stream content) { if (request.Verb == HttpVerb.PUT || request.Verb == HttpVerb.POST) { if (content != Stream.Null && !content.CanRead) { throw new Ds3.Runtime.Ds3RequestException(Resources.InvalidStreamException); } } DateTime date = DateTime.UtcNow; UriBuilder uriBuilder = new UriBuilder(_endpoint); uriBuilder.Path = HttpHelper.PercentEncodePath(request.Path); if (request.QueryParams.Count > 0) { uriBuilder.Query = BuildQueryParams(request.QueryParams); } HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(uriBuilder.ToString()); httpRequest.ServicePoint.ConnectionLimit = _connectionLimit; httpRequest.Method = request.Verb.ToString(); if (Proxy != null) { WebProxy webProxy = new WebProxy(); webProxy.Address = Proxy; httpRequest.Proxy = webProxy; } httpRequest.Date = date; httpRequest.Host = CreateHostString(_endpoint); httpRequest.AllowAutoRedirect = false; httpRequest.AllowWriteStreamBuffering = false; httpRequest.ReadWriteTimeout = this._readWriteTimeout; httpRequest.Timeout = this._requestTimeout; var md5 = ComputeChecksum(request.Md5, content); if (!string.IsNullOrEmpty(md5)) { httpRequest.Headers.Add(HttpHeaders.ContentMd5, md5); } httpRequest.Headers.Add(HttpHeaders.Authorization, S3Signer.AuthField( _creds, request.Verb.ToString(), date.ToString("r"), request.Path, request.QueryParams, md5: md5, amzHeaders: request.Headers )); foreach (var byteRange in request.GetByteRanges()) { httpRequest.AddRange(byteRange.Start, byteRange.End); } foreach (var header in request.Headers) { httpRequest.Headers.Add(header.Key, header.Value); } if (request.Verb == HttpVerb.PUT || request.Verb == HttpVerb.POST) { httpRequest.ContentLength = content.Length; if (content != Stream.Null) { using (var requestStream = httpRequest.GetRequestStream()) { if (content.Position != 0) { content.Seek(0, SeekOrigin.Begin); } content.CopyTo(requestStream, this.CopyBufferSize); requestStream.Flush(); } } } return(httpRequest); }