Beispiel #1
0
        private void PostHandler( string commandLine, Header header, string content )
        {
            string url = GetUrlFromCommandLine( commandLine );

              string[] queries = content.Split('&');

              Dictionary<string, string> queryPairs = new Dictionary<string, string>();

              foreach(string query in queries) {
            string[] keyValuePair = query.Split('=');
            queryPairs.Add(keyValuePair[0], keyValuePair[1]);
              }

              if(queryPairs["op"] == "%2B") //Ugly URL decoding for "+" and "/", since they are used
            queryPairs["op"] = "+";     //for inserting into the reply content, to set selection in the dropdown list.
              else if(queryPairs["op"] == "%2F")
            queryPairs["op"] = "/";

              double valueA = Double.Parse(queryPairs["valueA"]);
              double valueB = Double.Parse(queryPairs["valueB"]);
              double result = 0;

              switch(queryPairs["op"]) {
            case "+":
              result = valueA + valueB;
              break;
            case "-":
              result = valueA - valueB;
              break;
            case "*":
              result = valueA * valueB;
              break;
            case "/":
              result = valueA / valueB;
              break;
            default:
              break;
              }

              string replyContent = File.ReadAllText(ROOT_PATH + url).
                            Replace("name=\"valueA\"", "name=\"valueA\" value=\"" + queryPairs["valueA"] + "\"").
                            Replace("name=\"valueB\"", "name=\"valueB\" value=\"" + queryPairs["valueB"] + "\"").
                            Replace("\"" + queryPairs["op"] + "\"", "\"" + queryPairs["op"] + "\"" + " selected").
                            Replace("\"result\">", "\"result\">" + result);

              Header replyHeader = new Header();
              replyHeader.Add("Content-Type", "text/html");
              replyHeader.Add("Content-Length", replyContent.Length);

              SendLine("HTTP/1.1 200 OK");
              SendLine(replyHeader);
              SendLine("");
              SendLine(replyContent);
        }
Beispiel #2
0
        public void Run()
        {
            //WriteLine( "New connection from: " +
              //  connection.getInetAddress().getHostAddress() );

              WriteLine( "Client connected" );

              try {
            NetworkStream stream = new NetworkStream( connection );
            StreamReader reader = new StreamReader( stream );
            writer = new StreamWriter( stream );

            while ( true ) {
              string line;

              List<string> lines = new List<string>();

              while ( ( line = reader.ReadLine() ).Length > 0 ) {
            WriteLine( "header > " + line );
            lines.Add( line );
              }

              WriteLine( "request received" );

              Header header = new Header( lines, true );

              // new --- check if there is any content?

              string content;

              if ( header.Has( "content-length" ) ) {
            // note: there is no newline (i.e. empty line) after the content!
            content = ReadNChars( reader, Int32.Parse( header[ "content-length" ] ) );

            WriteLine( "content > " + content );
              } else
            content = string.Empty;

              // new --- end

              // new ----- added 'commandLine'-variable
              string commandLine = lines[ 0 ];
              string command = commandLine.Split()[ 0 ];

              WriteLine( "command: " + command );

              switch ( command.ToUpper() ) {
            case "GET":
              GetHandler( commandLine, header );
              break;

            case "POST":
              PostHandler( commandLine, header, content );
              break;
              }

              if ( header[ "CONNECTION" ].ToUpper() != "KEEP-ALIVE" )
            break;
            }

            // temp closing - keep alive?
            writer.Close();
            WriteLine( "Connection closed" );
              }
              catch ( NullReferenceException ) {
            WriteLine( "Client disconnected" );
              }
              catch ( Exception e ) {
            WriteLine( "I/O error" );
            WriteLine( e.Message );
            WriteLine( e.StackTrace );
              }
        }
Beispiel #3
0
        private void GetHandler( string commandLine, Header requestHeader )
        {
            string url = GetUrlFromCommandLine( commandLine );

              string content = File.ReadAllText( ROOT_PATH + url );

              Header replyHeader = new Header();
              if(commandLine.Split(' ')[1] != "/favicon.ico") {
            replyHeader.Add("Content-Type", "text/html");
            replyHeader.Add("Content-Length", content.Length);

            SendLine("HTTP/1.1 200 OK");
            SendLine(replyHeader);
            SendLine("");
            SendLine(content);
              } else
            WriteLine("No handling of favicon.ico request implemented");
        }