httpreqdata.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #pragma once
  2. #include <library/cpp/digest/lower_case/hash_ops.h>
  3. #include <util/str_stl.h>
  4. #include <util/system/defaults.h>
  5. #include <util/string/cast.h>
  6. #include <library/cpp/cgiparam/cgiparam.h>
  7. #include <util/network/address.h>
  8. #include <util/network/socket.h>
  9. #include <util/generic/hash.h>
  10. #include <util/system/yassert.h>
  11. #include <util/generic/string.h>
  12. #include <util/datetime/base.h>
  13. #include <util/generic/vector.h>
  14. #include <util/generic/maybe.h>
  15. using THttpHeadersContainer = THashMap<TString, TString, TCIOps, TCIOps>;
  16. class TBaseServerRequestData {
  17. public:
  18. TBaseServerRequestData(SOCKET s = INVALID_SOCKET);
  19. TBaseServerRequestData(TStringBuf qs, SOCKET s = INVALID_SOCKET);
  20. void SetHost(const TString& host, ui16 port) {
  21. Host_ = host;
  22. Port_ = ToString(port);
  23. }
  24. const TString& ServerName() const {
  25. return Host_;
  26. }
  27. NAddr::IRemoteAddrPtr ServerAddress() const {
  28. return NAddr::GetSockAddr(Socket_);
  29. }
  30. const TString& ServerPort() const {
  31. return Port_;
  32. }
  33. TStringBuf ScriptName() const {
  34. return Path_;
  35. }
  36. TStringBuf Query() const {
  37. return Query_;
  38. }
  39. TStringBuf OrigQuery() const {
  40. return OrigQuery_;
  41. }
  42. void AppendQueryString(TStringBuf str);
  43. TStringBuf RemoteAddr() const;
  44. void SetRemoteAddr(TStringBuf addr);
  45. // Returns nullptr when the header does not exist
  46. const TString* HeaderIn(TStringBuf key) const;
  47. // Throws on missing header
  48. TStringBuf HeaderInOrEmpty(TStringBuf key) const;
  49. const THttpHeadersContainer& HeadersIn() const {
  50. return HeadersIn_;
  51. }
  52. inline size_t HeadersCount() const noexcept {
  53. return HeadersIn_.size();
  54. }
  55. TString HeaderByIndex(size_t n) const noexcept;
  56. TStringBuf Environment(TStringBuf key) const;
  57. void Clear();
  58. void SetSocket(SOCKET s) noexcept {
  59. Socket_ = s;
  60. }
  61. ui64 RequestBeginTime() const noexcept {
  62. return BeginTime_;
  63. }
  64. void SetPath(TString path);
  65. const TString& GetCurPage() const;
  66. bool Parse(TStringBuf req);
  67. void AddHeader(const TString& name, const TString& value);
  68. private:
  69. mutable TMaybe<TString> Addr_;
  70. TString Host_;
  71. TString Port_;
  72. TString Path_;
  73. TStringBuf Query_;
  74. TStringBuf OrigQuery_;
  75. THttpHeadersContainer HeadersIn_;
  76. SOCKET Socket_;
  77. ui64 BeginTime_;
  78. mutable TString CurPage_;
  79. TVector<char> ParseBuf_;
  80. TString ModifiedQueryString_;
  81. };
  82. class TServerRequestData: public TBaseServerRequestData {
  83. public:
  84. TServerRequestData(SOCKET s = INVALID_SOCKET)
  85. : TBaseServerRequestData(s)
  86. {
  87. }
  88. TServerRequestData(TStringBuf qs, SOCKET s = INVALID_SOCKET)
  89. : TBaseServerRequestData(qs, s)
  90. {
  91. Scan();
  92. }
  93. void Scan() {
  94. CgiParam.Scan(Query());
  95. }
  96. public:
  97. TCgiParameters CgiParam;
  98. };