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); }
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"); }