Example #1
0
        /// <summary>
        /// Writes the HTTP request line to the stream.
        /// </summary>
        /// <param name="requestLine">The request line.</param>
        public void WriteRequestLine(HttpRequestLine requestLine)
        {
            // method
            writer.Write(requestLine.Method.ToString().ToUpper() + " ");

            // path
            writer.Write(HttpUtilities.EncodeString(requestLine.Path) + " ");

            // query
            if (requestLine.Query.Count > 0) {
                // query parameters start
                writer.Write("?");

                // pair up
                string[] pairs = new string[requestLine.Query.Count];

                int i = 0;
                foreach (KeyValuePair<string, string> kv in requestLine.Query) {
                    pairs[i] = HttpUtilities.EncodeString(kv.Key) + "=" + HttpUtilities.EncodeString(kv.Value);
                    i++;
                }

                // join pairs
                writer.Write(string.Join("&", pairs));
            }

            // version
            writer.WriteLine(requestLine.Version);

            // flush
            writer.Flush();
        }
Example #2
0
        /// <summary>
        /// Writes the HTTP request line to the stream.
        /// </summary>
        /// <param name="requestLine">The request line.</param>
        public void WriteRequestLine(HttpRequestLine requestLine)
        {
            // method
            writer.Write(requestLine.Method.ToString().ToUpper() + " ");

            // path
            writer.Write(HttpUtilities.EncodeString(requestLine.Path) + " ");

            // query
            if (requestLine.Query.Count > 0)
            {
                // query parameters start
                writer.Write("?");

                // pair up
                string[] pairs = new string[requestLine.Query.Count];

                int i = 0;
                foreach (KeyValuePair <string, string> kv in requestLine.Query)
                {
                    pairs[i] = HttpUtilities.EncodeString(kv.Key) + "=" + HttpUtilities.EncodeString(kv.Value);
                    i++;
                }

                // join pairs
                writer.Write(string.Join("&", pairs));
            }

            // version
            writer.WriteLine(requestLine.Version);

            // flush
            writer.Flush();
        }
Example #3
0
        /// <summary>
        /// Reads the HTTP request from the stream with the provided stream.
        /// </summary>
        /// <returns></returns>
        public HttpRequest ReadRequest()
        {
            // request line
            HttpRequestLine reqLine = ReadRequestLine();

            // create request
            HttpRequest req = new HttpRequest(transaction);

            req.Path   = reqLine.Path;
            req.Method = reqLine.Method;
            req.Query  = reqLine.Query;

            // headers
            req.Headers = ReadHeaders();

            // body
            object contentLength = req.Headers["Content-Length"];

            if (contentLength != null)
            {
                byte[] data = new byte[(int)contentLength];
                reader.BaseStream.Read(data, 0, (int)contentLength);
            }

            return(req);
        }
Example #4
0
        /// <summary>
        /// Reads a HTTP request line from the stream.
        /// </summary>
        /// <returns></returns>
        public HttpRequestLine ReadRequestLine()
        {
            // create request line
            HttpRequestLine requestLine = new HttpRequestLine();

            // read line
            string line = reader.ReadLine();

            // split
            string[] components = line.Split(' ');

            if (components.Length != 3)
            {
                throw new HttpException("The request line format is invalid", HttpStatus.BadRequest);
            }

            // version
            if (components[2].ToUpper() != "HTTP/1.1")
            {
                throw new HttpException("The version is invalid", HttpStatus.BadRequest);
            }

            requestLine.Version = components[2];

            // method
            bool validMethod = false;

            foreach (Enum e in Enum.GetValues(typeof(HttpMethod)))
            {
                if (e.ToString() == components[0].ToUpper())
                {
                    requestLine.Method = (HttpMethod)e;
                    validMethod        = true;
                    break;
                }
            }

            if (!validMethod)
            {
                throw new HttpException("The request method is not supported", HttpStatus.BadRequest);
            }

            // path
            string path = components[1];

            if (path.IndexOf('?') > -1)
            {
                // split at question mark
                string[] splitPath = path.Split('?');

                // check length
                if (splitPath.Length != 2)
                {
                    throw new HttpException("The path format is invalid", HttpStatus.BadRequest);
                }

                // path
                requestLine.Path = HttpUtilities.DecodeString(splitPath[0]);

                // query parameters
                string[] splitParams = splitPath[1].Split('&');

                foreach (string param in splitParams)
                {
                    // parameter
                    string[] paramSplit = param.Split('=');

                    if (paramSplit.Length != 2)
                    {
                        throw new HttpException("The path format is invalid", HttpStatus.BadRequest);
                    }

                    string key   = paramSplit[0];
                    string value = paramSplit[1];

                    // parameters
                    requestLine.Query.Add(key, value);
                }
            }
            else
            {
                requestLine.Path = HttpUtilities.DecodeString(path);
            }

            return(requestLine);
        }
Example #5
0
        /// <summary>
        /// Reads a HTTP request line from the stream.
        /// </summary>
        /// <returns></returns>
        public HttpRequestLine ReadRequestLine()
        {
            // create request line
            HttpRequestLine requestLine = new HttpRequestLine();

            // read line
            string line = reader.ReadLine();

            // split
            string[] components = line.Split(' ');

            if (components.Length != 3)
                throw new HttpException("The request line format is invalid", HttpStatus.BadRequest);

            // version
            if (components[2].ToUpper() != "HTTP/1.1")
                throw new HttpException("The version is invalid", HttpStatus.BadRequest);

            requestLine.Version = components[2];

            // method
            bool validMethod = false;

            foreach (Enum e in Enum.GetValues(typeof(HttpMethod))) {
                if (e.ToString() == components[0].ToUpper()) {
                    requestLine.Method = (HttpMethod)e;
                    validMethod = true;
                    break;
                }
            }

            if (!validMethod)
                throw new HttpException("The request method is not supported", HttpStatus.BadRequest);

            // path
            string path = components[1];

            if (path.IndexOf('?') > -1) {
                // split at question mark
                string[] splitPath = path.Split('?');

                // check length
                if (splitPath.Length != 2)
                    throw new HttpException("The path format is invalid", HttpStatus.BadRequest);

                // path
                requestLine.Path = HttpUtilities.DecodeString(splitPath[0]);

                // query parameters
                string[] splitParams = splitPath[1].Split('&');

                foreach (string param in splitParams) {
                    // parameter
                    string[] paramSplit = param.Split('=');

                    if (paramSplit.Length != 2)
                        throw new HttpException("The path format is invalid", HttpStatus.BadRequest);

                    string key = paramSplit[0];
                    string value = paramSplit[1];

                    // parameters
                    requestLine.Query.Add(key, value);
                }
            } else {
                requestLine.Path = HttpUtilities.DecodeString(path);
            }

            return requestLine;
        }