Aliendreamer

http request web server

Sep 29th, 2018
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.75 KB | None | 0 0
  1. namespace SIS.HTTP.Requests
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Net;
  7.     using Common;
  8.     using Enums;
  9.     using Exceptions;
  10.     using Headers;
  11.  
  12.     public class HttpRequest : IHttpRequest
  13.     {
  14.         public HttpRequest(string requestString)
  15.         {
  16.             this.FormData = new Dictionary<string, object>();
  17.             this.QueryData = new Dictionary<string, object>();
  18.             this.Headers = new HttpHeaderCollection();
  19.  
  20.             this.ParseRequest(requestString);
  21.         }
  22.  
  23.         public string Url { get; private set; }
  24.  
  25.         public string Path { get; private set; }
  26.  
  27.         public IDictionary<string, object> FormData { get; }
  28.  
  29.         public IDictionary<string, object> QueryData { get; }
  30.  
  31.         public IHttpHeaderCollection Headers { get; }
  32.  
  33.         public HttpRequestMethod RequestMethod { get; private set; }
  34.  
  35.  
  36.         private void ParseRequest(string requestString)
  37.         {
  38.  
  39.             string[] splitRequestContent = requestString
  40.                 .Split(new[] { Environment.NewLine }, StringSplitOptions.None);
  41.  
  42.  
  43.             string[] requestLine = splitRequestContent[0].Trim()
  44.                 .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  45.  
  46.             if (!this.IsValidRequestLine(requestLine))
  47.             {
  48.                 throw new BadRequestException();
  49.             }
  50.  
  51.             this.ParseRequestMethod(requestLine);
  52.  
  53.             this.ParseRequestUrl(requestLine);
  54.  
  55.             this.ParseRequestPath(this.Url);
  56.  
  57.             this.ParseHeaders(splitRequestContent.Skip(1).ToArray());
  58.  
  59.             string queryParams = splitRequestContent[splitRequestContent.Length - 1];
  60.  
  61.             this.ParseRequestParameters(queryParams);
  62.         }
  63.  
  64.         private void ParseRequestParameters(string bodyParams)
  65.         {
  66.             this.ParseQueryParameters();
  67.  
  68.  
  69.             if (this.RequestMethod == HttpRequestMethod.Get)
  70.             {
  71.                 return;
  72.             }
  73.  
  74.             this.ParseFormDataParams(bodyParams);
  75.            
  76.  
  77.  
  78.         }
  79.         //TODO check this one!and FormDataParams!
  80.         private void ParseQueryParameters()
  81.         {
  82.  
  83.             if (!this.Url.Contains("?"))
  84.             {
  85.                 return;
  86.             }
  87.  
  88.             var queryParams = this.Url.Split(new[] { '?', '#' })
  89.                 .Skip(1)
  90.                 .Take(1)
  91.                 .ToArray()[0];
  92.  
  93.             // if (string.IsNullOrEmpty(queryParams))
  94.             // {
  95.             //     throw  new BadRequestException();
  96.             // }
  97.  
  98.  
  99.             var queryKeyValuePairs = queryParams.Split('&', StringSplitOptions.RemoveEmptyEntries);
  100.  
  101.             FillData(queryKeyValuePairs, this.QueryData);
  102.  
  103.         }
  104.  
  105.         private void ParseFormDataParams(string bodyParams)
  106.         {
  107.  
  108.  
  109.             var dataParams = bodyParams.Split('&', StringSplitOptions.RemoveEmptyEntries);
  110.  
  111.             FillData(dataParams, this.FormData);
  112.         }
  113.  
  114.         private void FillData(IEnumerable<string> dataParams, IDictionary<string, object> data)
  115.         {
  116.             foreach (var queryPair in dataParams)
  117.             {
  118.                 var queryKvp = queryPair.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
  119.  
  120.                 if (queryKvp.Length != 2)
  121.                 {
  122.                     throw new BadRequestException();
  123.                 }
  124.  
  125.                 var dataFormKey = WebUtility.UrlDecode(queryKvp[0]);
  126.                 var dataFormValue = WebUtility.UrlDecode(queryKvp[1]);
  127.  
  128.                 data[dataFormKey] = dataFormValue;
  129.             }
  130.         }
  131.  
  132.  
  133.         private void ParseHeaders(string[] headers)
  134.         {
  135.             var emptyLineAfterHeadersIndex = Array.IndexOf(headers, string.Empty);
  136.  
  137.             for (int i = 0; i < emptyLineAfterHeadersIndex; i++)
  138.             {
  139.                 var currentLine = headers[i];
  140.                 var headerParts = currentLine.Split(new[] { ": " }, StringSplitOptions.RemoveEmptyEntries);
  141.  
  142.                 if (headerParts.Length != 2)
  143.                 {
  144.                     throw new BadRequestException();
  145.                 }
  146.  
  147.                 var headerKey = headerParts[0];
  148.                 var headerValue = headerParts[1].Trim();
  149.  
  150.                 var header = new HttpHeader(headerKey, headerValue);
  151.  
  152.                 this.Headers.Add(header);
  153.             }
  154.  
  155.             if (!this.Headers.ContainsHeader(GlobalConstants.HostHeaderKey))
  156.             {
  157.                 throw new BadRequestException();
  158.             }
  159.         }
  160.  
  161.         private void ParseRequestPath(string url)
  162.         {
  163.  
  164.             this.Path = url.Split(new[] { '?', '#' }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
  165.  
  166.         }
  167.  
  168.         private void ParseRequestUrl(string[] requestLine)
  169.         {
  170.             string url = requestLine[1];
  171.             if (string.IsNullOrEmpty(url))
  172.             {
  173.                 throw new BadRequestException();
  174.             }
  175.  
  176.             this.Url = url;
  177.  
  178.         }
  179.  
  180.         private void ParseRequestMethod(string[] requestLine)
  181.         {
  182.             if (!requestLine.Any())
  183.             {
  184.                 throw new BadRequestException();
  185.             }
  186.  
  187.             string reqMethod = requestLine[0];
  188.             bool tryParseReqMethod = Enum.TryParse(reqMethod, true, out HttpRequestMethod method);
  189.  
  190.             if (!tryParseReqMethod)
  191.             {
  192.                 throw new BadRequestException();
  193.             }
  194.  
  195.             this.RequestMethod = method;
  196.         }
  197.  
  198.         private bool IsValidRequestLine(string[] requestLine)
  199.         {
  200.             bool validRequest = requestLine.Length == 3 && requestLine[2] == GlobalConstants.HttpOneProtocolFragment;
  201.  
  202.             return validRequest;
  203.         }
  204.  
  205.  
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment