Exemplo n.º 1
0
        /// <summary>
        /// Get the server response
        /// </summary>
        /// <returns>Returns the handled request from the server to the client</returns>
        private HttpResponse GetResponse()
        {
            log4net.Config.XmlConfigurator.Configure();
            log.Info("Client connected");

            ReadingRequest reading = new ReadingRequest(stream);
            HttpRequest    request;

            try
            {
                request = reading.Read();
            }
            catch (BadRequestException)
            {
                errorLog.Error("Illegal request");
                return(new HttpResponse(400, "Illegal request"));
            }
            catch (MethodException)
            {
                errorLog.Error("Illegal method");
                return(new HttpResponse(400, "Illegal request"));
            }
            catch (ProtocolException)
            {
                errorLog.Error("Illegal protocol");
                return(new HttpResponse(400, "Illegal protocol"));
            }

            //TODO: log
            if (request.GetArguments.Count > 0)
            {
                log.Info("GET arguments:");
                foreach (KeyValuePair <string, string> pair in request.GetArguments)
                {
                    log.Info(pair.Key + ": " + pair.Value);
                }
            }
            if (request.PostArguments.Count > 0)
            {
                log.Info("POST arguments:");
                foreach (KeyValuePair <string, string> pair in request.PostArguments)
                {
                    log.Info(pair.Key + ": " + pair.Value);
                }
            }

            return(HandlingRequest.ProcessRequest(request));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parses the HTTP request
        /// </summary>
        /// <param name="line">HTTP request string</param>
        private void ParseRequestLine(string line)
        {
            XmlConfigurator.Configure();
            if (String.IsNullOrWhiteSpace(line))
            {
                throw new ArgumentException();
            }
            string[] requestWords = line.Split(' ');
            if (requestWords.Length != 3)
            {
                throw new ArgumentException();
            }

            Methods method;

            if (!Methods.TryParse(requestWords[0], out method))
            {
                errorLog.Error("Only GET requests are supported");
                throw new MethodException("Only GET requests are supported", "Method");
            }
            Method = method;

            Filename = requestWords[1];
            Filename = Uri.UnescapeDataString(Filename);
            Filename = Filename.Replace('+', ' ');

            int position = Filename.IndexOf('?');

            if (position >= 0)
            {
                string query = Filename.Substring(position + 1);
                IDictionary <string, string> dict = ReadingRequest.ParseQuery(query);
                if (dict != null)
                {
                    GetArguments = dict;
                }
                Filename = Filename.Substring(0, position);
            }

            if (Filename.Equals("/"))
            {
                Filename = INDEX_FILENAME;
            }

            Protocol = requestWords[2];

            string[] protocolWords = Protocol.Split('/');
            if (protocolWords.Length != 2)
            {
                errorLog.Error("Invalid protocol format");
                throw new ProtocolException("Invalid protocol format", "protocolWords");
            }
            if (!protocolWords[0].Equals("HTTP"))
            {
                errorLog.Error("Only HTTP is supported");
                throw new ProtocolException("Only HTTP is supported", "protocolWords[0]");
            }

            try
            {
                decimal protocolVersion = decimal.Parse(protocolWords[1]);
                if (protocolVersion < 1)
                {
                    errorLog.Error("Invalid HTTP version");
                    throw new ProtocolException("Invalid HTTP version", "protocolVersion");
                }
            }
            catch (FormatException)
            {
                errorLog.Error("Invalid HTTP version format");
                throw new ProtocolException("Invalid HTTP version format", "protocolVersion");
            }
        }