aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/http/misc/httpreqdata.h
blob: b5c9e446a997e7297c160a670e6370c08c3fb69b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#pragma once

#include <library/cpp/digest/lower_case/hash_ops.h>

#include <util/str_stl.h>

#include <util/system/defaults.h>
#include <util/string/cast.h>
#include <library/cpp/cgiparam/cgiparam.h>
#include <util/network/address.h>
#include <util/network/socket.h>
#include <util/generic/hash.h>
#include <util/system/yassert.h>
#include <util/generic/string.h>
#include <util/datetime/base.h>
#include <util/generic/vector.h>
#include <util/generic/maybe.h>

using THttpHeadersContainer = THashMap<TString, TString, TCIOps, TCIOps>;

class TBaseServerRequestData {
public:
    TBaseServerRequestData(SOCKET s = INVALID_SOCKET);
    TBaseServerRequestData(TStringBuf qs, SOCKET s = INVALID_SOCKET);

    void SetHost(const TString& host, ui16 port) {
        Host_ = host;
        Port_ = ToString(port);
    }

    const TString& ServerName() const {
        return Host_;
    }

    NAddr::IRemoteAddrPtr ServerAddress() const {
        return NAddr::GetSockAddr(Socket_);
    }

    const TString& ServerPort() const {
        return Port_;
    }

    TStringBuf ScriptName() const {
        return Path_;
    }

    TStringBuf Query() const {
        return Query_;
    }

    TStringBuf OrigQuery() const {
        return OrigQuery_;
    }

    void AppendQueryString(TStringBuf str);
    TStringBuf RemoteAddr() const;
    void SetRemoteAddr(TStringBuf addr);
    // Returns nullptr when the header does not exist
    const TString* HeaderIn(TStringBuf key) const;
    // Throws on missing header
    TStringBuf HeaderInOrEmpty(TStringBuf key) const;

    const THttpHeadersContainer& HeadersIn() const {
        return HeadersIn_;
    }

    inline size_t HeadersCount() const noexcept {
        return HeadersIn_.size();
    }

    TString HeaderByIndex(size_t n) const noexcept;
    TStringBuf Environment(TStringBuf key) const;

    void Clear();

    void SetSocket(SOCKET s) noexcept {
        Socket_ = s;
    }

    ui64 RequestBeginTime() const noexcept {
        return BeginTime_;
    }

    void SetPath(TString path);
    const TString& GetCurPage() const;
    bool Parse(TStringBuf req);
    void AddHeader(const TString& name, const TString& value);

private:
    mutable TMaybe<TString> Addr_;
    TString Host_;
    TString Port_;
    TString Path_;
    TStringBuf Query_;
    TStringBuf OrigQuery_;
    THttpHeadersContainer HeadersIn_;
    SOCKET Socket_;
    ui64 BeginTime_;
    mutable TString CurPage_;
    TVector<char> ParseBuf_;
    TString ModifiedQueryString_;
};

class TServerRequestData: public TBaseServerRequestData {
public:
    TServerRequestData(SOCKET s = INVALID_SOCKET)
        : TBaseServerRequestData(s)
    {
    }
    TServerRequestData(TStringBuf qs, SOCKET s = INVALID_SOCKET)
        : TBaseServerRequestData(qs, s)
    {
        Scan();
    }

    void Scan() {
        CgiParam.Scan(Query());
    }

public:
    TCgiParameters CgiParam;
};