Guest User

Untitled

a guest
Dec 15th, 2019
87
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace HTTPServer
  7. {
  8. public enum RequestMethod
  9. {
  10. GET,
  11. POST,
  12. HEAD
  13. }
  14.  
  15. public enum HTTPVersion
  16. {
  17. HTTP10,
  18. HTTP11,
  19. HTTP09
  20. }
  21.  
  22. class Request
  23. {
  24. string[] requestLines;
  25. RequestMethod method;
  26. public string relativeURI;
  27. Dictionary<string, string> headerLines;
  28.  
  29. public Dictionary<string, string> HeaderLines
  30. {
  31. get { return headerLines; }
  32. }
  33.  
  34. HTTPVersion httpVersion;
  35. string requestString;
  36. string[] contentLines;
  37.  
  38. public Request(string requestString)
  39. {
  40. this.requestString = requestString;
  41. }
  42. /// <summary>
  43. /// Parses the request string and loads the request line, header lines and content, returns false if there is a parsing error
  44. /// </summary>
  45. /// <returns>True if parsing succeeds, false otherwise.</returns>
  46. public bool ParseRequest()
  47. {
  48. //TODO: parse the receivedRequest using the \r\n delimeter
  49. requestLines = requestString.Split('\n'); //Msh rady y-split xD
  50.  
  51. // check that there is atleast 3 lines: Request line, Host Header, Blank line (usually 4 lines with the last empty line for empty content)
  52.  
  53.  
  54. // Validate blank line exists
  55.  
  56. // Load header lines into HeaderLines dictionary
  57. return (ValidateBlankLine() && ParseRequestLine() && LoadHeaderLines());
  58.  
  59. }
  60.  
  61. private bool ParseRequestLine()
  62. {
  63. bool parsed = false;
  64. bool HTTP = false;
  65. string request = requestLines[0];
  66. string[] get = request.Split(' ');
  67. if (get[0] == "GET")
  68. {
  69. method = RequestMethod.GET;
  70. parsed = true;
  71. }
  72. else if (get[0] == "POST")
  73. {
  74. method = RequestMethod.POST;
  75. parsed = true;
  76. }
  77. else if (get[0] == "HEAD")
  78. {
  79. method = RequestMethod.HEAD;
  80. parsed = true;
  81. }
  82.  
  83. // Parse Request line
  84. relativeURI = get[1];
  85. if (get[2] == "HTTP09")
  86. {
  87. httpVersion = HTTPVersion.HTTP09;
  88. HTTP = true;
  89. }
  90. else if (get[2] == "HTTP10")
  91. {
  92. httpVersion = HTTPVersion.HTTP10;
  93. HTTP = true;
  94. }
  95. else if (get[2] == "HTTP11")
  96. {
  97. httpVersion = HTTPVersion.HTTP11;
  98. HTTP = true;
  99. }
  100. return parsed & HTTP;
  101. }
  102.  
  103. private bool ValidateIsURI(string uri)
  104. {
  105. return Uri.IsWellFormedUriString(uri, UriKind.RelativeOrAbsolute);
  106. }
  107.  
  108. private bool LoadHeaderLines()
  109. {
  110. int counter = 1;
  111. bool Loaded = false;
  112. while (requestLines[counter] != "\r\n")
  113. {
  114. string[] headrs = requestLines[counter].Split(':');
  115. headerLines.Add(headrs[0], headrs[1]);
  116. counter++;
  117. Loaded = true;
  118. }
  119. return Loaded;
  120. }
  121.  
  122. private bool ValidateBlankLine()
  123. {
  124. for(int i=0;i<requestLines.Length;i++)
  125. {
  126. if (requestLines[i] == "\r\n")
  127. return true;
  128. }
  129. return false;
  130. }
  131.  
  132. }
  133. }
RAW Paste Data