aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/http/misc/httpreqdata.cpp
blob: e8bf6df1a1ceb0cd8c6dc97363a4f34f62b5ef0a (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "httpreqdata.h"

#include <library/cpp/case_insensitive_string/case_insensitive_string.h>

#include <util/stream/mem.h>
#include <util/string/join.h>

#include <array>

#ifdef _sse4_2_
#include <smmintrin.h>
#endif

TBaseServerRequestData::TBaseServerRequestData(SOCKET s)
    : Socket_(s)
    , BeginTime_(MicroSeconds())
{
}

TBaseServerRequestData::TBaseServerRequestData(TStringBuf qs, SOCKET s)
    : Query_(qs)
    , OrigQuery_(Query_)
    , Socket_(s)
    , BeginTime_(MicroSeconds())
{
}

void TBaseServerRequestData::AppendQueryString(TStringBuf str) {
    if (Y_UNLIKELY(!Query_.empty())) {
        TStringBuf separator = !Query_.EndsWith('&') && !str.StartsWith('&') ? "&"sv : ""sv;
        ModifiedQueryString_ = TString::Join(Query_, separator, str);
     } else {
        ModifiedQueryString_ = str;
     }
    Query_ = ModifiedQueryString_;
}

void TBaseServerRequestData::SetRemoteAddr(TStringBuf addr) {
    Addr_.ConstructInPlace(addr.substr(0, INET6_ADDRSTRLEN - 1));
}

TStringBuf TBaseServerRequestData::RemoteAddr() const {
    if (!Addr_) {
        auto& addr = Addr_.ConstructInPlace();
        addr.ReserveAndResize(INET6_ADDRSTRLEN);
        if (GetRemoteAddr(Socket_, addr.begin(), addr.size())) {
            if (auto pos = addr.find('\0'); pos != TString::npos) {
                addr.resize(pos);
            }
        } else {
            addr.clear();
        }
     }

    return *Addr_;
 }

const TString* TBaseServerRequestData::HeaderIn(TStringBuf key) const {
    return HeadersIn_.FindPtr(key);
}

TStringBuf TBaseServerRequestData::HeaderInOrEmpty(TStringBuf key) const {
    const auto* ptr = HeaderIn(key);
    return ptr ? TStringBuf{*ptr} : TStringBuf{};
}

TString TBaseServerRequestData::HeaderByIndex(size_t n) const noexcept {
    if (n >= HeadersIn_.size()) {
        return {};
    }

    const auto& [key, value] = *std::next(HeadersIn_.begin(), n);

    return TString::Join(key, ": ", value);
}

TStringBuf TBaseServerRequestData::Environment(TStringBuf key) const {
    TCaseInsensitiveStringBuf ciKey(key.data(), key.size());
    if (ciKey == "REMOTE_ADDR") {
        const auto ip = HeaderIn("X-Real-IP");
        return ip ? *ip : RemoteAddr();
    } else if (ciKey == "QUERY_STRING") {
        return Query();
    } else if (ciKey == "SERVER_NAME") {
        return ServerName();
    } else if (ciKey == "SERVER_PORT") {
        return ServerPort();
    } else if (ciKey == "SCRIPT_NAME") {
        return ScriptName();
    }
    return {};
}

 void TBaseServerRequestData::Clear() {
    HeadersIn_.clear();
    Addr_ = Nothing();
    Path_.clear();
    Query_ = {};
    OrigQuery_ = {};
    Host_.clear();
    Port_.clear();
    CurPage_.remove();
    ParseBuf_.clear();
    BeginTime_ = MicroSeconds();
}

const TString& TBaseServerRequestData::GetCurPage() const {
    if (!CurPage_ && Host_) {
        std::array<TStringBuf, 7> fragments;
        auto fragmentIt = fragments.begin();
        *fragmentIt++ = "http://"sv;
        *fragmentIt++ = Host_;
        if (Port_) {
            *fragmentIt++ = ":"sv;
            *fragmentIt++ = Port_;
        }
        *fragmentIt++ = Path_;
        if (!Query_.empty()) {
            *fragmentIt++ = "?"sv;
            *fragmentIt++ = Query_;
        }

        CurPage_ = JoinRange(""sv, fragments.begin(), fragmentIt);
    }
    return CurPage_;
}

bool TBaseServerRequestData::Parse(TStringBuf origReq) {
    ParseBuf_.reserve(origReq.size() + 16);
    ParseBuf_.assign(origReq.begin(), origReq.end());
    ParseBuf_.insert(ParseBuf_.end(), 16, ' ');
    char* req = ParseBuf_.data();

    while (*req == ' ' || *req == '\t')
        req++;
    if (*req != '/')
        return false;     // we are not a proxy
    while (req[1] == '/') // remove redundant slashes
        req++;

    char* pathBegin = req;
    char* queryBegin = nullptr;

#ifdef _sse4_2_
    const __m128i simdSpace = _mm_set1_epi8(' ');
    const __m128i simdTab = _mm_set1_epi8('\t');
    const __m128i simdHash = _mm_set1_epi8('#');
    const __m128i simdQuestion = _mm_set1_epi8('?');

    auto isEnd = [=](__m128i x) {
        const auto v = _mm_or_si128(
                _mm_or_si128(
                    _mm_cmpeq_epi8(x, simdSpace), _mm_cmpeq_epi8(x, simdTab)),
                _mm_cmpeq_epi8(x, simdHash));
        return !_mm_testz_si128(v, v);
    };

    // No need for the range check because we have padding of spaces at the end.
    for (;; req += 16) {
        const auto x = _mm_loadu_si128(reinterpret_cast<const __m128i *>(req));
        const auto isQuestionSimd = _mm_cmpeq_epi8(x, simdQuestion);
        const auto isQuestion = !_mm_testz_si128(isQuestionSimd, isQuestionSimd);
        if (isEnd(x)) {
            if (isQuestion) {
                // The prospective query end and a question sign are both in the
                // current block. Need to find out which comes first.
                for (;*req != ' ' && *req != '\t' && *req != '#'; ++req) {
                    if (*req == '?') {
                        queryBegin = req + 1;
                        break;
                    }
                }
            }
            break;
        }
        if (isQuestion) {
            // Find the exact query beginning
            for (queryBegin = req; *queryBegin != '?'; ++queryBegin) {
            }
            ++queryBegin;

            break;
        }
    }

    // If we bailed out because we found query string begin. Now look for the the end of the query
    if (queryBegin) {
        for (;; req += 16) {
            const auto x = _mm_loadu_si128(reinterpret_cast<const __m128i *>(req));
            if (isEnd(x)) {
                break;
            }
        }
    }
#else
    for (;*req != ' ' && *req != '\t' && *req != '#'; ++req) {
        if (*req == '?') {
            queryBegin = req + 1;
            break;
        }
    }
#endif

    while (*req != ' ' && *req != '\t' && *req != '#') {
        ++req;
    }

    char* pathEnd = queryBegin ? queryBegin - 1 : req;
    // Make sure Path_ and Query_ are actually zero-reminated.
    *pathEnd = '\0';
    *req = '\0';
    Path_ = TStringBuf{pathBegin, pathEnd};
    if (queryBegin) {
        Query_ = TStringBuf{queryBegin, req};
        OrigQuery_ = Query_;
    } else {
        Query_ = {};
        OrigQuery_ = {};
    }

    return true;
}

void TBaseServerRequestData::AddHeader(const TString& name, const TString& value) {
    HeadersIn_[name] = value;

    if (stricmp(name.data(), "Host") == 0) {
        size_t hostLen = strcspn(value.data(), ":");
        if (value[hostLen] == ':')
            Port_ = value.substr(hostLen + 1);
        Host_ = value.substr(0, hostLen);
    }
}

void TBaseServerRequestData::SetPath(TString path) {
    Path_ = std::move(path);
}