PageRenderTime 102ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 56ms app.codeStats 0ms

/libs/cgi/doc/src/user_guide/tutorial/10_min_intro.cpp

http://github.com/darrengarvey/cgi
C++ | 86 lines | 31 code | 20 blank | 35 comment | 2 complexity | 6f127eeb36b173f87f2a7339e94281f8 MD5 | raw file
 1#include <string>
 2#include <boost/cgi/cgi.hpp>
 3using namespace cgi;
 4
 5//[main
 6/*`
 7It is assumed you have included <boost/cgi/cgi.hpp> and are `using namespace cgi`.
 8*/
 9
10int main(int,char**)
11{
12  request req;
13
14/*`
15At this point, the environment variables are accessible. This includes cookie and form variables too, which by default are all parsed (this is optional).
16
17The `response` class provides a streaming interface for writing replies. You ['can] write to the request object directly, but for now we're going to just use the `response`, which works well for most situations. Writing to a `response` is buffered - whereas writing to the request directly isn't - so if an error occurs, you can simply `clear()` the response and send an error message, which is much cleaner than sending half a response to the client, followed by "... Sorry, I just broke!".
18*/
19
20  response resp;
21
22/*`
23Let's assume you now want to check if the user has a cookie, "user_name", set. We get at it like this:
24*/
25
26  std::string user_name( req.cookies["user_name"] );
27
28/*`
29If it's set, we'll be polite and say hello. If you are used to CGI programming, you'll notice the lack of any HTTP headers. If you don't want to bother with headers, a default header `'Content-type: text/plain'` is sent, followed by the usual HTTP end-of-line `'\r\n'` and a blank line which indicates the end of the headers and the start of content.
30*/
31
32  if (!user_name.empty())
33  {
34    resp<< "Hello there, " << req.cookies["user_name"] << ". How are you?";
35
36/*`
37That's all we want to say for now, so just send this back and quit.
38*/
39
40    resp.send(req);
41
42    return 0;
43  }
44
45/*`
46If the cookie isn't set, we will check if the user has posted a __GET__/__POST__ form with their name.
47*/
48  user_name = req.form["user_name"];
49
50  if (!user_name.empty())
51  {
52/*`
53If they have told us their name, we should set a cookie so we remember it next time. Then we can say hello and exit. There are two ways to set a cookie: either directly using `req.set_cookie("user_name", user_name)` or the method below. You can also send an expiry date for the cookie [rfc 822] and a path. Note that if you don't set a value for the cookie, the cookie will be deleted.
54
55Again, the request object isn't buffered, so we are going to keep using the `response` in case something breaks and we end up not wanting to set the cookie. The cookie we set below will expire when the client closes their browser.
56
57This time, we shall send a Date header. If we do this (ie. send a header ourselves), we must also set the Content-type header and terminate the headers, like below.
58*/
59
60    resp<< cookie("user_name", user_name)
61        << header("Date", "Tue, 15 Nov 1994 08:12:31 GMT")
62        << header("Content-type", "text/plain")
63        << "Hello there, " << user_name << ". You're new around here.";
64
65    resp.send(req);
66  }
67
68/*`
69Now, if we have no idea who they are, we'll send a form asking them for their name. As the default `"Content-type"` header is `"text/plain"`, we'll change this to `"text/html"` so the user's browser will display the HTML form. You can do this using `set_header(req, "Content-type", "text/html")` or `resp<< header("Content-type", "text/html")`. Since writing with raw strings is error-prone, the shortcut below is available.
70*/
71
72  resp<< content_type("text/html")
73      << "Hello there. What's your name?" "<p />"
74         "<form method='POST'>"
75         "<input type='text' name='user_name' />"
76         "<input type='submit' />";
77
78  resp.send(req);
79
80/*`
81And that's all we want to do for now, so we can close the program.
82*/
83
84  return 0;
85}
86//]