Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void default_handler (Soup.Server server, Soup.Message msg, string path,
- GLib.HashTable<string,string?>? query, Soup.ClientContext client)
- {
- var form_data = query;
- if (msg.method == "POST") {
- msg.request_body.flatten();
- form_data = Soup.Form.decode((string) msg.request_body.data);
- }
- var user = (form_data != null) ? form_data.get("user") : "";
- var welcome = "";
- if (user != null)
- welcome = "<p>Welcome %s</p>".printf(user);
- var data = new HashTable<string, string>(str_hash, str_equal);
- data.set("$user", user);
- data.set("$welcome", welcome);
- data.set("$path", path);
- string response_text = """
- <html>
- <body>
- <p>Current location: $path</p>
- $welcome
- <form name='input' action='$path' method='get'>
- Name: <input type='text' name='user' value='$user' />
- <input type='submit' value='Submit GET' />
- </form>
- <form name='input' action='$path' method='post'>
- Name: <input type='text' name='user' value='$user' />
- <input type='submit' value='Submit POST'/>
- </form>
- </body>
- </html>""";
- data.foreach((k, v) => {
- response_text = response_text.replace(k, v);
- });
- msg.set_response ("text/html", Soup.MemoryUse.COPY,
- response_text.data);
- }
- int main (string[] args) {
- var port = 8088;
- var server = new Soup.Server (Soup.SERVER_PORT, port);
- server.add_handler ("/", default_handler);
- stdout.printf("Serving on http://localhost:%d\n", port);
- server.run ();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement