root/trunk/VerboseApp.d

Revision 24, 1.8 kB (checked in by xammy, 10 months ago)

Moved examles from trunk/examples to trunk/

Line 
1 import  fcgi.Application;
2 import  fcgi.Connection;
3 import  fcgi.Request;
4
5 import tango.text.convert.Layout;
6 import tango.io.Print;
7 import tango.io.Buffer;
8
9 class MyApp : FastCGIApplication
10 {
11     private Print!(char) stdout;
12     private Print!(char) stderr;
13     private static int counter = 0;
14
15     this (int id, FastCGIRequest request)
16     {
17         auto layout = new Layout!(char) ();
18         stdout = new Print!(char) (layout, request.stdout);
19         stderr = new Print!(char) (layout, request.stderr);
20     }
21    
22     int run()
23     {
24         stdout ("Content-type: text/html\r\n\r\n") ("<head><title>My first page</title></head>");
25         stdout ("<body>");
26
27         // some counters
28         synchronized
29             counter++;
30         stdout ("<h1> Hello world </h1><br>") ("Request #") (counter) (", Thread #") (id) ("<br><br>\n");
31
32         // Write IP
33         auto ip = ("REMOTE_ADDR" in request.args);
34         if (ip)
35         {
36             stdout ("Your IP is ") (*ip) ("<br><br>\n");
37         }
38         else
39         {
40             stdout ("Cannot extract IP :(<br><br>\n");
41         }
42
43         // Write stdin
44         stdout ("Data: \"") ();
45         while (1)
46         {
47             char[10] string;
48             int rd = request.stdin.read(string);
49             if (rd > 0)
50                 stdout(string[0 .. rd]);
51             if (rd != string.length)
52             {
53                 stdout ("\"<br><br>");
54                 break;
55             }
56         }
57
58         // Write all arguments
59         stdout ("\nYour environment:<br>\n");
60         foreach (key, value; request.args)
61         {
62             stdout ("request.args[\"")(key) ("\"] = \"") (value) ("\"<br>\n");
63         }
64         stdout ("</body>\n") ();
65
66         return 0;
67     }
68 }
69
70 int main(char[][] args)
71 {
72     FastCGIConnection connection = new FastCGIConnection ();
73
74     return FastCGIApplication.loop!(MyApp) (connection, true, 10);
75 }
Note: See TracBrowser for help on using the browser.