PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 1ms 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>
  3. using namespace cgi;
  4. //[main
  5. /*`
  6. It is assumed you have included <boost/cgi/cgi.hpp> and are `using namespace cgi`.
  7. */
  8. int main(int,char**)
  9. {
  10. request req;
  11. /*`
  12. At this point, the environment variables are accessible. This includes cookie and form variables too, which by default are all parsed (this is optional).
  13. The `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!".
  14. */
  15. response resp;
  16. /*`
  17. Let's assume you now want to check if the user has a cookie, "user_name", set. We get at it like this:
  18. */
  19. std::string user_name( req.cookies["user_name"] );
  20. /*`
  21. If 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.
  22. */
  23. if (!user_name.empty())
  24. {
  25. resp<< "Hello there, " << req.cookies["user_name"] << ". How are you?";
  26. /*`
  27. That's all we want to say for now, so just send this back and quit.
  28. */
  29. resp.send(req);
  30. return 0;
  31. }
  32. /*`
  33. If the cookie isn't set, we will check if the user has posted a __GET__/__POST__ form with their name.
  34. */
  35. user_name = req.form["user_name"];
  36. if (!user_name.empty())
  37. {
  38. /*`
  39. If 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.
  40. Again, 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.
  41. This 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.
  42. */
  43. resp<< cookie("user_name", user_name)
  44. << header("Date", "Tue, 15 Nov 1994 08:12:31 GMT")
  45. << header("Content-type", "text/plain")
  46. << "Hello there, " << user_name << ". You're new around here.";
  47. resp.send(req);
  48. }
  49. /*`
  50. Now, 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.
  51. */
  52. resp<< content_type("text/html")
  53. << "Hello there. What's your name?" "<p />"
  54. "<form method='POST'>"
  55. "<input type='text' name='user_name' />"
  56. "<input type='submit' />";
  57. resp.send(req);
  58. /*`
  59. And that's all we want to do for now, so we can close the program.
  60. */
  61. return 0;
  62. }
  63. //]