Frequently Asked Questions

Version 9 (Pieter Libin, 12/23/2009 05:00 pm)

1 9 Pieter Libin
h1. Frequently Asked Questions
2 9 Pieter Libin
3 9 Pieter Libin
{{toc}}
4 9 Pieter Libin
5 5 Pieter Libin
h2. Building and deployment
6 1
7 1
8 5 Pieter Libin
h3. Q: How does Wt organize sessions in processes and threads ?
9 5 Pieter Libin
10 3 Pieter Libin
Wt makes a distinction in the conceptual organization (which is reflected in the API of WApplication)
11 3 Pieter Libin
and the way the application is actually deployed.
12 3 Pieter Libin
13 3 Pieter Libin
Conceptually, every user session is completely isolated from each other. For each new session, Wt
14 3 Pieter Libin
calls the function that is supplied to WRun(), to create a WApplication object for that session.
15 3 Pieter Libin
As a programmer, you should program for the general case where different WApplication objects
16 3 Pieter Libin
are in different processes, and thus if you wish to communicate between different session, you
17 3 Pieter Libin
have the following options: (in increasing order of flexibility traded for convenience):
18 7 Pieter Libin
* A database to which every session connects.
19 7 Pieter Libin
* A dedicated server daemon, with socket based communication.
20 7 Pieter Libin
* A combination of both, with possible peer-to-peer communication between different sessions.
21 3 Pieter Libin
22 1
Physically, Wt offers several choices for deployment, each of them with different trade-offs.
23 1
If an application sticks strictly to the previous rules, you can freely change between different
24 3 Pieter Libin
deployment options at deployment time.
25 3 Pieter Libin
26 1
The options that are available are:
27 6 Pieter Libin
* Dedicated-process mode: mapping one session to one process. Advantages are:
28 6 Pieter Libin
** Kernel-level isolation between processes (security and reliability!).
29 6 Pieter Libin
** Kernel-based sharing of read-only memory segments (simply UNIX feature).
30 6 Pieter Libin
** Development friendly: a new session uses the latest deployed binary, and valgrind may be used to
31 6 Pieter Libin
debug one particular session, by modifying the URL request.
32 3 Pieter Libin
33 6 Pieter Libin
* Shared-process mode: mapping multiple sessions in a fixed number of processes. Advantages are:
34 6 Pieter Libin
** No process and stack overhead per session.
35 6 Pieter Libin
36 3 Pieter Libin
Wt is capable of using multi-threading to improve performance for both situations. Threads are
37 3 Pieter Libin
used for simultaneous handling of requests. Even in dedicated-process mode, several requests
38 3 Pieter Libin
may be handled simultaneously, for example concurrent streaming of different WResource's. The
39 3 Pieter Libin
multi-threading feature however is more important for shared-process mode, for handling concurrent
40 3 Pieter Libin
requests for different sessions. In the latter case, however, the number of threads must not
41 3 Pieter Libin
equal the number of active sessions: threads are reused after every request is handled.
42 3 Pieter Libin
43 3 Pieter Libin
The shared-process mode has the notable disadvantage, inherent to C++, that memory corruption
44 3 Pieter Libin
may occur and can take down all sessions. It is however well suited for 'open' applications on
45 1
the Internet (and the Wt homepage and all examples are deployed this way). If you design a
46 1
restricted access application, or possibly a security sensitive application, or deploy the
47 3 Pieter Libin
application on a private intranet, the dedicated process mode may be more suitable.
48 3 Pieter Libin
49 3 Pieter Libin
50 5 Pieter Libin
h3. Q: How does it compare to Java servlets ?
51 5 Pieter Libin
52 3 Pieter Libin
Differences with Java Servlets are mostly due to the Java Virtual Machine. Java has the benefit
53 3 Pieter Libin
of automating pointer manipulation, and therefore eliminating unwanted interference between
54 3 Pieter Libin
different sessions because of pointer bugs. On the other hand, because of the high costs (both
55 1
run-time start up as well as memory usage) associated with a Java Virtual Machine instance,
56 1
Java cannot afford kernel-level isolation between different Java sessions. If not programmed properly,
57 1
two sessions can still interfere through for example the use of class static variables.
58 3 Pieter Libin
Unfortunately, some servlet based frameworks, like the often-used struts framework, actually encourage
59 3 Pieter Libin
sharing of for example form objects between different sessions for run-time efficiency reasons,
60 3 Pieter Libin
making session cross talk readily an issue.
61 3 Pieter Libin
62 3 Pieter Libin
Similarities between Wt and Java Servlets are the use of a thread pool to serve concurrent
63 3 Pieter Libin
requests, and the abstraction of actual deployment details from the API, allowing easy
64 3 Pieter Libin
scalability.
65 5 Pieter Libin
66 5 Pieter Libin
67 3 Pieter Libin
h3. Q: How do I build my newly written "Hello World!" application?
68 5 Pieter Libin
69 3 Pieter Libin
A:
70 3 Pieter Libin
Wt itself, and the examples, use "CMake":http://www.cmake.org, but that is entirely a
71 3 Pieter Libin
personal choice. You can use any build environment, like qmake, where
72 3 Pieter Libin
you:
73 3 Pieter Libin
74 6 Pieter Libin
* specify the library directory (Wt defaults to installing in /usr/local/lib)
75 6 Pieter Libin
* specify the link libraries: -lwt and one of -lwtfcgi or -lwthttp
76 6 Pieter Libin
* specfiy the include directory (Wt defaults to installing in /usr/local/include)
77 3 Pieter Libin
78 3 Pieter Libin
Unlike Qt, there is no need for special features such as moc for
79 3 Pieter Libin
starting a Wt project.
80 3 Pieter Libin
81 3 Pieter Libin
If you decide to use CMake, and have installed Wt in its default
82 3 Pieter Libin
location (within /usr/local), this CMakeLists.txt file should do it:
83 3 Pieter Libin
84 1
<pre>
85 3 Pieter Libin
ADD_EXECUTABLE(myprog.wt
86 3 Pieter Libin
MyProg1.C
87 3 Pieter Libin
OtherFile.C
88 3 Pieter Libin
AndEvenMoreCode.C
89 3 Pieter Libin
)
90 3 Pieter Libin
91 3 Pieter Libin
# For FastCGI deployment:
92 3 Pieter Libin
TARGET_LINK_LIBRARIES(myprog.wt
93 3 Pieter Libin
wtfcgi wt someotherlib
94 3 Pieter Libin
)
95 3 Pieter Libin
96 3 Pieter Libin
# Or, for built-in httpd deployment:
97 3 Pieter Libin
# TARGET_LINK_LIBRARIES(myprog.wt
98 3 Pieter Libin
#   wthttp wt someotherlib
99 3 Pieter Libin
# )
100 3 Pieter Libin
101 3 Pieter Libin
INCLUDE_DIRECTORIES(/usr/local/wt/include)
102 3 Pieter Libin
</pre>
103 3 Pieter Libin
104 1
The examples use a CMakeLists.txt which is customized for using the
105 3 Pieter Libin
current build of Wt, and not that one that is already installed some
106 3 Pieter Libin
place (with make install). Therefore, it is not really the recommended
107 3 Pieter Libin
way to bootstrap your own Wt project. Also, the ./deploy scripts are
108 3 Pieter Libin
very primitive, and are a bit specific for the examples. Deploying is
109 3 Pieter Libin
nothing more than copying the files to some directory in your html
110 3 Pieter Libin
root.
111 3 Pieter Libin
112 5 Pieter Libin
*The other methods are:*
113 3 Pieter Libin
114 3 Pieter Libin
To handle many sourcefiles, dependencies... you need a makefile. Obviously, the way Wt is designed, you should have quickly many files for the many classes that will compose your app. Wt uses cmake (cmake.org for details) to make makefiles and that is probably a good choice. Just like many others, I switched to cmake (from hand-written makefiles) because of wt and I am pretty happy with it.
115 3 Pieter Libin
make should produce  the executable. At this point,  you probably need to move the output of make to a directory available to your webserver; in practice you therefore need a script that is going to deploy the file. When you name the app, be sure the extension is recognized by the webserver.  Also, you may need to kill active processes of your app and maybe copy the css and some other files (icons...) to the directory available to the webserver.
116 3 Pieter Libin
In the end, I bundled all that in a deploy file located in the build directory (the one that is usually created for cmake). After I have have finished changing the source files, I just type ./deploy on the command line and I can refresh my web page.
117 3 Pieter Libin
<pre>
118 1
make
119 3 Pieter Libin
target_app=app.wt
120 3 Pieter Libin
target_path=httpdocs
121 3 Pieter Libin
ps -A | grep app.wt | awk '{print $1}' | xargs kill
122 3 Pieter Libin
rm -f "~/${target_path}/${target_app}"
123 3 Pieter Libin
cp "${target_app}" ~/${target_path}/
124 3 Pieter Libin
cp ../app.css  ~/${target_path}/
125 3 Pieter Libin
</pre>
126 3 Pieter Libin
127 5 Pieter Libin
*OR*
128 3 Pieter Libin
129 3 Pieter Libin
You can use install command instead of cp, more or less like this:
130 3 Pieter Libin
<pre>
131 3 Pieter Libin
install -m 0755 astariand.wt /var/www/game
132 3 Pieter Libin
install -m 0644 messages.xml /var/www/game
133 3 Pieter Libin
install -m 0644 astariand.css /var/www/game
134 3 Pieter Libin
install -m 0644 login.php /var/www/game
135 3 Pieter Libin
install -m 0644 includes.php /var/www/game
136 3 Pieter Libin
install -m 0755 -d /var/www/game/media
137 3 Pieter Libin
install -m 0755 -d /var/www/game/media/icons
138 3 Pieter Libin
install -m 0644 media/icons/* /var/www/game/media/icons
139 3 Pieter Libin
install -m 0755 -d /var/www/game/media/images
140 1
install -m 0644 media/images/* /var/www/game/media/images
141 1
</pre>
142 1
143 1
Using install has two advantages. First, it allows you to set permissions
144 1
on the fly (just as user and group, but I don't use this). Second, with
145 3 Pieter Libin
dedicated process session management you don't need to kill all processes
146 3 Pieter Libin
beforehand - old connections will keep using the old binary and new
147 3 Pieter Libin
connections will use the new one, until all old connections "die from
148 3 Pieter Libin
natural reasons".
149 3 Pieter Libin
150 5 Pieter Libin
151 5 Pieter Libin
h3. Q: My browser shows a window with a message like 'Wt internal error: ReferenceError: Ext is not defined, code: undefined, description: undefined'. How do I resolve it?
152 5 Pieter Libin
153 3 Pieter Libin
A:
154 5 Pieter Libin
Check your log for 404 messages regarding ExtJs. Download Ext 2.0.1 or 2.0.2 from the ExtJs homepage and install it as described "here":http://www.webtoolkit.eu/wt/doc/reference/html/group__ext.html. ExtJs 2.0.2 is available for download "here":http://gwt-ext.com/download/.
155 3 Pieter Libin
156 5 Pieter Libin
You will receive similar error messages when you use a WTextEdit and TinyMCE is not properly deployed. Download TinyMCE from the "TinyMCE homepage":http://tinymce.moxiecode.com/.
157 3 Pieter Libin
158 3 Pieter Libin
ExtJS and TinyMCE need to be available in the document root of your web server. By default, Wt expects ext-related files to be found in 'ext/' (relative to your application deployment location), and TinyMCE in 'resources/tiny_mce/'.
159 3 Pieter Libin
160 3 Pieter Libin
For example (Wt 2.2.1), to run the widgetgallery example (which needs both ExtJS and TinyMCE) from within its source directory, you need the following organisation of auxiliary files:
161 3 Pieter Libin
162 5 Pieter Libin
<pre>
163 1
 $ pwd
164 3 Pieter Libin
 /home/.../wt/examples/widgetgallery
165 3 Pieter Libin
 $ ls ext/
166 3 Pieter Libin
 ext-all.js  ext-base.js  resources
167 1
 $ ls resources/
168 3 Pieter Libin
 collapse.gif      items-ok.gif     nav-minus.gif              nav-plus-line-middle.gif  sort-arrow-down.gif  tab_l.gif
169 1
 expand.gif        line-last.gif    nav-minus-line-last.gif    orbited.js                sort-arrow-none.gif  tab_r.gif
170 1
 iframe.js         line-middle.gif  nav-minus-line-middle.gif  orbited_LICENSE.txt       sort-arrow-up.gif    tiny_mce
171 3 Pieter Libin
 items.gif         line-trunk.gif   nav-plus.gif               slider-thumb-h.gif        stripes              tv-line-last.gif
172 1
 items-not-ok.gif  loading.png      nav-plus-line-last.gif     slider-thumb-v.gif        tab_b.gif
173 1
 $ ls resources/tiny_mce/
174 1
 langs  license.txt  plugins  themes  tiny_mce.js  tiny_mce.js.gz  tiny_mce_popup.js  tiny_mce_src.js  utils
175 5 Pieter Libin
</pre>
176 3 Pieter Libin
177 3 Pieter Libin
and then you can run the example using the following command line:
178 3 Pieter Libin
179 5 Pieter Libin
<pre>
180 1
 $ ../../build/examples/widgetgallery/widgetgallery.wt --http-address=0.0.0.0 --http-port=8080 --docroot .
181 5 Pieter Libin
</pre>
182 3 Pieter Libin
183 3 Pieter Libin
184 5 Pieter Libin
h2. API
185 1
186 5 Pieter Libin
187 5 Pieter Libin
h3. Q: How do I deal with look and layout ? Does Wt support CSS ?
188 5 Pieter Libin
189 3 Pieter Libin
Wt uses CSS for layout, and CSS may be either specified in CSS style sheets, or
190 3 Pieter Libin
manipulated programmatorically. Tomasz Mazurek contributed [[Using CSS|a tutorial]] about
191 3 Pieter Libin
it.
192 3 Pieter Libin
193 3 Pieter Libin
194 5 Pieter Libin
h3. Q: How do I pass an additional argument from a signal to a slot ?
195 5 Pieter Libin
196 3 Pieter Libin
Frequently, you may want to connect many different signals to a single slot, and identify the original sender in the slot.
197 3 Pieter Libin
198 3 Pieter Libin
For example:
199 1
200 5 Pieter Libin
<pre>
201 3 Pieter Libin
 void Test::createWidgets()
202 1
 {
203 3 Pieter Libin
   // create text1, text2, text3 widgets
204 3 Pieter Libin
 
205 3 Pieter Libin
   text1->clicked.connect(SLOT(this, Test::onClick));
206 3 Pieter Libin
   text2->clicked.connect(SLOT(this, Test::onClick));
207 3 Pieter Libin
   text3->clicked.connect(SLOT(this, Test::onClick));
208 3 Pieter Libin
 }
209 3 Pieter Libin
 
210 3 Pieter Libin
 void Test::onClick()
211 3 Pieter Libin
 {
212 3 Pieter Libin
   // How to know which widget?
213 3 Pieter Libin
 }
214 5 Pieter Libin
</pre>
215 3 Pieter Libin
216 5 Pieter Libin
The solution is to use a "WSignalMapper":http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WSignalMapper.html like this:
217 3 Pieter Libin
218 5 Pieter Libin
<pre>
219 3 Pieter Libin
 void Test::createWidgets()
220 3 Pieter Libin
 {
221 1
   Wt::WSignalMapper<Wt::WText> *myMap = new Wt::WSignalMapper<Wt::WText*>(this);
222 3 Pieter Libin
 
223 1
   myMap->mapped.connect(SLOT(this, Test::onClick));
224 1
   myMap->mapConnect(text1->clicked, text1);
225 1
   myMap->mapConnect(text2->clicked, text2);
226 3 Pieter Libin
   myMap->mapConnect(text3->clicked, text3);
227 3 Pieter Libin
 }
228 1
 
229 1
 void Test::onClick(Wt::WText* source)
230 1
 {
231 3 Pieter Libin
   // source is where it is coming from
232 3 Pieter Libin
   // ...
233 3 Pieter Libin
 }
234 5 Pieter Libin
</pre>
235 1
236 1
The additional argument can be of any type, since WSignalMapper is a template class. It could for example be the button text, or some other information specific to the widget that is activated.
237 3 Pieter Libin
238 3 Pieter Libin
239 5 Pieter Libin
h2. Security
240 1
241 5 Pieter Libin
242 5 Pieter Libin
h3. Q: Building web applications in a low-level language like C? Have you never heard of buffer overruns??
243 5 Pieter Libin
244 3 Pieter Libin
We are well aware of the hostile environment that is the Internet. We believe that Wt provides some unique benefits compared to other solutions to handle the most common attacks:
245 3 Pieter Libin
246 8 Pieter Libin
* Cross-Site scripting attacks (XSS): an attacker forces the display of some script by letting the application render it to the browser of a victim that is also using the web application.
247 8 Pieter Libin
** Unlike other web technologies, Wt does not require any effort from the programmer to avoid XSS attacks. Instead, any 'rich' XHTML text that needs to be displayed (for example in a WText using XHTMLFormatting) is filtered by a built-in XML parser for any potentially malicious tags or attributes (which is anything that may execute some JavaScript code). Unlike other (low-level) frameworks, Wt provide this protection because there is no raw 'print' command. Instead, Wt generates all HTML/JavaScript from widgets and therefore it knows that rich text should only be "passive" rich text and not contain any "active" content.
248 1
249 8 Pieter Libin
* Cross-site request forgery attacks (CSRF): an attacker tricks a user into sending a request to a trusted website passing its credentials in a cookie.
250 8 Pieter Libin
** This kind of attack is eliminated since Wt uses a secure random number generator for the session ID (on platforms that provide this kernel-level service, such as Linux and Win32 platforms), and even when using cookies for session tracking, the session ID is always sent within the request as well, and verified within Wt (since Wt 2.2.0).
251 3 Pieter Libin
252 8 Pieter Libin
* Attacks against the *application logic*: an attacker issues a request to some page or service that is only accessible after authorization. 
253 8 Pieter Libin
** Wt protects the application logic because all incoming requests are interpreted in one central, well-tested routine. The request is parsed and only _*exposed event signals*_ may be triggered. Exposed event signals are attached to widgets that are currently rendered on the screen. For example, a button click on a button that is currently shown on the screen. In this way, the logic of the application (such as for example: you need to first log
254 8 Pieter Libin
in, and then only you may request for a payment) is automatically validated: only code in slots connected to exposed signals can be invoked by the user.
255 5 Pieter Libin
256 8 Pieter Libin
* Session cross-talk: sensitive data from one session spills in another session because of a programming error, where data is shared.
257 8 Pieter Libin
** Wt is the only solution which may eliminate any cross-talk between sessions by deploying each session in a dedicated process, and thus using kernel-level protection (Dedicated Process mode of deployment). In the case of a bug, data from other sessions cannot be accessed and this is guaranteed by the kernel. This feature is especially valuable in sensitive areas such as financial transactions.
258 8 Pieter Libin
** In other web application frameworks, such as Python/PHP/Java solutions, cross-talk between sessions is always a risk since sessions run within the same process for performance reasons since virtual machines and byte interpreters take their time to load. Cross-talk can be the consequence of a programming mistake where data structures are shared between sessions. In fact, many popular Java servlet-based frameworks encourage sharing of data structures, again for performance reasons, to avoid (expensive) object creation. For example, in struts _*form beans*_ should be shared, and be reused by reinitialization rather than reconstruction.
259 3 Pieter Libin
260 8 Pieter Libin
* Buffer over-runs: A low-level C programming mistake is abused by an attacker to exucute arbitrary code.
261 8 Pieter Libin
** While it is true that C applications may suffer this problem, this is no longer a valid concern for modern C++ code. The main source of these programming mistakes was string manipulation in C, relying on careful memory management of the string buffers. In C++, std::string avoids this issues entirely by automated memory management and buffer sizing. Furthermore, Wt is developed using the highest standards for code clarity, and is thoroughly checked for memory-related problems by running it through memory checking tools such as valgrind.
262 3 Pieter Libin
263 3 Pieter Libin
All these attacks (except for the last one) are commonly exploited against current-day web applications which are vulnerable by the simple fact that too many web-related details are in the hands and responsibility of the developer. In contrast, Wt actively helps in avoiding programming mistakes which may lead to these exploits.
264 3 Pieter Libin
265 3 Pieter Libin
266 5 Pieter Libin
h3. Q: How do I use the built-in HTTPS server in wthttpd ?
267 5 Pieter Libin
268 3 Pieter Libin
You will need a private server key that is signed by a certificate authority, and a temporary file containing random Diffie-Hellman parameters. If you are simply experimenting with the feature, then you can create and sign a key yourself, or use the one that comes with the OpenSSL distribution (server.pem, which has the password 'test'). The file with Diffie-Hellman parameters can be created using the command:
269 3 Pieter Libin
270 3 Pieter Libin
<pre>
271 3 Pieter Libin
$ openssl dhparam -check -text -5 512 -out dh512.pem
272 3 Pieter Libin
</pre>
273 1
274 1
Then start Wt using:
275 1
276 1
<pre>
277 1
$ ./app.wt --https-address=0.0.0.0 --ssl-certificate=server.pem --ssl-private-key=server.pem --ssl-tmp-dh=dh512.pem
278 1
</pre>
279 1
280 1
Provide the password at the prompt.
281 1
282 1
283 5 Pieter Libin
h2. Trouble shooting
284 5 Pieter Libin
285 5 Pieter Libin
286 5 Pieter Libin
h3. Q: My application crashes, and my apache error log shows no information.
287 1
288 1
There is a known problem with mod_fcgid: STDERR (including everything printed to std::cerr) is not
289 1
saved to the apache error log.
290 1
291 1
Wt uses STDERR by default for all error reporting. You can use a different log file in your wt_config.xml file (<log-file>).
292 1
293 1
You may also consider using mod_fastcgi or the built-in web server (wthttpd) during development. The latter is especially convenient for development as it allows you to start from within a debugger, or diagnose memory-related problems with valgrind.