example.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // This is C# example on how to use Mongoose embeddable web server,
  2. // http://code.google.com/p/mongoose
  3. //
  4. // Before using the mongoose module, make sure that Mongoose shared library is
  5. // built and present in the current (or system library) directory
  6. using System;
  7. using System.Runtime.InteropServices;
  8. public class Program {
  9. // This function is called when user types in his browser http://127.0.0.1:8080/foo
  10. static private void UriHandler(MongooseConnection conn, MongooseRequestInfo ri) {
  11. conn.write("HTTP/1.1 200 OK\r\n\r\n");
  12. conn.write("Hello from C#!\n");
  13. conn.write("Your user-agent is: " + conn.get_header("User-Agent") + "\n");
  14. }
  15. static private void UriDumpInfo(MongooseConnection conn, MongooseRequestInfo ri)
  16. {
  17. conn.write("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  18. conn.write("<html><body><head>Calling Info</head>");
  19. conn.write("<p>Request: " + ri.request_method + "</p>");
  20. conn.write("<p>URI: " + ri.uri + "</p>");
  21. conn.write("<p>Query: " + ri.query_string + "</p>");
  22. if (ri.post_data_len > 0) conn.write("<p>Post(" + ri.post_data_len + ")[@" + ri.post_data + "]: '" + Marshal.PtrToStringAnsi(ri.post_data) + "'</p>");
  23. conn.write("<p>User:" + ri.remote_user + "</p>");
  24. conn.write("<p>IP: " + ri.remote_ip + "</p>");
  25. conn.write("<p>HTTP: " + ri.http_version + "</p>");
  26. conn.write("<p>Port: " + ri.remote_port + "</p>");
  27. conn.write("<p>NUM Headers: " + ri.num_headers + "</p>");
  28. for (int i = 0; i < Math.Min(64, ri.num_headers); i++)
  29. {
  30. conn.write("<p>" + i + ":" + Marshal.PtrToStringAnsi(ri.http_headers[i].name)
  31. + ":" + Marshal.PtrToStringAnsi(ri.http_headers[i].value) + "</p>");
  32. }
  33. conn.write("</body></html>");
  34. }
  35. static void Main() {
  36. Mongoose web_server = new Mongoose();
  37. // Set options and /foo URI handler
  38. web_server.set_option("ports", "8080");
  39. web_server.set_option("root", "c:\\");
  40. web_server.set_uri_callback("/foo", new MongooseCallback(UriHandler));
  41. web_server.set_uri_callback("/dumpinfo", new MongooseCallback(UriDumpInfo));
  42. // Serve requests until user presses "enter" on a keyboard
  43. Console.ReadLine();
  44. }
  45. }