internal MSearchResponse(IHttpResponse response)
        {
            try
            {
                ResponseCastMethod = Convert.GetCastMetod(response);
                HostIp             = response.RemoteAddress;
                HostPort           = response.RemotePort;
                StatusCode         = response.StatusCode;
                ResponseReason     = response.ResponseReason;
                CacheControl       = TimeSpan.FromSeconds(Convert.GetMaxAge(response.Headers));
                Location           = Convert.UrlToUri(Convert.GetHeaderValue(response.Headers, "LOCATION"));
                Date           = Convert.ToRfc2616Date(Convert.GetHeaderValue(response.Headers, "DATE"));
                Ext            = response.Headers.ContainsKey("EXT");
                Server         = Convert.ConvertToServer(Convert.GetHeaderValue(response.Headers, "SERVER"));
                ST             = Convert.GetHeaderValue(response.Headers, "ST");
                USN            = Convert.GetHeaderValue(response.Headers, "USN");
                BOOTID         = Convert.GetHeaderValue(response.Headers, "BOOTID.UPNP.ORG");
                CONFIGID       = Convert.GetHeaderValue(response.Headers, "CONFIGID.UPNP.ORG");
                SEARCHPORT     = Convert.GetHeaderValue(response.Headers, "SEARCHPORT.UPNP.ORG");
                SECURELOCATION = Convert.GetHeaderValue(response.Headers, "SECURELOCATION.UPNP.ORG");

                Headers = HeaderHelper.SingleOutAdditionalHeaders(new List <string>
                {
                    "HOST", "CACHE-CONTROL", "LOCATION", "DATE", "EXT", "SERVER", "ST", "USN",
                    "BOOTID.UPNP.ORG", "CONFIGID.UPNP.ORG", "SEARCHPORT.UPNP.ORG", "SECURELOCATION.UPNP.ORG"
                }, response.Headers);
            }
            catch (Exception)
            {
                InvalidRequest = true;
            }
        }
        public MSearchRequest(IHttpRequest request, ILogger logger = null)
        {
            try
            {
                LocalIpEndPoint  = request.LocalIpEndPoint;
                RemoteIpEndPoint = request.RemoteIpEndPoint;
                TransportType    = Convert.GetCastMetod(request);
                MAN       = Convert.GetHeaderValue(request.Headers, "MAN");
                MX        = TimeSpan.FromSeconds(Convert.ConvertStringToInt(Convert.GetHeaderValue(request.Headers, "MX")));
                ST        = new ST(Convert.GetHeaderValue(request.Headers, "ST"), ignoreError: true);
                UserAgent = Convert.ConvertToUserAgent(Convert.GetHeaderValue(request.Headers, "USER-AGENT"));
                HOST      = Convert.GetHeaderValue(request.Headers, "HOST");

                CPFN           = Convert.GetHeaderValue(request.Headers, "CPFN.UPNP.ORG");
                CPUUID         = Convert.GetHeaderValue(request.Headers, "CPUUID.UPNP.ORG");
                TCPPORT        = Convert.GetHeaderValue(request.Headers, "TCPPORT.UPNP.ORG");
                SECURELOCATION = Convert.GetHeaderValue(request.Headers, "SECURELOCATION.UPNP.ORG");

                Headers = HeaderHelper.SingleOutAdditionalHeaders(new List <string>
                {
                    "HOST", "CACHE-CONTROL", "MAN", "MX", "ST", "USER-AGENT",
                    "CPFN.UPNP.ORG", "CPUUID.UPNP.ORG", "TCPPORT.UPNP.ORG", "SECURELOCATION.UPNP.ORG"
                }, request.Headers);

                HasParsingError = request.HasParsingErrors;
            }
            catch (Exception ex)
            {
                logger?.Error(ex);
                InvalidRequest = true;
            }
        }
        public MSearchRequest(IHttpRequest request)
        {
            try
            {
                SearchCastMethod = Convert.GetCastMetod(request);
                MAN            = Convert.GetHeaderValue(request.Headers, "MAN");
                HostIp         = request.RemoteAddress;
                HostPort       = request.RemotePort;
                MX             = TimeSpan.FromSeconds(Convert.ConvertStringToInt(Convert.GetHeaderValue(request.Headers, "MX")));
                ST             = Convert.GetHeaderValue(request.Headers, "ST");
                UserAgent      = Convert.ConvertToUserAgent(Convert.GetHeaderValue(request.Headers, "USER-AGENT"));
                CPFN           = Convert.GetHeaderValue(request.Headers, "CPFN.UPNP.ORG");
                CPUUID         = Convert.GetHeaderValue(request.Headers, "CPUUID.UPNP.ORG");
                TCPPORT        = Convert.GetHeaderValue(request.Headers, "TCPPORT.UPNP.ORG");
                SECURELOCATION = Convert.GetHeaderValue(request.Headers, "SECURELOCATION.UPNP.ORG");

                Headers = HeaderHelper.SingleOutAdditionalHeaders(new List <string>
                {
                    "HOST", "CACHE-CONTROL", "MAN", "MX", "ST", "USER-AGENT",
                    "CPFN.UPNP.ORG", "CPUUID.UPNP.ORG", "TCPPORT.UPNP.ORG", "SECURELOCATION.UPNP.ORG"
                }, request.Headers);
            }
            catch (Exception)
            {
                InvalidRequest = true;
            }
        }
Example #4
0
        private static byte[] ComposeNotifyDatagram(INotifySsdp notifySsdp)
        {
            var stringBuilder = new StringBuilder();

            stringBuilder.Append("NOTIFY * HTTP/1.1\r\n");

            stringBuilder.Append(notifySsdp.NotifyCastMethod == CastMethod.Multicast
                ? "HOST: 239.255.255.250:1900\r\n"
                : $"HOST: {notifySsdp.HostIp}:{notifySsdp.HostPort}\r\n");

            if (notifySsdp.NTS == NTS.Alive)
            {
                stringBuilder.Append($"CACHE-CONTROL: max-age = {notifySsdp.CacheControl.TotalSeconds}\r\n");
            }

            if (notifySsdp.NTS == NTS.Alive || notifySsdp.NTS == NTS.Update)
            {
                stringBuilder.Append($"LOCATION: {notifySsdp.Location.AbsoluteUri}\r\n");
            }

            stringBuilder.Append($"NT: {notifySsdp.NT}\r\n");
            stringBuilder.Append($"NTS: {Convert.GetNtsString(notifySsdp.NTS)}\r\n");

            if (notifySsdp.NTS == NTS.Alive)
            {
                stringBuilder.Append($"SERVER: " +
                                     $"{notifySsdp.Server.OperatingSystem}/{notifySsdp.Server.OperatingSystemVersion}/" +
                                     $" " +
                                     $"UPnP/{notifySsdp.Server.UpnpMajorVersion}.{notifySsdp.Server.UpnpMinorVersion}" +
                                     $" " +
                                     $"{notifySsdp.Server.ProductName}/{notifySsdp.Server.ProductVersion}\r\n");
            }

            stringBuilder.Append($"USN: {notifySsdp.USN}\r\n");
            stringBuilder.Append($"BOOTID.UPNP.ORG: {notifySsdp.BOOTID}\r\n");
            stringBuilder.Append($"CONFIGID.UPNP.ORG: {notifySsdp.CONFIGID}\r\n");

            if (notifySsdp.NTS == NTS.Alive || notifySsdp.NTS == NTS.Update)
            {
                HeaderHelper.AddOptionalHeader(stringBuilder, "SEARCHPORT.UPNP.ORG", notifySsdp.SEARCHPORT);
                HeaderHelper.AddOptionalHeader(stringBuilder, "SECURELOCATION.UPNP.ORG", notifySsdp.SECURELOCATION);
            }

            // Adding additional vendor specific headers if such are specified
            if (notifySsdp.Headers?.Any() ?? false)
            {
                foreach (var header in notifySsdp.Headers)
                {
                    stringBuilder.Append($"{header.Key}: {header.Value}\r\n");
                }
            }

            stringBuilder.Append("\r\n");

            return(Encoding.UTF8.GetBytes(stringBuilder.ToString()));
        }
Example #5
0
        internal MSearchResponse(IHttpResponse response, ILogger logger = null)
        {
            try
            {
                LocalIpEndPoint  = response.LocalIpEndPoint;
                RemoteIpEndPoint = response.RemoteIpEndPoint;
                HasParsingError  = response.HasParsingErrors;
                TransportType    = Convert.GetCastMetod(response);
                StatusCode       = response.StatusCode;
                ResponseReason   = response.ResponseReason;
                CacheControl     = TimeSpan.FromSeconds(Convert.GetMaxAge(response.Headers));
                Location         = Convert.UrlToUri(Convert.GetHeaderValue(response.Headers, "LOCATION"));
                Date             = Convert.ToRfc2616Date(Convert.GetHeaderValue(response.Headers, "DATE"));
                Ext            = response.Headers.ContainsKey("EXT");
                Server         = Convert.ConvertToServer(Convert.GetHeaderValue(response.Headers, "SERVER"));
                ST             = new ST(Convert.GetHeaderValue(response.Headers, "ST"), ignoreError: true);
                USN            = new USN(Convert.GetHeaderValue(response.Headers, "USN")); Convert.GetHeaderValue(response.Headers, "USN");
                BOOTID         = int.TryParse(Convert.GetHeaderValue(response.Headers, "BOOTID.UPNP.ORG"), out var b) ? b : 0;
                CONFIGID       = int.TryParse(Convert.GetHeaderValue(response.Headers, "CONFIGID.UPNP.ORG"), out var c) ? c : 0;
                SEARCHPORT     = int.TryParse(Convert.GetHeaderValue(response.Headers, "SEARCHPORT.UPNP.ORG"), out var s) ? s : 0;
                SECURELOCATION = Convert.GetHeaderValue(response.Headers, "SECURELOCATION.UPNP.ORG");

                Headers = HeaderHelper.SingleOutAdditionalHeaders(new List <string>
                {
                    "HOST", "CACHE-CONTROL", "LOCATION", "DATE", "EXT", "SERVER", "ST", "USN",
                    "BOOTID.UPNP.ORG", "CONFIGID.UPNP.ORG", "SEARCHPORT.UPNP.ORG", "SECURELOCATION.UPNP.ORG"
                }, response.Headers);

                RemoteIpEndPoint = response.RemoteIpEndPoint;
            }
            catch (Exception ex)
            {
                logger?.Error(ex);
                InvalidRequest = true;
            }
        }