jellyfin/MediaBrowser.Dlna/Ssdp/SsdpHelper.cs

46 lines
1.5 KiB
C#
Raw Normal View History

2014-02-26 21:31:47 +00:00
using System;
2014-03-25 05:25:03 +00:00
using System.Collections.Generic;
using System.IO;
2014-04-27 03:42:05 +00:00
using System.Net;
2014-02-26 21:31:47 +00:00
using System.Text;
2014-03-25 05:25:03 +00:00
namespace MediaBrowser.Dlna.Ssdp
2014-02-26 21:31:47 +00:00
{
public class SsdpHelper
{
2014-04-27 03:42:05 +00:00
public static SsdpMessageEventArgs ParseSsdpResponse(byte[] data, IPEndPoint endpoint)
2014-02-26 21:31:47 +00:00
{
2014-04-27 03:42:05 +00:00
using (var ms = new MemoryStream(data))
2014-03-25 05:25:03 +00:00
{
2014-04-27 03:42:05 +00:00
using (var reader = new StreamReader(ms, Encoding.ASCII))
2014-03-25 05:25:03 +00:00
{
2014-04-27 03:42:05 +00:00
var proto = (reader.ReadLine() ?? string.Empty).Trim();
var method = proto.Split(new[] { ' ' }, 2)[0];
var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
for (var line = reader.ReadLine(); line != null; line = reader.ReadLine())
2014-03-25 05:25:03 +00:00
{
2014-04-27 03:42:05 +00:00
line = line.Trim();
if (string.IsNullOrEmpty(line))
{
break;
}
var parts = line.Split(new[] { ':' }, 2);
if (parts.Length >= 2)
{
headers[parts[0]] = parts[1].Trim();
}
2014-03-25 05:25:03 +00:00
}
2014-02-26 21:31:47 +00:00
2014-04-27 03:42:05 +00:00
return new SsdpMessageEventArgs
2014-03-25 05:25:03 +00:00
{
2014-04-27 03:42:05 +00:00
Method = method,
Headers = headers,
EndPoint = endpoint
};
2014-03-25 05:25:03 +00:00
}
}
2014-02-26 21:31:47 +00:00
}
}
}