public WebCommandEventArgs(WebCommand command)
 {
     Command = command;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Parses a raw web request and filters out the command and arguments.
        /// </summary>
        /// <param name="rawData">The raw web request (including headers).</param>
        /// <returns>The parsed WebCommand if the request is valid, otherwise Null.</returns>
        private WebCommand InterpretRequest(string rawData)
        {
            string commandData;

            // Remove GET + Space
            if (rawData.Length > 5)
                commandData = rawData.Substring(5, rawData.Length - 5);
            else
                return null;

            // Remove everything after first space
            int idx = commandData.IndexOf("HTTP/1.1");
            commandData = commandData.Substring(0, idx - 1);

            // Split command and arguments
            string[] parts = commandData.Split('/');

            string command = null;
            if (parts.Length > 0)
            {
                // Parse first part to command
                command = parts[0].ToLower();
            }

            // Check if this is a valid command
            WebCommand outCmd = null;
            foreach (WebCommand cmd in AllowedCommands)
            {
                if (cmd.CommandString.ToLower() == command)
                {
                    outCmd = new WebCommand(cmd.CommandString, cmd.ArgumentCount);
                    break;
                }
            }
            if (outCmd == null)
            {
                return null;
            }
            else // Get command arguments
            {
                if ((parts.Length - 1) != outCmd.ArgumentCount)
                {
                    return null;
                }
                else
                {
                    outCmd.Arguments = new string[parts.Length - 1];

                    for (int i = 1; i < parts.Length; i++)
                    {
                        outCmd.Arguments[i - 1] = parts[i];
                    }
                    return outCmd;
                }
            }
        }