| 1 | // Licensed to the .NET Foundation under one or more agreements.
|
|---|
| 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
|---|
| 3 |
|
|---|
| 4 | //
|
|---|
| 5 | // System.Net.ListenerPrefix
|
|---|
| 6 | //
|
|---|
| 7 | // Author:
|
|---|
| 8 | // Gonzalo Paniagua Javier (gonzalo@novell.com)
|
|---|
| 9 | // Oleg Mihailik (mihailik gmail co_m)
|
|---|
| 10 | //
|
|---|
| 11 | // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
|
|---|
| 12 | //
|
|---|
| 13 | // Permission is hereby granted, free of charge, to any person obtaining
|
|---|
| 14 | // a copy of this software and associated documentation files (the
|
|---|
| 15 | // "Software"), to deal in the Software without restriction, including
|
|---|
| 16 | // without limitation the rights to use, copy, modify, merge, publish,
|
|---|
| 17 | // distribute, sublicense, and/or sell copies of the Software, and to
|
|---|
| 18 | // permit persons to whom the Software is furnished to do so, subject to
|
|---|
| 19 | // the following conditions:
|
|---|
| 20 | //
|
|---|
| 21 | // The above copyright notice and this permission notice shall be
|
|---|
| 22 | // included in all copies or substantial portions of the Software.
|
|---|
| 23 | //
|
|---|
| 24 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|---|
| 25 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|---|
| 26 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|---|
| 27 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|---|
| 28 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|---|
| 29 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|---|
| 30 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|---|
| 31 | //
|
|---|
| 32 |
|
|---|
| 33 | // ReSharper disable RedundantUsingDirective
|
|---|
| 34 | // ReSharper disable NonReadonlyMemberInGetHashCode
|
|---|
| 35 |
|
|---|
| 36 | using System;
|
|---|
| 37 | using System.Diagnostics.CodeAnalysis;
|
|---|
| 38 | using System.Net;
|
|---|
| 39 |
|
|---|
| 40 | namespace SpaceWizards.HttpListener
|
|---|
| 41 | {
|
|---|
| 42 | internal sealed class ListenerPrefix
|
|---|
| 43 | {
|
|---|
| 44 | private string _original;
|
|---|
| 45 | private string? _host;
|
|---|
| 46 | private ushort _port;
|
|---|
| 47 | private string? _path;
|
|---|
| 48 | private bool _secure;
|
|---|
| 49 | private IPAddress[]? _addresses;
|
|---|
| 50 | internal HttpListener? _listener;
|
|---|
| 51 |
|
|---|
| 52 | public ListenerPrefix(string prefix)
|
|---|
| 53 | {
|
|---|
| 54 | _original = prefix;
|
|---|
| 55 | Parse(prefix);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | public override string ToString()
|
|---|
| 59 | {
|
|---|
| 60 | return _original;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | public IPAddress[]? Addresses
|
|---|
| 64 | {
|
|---|
| 65 | get { return _addresses; }
|
|---|
| 66 | set { _addresses = value; }
|
|---|
| 67 | }
|
|---|
| 68 | public bool Secure
|
|---|
| 69 | {
|
|---|
| 70 | get { return _secure; }
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | public string? Host
|
|---|
| 74 | {
|
|---|
| 75 | get { return _host; }
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | public int Port
|
|---|
| 79 | {
|
|---|
| 80 | get { return _port; }
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | public string? Path
|
|---|
| 84 | {
|
|---|
| 85 | get { return _path; }
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | // Equals and GetHashCode are required to detect duplicates in HttpListenerPrefixCollection.
|
|---|
| 89 | #if UNITY_NETFRAMEWORK
|
|---|
| 90 | public override bool Equals(object? o)
|
|---|
| 91 | #else
|
|---|
| 92 | public override bool Equals([NotNullWhen(true)] object? o)
|
|---|
| 93 | #endif
|
|---|
| 94 | {
|
|---|
| 95 | ListenerPrefix? other = o as ListenerPrefix;
|
|---|
| 96 | if (other == null)
|
|---|
| 97 | return false;
|
|---|
| 98 |
|
|---|
| 99 | return (_original == other._original);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | public override int GetHashCode()
|
|---|
| 103 | {
|
|---|
| 104 | return _original.GetHashCode();
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | private void Parse(string uri)
|
|---|
| 108 | {
|
|---|
| 109 | ushort default_port = 80;
|
|---|
| 110 | if (uri.StartsWith("https://", StringComparison.Ordinal))
|
|---|
| 111 | {
|
|---|
| 112 | default_port = 443;
|
|---|
| 113 | _secure = true;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | int length = uri.Length;
|
|---|
| 117 | int start_host = uri.IndexOf(':') + 3;
|
|---|
| 118 | if (start_host >= length)
|
|---|
| 119 | throw new ArgumentException(SR.net_listener_host);
|
|---|
| 120 |
|
|---|
| 121 | int end_host = ServiceNameStore.FindEndOfHostname(uri, start_host);
|
|---|
| 122 | int root;
|
|---|
| 123 | if (uri[end_host] == ':')
|
|---|
| 124 | {
|
|---|
| 125 | _host = uri.Substring(start_host, end_host - start_host);
|
|---|
| 126 | root = uri.IndexOf('/', end_host, length - end_host);
|
|---|
| 127 | #if UNITY_NETFRAMEWORK
|
|---|
| 128 | string portPart = uri.Substring (end_host + 1, root - end_host - 1);
|
|---|
| 129 | _port = (ushort)int.Parse (portPart);
|
|---|
| 130 | #else
|
|---|
| 131 | _port = (ushort)int.Parse(uri.AsSpan(end_host + 1, root - end_host - 1));
|
|---|
| 132 | #endif
|
|---|
| 133 | _path = uri.Substring(root);
|
|---|
| 134 | }
|
|---|
| 135 | else
|
|---|
| 136 | {
|
|---|
| 137 | root = uri.IndexOf('/', start_host, length - start_host);
|
|---|
| 138 | _host = uri.Substring(start_host, root - start_host);
|
|---|
| 139 | _port = default_port;
|
|---|
| 140 | _path = uri.Substring(root);
|
|---|
| 141 | }
|
|---|
| 142 | if (_path.Length != 1)
|
|---|
| 143 | _path = _path.Substring(0, _path.Length - 1);
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 | }
|
|---|