| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Net.Security; |
| | | 4 | | |
| | | 5 | | namespace Ice.Internal; |
| | | 6 | | |
| | | 7 | | internal sealed class WSEndpoint : EndpointI |
| | | 8 | | { |
| | | 9 | | internal WSEndpoint(ProtocolInstance instance, EndpointI del, string res) |
| | | 10 | | { |
| | | 11 | | _instance = instance; |
| | | 12 | | _delegate = del; |
| | | 13 | | _resource = res; |
| | | 14 | | } |
| | | 15 | | |
| | | 16 | | internal WSEndpoint(ProtocolInstance instance, EndpointI del, List<string> args) |
| | | 17 | | { |
| | | 18 | | _instance = instance; |
| | | 19 | | _delegate = del; |
| | | 20 | | |
| | | 21 | | initWithOptions(args); |
| | | 22 | | |
| | | 23 | | _resource ??= "/"; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | internal WSEndpoint(ProtocolInstance instance, EndpointI del, Ice.InputStream s) |
| | | 27 | | { |
| | | 28 | | _instance = instance; |
| | | 29 | | _delegate = del; |
| | | 30 | | |
| | | 31 | | _resource = s.readString(); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | public override EndpointInfo getInfo() => new WSEndpointInfo(_delegate.getInfo(), _resource); |
| | | 35 | | |
| | | 36 | | public override short type() => _delegate.type(); |
| | | 37 | | |
| | | 38 | | public override string protocol() => _delegate.protocol(); |
| | | 39 | | |
| | | 40 | | public override void streamWriteImpl(Ice.OutputStream s) |
| | | 41 | | { |
| | | 42 | | _delegate.streamWriteImpl(s); |
| | | 43 | | s.writeString(_resource); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | public override int timeout() => _delegate.timeout(); |
| | | 47 | | |
| | | 48 | | public override EndpointI timeout(int timeout) |
| | | 49 | | { |
| | | 50 | | if (timeout == _delegate.timeout()) |
| | | 51 | | { |
| | | 52 | | return this; |
| | | 53 | | } |
| | | 54 | | else |
| | | 55 | | { |
| | | 56 | | return new WSEndpoint(_instance, _delegate.timeout(timeout), _resource); |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | public override string connectionId() => _delegate.connectionId(); |
| | | 61 | | |
| | | 62 | | public override EndpointI connectionId(string connectionId) |
| | | 63 | | { |
| | | 64 | | if (connectionId.Equals(_delegate.connectionId(), StringComparison.Ordinal)) |
| | | 65 | | { |
| | | 66 | | return this; |
| | | 67 | | } |
| | | 68 | | else |
| | | 69 | | { |
| | | 70 | | return new WSEndpoint(_instance, _delegate.connectionId(connectionId), _resource); |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | public override bool compress() => _delegate.compress(); |
| | | 75 | | |
| | | 76 | | public override EndpointI compress(bool compress) |
| | | 77 | | { |
| | | 78 | | if (compress == _delegate.compress()) |
| | | 79 | | { |
| | | 80 | | return this; |
| | | 81 | | } |
| | | 82 | | else |
| | | 83 | | { |
| | | 84 | | return new WSEndpoint(_instance, _delegate.compress(compress), _resource); |
| | | 85 | | } |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | public override bool datagram() => _delegate.datagram(); |
| | | 89 | | |
| | | 90 | | public override bool secure() => _delegate.secure(); |
| | | 91 | | |
| | | 92 | | public override Transceiver transceiver() => null; |
| | | 93 | | |
| | | 94 | | private sealed class EndpointI_connectorsI : EndpointI_connectors |
| | | 95 | | { |
| | 1 | 96 | | public EndpointI_connectorsI(ProtocolInstance instance, string host, string res, EndpointI_connectors cb) |
| | | 97 | | { |
| | 1 | 98 | | _instance = instance; |
| | 1 | 99 | | _host = host; |
| | 1 | 100 | | _resource = res; |
| | 1 | 101 | | _callback = cb; |
| | 1 | 102 | | } |
| | | 103 | | |
| | | 104 | | public void connectors(List<Connector> connectors) |
| | | 105 | | { |
| | 1 | 106 | | var l = new List<Connector>(); |
| | 1 | 107 | | foreach (Connector c in connectors) |
| | | 108 | | { |
| | 1 | 109 | | l.Add(new WSConnector(_instance, c, _host, _resource)); |
| | | 110 | | } |
| | 1 | 111 | | _callback.connectors(l); |
| | 1 | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | 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 | | { |
| | | 124 | | string host = ""; |
| | | 125 | | for (Ice.EndpointInfo p = _delegate.getInfo(); p != null; p = p.underlying) |
| | | 126 | | { |
| | | 127 | | if (p is Ice.IPEndpointInfo) |
| | | 128 | | { |
| | | 129 | | var ipInfo = (Ice.IPEndpointInfo)p; |
| | | 130 | | host = ipInfo.host + ":" + ipInfo.port; |
| | | 131 | | break; |
| | | 132 | | } |
| | | 133 | | } |
| | | 134 | | _delegate.connectors_async(new EndpointI_connectorsI(_instance, host, _resource, callback)); |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | public override Acceptor acceptor(string adapterName, SslServerAuthenticationOptions serverAuthenticationOptions) => |
| | | 138 | | new WSAcceptor(this, _instance, _delegate.acceptor(adapterName, serverAuthenticationOptions)); |
| | | 139 | | |
| | | 140 | | public WSEndpoint endpoint(EndpointI delEndp) |
| | | 141 | | { |
| | | 142 | | if (delEndp == _delegate) |
| | | 143 | | { |
| | | 144 | | return this; |
| | | 145 | | } |
| | | 146 | | else |
| | | 147 | | { |
| | | 148 | | return new WSEndpoint(_instance, delEndp, _resource); |
| | | 149 | | } |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | public override List<EndpointI> expandHost() => |
| | | 153 | | _delegate.expandHost().Select(e => endpoint(e) as EndpointI).ToList(); |
| | | 154 | | |
| | | 155 | | public override bool isLoopbackOrMulticast() => _delegate.isLoopbackOrMulticast(); |
| | | 156 | | |
| | | 157 | | public override EndpointI toPublishedEndpoint(string publishedHost) => |
| | | 158 | | endpoint(_delegate.toPublishedEndpoint(publishedHost)); |
| | | 159 | | |
| | | 160 | | public override bool equivalent(EndpointI endpoint) |
| | | 161 | | { |
| | | 162 | | if (!(endpoint is WSEndpoint)) |
| | | 163 | | { |
| | | 164 | | return false; |
| | | 165 | | } |
| | | 166 | | var wsEndpointI = (WSEndpoint)endpoint; |
| | | 167 | | return _delegate.equivalent(wsEndpointI._delegate); |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | public override string options() |
| | | 171 | | { |
| | | 172 | | // |
| | | 173 | | // WARNING: Certain features, such as proxy validation in Glacier2, |
| | | 174 | | // depend on the format of proxy strings. Changes to toString() and |
| | | 175 | | // methods called to generate parts of the reference string could break |
| | | 176 | | // these features. Please review for all features that depend on the |
| | | 177 | | // format of proxyToString() before changing this and related code. |
| | | 178 | | // |
| | | 179 | | string s = _delegate.options(); |
| | | 180 | | |
| | | 181 | | if (_resource != null && _resource.Length > 0) |
| | | 182 | | { |
| | | 183 | | s += " -r "; |
| | | 184 | | bool addQuote = _resource.Contains(':', StringComparison.Ordinal); |
| | | 185 | | if (addQuote) |
| | | 186 | | { |
| | | 187 | | s += "\""; |
| | | 188 | | } |
| | | 189 | | s += _resource; |
| | | 190 | | if (addQuote) |
| | | 191 | | { |
| | | 192 | | s += "\""; |
| | | 193 | | } |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | return s; |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | public override int GetHashCode() => HashCode.Combine(_delegate.GetHashCode(), _resource); |
| | | 200 | | |
| | | 201 | | public override int CompareTo(EndpointI obj) |
| | | 202 | | { |
| | | 203 | | if (!(obj is WSEndpoint)) |
| | | 204 | | { |
| | | 205 | | return type() < obj.type() ? -1 : 1; |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | var p = (WSEndpoint)obj; |
| | | 209 | | if (this == p) |
| | | 210 | | { |
| | | 211 | | return 0; |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | int v = string.Compare(_resource, p._resource, StringComparison.Ordinal); |
| | | 215 | | if (v != 0) |
| | | 216 | | { |
| | | 217 | | return v; |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | return _delegate.CompareTo(p._delegate); |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | protected override bool checkOption(string option, string argument, string endpoint) |
| | | 224 | | { |
| | | 225 | | switch (option[1]) |
| | | 226 | | { |
| | | 227 | | case 'r': |
| | | 228 | | { |
| | | 229 | | _resource = argument ?? throw new ParseException( |
| | | 230 | | $"no argument provided for -r option in endpoint '{endpoint}{_delegate.options()}'"); |
| | | 231 | | return true; |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | default: |
| | | 235 | | { |
| | | 236 | | return false; |
| | | 237 | | } |
| | | 238 | | } |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | private readonly ProtocolInstance _instance; |
| | | 242 | | private readonly EndpointI _delegate; |
| | | 243 | | private string _resource; |
| | | 244 | | } |
| | | 245 | | |
| | | 246 | | public class WSEndpointFactory : EndpointFactoryWithUnderlying |
| | | 247 | | { |
| | | 248 | | public WSEndpointFactory(ProtocolInstance instance, short type) |
| | | 249 | | : base(instance, type) |
| | | 250 | | { |
| | | 251 | | } |
| | | 252 | | |
| | | 253 | | public override EndpointFactory cloneWithUnderlying(ProtocolInstance instance, short underlying) => |
| | | 254 | | new WSEndpointFactory(instance, underlying); |
| | | 255 | | |
| | | 256 | | protected override EndpointI createWithUnderlying(EndpointI underlying, List<string> args, bool oaEndpoint) => |
| | | 257 | | new WSEndpoint(instance_, underlying, args); |
| | | 258 | | |
| | | 259 | | protected override EndpointI readWithUnderlying(EndpointI underlying, Ice.InputStream s) => |
| | | 260 | | new WSEndpoint(instance_, underlying, s); |
| | | 261 | | } |