2014-02-26 14:31:47 -07:00
|
|
|
|
using System;
|
2014-03-24 22:25:03 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2014-04-26 20:42:05 -07:00
|
|
|
|
using System.Net;
|
2014-02-26 14:31:47 -07:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
2014-03-24 22:25:03 -07:00
|
|
|
|
namespace MediaBrowser.Dlna.Ssdp
|
2014-02-26 14:31:47 -07:00
|
|
|
|
{
|
|
|
|
|
public class SsdpHelper
|
|
|
|
|
{
|
2014-04-26 20:42:05 -07:00
|
|
|
|
public static SsdpMessageEventArgs ParseSsdpResponse(byte[] data, IPEndPoint endpoint)
|
2014-02-26 14:31:47 -07:00
|
|
|
|
{
|
2014-04-26 20:42:05 -07:00
|
|
|
|
using (var ms = new MemoryStream(data))
|
2014-03-24 22:25:03 -07:00
|
|
|
|
{
|
2014-04-26 20:42:05 -07:00
|
|
|
|
using (var reader = new StreamReader(ms, Encoding.ASCII))
|
2014-03-24 22:25:03 -07:00
|
|
|
|
{
|
2014-04-26 20:42:05 -07: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-24 22:25:03 -07:00
|
|
|
|
{
|
2014-04-26 20:42:05 -07: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-24 22:25:03 -07:00
|
|
|
|
}
|
2014-02-26 14:31:47 -07:00
|
|
|
|
|
2014-04-26 20:42:05 -07:00
|
|
|
|
return new SsdpMessageEventArgs
|
2014-03-24 22:25:03 -07:00
|
|
|
|
{
|
2014-04-26 20:42:05 -07:00
|
|
|
|
Method = method,
|
|
|
|
|
Headers = headers,
|
|
|
|
|
EndPoint = endpoint
|
|
|
|
|
};
|
2014-03-24 22:25:03 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-26 14:31:47 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|