HttpHeader.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. //-----------------------------------------------------------------------------
  5. // Copyright 2020-2021 RenderHeads Ltd. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. namespace RenderHeads.Media.AVProVideo
  8. {
  9. [System.Serializable]
  10. public struct HttpHeader
  11. {
  12. public string name;
  13. public string value;
  14. public HttpHeader(string name, string value) { this.name = name; this.value = value; }
  15. public bool IsComplete()
  16. {
  17. return (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value));
  18. }
  19. public string ToValidatedString()
  20. {
  21. string result = null;
  22. if (IsComplete())
  23. {
  24. if (IsValid())
  25. {
  26. result = string.Format("{0}:{1}\r\n", name, value);
  27. }
  28. }
  29. return result;
  30. }
  31. public static bool IsValid(string text)
  32. {
  33. if (!string.IsNullOrEmpty(text))
  34. {
  35. if (!IsAscii(text)) return false;
  36. if (text.Contains("\r") || text.Contains("\n")) return false;
  37. }
  38. return true;
  39. }
  40. private static bool IsAscii(string text)
  41. {
  42. foreach (char c in text)
  43. {
  44. if (c >= 128) {
  45. return false;
  46. }
  47. }
  48. return true;
  49. }
  50. private bool IsValid()
  51. {
  52. if (!IsValid(name) || !IsValid(value))
  53. {
  54. return false;
  55. }
  56. // TODO: check via regular expression
  57. return true;
  58. }
  59. }
  60. /// <summary>
  61. /// Data for handling custom HTTP header fields
  62. /// </summary>
  63. [System.Serializable]
  64. public class HttpHeaderData : IEnumerable
  65. {
  66. [SerializeField]
  67. private List<HttpHeader> httpHeaders = new List<HttpHeader>();
  68. public IEnumerator GetEnumerator()
  69. {
  70. return httpHeaders.GetEnumerator();
  71. }
  72. public HttpHeader this[int index]
  73. {
  74. get
  75. {
  76. return httpHeaders[index];
  77. }
  78. }
  79. public void Clear()
  80. {
  81. httpHeaders.Clear();
  82. }
  83. public void Add(string name, string value)
  84. {
  85. httpHeaders.Add(new HttpHeader(name, value));
  86. }
  87. public bool IsModified()
  88. {
  89. return (httpHeaders != null && httpHeaders.Count > 0);
  90. }
  91. public string ToValidatedString()
  92. {
  93. string result = string.Empty;
  94. foreach (HttpHeader header in httpHeaders)
  95. {
  96. if (header.IsComplete())
  97. {
  98. string line = header.ToValidatedString();
  99. if (!string.IsNullOrEmpty(line))
  100. {
  101. result += line;
  102. }
  103. else
  104. {
  105. Debug.LogWarning("[AVProVideo] Custom HTTP header field ignored due to invalid format");
  106. }
  107. }
  108. }
  109. return result;
  110. }
  111. }
  112. }