Skip to content

Commit 36ecce8

Browse files
authored
Merge pull request #22 from jimm98y/features/disable-100-continue
F disable 100-continue
2 parents 9ac191e + 8b5f2ea commit 36ecce8

File tree

5 files changed

+110
-6
lines changed

5 files changed

+110
-6
lines changed

src/OnvifClient/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
}
1414
else
1515
{
16-
using (var client = new SimpleOnvifClient(device, "admin", "password"))
16+
using (var client = new SimpleOnvifClient(device, "admin", "password", true))
1717
{
1818
var deviceInfo = await client.GetDeviceInformationAsync();
1919
var services = await client.GetServicesAsync(true);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.ServiceModel.Channels;
4+
using System.ServiceModel.Description;
5+
using System.ServiceModel.Dispatcher;
6+
7+
namespace SharpOnvifClient.Behaviors
8+
{
9+
public class DisableExpect100ContinueBehavior : IEndpointBehavior
10+
{
11+
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
12+
{
13+
bindingParameters.Add(new Func<HttpClientHandler, HttpMessageHandler>(GetHttpMessageHandler));
14+
}
15+
16+
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { }
17+
18+
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }
19+
20+
public void Validate(ServiceEndpoint endpoint) { }
21+
22+
public HttpMessageHandler GetHttpMessageHandler(HttpClientHandler httpClientHandler)
23+
{
24+
return new DisableExpect100ContinueMessageHandler(httpClientHandler);
25+
}
26+
}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.ServiceModel;
3+
4+
namespace SharpOnvifClient.Behaviors
5+
{
6+
public static class DisableExpect100ContinueBehaviorExtensions
7+
{
8+
public static void SetDisableExpect100Continue<TChannel>(
9+
this ClientBase<TChannel> channel,
10+
System.ServiceModel.Description.IEndpointBehavior disableExpect100Continue = null) where TChannel : class
11+
{
12+
if (disableExpect100Continue == null)
13+
return;
14+
15+
if (!channel.Endpoint.EndpointBehaviors.Contains(disableExpect100Continue))
16+
{
17+
channel.Endpoint.EndpointBehaviors.Add(disableExpect100Continue);
18+
}
19+
}
20+
}
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Net.Http;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
5+
namespace SharpOnvifClient.Behaviors
6+
{
7+
public class DisableExpect100ContinueMessageHandler : DelegatingHandler
8+
{
9+
public DisableExpect100ContinueMessageHandler(HttpMessageHandler innerHandler)
10+
{
11+
InnerHandler = innerHandler;
12+
}
13+
14+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
15+
{
16+
HttpResponseMessage response;
17+
18+
request.Headers.ExpectContinue = false;
19+
response = await base.SendAsync(request, cancellationToken);
20+
21+
return response;
22+
}
23+
}
24+
}

src/SharpOnvifClient/SimpleOnvifClient.cs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using SharpOnvifClient.DeviceMgmt;
1+
using SharpOnvifClient.Behaviors;
2+
using SharpOnvifClient.DeviceMgmt;
23
using SharpOnvifClient.Events;
34
using SharpOnvifClient.Media;
45
using SharpOnvifClient.PTZ;
@@ -13,6 +14,9 @@
1314

1415
namespace SharpOnvifClient
1516
{
17+
/// <summary>
18+
/// Simple Onvif client implements the basic Onvif device operations such as device information, media profiles, stream URI, PTZ operations and event subscriptions.
19+
/// </summary>
1620
public class SimpleOnvifClient : IDisposable
1721
{
1822
private bool _disposedValue;
@@ -25,14 +29,36 @@ public class SimpleOnvifClient : IDisposable
2529
private readonly System.Net.NetworkCredential _credentials;
2630
private readonly OnvifAuthentication _authentication;
2731
private readonly IEndpointBehavior _legacyAuth;
28-
29-
public SimpleOnvifClient(string onvifUri) : this(onvifUri, null, null, OnvifAuthentication.None)
32+
private readonly IEndpointBehavior _disableExpect100ContinueBehavior;
33+
34+
/// <summary>
35+
/// Creates an instance of <see cref="SimpleOnvifClient"/>.
36+
/// </summary>
37+
/// <param name="onvifUri">Onvif URI.</param>
38+
/// <param name="disableExpect100Continue">Disables the default Expect: 100-continue HTTP header.</param>
39+
public SimpleOnvifClient(string onvifUri, bool disableExpect100Continue = false) : this(onvifUri, null, null, OnvifAuthentication.None, disableExpect100Continue)
3040
{ }
3141

32-
public SimpleOnvifClient(string onvifUri, string userName, string password) : this(onvifUri, userName, password, OnvifAuthentication.WsUsernameToken | OnvifAuthentication.HttpDigest)
42+
/// <summary>
43+
/// Creates an instance of <see cref="SimpleOnvifClient"/>.
44+
/// </summary>
45+
/// <param name="onvifUri">Onvif URI.</param>
46+
/// <param name="userName">User name.</param>
47+
/// <param name="password">Password.</param>
48+
/// <param name="disableExpect100Continue">Disables the default Expect: 100-continue HTTP header.</param>
49+
public SimpleOnvifClient(string onvifUri, string userName, string password, bool disableExpect100Continue = false) : this(onvifUri, userName, password, OnvifAuthentication.WsUsernameToken | OnvifAuthentication.HttpDigest, disableExpect100Continue)
3350
{ }
3451

35-
public SimpleOnvifClient(string onvifUri, string userName, string password, OnvifAuthentication authentication)
52+
/// <summary>
53+
/// Creates an instance of <see cref="SimpleOnvifClient"/>.
54+
/// </summary>
55+
/// <param name="onvifUri">Onvif URI.</param>
56+
/// <param name="userName">User name.</param>
57+
/// <param name="password">Password.</param>
58+
/// <param name="authentication">Type of the authentication to use: <see cref="OnvifAuthentication"/>.</param>
59+
/// <param name="disableExpect100Continue">Disables the default Expect: 100-continue HTTP header.</param>
60+
/// <exception cref="ArgumentNullException">Thrown when onvifUri is empty.</exception>
61+
public SimpleOnvifClient(string onvifUri, string userName, string password, OnvifAuthentication authentication, bool disableExpect100Continue = false)
3662
{
3763
if (string.IsNullOrWhiteSpace(onvifUri))
3864
throw new ArgumentNullException(nameof(onvifUri));
@@ -50,6 +76,11 @@ public SimpleOnvifClient(string onvifUri, string userName, string password, Onvi
5076
}
5177
}
5278

79+
if (disableExpect100Continue)
80+
{
81+
_disableExpect100ContinueBehavior = new DisableExpect100ContinueBehavior();
82+
}
83+
5384
_onvifUri = onvifUri;
5485
_authentication = authentication;
5586
}
@@ -79,6 +110,7 @@ public TClient GetOrCreateClient<TClient, TChannel>(string uri, Func<string, TCl
79110
{
80111
var client = creator(uri);
81112
client.SetOnvifAuthentication(_authentication, _credentials, _legacyAuth);
113+
client.SetDisableExpect100Continue(_disableExpect100ContinueBehavior);
82114
_clients.Add(key, client);
83115
return client;
84116
}

0 commit comments

Comments
 (0)