예제 #1
0
        /// <summary>
        /// Creates a new context object for a web request
        /// but reading the data sent to a recently opened
        /// socket.
        /// </summary>
        public static LPWebContext FromSocket(Socket socket)
        {
            var ret = new LPWebContext();

            ret.ConnectedSocket = socket;
            ret.Request         = LPWebRequest.Read(socket);
            ret.Response        = new LPWebResponse(ret);
            return(ret);
        }
예제 #2
0
        /// <summary>
        /// Creates a web request record from an incoming request
        /// converted to a string.
        /// </summary>
        public static LPWebRequest Parse(string request)
        {
            var ret = new LPWebRequest();

            request = request.Replace("\r\n", "\n");
            var lines = request.Split('\n');

            // parse the first line
            if (lines.Length == 0 || string.IsNullOrEmpty(lines[0]))
            {
                throw new Exception("Invalid first line of request.");
            }

            var httpVersionIndex = lines[0].LastIndexOf(" HTTP");

            if (httpVersionIndex > -1)
            {
                lines[0] = lines[0].Substring(0, httpVersionIndex);
            }

            var blankIndex = lines[0].IndexOf(" ");

            if (blankIndex == -1)
            {
                throw new Exception("Invalid first line of request.");
            }

            ret.Method = lines[0].Substring(0, blankIndex);
            ret.RawUrl = lines[0].Substring(blankIndex + 1);

            // parse the raw url
            var qIndex = ret.RawUrl.IndexOf("?");

            if (qIndex > -1)
            {
                ret.Path  = ret.RawUrl.Substring(0, qIndex);
                ret.Query = ret.RawUrl.Substring(qIndex + 1);
                foreach (var pair in ret.Query.Split('&'))
                {
                    var i = pair.IndexOf('=');
                    if (i > -1)
                    {
                        string key   = pair.Substring(0, i);
                        var    value = System.Net.WebUtility.UrlDecode(pair.Substring(i + 1));
                        ret.QueryString.Add(key, value);
                    }
                    else
                    {
                        ret.QueryString.Add(pair, "");
                    }
                }
            }
            else
            {
                ret.Query = null;
                ret.Path  = ret.RawUrl;
            }

            // parse the headers
            for (var i = 1; i < lines.Length; i++)
            {
                if (lines[i] == "")
                {
                    break;
                }
                var colon = lines[i].IndexOf(": ");
                if (colon == -1)
                {
                    continue;
                }
                var key   = lines[i].Substring(0, colon);
                var value = lines[i].Substring(colon + 2);
                ret.Headers.Add(key, value);
            }

            return(ret);
        }