Advertisement
Guest User

Untitled

a guest
Nov 12th, 2011
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 1.59 KB | None | 0 0
  1. void default_handler (Soup.Server server, Soup.Message msg, string path,
  2.                       GLib.HashTable<string,string?>? query, Soup.ClientContext client)
  3. {
  4.   var form_data = query;
  5.   if (msg.method == "POST") {
  6.     msg.request_body.flatten();
  7.     form_data = Soup.Form.decode((string) msg.request_body.data);
  8.   }
  9.   var user = (form_data != null) ? form_data.get("user") : "";
  10.  
  11.   var welcome = "";
  12.   if (user != null)
  13.     welcome = "<p>Welcome %s</p>".printf(user);
  14.    
  15.   var data = new HashTable<string, string>(str_hash, str_equal);
  16.   data.set("$user", user);
  17.   data.set("$welcome", welcome);
  18.   data.set("$path", path);
  19.  
  20.   string response_text = """
  21.    <html>
  22.      <body>
  23.        <p>Current location: $path</p>
  24.        $welcome
  25.        <form name='input' action='$path' method='get'>
  26.          Name: <input type='text' name='user' value='$user' />
  27.        <input type='submit' value='Submit GET' />
  28.        </form>
  29.        <form name='input' action='$path' method='post'>
  30.          Name: <input type='text' name='user' value='$user' />
  31.        <input type='submit' value='Submit POST'/>
  32.        </form>
  33.      </body>
  34.    </html>""";
  35.  
  36.   data.foreach((k, v) => {
  37.     response_text = response_text.replace(k, v);
  38.   });
  39.  
  40.   msg.set_response ("text/html", Soup.MemoryUse.COPY,
  41.                     response_text.data);
  42. }
  43.  
  44. int main (string[] args) {
  45.     var port = 8088;
  46.     var server = new Soup.Server (Soup.SERVER_PORT, port);
  47.     server.add_handler ("/", default_handler);
  48.     stdout.printf("Serving on http://localhost:%d\n", port);
  49.     server.run ();
  50.  
  51.     return 0;
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement