You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

34 lines
1.1 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.AspNetCore.Http;
  5. namespace MQTTnet.AspNetCore
  6. {
  7. public static class MqttSubProtocolSelector
  8. {
  9. public static string SelectSubProtocol(HttpRequest request)
  10. {
  11. if (request == null) throw new ArgumentNullException(nameof(request));
  12. string subProtocol = null;
  13. if (request.Headers.TryGetValue("Sec-WebSocket-Protocol", out var requestedSubProtocolValues))
  14. {
  15. subProtocol = SelectSubProtocol(requestedSubProtocolValues);
  16. }
  17. return subProtocol;
  18. }
  19. public static string SelectSubProtocol(IList<string> requestedSubProtocolValues)
  20. {
  21. if (requestedSubProtocolValues == null) throw new ArgumentNullException(nameof(requestedSubProtocolValues));
  22. // Order the protocols to also match "mqtt", "mqttv-3.1", "mqttv-3.11" etc.
  23. return requestedSubProtocolValues
  24. .OrderByDescending(p => p.Length)
  25. .FirstOrDefault(p => p.ToLower().StartsWith("mqtt"));
  26. }
  27. }
  28. }