コード例 #1
0
ファイル: HttpRequest.cs プロジェクト: ft-/silversim-testing
        public string GetUrlAndQueryParams(Dictionary <string, string> query)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }
            int qpos = RawUrl.IndexOf('?');

            query.Clear();
            if (qpos < 0)
            {
                return(RawUrl);
            }
            else
            {
                string url = RawUrl.Substring(0, qpos);
                foreach (string qp in RawUrl.Substring(qpos + 1).Split('&'))
                {
                    string qptrimmed = qp.Trim();
                    if (qptrimmed.Length == 0)
                    {
                        continue;
                    }
                    string[] vp = qptrimmed.Split('=');
                    query[vp[0]] = vp.Length < 2 ? string.Empty : vp[1];
                }
                return(url);
            }
        }
コード例 #2
0
        private void ensureprameter()
        {
            if (result == null)
            {
                result = new Dictionary <string, string>();
                //"/test.jpg?x=1&y=2"

                if (RawUrl != null)
                {
                    if (RawUrl.Contains('?'))
                    {
                        int      index = RawUrl.IndexOf('?');
                        string   sub   = RawUrl.Substring(index + 1);
                        string[] prams = sub.Split('&');
                        for (int i = 0; i < prams.Length; i++)
                        {
                            string[] pairs = prams[i].Split('=');
                            result.Add(pairs[0], pairs[1]);
                        }
                    }
                }
            }
        }
コード例 #3
0
ファイル: HttpRequest.cs プロジェクト: ft-/silversim-testing
        public string GetUrlAndQueryParams(List <KeyValuePair <string, string> > query)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }
            int qpos = RawUrl.IndexOf('?');

            query.Clear();
            if (qpos < 0)
            {
                return(RawUrl);
            }
            else
            {
                string url = RawUrl.Substring(0, qpos);
                foreach (string qp in RawUrl.Substring(qpos + 1).Split('&'))
                {
                    string qptrimmed = qp.Trim();
                    if (qptrimmed.Length == 0)
                    {
                        continue;
                    }
                    string[] vp = qptrimmed.Split('=');
                    if (vp.Length < 2)
                    {
                        query.Add(new KeyValuePair <string, string>(vp[0], string.Empty));
                    }
                    else
                    {
                        query.Add(new KeyValuePair <string, string>(vp[0], vp[1]));
                    }
                }
                return(url);
            }
        }
コード例 #4
0
        public HTTPHeader(String myHeader) : this()
        {
            PlainHeader    = myHeader;
            HttpStatusCode = HTTPStatusCodes.OK;
            KeepAlive      = false;
            String[] ProtocolArray;

            // Split the Request into lines
            String[] RequestParts = myHeader.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            if (RequestParts.Length == 0)
            {
                HttpStatusCode = HTTPStatusCodes.BadRequest;
                return;
            }

            #region Header first line

            // The first line containing the information about the HTTPMethod, the current folder/myPath (destination) and the protocol
            // e.g: PROPFIND /file/file Name HTTP/1.1
            String[] HTTPMethodHeader = RequestParts[0].Split(new char[] { ' ' });

            if (HTTPMethodHeader.Length < 3)
            {
                HttpStatusCode = HTTPStatusCodes.BadRequest;
                return;
            }

            // This is a response
            if (HTTPMethodHeader[0].StartsWith("HTTP"))
            {
                Protocol        = HTTPMethodHeader[0];
                ProtocolArray   = Protocol.Split(_SlashSeperator);
                ProtocolName    = ProtocolArray[0].ToLower();
                ProtocolVersion = new Version(ProtocolArray[1]);
                HttpStatusCode  = (HTTPStatusCodes)Enum.Parse(typeof(HTTPStatusCodes), HTTPMethodHeader[1]);
            }

            // This is a request
            else
            {
                if (!Enum.IsDefined(typeof(HTTPMethods), HTTPMethodHeader[0]))
                {
                    //HttpStatusCode = HTTPStatusCodes.NotImplemented;
                    HttpMethod       = HTTPMethods.UNKNOWN;
                    HttpMethodString = HTTPMethodHeader[0];
                    //return;
                    //throw new Exception("Invalid HTTP Method " + HTTPMethodHeader[0]);
                }
                else
                {
                    HttpMethod       = (HTTPMethods)Enum.Parse(typeof(HTTPMethods), HTTPMethodHeader[0]);
                    HttpMethodString = HTTPMethodHeader[0];
                }

                // Decode UTF-8 Hex encoding "%C3%B6" -> "ö" etc...
                Destination = RawUrl = HTTPMethodHeader[1];// HttpUtility.UrlDecode(HTTPMethodHeader[1]);
                //if (Destination.Length > 1 && Destination.EndsWith("/") && !Destination.Contains("/?"))
                //{
                //    RawUrl = HttpUtility.UrlDecode(HTTPMethodHeader[1]);//.TrimEnd(((String)"/").ToCharArray());
                //    if (RawUrl[RawUrl.Length-1] == '/')
                //    {
                //        RawUrl = RawUrl.Substring(0, RawUrl.Length - 1);
                //    }
                //    Destination = RawUrl;
                //}

                // Parse QueryString after '?' and maybe fix the Destination
                var _Questionmark = RawUrl.IndexOf('?');
                if (_Questionmark > -1)
                {
                    Destination = RawUrl.Substring(0, _Questionmark);
                    QueryString = HttpUtility.ParseQueryString(RawUrl.Substring(_Questionmark + 1));
                }

                //!svn/vcc/default
                var _Exclamationmark = RawUrl.IndexOf('!');
                if (_Exclamationmark > -1)
                {
                    SVNParameters = RawUrl.Substring(_Exclamationmark + 1);
                    Destination   = RawUrl.Substring(0, _Exclamationmark - 1);
                    if (Destination == "")
                    {
                        Destination = "/";
                    }
                }

                Protocol        = HTTPMethodHeader[2];
                ProtocolArray   = Protocol.Split(_SlashSeperator);
                ProtocolName    = ProtocolArray[0].ToLower();
                ProtocolVersion = new Version(ProtocolArray[1]);

                RawUrl = HTTPMethodHeader[1];
            }


            #endregion

            if (ProtocolVersion >= new Version(1, 1))
            {
                KeepAlive = true;
            }

            #region Parse all other Header informations

            Headers     = new NameValueCollection();
            AcceptTypes = new List <AcceptType>();

            for (Int16 i = 1; i < RequestParts.Length; i++)
            {
                // The Header is finished
                if (RequestParts[i] == String.Empty)
                {
                    break;
                }

                // Should never happen, however
                if (!RequestParts[i].Contains(':'))
                {
                    continue;
                }

                String Key   = RequestParts[i].Substring(0, RequestParts[i].IndexOf(':'));
                String Value = RequestParts[i].Substring(Key.Length + 1).Trim();

                if (Key.ToLower() == "accept")
                {
                    if (Value.Contains(","))
                    {
                        UInt32 place = 0;
                        foreach (var acc in Value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                        {
                            AcceptTypes.Add(new AcceptType(acc.Trim(), place++));
                        }
                    }
                    else
                    {
                        AcceptTypes.Add(new AcceptType(Value.Trim()));
                    }
                }
                else if (Key.ToLower() == "content-length")
                {
                    UInt64.TryParse(Value, out _ContentLength);
                }
                else if (Key.ToLower() == "content-type")
                {
                    ContentType = new System.Net.Mime.ContentType(Value);
                }
                else if (Key.ToLower() == "host")
                {
                    HostName = Value;
                }
                else if (Key.ToLower() == "accept-encoding")
                {
                    AcceptEncoding = Value;
                }

                /*
                 *  try
                 *  {
                 *      ContentEncoding = Encoding.GetEncoding(Value);
                 *  }
                 *  catch
                 *  {
                 *      ContentEncoding = Encoding.UTF8;
                 *  }
                 * */
                else if (Key.ToLower() == "user-agent")
                {
                    if (Value.Contains("Microsoft-WebDAV-MiniRedir"))
                    {
                        ClientType = ClientTypes.MicrosoftWebDAVMiniRedir;
                    }
                    else if (Value.Contains("MSIE 7"))
                    {
                        ClientType = ClientTypes.MSInternetExplorer7;
                    }
                    else if (Value.Contains("MSIE 8"))
                    {
                        ClientType = ClientTypes.MSInternetExplorer8;
                    }
                    else if (Value.Contains("Firefox"))
                    {
                        ClientType = ClientTypes.Firefox;
                    }
                    else if (Value.ToLower().Contains("svn"))
                    {
                        ClientType = ClientTypes.SVN;
                    }
                    else
                    {
                        ClientType = ClientTypes.Other;
                    }
                }
                else if (Key.ToLower() == "connection" && Value.ToLower().Contains("keep-alive"))
                {
                    KeepAlive = true;
                }
                else if (Key.ToLower() == "authorization")
                {
                    try
                    {
                        Authorization = new HTTPCredentials(Value);
                    }
                    catch (Exception)
                    {
                        //NLOG: temporarily commented
                        //_Logger.ErrorException("could not parse authorization of HTTPHeader", ex);
                    }
                }
                else
                {
                    Headers.Add(Key, Value);
                }
            }

            AcceptTypes.Sort();
            //AcceptTypes.Sort(new Comparison<AcceptType>((at1, at2) =>
            //{
            //    if (at1.Quality > at2.Quality) return 1;
            //    else if (at1.Quality == at2.Quality) return 0;
            //    else return -1;
            //    //if (at2.Quality > at1.Quality) return -1;
            //    //else return 1;
            //    //return at1.Quality.CompareTo(at2.Quality);
            //}
            //    ));

            #endregion
        }