/// <summary>Creates a new scheme.</summary> /// <remarks> /// Creates a new scheme. /// Whether the created scheme allows for layered connections /// depends on the class of <code>factory</code>. /// </remarks> /// <param name="name"> /// the scheme name, for example "http". /// The name will be converted to lowercase. /// </param> /// <param name="port">the default port for this scheme</param> /// <param name="factory"> /// the factory for creating sockets for communication /// with this scheme /// </param> /// <since>4.1</since> public Scheme(string name, int port, SchemeSocketFactory factory) { Args.NotNull(name, "Scheme name"); Args.Check(port > 0 && port <= unchecked ((int)(0xffff)), "Port is invalid"); Args.NotNull(factory, "Socket factory"); this.name = name.ToLower(Sharpen.Extensions.GetEnglishCulture()); this.defaultPort = port; if (factory is SchemeLayeredSocketFactory) { this.layered = true; this.socketFactory = factory; } else { if (factory is LayeredSchemeSocketFactory) { this.layered = true; this.socketFactory = new SchemeLayeredSocketFactoryAdaptor2((LayeredSchemeSocketFactory )factory); } else { this.layered = false; this.socketFactory = factory; } } }
/// <summary> /// Creates a new instance of /// <see cref="ContentType">ContentType</see> /// . /// </summary> /// <param name="mimeType"> /// MIME type. It may not be <code>null</code> or empty. It may not contain /// characters <">, <;>, <,> reserved by the HTTP specification. /// </param> /// <param name="charset">charset.</param> /// <returns>content type</returns> public static Org.Apache.Http.Entity.ContentType Create(string mimeType, Encoding charset) { string type = Args.NotBlank(mimeType, "MIME type").ToLower(CultureInfo.InvariantCulture ); Args.Check(Valid(type), "MIME type may not contain reserved characters"); return(new Org.Apache.Http.Entity.ContentType(type, charset)); }
public void ShouldReturnSameReference() { // Arrange var obj = new object(); // Act var result = Args.Check(() => obj).NotNull().Value; // Assert result.Should().BeSameAs(obj); }
public void ShouldNotThrowExceptionForNonNullReference() { // Arrange var obj = new object(); // Act Action action = () => Args.Check(() => obj).NotNull(); // Assert action.ShouldNotThrow(); }
public void ShouldThrowArgumentNullExceptionForNullReference() { // Arrange object abc = null; // Act Action action = () => Args.Check(() => abc).NotNull(); // Assert action.ShouldThrow <ArgumentNullException>() .And.ParamName.Should().Be("abc"); }
/// <summary>Compares this protocol version with another one.</summary> /// <remarks> /// Compares this protocol version with another one. /// Only protocol versions with the same protocol name can be compared. /// This method does <i>not</i> define a total ordering, as it would be /// required for /// <see cref="System.IComparable{T}">System.IComparable<T></see> /// . /// </remarks> /// <param name="that">the protocol version to compare with</param> /// <returns> /// a negative integer, zero, or a positive integer /// as this version is less than, equal to, or greater than /// the argument version. /// </returns> /// <exception cref="System.ArgumentException"> /// if the argument has a different protocol name than this object, /// or if the argument is <code>null</code> /// </exception> public virtual int CompareToVersion(Org.Apache.Http.ProtocolVersion that) { Args.NotNull(that, "Protocol version"); Args.Check(this.protocol.Equals(that.protocol), "Versions for different protocols cannot be compared: %s %s" , this, that); int delta = GetMajor() - that.GetMajor(); if (delta == 0) { delta = GetMinor() - that.GetMinor(); } return(delta); }
// static array with english reason phrases defined below // no body /// <summary>Obtains the reason phrase for a status code.</summary> /// <remarks>Obtains the reason phrase for a status code.</remarks> /// <param name="status">the status code, in the range 100-599</param> /// <param name="loc">ignored</param> /// <returns>the reason phrase, or <code>null</code></returns> public virtual string GetReason(int status, CultureInfo loc) { Args.Check(status >= 100 && status < 600, "Unknown category for status code " + status ); int category = status / 100; int subcode = status - 100 * category; string reason = null; if (ReasonPhrases[category].Length > subcode) { reason = ReasonPhrases[category][subcode]; } return(reason); }
public HttpHost GetHopTarget(int hop) { Args.NotNegative(hop, "Hop index"); int hopcount = GetHopCount(); Args.Check(hop < hopcount, "Hop index exceeds tracked route length"); if (hop < hopcount - 1) { return(this.proxyChain[hop]); } else { return(this.targetHost); } }
private HttpRoute(HttpHost target, IPAddress local, IList <HttpHost> proxies, bool secure, RouteInfo.TunnelType tunnelled, RouteInfo.LayerType layered) { Args.NotNull(target, "Target host"); this.targetHost = target; this.localAddress = local; if (proxies != null && !proxies.IsEmpty()) { this.proxyChain = new AList <HttpHost>(proxies); } else { this.proxyChain = null; } if (tunnelled == RouteInfo.TunnelType.Tunnelled) { Args.Check(this.proxyChain != null, "Proxy required if tunnelled"); } this.secure = secure; this.tunnelled = tunnelled != null ? tunnelled : RouteInfo.TunnelType.Plain; this.layered = layered != null ? layered : RouteInfo.LayerType.Plain; }
/// <summary> /// Sets the factor to use when backing off; the new /// per-host limit will be roughly the current max times /// this factor. /// </summary> /// <remarks> /// Sets the factor to use when backing off; the new /// per-host limit will be roughly the current max times /// this factor. <code>Math.floor</code> is applied in the /// case of non-integer outcomes to ensure we actually /// decrease the pool size. Pool sizes are never decreased /// below 1, however. Defaults to 0.5. /// </remarks> /// <param name="d">must be between 0.0 and 1.0, exclusive.</param> public virtual void SetBackoffFactor(double d) { Args.Check(d > 0.0 && d < 1.0, "Backoff factor must be 0.0 < f < 1.0"); backoffFactor = d; }
/// <summary> /// Removes dot segments according to RFC 3986, section 5.2.4 and /// Syntax-Based Normalization according to RFC 3986, section 6.2.2. /// </summary> /// <remarks> /// Removes dot segments according to RFC 3986, section 5.2.4 and /// Syntax-Based Normalization according to RFC 3986, section 6.2.2. /// </remarks> /// <param name="uri">the original URI</param> /// <returns>the URI without dot segments</returns> private static URI NormalizeSyntax(URI uri) { if (uri.IsOpaque() || uri.GetAuthority() == null) { // opaque and file: URIs return(uri); } Args.Check(uri.IsAbsolute(), "Base URI must be absolute"); string path = uri.GetPath() == null ? string.Empty : uri.GetPath(); string[] inputSegments = path.Split("/"); Stack <string> outputSegments = new Stack <string>(); foreach (string inputSegment in inputSegments) { if ((inputSegment.Length == 0) || (".".Equals(inputSegment))) { } else { // Do nothing if ("..".Equals(inputSegment)) { if (!outputSegments.IsEmpty()) { outputSegments.Pop(); } } else { outputSegments.Push(inputSegment); } } } StringBuilder outputBuffer = new StringBuilder(); foreach (string outputSegment in outputSegments) { outputBuffer.Append('/').Append(outputSegment); } if (path.LastIndexOf('/') == path.Length - 1) { // path.endsWith("/") || path.equals("") outputBuffer.Append('/'); } try { string scheme = uri.GetScheme().ToLower(); string auth = uri.GetAuthority().ToLower(); URI @ref = new URI(scheme, auth, outputBuffer.ToString(), null, null); if (uri.GetQuery() == null && uri.GetFragment() == null) { return(@ref); } StringBuilder normalized = new StringBuilder(@ref.ToASCIIString()); if (uri.GetQuery() != null) { // query string passed through unchanged normalized.Append('?').Append(uri.GetRawQuery()); } if (uri.GetFragment() != null) { // fragment passed through unchanged normalized.Append('#').Append(uri.GetRawFragment()); } return(URI.Create(normalized.ToString())); } catch (URISyntaxException e) { throw new ArgumentException(e); } }