exploringrequests.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. This chapter will deal with the information which the client sends to the
  2. server at every request. We are going to examine the most useful fields of such an request
  3. and print them out in a readable manner. This could be useful for logging facilities.
  4. The starting point is the @emph{hellobrowser} program with the former response removed.
  5. This time, we just want to collect information in the callback function, thus we will
  6. just return MHD_NO after we have probed the request. This way, the connection is closed
  7. without much ado by the server.
  8. @verbatim
  9. static int
  10. answer_to_connection (void *cls, struct MHD_Connection *connection,
  11. const char *url,
  12. const char *method, const char *version,
  13. const char *upload_data,
  14. size_t *upload_data_size, void **req_cls)
  15. {
  16. ...
  17. return MHD_NO;
  18. }
  19. @end verbatim
  20. @noindent
  21. The ellipsis marks the position where the following instructions shall be inserted.
  22. We begin with the most obvious information available to the server, the request line. You should
  23. already have noted that a request consists of a command (or "HTTP method") and a URI (e.g. a filename).
  24. It also contains a string for the version of the protocol which can be found in @code{version}.
  25. To call it a "new request" is justified because we return only @code{MHD_NO}, thus ensuring the
  26. function will not be called again for this connection.
  27. @verbatim
  28. printf ("New %s request for %s using version %s\n", method, url, version);
  29. @end verbatim
  30. @noindent
  31. The rest of the information is a bit more hidden. Nevertheless, there is lot of it sent from common
  32. Internet browsers. It is stored in "key-value" pairs and we want to list what we find in the header.
  33. As there is no mandatory set of keys a client has to send, each key-value pair is printed out one by
  34. one until there are no more left. We do this by writing a separate function which will be called for
  35. each pair just like the above function is called for each HTTP request.
  36. It can then print out the content of this pair.
  37. @verbatim
  38. int print_out_key (void *cls, enum MHD_ValueKind kind,
  39. const char *key, const char *value)
  40. {
  41. printf ("%s: %s\n", key, value);
  42. return MHD_YES;
  43. }
  44. @end verbatim
  45. @noindent
  46. To start the iteration process that calls our new function for every key, the line
  47. @verbatim
  48. MHD_get_connection_values (connection, MHD_HEADER_KIND, &print_out_key, NULL);
  49. @end verbatim
  50. @noindent
  51. needs to be inserted in the connection callback function too. The second parameter tells the function
  52. that we are only interested in keys from the general HTTP header of the request. Our iterating
  53. function @code{print_out_key} does not rely on any additional information to fulfill its duties
  54. so the last parameter can be NULL.
  55. All in all, this constitutes the complete @code{logging.c} program for this chapter which can be
  56. found in the @code{examples} section.
  57. Connecting with any modern Internet browser should yield a handful of keys. You should try to
  58. interpret them with the aid of @emph{RFC 2616}.
  59. Especially worth mentioning is the "Host" key which is often used to serve several different websites
  60. hosted under one single IP address but reachable by different domain names (this is called virtual hosting).
  61. @heading Conclusion
  62. The introduced capabilities to itemize the content of a simple GET request---especially the
  63. URI---should already allow the server to satisfy clients' requests for small specific resources
  64. (e.g. files) or even induce alteration of server state. However, the latter is not
  65. recommended as the GET method (including its header data) is by convention considered a "safe"
  66. operation, which should not change the server's state in a significant way. By convention,
  67. GET operations can thus be performed by crawlers and other automatic software. Naturally
  68. actions like searching for a passed string are fine.
  69. Of course, no transmission can occur while the return value is still set to @code{MHD_NO} in the
  70. callback function.
  71. @heading Exercises
  72. @itemize @bullet
  73. @item
  74. By parsing the @code{url} string and delivering responses accordingly, implement a small server for
  75. "virtual" files. When asked for @code{/index.htm@{l@}}, let the response consist of a HTML page
  76. containing a link to @code{/another.html} page which is also to be created "on the fly" in case of
  77. being requested. If neither of these two pages are requested, @code{MHD_HTTP_NOT_FOUND} shall be
  78. returned accompanied by an informative message.
  79. @item
  80. A very interesting information has still been ignored by our logger---the client's IP address.
  81. Implement a callback function
  82. @verbatim
  83. static int on_client_connect (void *cls,
  84. const struct sockaddr *addr,
  85. socklen_t addrlen)
  86. @end verbatim
  87. @noindent
  88. that prints out the IP address in an appropriate format. You might want to use the POSIX function
  89. @code{inet_ntoa} but bear in mind that @code{addr} is actually just a structure containing other
  90. substructures and is @emph{not} the variable this function expects.
  91. Make sure to return @code{MHD_YES} so that the library knows the client is allowed to connect
  92. (and to then process the request). If one wanted to limit access basing on IP addresses, this would be the place
  93. to do it. The address of your @code{on_client_connect} function must be passed as the third parameter to the
  94. @code{MHD_start_daemon} call.
  95. @end itemize