Exemplo n.º 1
0
        /// <summary>Parses the request line sent from the client</summary>
        /// <param name="requestLine">String containing the received request line</param>
        private void parseRequestLine(string requestLine)
        {
            // The RFC doesn't say that the request line must not contain any additional
            // spaces, so in the we will assume the first space terminates the method and the
            // last space terminates the URI.
            int uriDelimiterIndex = requestLine.IndexOf(' ');

            if (uriDelimiterIndex == -1)
            {
                throw Errors.BadRequest("Request-line is missing an URI");
            }

            // If there's only one space character, then the request is missing the version
            // of the HTTP protocol used.
            int versionDelimiterIndex = requestLine.LastIndexOf(' ');

            if (versionDelimiterIndex == uriDelimiterIndex)
            {
                throw Errors.BadRequest("Request-line does not specify HTTP version");
            }

            // Request seems to be at least be in the right layout. Extract the individual
            // components and pass them to the request container builder (validation of
            // the actual settings takes place once we have a complete request).
            requestBuilder.Method = requestLine.Substring(0, uriDelimiterIndex);
            requestBuilder.Uri    = requestLine.Substring(
                uriDelimiterIndex + 1, versionDelimiterIndex - uriDelimiterIndex - 1
                );
            requestBuilder.Version = requestLine.Substring(versionDelimiterIndex + 1);

            // We expect HTTP/1.* to stay compatible with the general format of the request.
            // Any other version of the protocol may include major changes to the request
            // format, thus we only accept HTTP/1.*.
            if (!requestBuilder.Version.StartsWith("HTTP/1."))
            {
                throw Errors.UnsupportedProtocolVersion();
            }
        }