Exemplo n.º 1
0
 public HttpRequest(RawRequest request)
     : base(request)
 {
     type = HttpRequestType.HttpPage;
     Requests = new Dictionary<string, string>();
     UrlParameters = new Dictionary<string, string>();
 }
Exemplo n.º 2
0
 public ApplicationRequest(RawRequest request)
 {
     this.rawrequest = request;
 }
Exemplo n.º 3
0
        /// <summary>
        /// This function check if the RawRequest be received from the browser is well formed.
        /// </summary>
        /// <param name="e"></param>
        /// <param name="req"></param>
        /// <returns></returns>
        public static bool TryValidate(RawRequest e, out HttpRequest req)
        {
            HttpRequestType request = HttpRequestType.HttpPage;
            try
            {
                ///
                /// There are a lot of tecnique to extract the information in an http browser request,
                /// this solutions split the request string and analize each blocks.
                ///

                req = new HttpRequest(e);
                ///
                ///Decode the bytes in Utf8 chars and Split the string request by "\r\n"
                ///
                req.CompleteRequest = new String(Encoding.UTF8.GetChars(e.RawData));

                string[] groups = req.CompleteRequest.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < groups.Length; i++)
                {
                    string headerblock = groups[i];

                    if (i > 0 && headerblock.Contains(":"))
                    {
                        //From the second block we have fileds with the pattern <name:value>
                        string[] block = headerblock.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                        req.Requests.Add(block[0], block[1]);
                    }
                    else
                    {
                        ///
                        /// The first block always include the request path ,the method and the protocol http.
                        /// ex. GET /path/resource.ext HTTP/1.1
                        ///

                        string[] subparts = headerblock.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                        //copy the path
                        req.CompletePath = subparts[1];
                        if (subparts[0] == "GET") req.Method = HttpMethodType.Get;
                        if (subparts[0] == "POST") req.Method = HttpMethodType.Post;
                        if (!String.IsNullOrEmpty(subparts[1]))
                        {
                            //split the path in "directories"
                            string[] resourcePaths = subparts[1].Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                            if (resourcePaths.Length > 0)
                            {
                                ///
                                /// We make a distinction between HttpStaticRequest and HttpPage
                                ///

                                string resourceFullName = resourcePaths[resourcePaths.Length - 1];

                                if (HttpHelper.IsStaticResource(resourceFullName))
                                {
                                    request = HttpRequestType.HttpStaticRequest;
                                }
                            }
                        }
                        else throw new InvalidOperationException("Invalid Request : " + req.CompleteRequest);

                        ///
                        /// separate the request path from possibly query-url
                        ///
                        string[] subPaths = subparts[1].Split(new string[] { "?" }, StringSplitOptions.RemoveEmptyEntries);

                        req.Path = subPaths[0];
                        req.Paths = subPaths[0].Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    }
                }

                //Save the query url parts in a dictionary : req.UrlParameters
                string[] queryparams = HttpHelper.GetUrlQueries(HttpHelper.RemoveToken(req.CompletePath));
                foreach (string p in queryparams)
                {
                    string[] query = p.Split(new String[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                    if (query.Length == 2)
                    {
                        req.UrlParameters.Add(query[0], query[1]);
                    }
                }
            }
            catch (Exception)
            {
                //If somethig goes wrong the validation return false.
                req = null;
                return false;
            }
            req.Type = request;
            return true;
        }