/// <summary> /// Creates or updates a given file resource in the file system in one blocking operation. /// </summary> /// <param name="virtualFilePath">The qualified path of the file to be created.</param> /// <param name="input">A stream that provides the file's contents.</param> /// <param name="overwrite">Whether an existing file should be overwritten /// or not. If this parameter is false and the file already exists, a /// <see cref="ResourceOverwriteException"/> is thrown.</param> /// <param name="resourceLength">The length of the resource to be uploaded in bytes.</param> /// <param name="contentType">The content type of the uploaded resource.</param> /// <exception cref="ResourceAccessException">In case of invalid or prohibited /// resource access.</exception> /// <exception cref="ResourceOverwriteException">If a file already exists at the /// specified location, and the <paramref name="overwrite"/> flag was not set.</exception> /// <exception cref="ArgumentNullException">If any of the parameters is a null reference.</exception> public void WriteFile(string virtualFilePath, Stream input, bool overwrite, long resourceLength, string contentType) { string actionUri = VfsUris.Default.WriteFileContentsUri; actionUri = actionUri.ConstructUri(Uris.PatternFilePath, virtualFilePath); //set headers RequestHeaders requestHeader = new RequestHeaders(); VfsHttpHeaders headers = VfsHttpHeaders.Default; requestHeader.Add(headers.OverwriteExistingResource, overwrite.ToString(CultureInfo.InvariantCulture)); requestHeader.Add("Content-Length", resourceLength.ToString(CultureInfo.InvariantCulture)); requestHeader.Add("Content-Type", contentType); Func <HttpClient, HttpResponseMessage> func = c => { HttpContent content = HttpContent.Create(() => input); return(c.Send(HttpMethod.POST, actionUri, requestHeader, content)); }; SecureRequest(FileSystemTask.StreamedFileUploadRequest, func, () => String.Format("Could not write data for file [{0}] to file system.", virtualFilePath)); }
/// <summary> /// Posts the data of a given data block to the server. /// </summary> private HttpResponseMessage SendDataBlock(HttpClient client, StreamedDataBlock dataBlock) { string actionUri = VfsUris.Default.WriteStreamedDataBlockUri; actionUri = actionUri.ConstructUri(Uris.PatternTransferId, dataBlock.TransferTokenId); actionUri = actionUri.ConstructUri(Uris.PatternBlockNumber, dataBlock.BlockNumber.ToString(CultureInfo.InvariantCulture)); RequestHeaders requestHeader = new RequestHeaders(); //write the HTTP headers VfsHttpHeaders headers = VfsHttpHeaders.Default; requestHeader.Add(headers.TransferId, dataBlock.TransferTokenId); //duplicate transfer ID, might be useful in some scenarios requestHeader.Add(headers.BlockNumber, dataBlock.BlockNumber.ToString()); requestHeader.Add(headers.IsLastBlock, Convert.ToString(dataBlock.IsLastBlock).ToLowerInvariant()); requestHeader.Add(headers.BlockOffset, dataBlock.Offset.ToString()); if (dataBlock.BlockLength.HasValue) { requestHeader.Add(headers.BlockLength, dataBlock.BlockLength.ToString()); } using (dataBlock.Data) { return(client.Send(HttpMethod.POST, actionUri, requestHeader, HttpContent.Create(() => dataBlock.Data))); } }
public void GivenRequestHeader_WhenApplyingMultipleValuesToOneKey_ValuesAddedToHttpClient() { HttpClient httpClient = new HttpClient(); RequestHeaders requestHeader = new RequestHeaders(); requestHeader.Add("test", "value"); requestHeader.Add("test", "value2"); requestHeader.Apply(httpClient); var headers = httpClient.DefaultRequestHeaders.GetValues("test"); Assert.Collection(headers, item => Assert.Equal("value", item), item => Assert.Equal("value2", item)); }
public void GivenRequestHeader_WhenDuplicatingEmptyStringValue_ThenThisIsAllowed() { HttpClient httpClient = new HttpClient(); RequestHeaders requestHeader = new RequestHeaders(); requestHeader.Add("test", ""); requestHeader.Add("test", ""); requestHeader.Apply(httpClient); var headers = httpClient.DefaultRequestHeaders.GetValues("test"); Assert.Collection(headers, item => Assert.Equal("", item), item => Assert.Equal("", item)); }
public void SetBasicAuthenticationHeader(string userName, string password) { string authentication = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(userName + ":" + password)); RequestHeaders.Add("Authorization", authentication); }
private void PopulateRequestHeaders(Dictionary <string, string> dict) { foreach (KeyValuePair <string, string> keyValuePair in dict) { RequestHeaders.Add(keyValuePair.Key, keyValuePair.Value); } }
public void GivenRequestHeader_WhenAddingEmptyValue_NoArgumentIsThrown() { RequestHeaders requestHeader = new RequestHeaders(); Exception e = Record.Exception(() => requestHeader.Add("Header", "")); Assert.Null(e); }
protected void MockCallback(Callback callback) { RequestHeaders.Add(ControllerBaseExtensions.CallbackUrlHeader, callback.Url.ToString()); Connection.SetupGet(c => c.RemoteIpAddress).Returns(callback.RegisteredIp); Callbacks .Setup(r => r.AddAsync(callback.RegisteredIp, callback.Url, It.IsAny <CancellationToken>())) .ReturnsAsync(callback); }
public void GivenRequestHeader_WhenApplyingValues_ValuesAddedToHttpClient() { HttpClient httpClient = new HttpClient(); RequestHeaders requestHeader = new RequestHeaders(); requestHeader.Add("test", "value"); requestHeader.Apply(httpClient); Assert.Equal("value", httpClient.DefaultRequestHeaders.GetValues("test").First()); }
public SpeechWebSocket SetCredentials(string userName, string password, bool isSSL = false) { RequestHeaders.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(userName + ":" + password))); if (isSSL) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3; } return(this); }
protected override void AddToRequestParam() { List <dynamic> paramList = new List <dynamic>(); dynamic reqParam = new ExpandoObject(); reqParam.abc = "efg"; // TODO: We will parse the value from request header or param RequestHeaders.Add(reqParam); }
private void ParseRequest_(byte[] request_bytes) { string request_str = Encoding.UTF8.GetString(request_bytes); int current_index = 0; int scan_index = request_str.IndexOf(' '); this.Method = request_str.Substring(current_index, scan_index); current_index = scan_index + 1; scan_index = request_str.IndexOf(' ', current_index); this.RequestUri = request_str.Substring(current_index, scan_index - current_index); Uri uri = new Uri(this.RequestUri); this.Scheme = uri.Scheme; this.Path = uri.LocalPath; this.PathBase = ""; this.QueryString = uri.Query; current_index = scan_index + 1; scan_index = request_str.IndexOf('\r', current_index); this.HttpVersion = request_str.Substring(current_index, scan_index - current_index); current_index = scan_index + 2; string header_line; int header_scan_index = 0; while (current_index != request_str.Length && request_str[current_index] != '\r') { scan_index = request_str.IndexOf('\r', current_index); header_line = request_str.Substring(current_index, scan_index - current_index); if (header_line.IndexOf("Connection") == 0 && header_line.IndexOf("keep-alive") != -1) { _keepAlive = true; } header_scan_index = header_line.IndexOf(':'); this.RequestHeaders.Add(header_line.Substring(0, header_scan_index), header_line.Substring(header_scan_index + 2, header_line.Length - header_scan_index - 2)); current_index = scan_index + 2; } if (current_index < request_str.Length) { current_index += 2; string body_str = request_str.Substring(current_index, request_str.Length - current_index); byte[] body_bytes = Encoding.UTF8.GetBytes(body_str); this.RequestBody.Write(body_bytes, 0, body_bytes.Length); this.RequestBody.Flush(); this.RequestBody.Position = 0; if (!RequestHeaders.ContainsKey("Content-Length")) { RequestHeaders.Add("Content-Length", body_bytes.Length.ToString()); } } _responseStarted = true; }
public void GivenRequestHeader_WhenCallingApplyMoreThanOnce_ThenOnlyOneValueIsPresent() { HttpClient httpClient = new HttpClient(); RequestHeaders requestHeader = new RequestHeaders(); requestHeader.Add("test", "value"); requestHeader.Apply(httpClient); requestHeader.Apply(httpClient); IEnumerable <string> headers = httpClient.DefaultRequestHeaders.GetValues("test"); Assert.Single(headers); }
public void SetRequestHeader(string headerName, string headerValue) { if (RequestHeaders.ContainsKey(headerName)) { RequestHeaders[headerName][0] = headerValue; return; } RequestHeaders.Add(headerName, new string[] { headerValue }); }
private void HandleHttpRequests() { while (_listener.IsListening) { try { var ctx = _listener.GetContext(); OnRequestReceived(ctx); if (ShouldDeserializeTraces) { var spans = MessagePackSerializer.Deserialize <IList <IList <Span> > >(ctx.Request.InputStream); OnRequestDeserialized(spans); lock (this) { // we only need to lock when replacing the span collection, // not when reading it because it is immutable Spans = Spans.AddRange(spans.SelectMany(trace => trace)); RequestHeaders = RequestHeaders.Add(new NameValueCollection(ctx.Request.Headers)); } } // NOTE: HttpStreamRequest doesn't support Transfer-Encoding: Chunked // (Setting content-length avoids that) ctx.Response.ContentType = "application/json"; var buffer = Encoding.UTF8.GetBytes("{}"); ctx.Response.ContentLength64 = buffer.LongLength; ctx.Response.OutputStream.Write(buffer, 0, buffer.Length); ctx.Response.Close(); } catch (HttpListenerException) { // listener was stopped, // ignore to let the loop end and the method return } catch (ObjectDisposedException) { // the response has been already disposed. } catch (InvalidOperationException) { // this can occur when setting Response.ContentLength64, with the framework claiming that the response has already been submitted // for now ignore, and we'll see if this introduces downstream issues } catch (Exception) when(!_listener.IsListening) { // we don't care about any exception when listener is stopped } } }
public Task PostAsync_ErrorWhileStoringCallback_ShouldReleaseAddressReservation() { return(AsynchronousTesting.WithCancellationTokenAsync(async cancellationToken => { // Arrange. var requester = IPAddress.Parse("192.168.1.2"); var callback = new Uri("http://localhost/a"); var ex = new Exception(); var request = new ReceivingRequest() { TargetAmount = new PropertyAmount(10000), }; RequestHeaders.Add(CallbackUrl, callback.ToString()); Connection.SetupGet(c => c.RemoteIpAddress).Returns(requester); this.pool .Setup(p => p.TryLockAddressAsync(It.IsAny <CancellationToken>())) .ReturnsAsync(this.reservation); this.Callbacks .Setup(r => r.AddAsync(It.IsAny <IPAddress>(), It.IsAny <Uri>(), It.IsAny <CancellationToken>())) .ThrowsAsync(ex); // Act. var result = await Assert.ThrowsAnyAsync <Exception>( () => Subject.PostAsync(request, cancellationToken)); // Assert. Assert.Same(ex, result); this.pool.Verify(p => p.TryLockAddressAsync(cancellationToken), Times.Once()); this.Callbacks.Verify(r => r.AddAsync(requester, callback, CancellationToken.None), Times.Once()); this.watcher.Verify( w => w.StartWatchAsync( It.IsAny <ReceivingAddressReservation>(), It.IsAny <PropertyAmount>(), It.IsAny <int>(), It.IsAny <TimeSpan>(), It.IsAny <TokenReceivingCallback>(), It.IsAny <CancellationToken>()), Times.Never()); this.pool.Verify(p => p.ReleaseAddressAsync(this.reservation.Id, CancellationToken.None), Times.Once()); })); }
public async Task GivenHeader_WhenCallingExecuteGetRequest_HeaderIsSetInRequestMessage() { string timestamp = DateTime.UtcNow.ToString(); FakeHttpMessageHandler fakeHttpMessageHandler = new FakeHttpMessageHandler(); RequestHeaders requestHeaders = new RequestHeaders(); requestHeaders.Add("timestamp", timestamp); HttpClient httpClient = new HttpClient(fakeHttpMessageHandler); GetRequest getRequest = new GetRequest(httpClient) { RequestHeaders = requestHeaders }; HttpResponseMessage response = await getRequest.Execute("http://www.google.com"); Assert.Equal(timestamp, response.RequestMessage.Headers.GetValues("timestamp").First()); }
public void SetRequestHeaders(IEnumerable <KeyValuePair <string, string> > requestHeaders) { if (requestHeaders != null) { foreach (var header in requestHeaders) { if (RequestHeaders.ContainsKey(header.Key)) { RequestHeaders[header.Key] = header.Value; } else { RequestHeaders.Add(header.Key, header.Value); } } } }
/// <summary> /// HTTPリクエスト文字列からHTTPRequestオブジェクトを構築します /// </summary> /// <param name="requests">行毎に区切られたHTTPリクエストの文字列表現</param> public HttpRequest(HttpRequestLine reqLine, IEnumerable <string> requests) { Method = reqLine.Method; Protocol = reqLine.Protocol; PathAndQuery = reqLine.PathAndQuery; var headers = new RequestHeaders(); foreach (var req in requests) { Match match = null; if ((match = OtherHeaderRegex.Match(req)).Success) { headers.Add(match.Groups[1].Value, match.Groups[2].Value.Trim()); } } Headers = headers; }
// // Summary: // Called on the CEF IO thread when a resource load has completed. // // Parameters: // frame: // The frame that is being redirected. // // request: // the request object - cannot be modified in this callback // // response: // the response object - cannot be modified in this callback // // status: // indicates the load completion status // // receivedContentLength: // is the number of response bytes actually read. public void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength) { if (request.Url.Contains(".user.js")) { var code = browserControl.GetSourceAsync().ToString(); myForm.monyet.InstallScript(request.Url, code); } if (oldAddress != browserControl.Address || oldAddress == "") { oldAddress = browserControl.Address; Resources.Clear(); ResponseHeaders.Clear(); RequestHeaders.Clear(); } Dictionary <string, string> dictionary = response.Headers.AllKeys.ToDictionary((string x) => x, (string x) => response.Headers[x]); ResponseHeaders.Add(new HeaderWrapper(request.Identifier, dictionary)); Dictionary <string, string> strs = request.Headers.AllKeys.ToDictionary((string x) => x, (string x) => request.Headers[x]); RequestHeaders.Add(new HeaderWrapper(request.Identifier, strs)); if (responseDictionary.TryGetValue(request.Identifier, out MemoryStreamResponseFilter memoryStreamResponseFilter)) { byte[] data = memoryStreamResponseFilter.Data; Resources.Add(new RequestWrapper(request.Url, request.ResourceType, response.MimeType, data, request.Identifier)); } //var x = response.MimeType; //scrape all link on current web here //myForm.X.Appender(request.Url.GetQuery("path")); //myForm.X.Appender(request.Url + " = " + status); /* * if (request.Url.Contains(myForm.ViewsourceURL)) * { * * var code = myForm.viewer.View(request.Url.GetQuery("path")); * MessageBox.Show(code.ToString()); * var t = string.Format("var x = @{0}{1}{0};", '"', code); * t += string.Format("$({0}#xmpTagId{0}).text(x);", '"'); * myForm.X.ExecuteJs(t); * * } */ }
private void HandleHttpRequests() { while (_listener.IsListening) { try { var ctx = _listener.GetContext(); OnRequestReceived(ctx); if (ShouldDeserializeTraces) { var spans = MessagePackSerializer.Deserialize <IList <IList <Span> > >(ctx.Request.InputStream); OnRequestDeserialized(spans); lock (this) { // we only need to lock when replacing the span collection, // not when reading it because it is immutable Spans = Spans.AddRange(spans.SelectMany(trace => trace)); RequestHeaders = RequestHeaders.Add(new NameValueCollection(ctx.Request.Headers)); } } ctx.Response.ContentType = "application/json"; var buffer = Encoding.UTF8.GetBytes("{}"); ctx.Response.OutputStream.Write(buffer, 0, buffer.Length); ctx.Response.Close(); } catch (HttpListenerException) { // listener was stopped, // ignore to let the loop end and the method return } catch (ObjectDisposedException) { // the response has been already disposed. } catch (Exception) when(!_listener.IsListening) { // we don't care about any exception when listener is stopped } } }
private static RequestHeaders ParseHeader(string header) { if (string.IsNullOrEmpty(header)) { return(null); } var headers = new RequestHeaders(); foreach (string line in header.Split('\n')) { var pos = line.IndexOf(':'); if (pos == -1) { continue; } headers.Add(new KeyValuePair <string, string>(line.Substring(0, pos).Trim(), line.Substring(pos + 1).Trim())); } return(headers); }
public Task PostAsync_WithInvalidCallbackUrl_ShouldReleaseAddressReservation(string url) { return(AsynchronousTesting.WithCancellationTokenAsync(async cancellationToken => { // Arrange. var request = new ReceivingRequest() { TargetAmount = new PropertyAmount(1), }; RequestHeaders.Add(CallbackUrl, url); this.pool .Setup(p => p.TryLockAddressAsync(It.IsAny <CancellationToken>())) .ReturnsAsync(this.reservation); // Act. await Assert.ThrowsAsync <InvalidCallbackUrlException>( () => Subject.PostAsync(request, cancellationToken)); // Assert. this.pool.Verify(p => p.TryLockAddressAsync(cancellationToken), Times.Once()); this.watcher.Verify( w => w.StartWatchAsync( It.IsAny <ReceivingAddressReservation>(), It.IsAny <PropertyAmount>(), It.IsAny <int>(), It.IsAny <TimeSpan>(), It.IsAny <TokenReceivingCallback>(), It.IsAny <CancellationToken>()), Times.Never()); this.pool.Verify(p => p.ReleaseAddressAsync(this.reservation.Id, CancellationToken.None), Times.Once()); })); }
/// <summary> /// Adds the given header object to Request /// </summary> /// <param name="newHeader"></param> public void AddHeader(HttpHeader newHeader) { if (NonUniqueRequestHeaders.ContainsKey(newHeader.Name)) { NonUniqueRequestHeaders[newHeader.Name].Add(newHeader); return; } if (RequestHeaders.ContainsKey(newHeader.Name)) { var existing = RequestHeaders[newHeader.Name]; RequestHeaders.Remove(newHeader.Name); NonUniqueRequestHeaders.Add(newHeader.Name, new List <HttpHeader>() { existing, newHeader }); } else { RequestHeaders.Add(newHeader.Name, newHeader); } }
/// <summary> /// Sets Basic Authorization header /// </summary> /// <param name="username">Username</param> /// <param name="password">password</param> public void BasicAuth(string username, string password) { string encoded = String.Format("Basic {0}", Convert.ToBase64String(Encoding.UTF8.GetBytes(String.Format("{0}:{1}", username, password)))); RequestHeaders.Add("Authorization", encoded); }
public void BearToken(string bearertoken) { string encoded = String.Format("Bearer {0}", bearertoken); RequestHeaders.Add("Authorization", encoded); }
public void GivenRequestHeader_WhenAddingEmptyKey_ArgumentExceptionIsThrown() { RequestHeaders requestHeader = new RequestHeaders(); Assert.Throws <ArgumentException>(() => requestHeader.Add("", "")); }
public void SetHeader(string name, string value) { RequestHeaders.Add(name, value); }
IRequestBuilder IMessageBuilder <IRequestBuilder> .WithHeader(string key, params string[] values) { RequestHeaders.Add(key, values); return(this); }
public SpeechWebSocket WithHeader(string headerName, string headerValue) { RequestHeaders.Add(headerName, headerValue); return(this); }