< Summary

Information
Class: Ice.Internal.WSEndpoint
Assembly: Ice
File(s): /_/csharp/src/Ice/Internal/WSEndpoint.cs
Tag: 125_37167941578
Line coverage
88%
Covered lines: 97
Uncovered lines: 13
Coverable lines: 110
Total lines: 306
Line coverage: 88.1%
Branch coverage
75%
Covered branches: 33
Total branches: 44
Branch coverage: 75%
Method coverage
90%
Covered methods: 28
Fully covered methods: 21
Total methods: 31
Method coverage: 90.3%
Full method coverage: 67.7%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor(...)100%22100%
.ctor(...)100%11100%
getInfo()100%11100%
type()100%11100%
protocol()100%11100%
streamWriteImpl(...)100%11100%
timeout()100%210%
timeout(...)50%2266.67%
connectionId()100%11100%
connectionId(...)100%22100%
compress()100%11100%
compress(...)100%22100%
datagram()100%11100%
secure()100%210%
transceiver()100%11100%
.ctor(...)100%11100%
connectors(...)100%22100%
exception(...)100%210%
connectors_async(...)100%44100%
acceptor(...)50%2288.89%
parseAllowedOrigins(...)100%4477.78%
endpoint(...)100%22100%
expandHost()100%11100%
isLoopbackOrMulticast()100%11100%
toPublishedEndpoint(...)100%11100%
equivalent(...)50%2275%
options()75%9880%
GetHashCode()100%11100%
CompareTo(...)50%9877.78%
checkOption(...)50%4480%

File(s)

/_/csharp/src/Ice/Internal/WSEndpoint.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using System.Net.Security;
 4
 5namespace Ice.Internal;
 6
 7internal sealed class WSEndpoint : EndpointI
 8{
 19    internal WSEndpoint(ProtocolInstance instance, EndpointI del, string res)
 10    {
 111        _instance = instance;
 112        _delegate = del;
 113        _resource = res;
 114    }
 15
 116    internal WSEndpoint(ProtocolInstance instance, EndpointI del, List<string> args)
 17    {
 118        _instance = instance;
 119        _delegate = del;
 20
 121        initWithOptions(args);
 22
 123        _resource ??= "/";
 124    }
 25
 126    internal WSEndpoint(ProtocolInstance instance, EndpointI del, Ice.InputStream s)
 27    {
 128        _instance = instance;
 129        _delegate = del;
 30
 131        _resource = s.readString();
 132    }
 33
 134    public override EndpointInfo getInfo() => new WSEndpointInfo(_delegate.getInfo(), _resource);
 35
 136    public override short type() => _delegate.type();
 37
 138    public override string protocol() => _delegate.protocol();
 39
 40    public override void streamWriteImpl(Ice.OutputStream s)
 41    {
 142        _delegate.streamWriteImpl(s);
 143        s.writeString(_resource);
 144    }
 45
 046    public override int timeout() => _delegate.timeout();
 47
 48    public override EndpointI timeout(int timeout)
 49    {
 150        if (timeout == _delegate.timeout())
 51        {
 052            return this;
 53        }
 54        else
 55        {
 156            return new WSEndpoint(_instance, _delegate.timeout(timeout), _resource);
 57        }
 58    }
 59
 160    public override string connectionId() => _delegate.connectionId();
 61
 62    public override EndpointI connectionId(string connectionId)
 63    {
 164        if (connectionId.Equals(_delegate.connectionId(), StringComparison.Ordinal))
 65        {
 166            return this;
 67        }
 68        else
 69        {
 170            return new WSEndpoint(_instance, _delegate.connectionId(connectionId), _resource);
 71        }
 72    }
 73
 174    public override bool compress() => _delegate.compress();
 75
 76    public override EndpointI compress(bool compress)
 77    {
 178        if (compress == _delegate.compress())
 79        {
 180            return this;
 81        }
 82        else
 83        {
 184            return new WSEndpoint(_instance, _delegate.compress(compress), _resource);
 85        }
 86    }
 87
 188    public override bool datagram() => _delegate.datagram();
 89
 090    public override bool secure() => _delegate.secure();
 91
 192    public override Transceiver transceiver() => null;
 93
 94    private sealed class EndpointI_connectorsI : EndpointI_connectors
 95    {
 196        public EndpointI_connectorsI(ProtocolInstance instance, string host, string res, EndpointI_connectors cb)
 97        {
 198            _instance = instance;
 199            _host = host;
 1100            _resource = res;
 1101            _callback = cb;
 1102        }
 103
 104        public void connectors(List<Connector> connectors)
 105        {
 1106            var l = new List<Connector>();
 1107            foreach (Connector c in connectors)
 108            {
 1109                l.Add(new WSConnector(_instance, c, _host, _resource));
 110            }
 1111            _callback.connectors(l);
 1112        }
 113
 0114        public void exception(Ice.LocalException ex) => _callback.exception(ex);
 115
 116        private readonly ProtocolInstance _instance;
 117        private readonly string _host;
 118        private readonly string _resource;
 119        private readonly EndpointI_connectors _callback;
 120    }
 121
 122    public override void connectors_async(EndpointI_connectors callback)
 123    {
 1124        string host = "";
 1125        for (Ice.EndpointInfo p = _delegate.getInfo(); p != null; p = p.underlying)
 126        {
 1127            if (p is Ice.IPEndpointInfo)
 128            {
 1129                var ipInfo = (Ice.IPEndpointInfo)p;
 1130                host = ipInfo.host + ":" + ipInfo.port;
 1131                break;
 132            }
 133        }
 1134        _delegate.connectors_async(new EndpointI_connectorsI(_instance, host, _resource, callback));
 1135    }
 136
 137    public override Acceptor acceptor(string adapterName, SslServerAuthenticationOptions serverAuthenticationOptions)
 138    {
 139        // Parse AllowedOrigins before creating the delegate acceptor so a malformed property doesn't leave an open
 140        // socket behind.
 141        HashSet<string> allowedOrigins;
 1142        if (adapterName.Length > 0)
 143        {
 1144            string propertyName = $"{adapterName}.AllowedOrigins";
 1145            allowedOrigins = parseAllowedOrigins(_instance.properties().getPropertyAsList(propertyName), propertyName);
 146        }
 147        else
 148        {
 0149            allowedOrigins = new HashSet<string>();
 150        }
 1151        return new WSAcceptor(
 1152            this,
 1153            _instance,
 1154            _delegate.acceptor(adapterName, serverAuthenticationOptions),
 1155            allowedOrigins);
 156    }
 157
 158    // Parse the values of the ObjectAdapter property "AllowedOrigins" into a canonicalized set of origins.
 159    // Each entry is "scheme://host[:port]", lowercased, with the default port for the scheme (80/443) omitted.
 160    // The literal "*" disables enforcement; in that case the returned set is empty, since handleRequest treats an
 161    // empty allowlist as "allow any origin" -- the two cases (unset and wildcard) collapse into one.
 162    // Throws PropertyException if any entry is not a syntactically valid origin; propertyName is included in the
 163    // message so the operator can identify which adapter is misconfigured.
 164    private static HashSet<string> parseAllowedOrigins(string[] entries, string propertyName)
 165    {
 1166        var result = new HashSet<string>();
 1167        foreach (string entry in entries)
 168        {
 1169            if (entry == "*")
 170            {
 1171                return new HashSet<string>();
 172            }
 173            try
 174            {
 1175                result.Add(WSTransceiver.canonicalizeOrigin(entry));
 1176            }
 0177            catch (ArgumentException)
 178            {
 0179                throw new PropertyException($"malformed origin '{entry}' in property '{propertyName}'");
 180            }
 181        }
 1182        return result;
 183    }
 184
 185    public WSEndpoint endpoint(EndpointI delEndp)
 186    {
 1187        if (delEndp == _delegate)
 188        {
 1189            return this;
 190        }
 191        else
 192        {
 1193            return new WSEndpoint(_instance, delEndp, _resource);
 194        }
 195    }
 196
 197    public override List<EndpointI> expandHost() =>
 1198        _delegate.expandHost().Select(e => endpoint(e) as EndpointI).ToList();
 199
 1200    public override bool isLoopbackOrMulticast() => _delegate.isLoopbackOrMulticast();
 201
 202    public override EndpointI toPublishedEndpoint(string publishedHost) =>
 1203        endpoint(_delegate.toPublishedEndpoint(publishedHost));
 204
 205    public override bool equivalent(EndpointI endpoint)
 206    {
 1207        if (!(endpoint is WSEndpoint))
 208        {
 0209            return false;
 210        }
 1211        var wsEndpointI = (WSEndpoint)endpoint;
 1212        return _delegate.equivalent(wsEndpointI._delegate);
 213    }
 214
 215    public override string options()
 216    {
 217        //
 218        // WARNING: Certain features, such as proxy validation in Glacier2,
 219        // depend on the format of proxy strings. Changes to toString() and
 220        // methods called to generate parts of the reference string could break
 221        // these features. Please review for all features that depend on the
 222        // format of proxyToString() before changing this and related code.
 223        //
 1224        string s = _delegate.options();
 225
 1226        if (_resource != null && _resource.Length > 0)
 227        {
 1228            s += " -r ";
 1229            bool addQuote = _resource.Contains(':', StringComparison.Ordinal);
 1230            if (addQuote)
 231            {
 0232                s += "\"";
 233            }
 1234            s += _resource;
 1235            if (addQuote)
 236            {
 0237                s += "\"";
 238            }
 239        }
 240
 1241        return s;
 242    }
 243
 1244    public override int GetHashCode() => HashCode.Combine(_delegate.GetHashCode(), _resource);
 245
 246    public override int CompareTo(EndpointI obj)
 247    {
 1248        if (!(obj is WSEndpoint))
 249        {
 0250            return type() < obj.type() ? -1 : 1;
 251        }
 252
 1253        var p = (WSEndpoint)obj;
 1254        if (this == p)
 255        {
 1256            return 0;
 257        }
 258
 1259        int v = string.Compare(_resource, p._resource, StringComparison.Ordinal);
 1260        if (v != 0)
 261        {
 0262            return v;
 263        }
 264
 1265        return _delegate.CompareTo(p._delegate);
 266    }
 267
 268    protected override bool checkOption(string option, string argument, string endpoint)
 269    {
 1270        switch (option[1])
 271        {
 272            case 'r':
 273            {
 1274                _resource = argument ?? throw new ParseException(
 1275                    $"no argument provided for -r option in endpoint '{endpoint}{_delegate.options()}'");
 1276                return true;
 277            }
 278
 279            default:
 280            {
 0281                return false;
 282            }
 283        }
 284    }
 285
 286    private readonly ProtocolInstance _instance;
 287    private readonly EndpointI _delegate;
 288    private string _resource;
 289}
 290
 291public class WSEndpointFactory : EndpointFactoryWithUnderlying
 292{
 293    public WSEndpointFactory(ProtocolInstance instance, short type)
 294        : base(instance, type)
 295    {
 296    }
 297
 298    public override EndpointFactory cloneWithUnderlying(ProtocolInstance instance, short underlying) =>
 299        new WSEndpointFactory(instance, underlying);
 300
 301    protected override EndpointI createWithUnderlying(EndpointI underlying, List<string> args, bool oaEndpoint) =>
 302        new WSEndpoint(instance_, underlying, args);
 303
 304    protected override EndpointI readWithUnderlying(EndpointI underlying, Ice.InputStream s) =>
 305        new WSEndpoint(instance_, underlying, s);
 306}