/wshare/request.h

http://github.com/foodmike/PTypes · C Header · 168 lines · 108 code · 35 blank · 25 comment · 0 complexity · 052511ed746057af381944cda4d160dd MD5 · raw file

  1. /*
  2. *
  3. * C++ Portable Types Library (PTypes)
  4. * Version 2.1.1 Released 27-Jun-2007
  5. *
  6. * Copyright (C) 2001-2007 Hovik Melikyan
  7. *
  8. * http://www.melikyan.com/ptypes/
  9. *
  10. */
  11. #ifndef W_REQUEST_H
  12. #define W_REQUEST_H
  13. #include <ptypes.h>
  14. #include <pstreams.h>
  15. #include <pinet.h>
  16. #include "sysutils.h"
  17. #include "urlutils.h"
  18. USING_PTYPES
  19. enum http_version_t {
  20. HTTP_VER_09,
  21. HTTP_VER_10,
  22. HTTP_VER_11,
  23. HTTP_VER_UNKNOWN,
  24. HTTP_VER_MAX
  25. };
  26. enum http_method_t {
  27. HTTP_GET,
  28. HTTP_HEAD,
  29. HTTP_OTHER,
  30. HTTP_METHOD_MAX
  31. };
  32. enum req_stat_t
  33. {
  34. STAT_READ,
  35. STAT_WRITE,
  36. STAT_WAIT,
  37. STAT_MAX
  38. };
  39. struct ehttp
  40. {
  41. int code;
  42. ehttp(int icode): code(icode) {}
  43. };
  44. class request_rec
  45. {
  46. public:
  47. datetime started;
  48. int rsp_code; // rsp: response code, set through begin_response(), used for logging
  49. req_stat_t stat; // for status requests: READ, WRITE or WAIT (if keep-alive)
  50. instm* sockin; // client input stream
  51. outstm* sockout; // client output stream
  52. ipaddress client_ip; // req: client's IP
  53. http_version_t version; // req/rsp: HTTP version, 0.9, 1.0, 1.1 or 1.*
  54. http_method_t method; // req: HTTP method, currently GET or HEAD
  55. string method_str; // method string, for other method handlers
  56. bool keep_alive; // req/rsp: whether to close the connection; determined
  57. // based on the HTTP version and the "Connection:" header;
  58. // can be forced to false for some response types, e.g. 400 Bad Request
  59. datetime if_modified; // req: "If-modified-since:" if present, invdatetime otherwise
  60. string req_line; // req: the whole request line, e.g. "GET / HTTP/1.1"
  61. string uri; // req: request-URI, as is
  62. string host; // req: "Host:" header, if present
  63. string referer; // req: "Referer:"; may be converted to relative URI
  64. bool partial; // req: partial content requested (see rsp_file())
  65. large range_min; // req: partial content
  66. large range_max; // req: partial content
  67. textmap headers; // other headers
  68. // requested object info
  69. urlrec url; // req: the request-URI parsed and split into components
  70. strlist path_parts; // request-URI path split into components
  71. unknown* user; // user data for custom handlers, freed automatically by ~request_rec()
  72. // helpers for method handlers
  73. string get_token(const cset& chars);
  74. string get_uri();
  75. void parse_request_line(); // ... excluding the method string
  76. void parse_hdr(string& fname, string& fvalue);
  77. void parse_uri(); // sets url and path_parts fields
  78. void analyze_uri(); // sets all fields starting from file_type
  79. // response utilities; headers are not sent if the HTTP version is 0.9
  80. void begin_response(int code, const char* msg);
  81. void put_header(const char* name, const char* value);
  82. void put_header(const char* name, const string& value);
  83. void put_content_type(const char* mime);
  84. void put_content_length(large length);
  85. void end_headers();
  86. void std_response(bool conn_close, int code, const char* msg, const char* descr);
  87. void std_response(bool conn_close, int code, const char* msg, const char* descr, const string& dparam);
  88. // standard responses; all functions of this group raise ehttp exceptions
  89. void rsp_not_found();
  90. void rsp_bad_request();
  91. void rsp_bad_method(const char* ok_methods);
  92. void rsp_uri_too_long();
  93. void rsp_forbidden();
  94. void rsp_dir_index_forbidden();
  95. void rsp_overloaded();
  96. void abort_request();
  97. void rsp_redirect(const string& newurl);
  98. void rsp_not_modified();
  99. void end_response();
  100. protected:
  101. string location; // rsp: add "Location:" header to the response; set through
  102. // rsp_redirect()
  103. int hdr_size; // byte size of response headers; used for logging, to determine
  104. // the actual response content length (see respond())
  105. // request parsers
  106. void parse_method();
  107. void parse_headers();
  108. // the boss
  109. void respond();
  110. // reset the state between requests when keep-alive
  111. void reset_state();
  112. request_rec(instm& isockin, outstm& isockout, ipaddress iclient_ip);
  113. ~request_rec();
  114. };
  115. // this structure is created and handled in mod_file, however, we
  116. // declare it here since this information can be passed to the
  117. // file extension handlers.
  118. class file_request_rec
  119. {
  120. public:
  121. file_type_t file_type; // file, directory or other (device or pipe)
  122. bool sym_link; // the object is a symbolic link (Unix only)
  123. bool executable; // the object is executable (binary on Unix, .exe on Windows)
  124. string abs_path; // absolute file path to the requested object
  125. string rel_path; // file path to the requested object relative to document root,
  126. // may not be the same as url.path
  127. string file_name; // file name
  128. string file_ext; // file extension, including the leading dot
  129. file_request_rec(): file_type(FT_ERROR), sym_link(false), executable(false),
  130. abs_path(), rel_path(), file_name(), file_ext() {}
  131. ~file_request_rec() {}
  132. };
  133. #endif