1 /********************************************************************
2 Hello World Application for EhttpD
3
4 Simple example of how to use EasyHTTPD
5 Step by Step
6
7
8 EhttpD - EasyHTTP Server/Parser C++ Class
9
10 http://www.littletux.com
11
12 Copyright (c) 2007, Barry Sprajc
13
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 "AS IS". USE AT YOUR OWN RISK.
17
18 ********************************************************************/
19 #include <time.h>
20 #include <stdio.h>
21
22 #include "./embedhttp.h" //The header file
23 #include "./connection.h" //A helper class to listen and accept
24 //incomming connections. You don't
25 //have to use this class, you can use
26 //your own method to get a socket
27
28
29 int handleDefault( ehttp &obj, void *cookie );
30
31 int main( int argc, char *argv[] )
32 {
33 //---------------------------------------------------------------
34 // #1a
35 // Instantiate an ehhtpd object, and call the objects init()
36 // method.
37 //---------------------------------------------------------------
38 ehttp http;
39 if( http.init() != EHTTP_ERR_OK )
40 {
41 printf("Can't initalize ehttpd\r\n");
42 exit(1);
43 }
44
45
46 //---------------------------------------------------------------
47 // #2a
48 // Add at least one request handler to the object.
49 // Allways add a default handler. The default
50 // handler has the filename parameter == NULL.
51 //---------------------------------------------------------------
52 http.add_handler(NULL,handleDefault);
53
54
55 // Listen on port 80 for requests
56 Connection c(1);
57 if( c.init(80) )
58 exit(1);
59
60
61 // Process incomming requests
62 int s=0;
63 printf("Wait for request\r\n");
64 while(s>-1)
65 {
66 printf("accept...\r\n");
67 int s=c.accept();
68 if( s >-1 )
69 {
70 printf("parse...\r\n");
71
72 //---------------------------------------------------------------
73 // #3a
74 // For each socket accepted, call the objects parse_request()
75 // method. The second param is a void* which is passed to the
76 // request handler, and provides a link between the request
77 // handler and your application.
78 //---------------------------------------------------------------
79
80 http.parse_request(s, NULL);
81 printf("close...\r\n");
82 c.close(s);
83 }
84 }
85
86 // Close down our connection class
87 c.terminate();
88
89 return 0;
90 }
91
92 /*
93 This is the default request handler.
94 You must allways have a default request handler, to
95 handle those requests which are not supported.
96
97 The default handler filename is NULL when you add_handler()
98 */
99 int handleDefault( ehttp &obj, void *cookie )
100 {
101 //---------------------------------------------------------------
102 // #1b
103 // Specify the name of the HTML template file to be used.
104 // The template file is just a HTML file, with ##TAGS## embedded
105 // and these tags get replace with something when out_replace
106 // is called
107 //---------------------------------------------------------------
108 obj.out_set_file("helloworld_template.html");
109
110 //---------------------------------------------------------------
111 // #2b
112 // This is where most of the work happens in the request
113 // handler.
114 //
115 // Create your dynamic content. In this case, we simply replace
116 // the ##MESSAGE## tag in the template file with the "Hello
117 // World" string.
118 //
119 // At this point, you might check for get or post requests
120 // and pass commands to your app via the cookie, or get content
121 // (application status as one example) via the cookie.
122 //---------------------------------------------------------------
123 obj.out_replace_token("MESSAGE","Hello World");
124
125 //---------------------------------------------------------------
126 // #3b
127 // Replace all the tags in the template file with tokens
128 //---------------------------------------------------------------
129 obj.out_replace();
130
131 //---------------------------------------------------------------
132 // #4b
133 // Write the content to the socket
134 //---------------------------------------------------------------
135 return obj.out_commit();
136 }
syntax highlighted by Code2HTML, v. 0.9.1