aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/http/http.h
blob: a2d8004a64be7da236a2a22ffda9c885d58caa09 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
#pragma once
#include <util/datetime/base.h>
#include <util/string/builder.h>
#include <util/system/thread.h>
#include <util/system/hp_timer.h>
#include <util/generic/hash_set.h>
#include <util/generic/buffer.h>
#include <util/generic/intrlist.h>
#include "http_config.h"

// TODO(xenoxeno): hide in implementation
template <typename Type>
struct THash<TIntrusivePtr<Type>> {
    size_t operator ()(const TIntrusivePtr<Type>& ptr) const { return reinterpret_cast<size_t>(ptr.Get()); }
};

template<>
inline void Out<NHttp::THttpConfig::SocketAddressType>(IOutputStream& o, const NHttp::THttpConfig::SocketAddressType& x) {
    o << x->ToString();
}

namespace NHttp {

bool IsIPv6(const TString& host);
bool IsIPv4(const TString& host);
bool CrackURL(TStringBuf url, TStringBuf& scheme, TStringBuf& host, TStringBuf& uri);
void CrackAddress(const TString& address, TString& hostname, TIpPort& port);
void TrimBegin(TStringBuf& target, char delim);
void TrimEnd(TStringBuf& target, char delim);
void Trim(TStringBuf& target, char delim);
void TrimEnd(TString& target, char delim);
TString CompressDeflate(TStringBuf source);
TString DecompressDeflate(TStringBuf source);

struct TLessNoCase {
    bool operator()(TStringBuf l, TStringBuf r) const {
        auto ll = l.length();
        auto rl = r.length();
        if (ll != rl) {
            return ll < rl;
        }
        return strnicmp(l.data(), r.data(), ll) < 0;
    }
};

struct TEqNoCase {
    bool operator()(TStringBuf l, TStringBuf r) const {
        auto ll = l.length();
        auto rl = r.length();
        if (ll != rl) {
            return false;
        }
        return strnicmp(l.data(), r.data(), ll) == 0;
    }
};

struct TSensors {
    TString Direction;
    TString Host;
    TString Url;
    TString Status;
    TDuration Time;

    TSensors(
            TStringBuf direction,
            TStringBuf host,
            TStringBuf url,
            TStringBuf status,
            TDuration time)
        : Direction(direction)
        , Host(host)
        , Url(url)
        , Status(status)
        , Time(time)
    {}
};

struct TUrlParameters {
    THashMap<TStringBuf, TStringBuf> Parameters;

    TUrlParameters(TStringBuf url);
    TString operator [](TStringBuf name) const;
    bool Has(TStringBuf name) const;
    TStringBuf Get(TStringBuf name) const; // raw
    TString Render() const;
};

struct TCookies {
    THashMap<TStringBuf, TStringBuf> Cookies;

    TCookies(TStringBuf cookie);
    TCookies(const TCookies&) = delete;
    TStringBuf operator [](TStringBuf name) const;
    bool Has(TStringBuf name) const;
    TStringBuf Get(TStringBuf name) const; // raw
    TString Render() const;
};

struct TCookiesBuilder : TCookies {
    TDeque<std::pair<TString, TString>> Data;

    TCookiesBuilder();
    void Set(TStringBuf name, TStringBuf data);
};

struct THeaders {
    TMap<TStringBuf, TStringBuf, TLessNoCase> Headers;

    THeaders() = default;
    THeaders(TStringBuf headers);
    THeaders(const THeaders&) = delete;
    const TStringBuf operator [](TStringBuf name) const;
    bool Has(TStringBuf name) const;
    TStringBuf Get(TStringBuf name) const; // raw
    size_t Parse(TStringBuf headers);
    TString Render() const;
};

struct THeadersBuilder : THeaders {
    TDeque<std::pair<TString, TString>> Data;

    THeadersBuilder();
    THeadersBuilder(TStringBuf headers);
    THeadersBuilder(const THeadersBuilder& builder);
    void Set(TStringBuf name, TStringBuf data);
    void Erase(TStringBuf name);
};

class TSocketBuffer : public TBuffer, public THttpConfig {
public:
    TSocketBuffer()
        : TBuffer(BUFFER_SIZE)
    {}

    bool EnsureEnoughSpaceAvailable(size_t need) {
        size_t avail = Avail();
        if (avail < need) {
            Reserve(Capacity() + std::max(need, BUFFER_MIN_STEP));
            return false;
        }
        return true;
    }

    // non-destructive variant of AsString
    TString AsString() const {
        return TString(Data(), Size());
    }
};

class THttpRequest {
public:
    TStringBuf Method;
    TStringBuf URL;
    TStringBuf Protocol;
    TStringBuf Version;
    TStringBuf Headers;

    TStringBuf Host;
    TStringBuf Accept;
    TStringBuf Connection;
    TStringBuf ContentType;
    TStringBuf ContentLength;
    TStringBuf AcceptEncoding;
    TStringBuf TransferEncoding;

    TStringBuf Body;

    static const TMap<TStringBuf, TStringBuf THttpRequest::*, TLessNoCase> HeadersLocation;

    template <TStringBuf THttpRequest::* Header>
    static TStringBuf GetName();
    void Clear();
};

class THttpResponse {
public:
    TStringBuf Protocol;
    TStringBuf Version;
    TStringBuf Status;
    TStringBuf Message;
    TStringBuf Headers;

    TStringBuf Connection;
    TStringBuf ContentType;
    TStringBuf ContentLength;
    TStringBuf TransferEncoding;
    TStringBuf LastModified;
    TStringBuf ContentEncoding;

    TStringBuf Body;

    static const TMap<TStringBuf, TStringBuf THttpResponse::*, TLessNoCase> HeadersLocation;

    template <TStringBuf THttpResponse::* Header>
    static TStringBuf GetName();
    void Clear();
};

template <typename HeaderType, typename BufferType>
class THttpParser : public HeaderType, public BufferType {
public:
    enum class EParseStage : ui8 {
        Method,
        URL,
        Protocol,
        Version,
        Status,
        Message,
        Header,
        Body,
        ChunkLength,
        ChunkData,
        Done,
        Error,
    };

    static constexpr size_t MaxMethodSize = 8;
    static constexpr size_t MaxURLSize = 2048;
    static constexpr size_t MaxProtocolSize = 4;
    static constexpr size_t MaxVersionSize = 4;
    static constexpr size_t MaxStatusSize = 3;
    static constexpr size_t MaxMessageSize = 1024;
    static constexpr size_t MaxHeaderSize = 8192;
    static constexpr size_t MaxChunkLengthSize = 8;
    static constexpr size_t MaxChunkSize = 256 * 1024 * 1024;
    static constexpr size_t MaxChunkContentSize = 1 * 1024 * 1024 * 1024;

    EParseStage Stage;
    EParseStage LastSuccessStage;
    TStringBuf Line;
    TStringBuf& Header = Line;
    size_t ChunkLength = 0;
    size_t ContentSize = 0;
    TString Content; // body storage
    std::optional<size_t> TotalSize;

    THttpParser(const THttpParser& src)
        : HeaderType(src)
        , BufferType(src)
        , Stage(src.Stage)
        , LastSuccessStage(src.LastSuccessStage)
        , Line()
        , Header(Line)
        , ChunkLength(src.ChunkLength)
        , ContentSize(src.ContentSize)
        , Content(src.Content)
    {}

    template <typename StringType>
    bool ProcessData(StringType& target, TStringBuf& source, char delim, size_t maxLen) {
        TStringBuf maxSource(source.substr(0, maxLen + 1 - target.size()));
        size_t pos = maxSource.find(delim);
        target += maxSource.substr(0, pos);
        source.Skip(pos);
        if (target.size() > maxLen) {
            Stage = EParseStage::Error;
            return false;
        }
        if (!source.empty() && *source.begin() == delim) {
            source.Skip(1);
        }
        return pos != TStringBuf::npos;
    }

    template <typename StringType>
    bool ProcessData(StringType& target, TStringBuf& source, TStringBuf delim, size_t maxLen) {
        if (delim.empty()) {
            return false;
        }
        if (delim.size() == 1) {
            return ProcessData(target, source, delim[0], maxLen);
        }
        if (ProcessData(target, source, delim.back(), maxLen + 1)) {
            for (signed i = delim.size() - 2; i >= 0; --i) {
                TrimEnd(target, delim[i]);
            }
            return true;
        }
        return false;
    }

    template <typename StringType>
    bool ProcessData(StringType& target, TStringBuf& source, size_t size) {
        TStringBuf maxSource(source.substr(0, size - target.size()));
        target += maxSource;
        source.Skip(maxSource.size());
        if (target.size() > size && !source.empty()) {
            Stage = EParseStage::Error;
            return false;
        }
        return target.size() == size;
    }

    void ProcessHeader(TStringBuf& header) {
        TStringBuf name = header.NextTok(':');
        TrimBegin(name, ' ');
        TStringBuf value = header;
        Trim(value, ' ');
        auto cit = HeaderType::HeadersLocation.find(name);
        if (cit != HeaderType::HeadersLocation.end()) {
            this->*cit->second = value;
        }
        header.Clear();
    }

    size_t ParseHex(TStringBuf value) {
        size_t result = 0;
        for (char ch : value) {
            if (ch >= '0' && ch <= '9') {
                result *= 16;
                result += ch - '0';
            } else if (ch >= 'a' && ch <= 'f') {
                result *= 16;
                result += 10 + ch - 'a';
            } else if (ch >= 'A' && ch <= 'F') {
                result *= 16;
                result += 10 + ch - 'A';
            } else if (ch == ';') {
                break;
            } else if (isspace(ch)) {
                continue;
            } else {
                Stage = EParseStage::Error;
                return 0;
            }
        }
        return result;
    }

    void Advance(size_t len);
    void ConnectionClosed();

    size_t GetBodySizeFromTotalSize() const {
        return TotalSize.value() - (HeaderType::Headers.end() - BufferType::Data());
    }

    void Clear() {
        BufferType::Clear();
        HeaderType::Clear();
        Stage = GetInitialStage();
        Line.Clear();
        Content.clear();
    }

    bool IsReady() const {
        return Stage == EParseStage::Done;
    }

    bool IsError() const {
        return Stage == EParseStage::Error;
    }

    TStringBuf GetErrorText() const {
        switch (LastSuccessStage) {
        case EParseStage::Method:
            return "Invalid http method";
        case EParseStage::URL:
            return "Invalid url";
        case EParseStage::Protocol:
            return "Invalid http protocol";
        case EParseStage::Version:
            return "Invalid http version";
        case EParseStage::Status:
            return "Invalid http status";
        case EParseStage::Message:
            return "Invalid http message";
        case EParseStage::Header:
            return "Invalid http header";
        case EParseStage::Body:
            return "Invalid content body";
        case EParseStage::ChunkLength:
        case EParseStage::ChunkData:
            return "Broken chunked data";
        case EParseStage::Done:
            return "Everything is fine";
        case EParseStage::Error:
            return "Error on error"; // wat? ...because we don't want to include default label here
        }
    }

    bool IsDone() const {
        return IsReady() || IsError();
    }

    bool HaveBody() const;

    bool EnsureEnoughSpaceAvailable(size_t need = BufferType::BUFFER_MIN_STEP) {
        bool result = BufferType::EnsureEnoughSpaceAvailable(need);
        if (!result && !BufferType::Empty()) {
            Reparse();
        }
        return true;
    }

    void Reparse() {
        size_t size = BufferType::Size();
        Clear();
        Advance(size);
    }

    TStringBuf GetRawData() const {
        return TStringBuf(BufferType::Data(), BufferType::Size());
    }

    TString GetObfuscatedData() const {
        THeaders headers(HeaderType::Headers);
        TStringBuf authorization(headers["Authorization"]);
        TStringBuf cookie(headers["Cookie"]);
        TStringBuf x_ydb_auth_ticket(headers["x-ydb-auth-ticket"]);
        TStringBuf x_yacloud_subjecttoken(headers["x-yacloud-subjecttoken"]);
        TString data(GetRawData());
        if (!authorization.empty()) {
            auto pos = data.find(authorization);
            if (pos != TString::npos) {
                data.replace(pos, authorization.size(), TString("<obfuscated>"));
            }
        }
        if (!cookie.empty()) {
            auto pos = data.find(cookie);
            if (pos != TString::npos) {
                data.replace(pos, cookie.size(), TString("<obfuscated>"));
            }
        }
        if (!x_ydb_auth_ticket.empty()) {
            auto pos = data.find(x_ydb_auth_ticket);
            if (pos != TString::npos) {
                data.replace(pos, x_ydb_auth_ticket.size(), TString("<obfuscated>"));
            }
        }
        if (!x_yacloud_subjecttoken.empty()) {
            auto pos = data.find(x_yacloud_subjecttoken);
            if (pos != TString::npos) {
                data.replace(pos, x_yacloud_subjecttoken.size(), TString("<obfuscated>"));
            }
        }
        return data;
    }

    static EParseStage GetInitialStage();

    THttpParser()
        : Stage(GetInitialStage())
        , LastSuccessStage(Stage)
    {}

    THttpParser(TStringBuf data)
        : Stage(GetInitialStage())
        , LastSuccessStage(Stage)
    {
        BufferType::Assign(data.data(), data.size());
        BufferType::Clear(); // reset position to 0
        TotalSize = data.size();
        Advance(data.size());
    }
};

template <typename HeaderType, typename BufferType>
class THttpRenderer : public HeaderType, public BufferType {
public:
    enum class ERenderStage {
        Init,
        Header,
        Body,
        Done,
        Error,
    };

    ERenderStage Stage = ERenderStage::Init;
    TString Content; // body storage

    //THttpRenderer(TStringBuf method, TStringBuf url, TStringBuf protocol, TStringBuf version); // request
    void InitRequest(TStringBuf method, TStringBuf url, TStringBuf protocol, TStringBuf version) {
        Y_VERIFY_DEBUG(Stage == ERenderStage::Init);
        AppendParsedValue<&THttpRequest::Method>(method);
        Append(' ');
        AppendParsedValue<&THttpRequest::URL>(url);
        Append(' ');
        AppendParsedValue<&THttpRequest::Protocol>(protocol);
        Append('/');
        AppendParsedValue<&THttpRequest::Version>(version);
        Append("\r\n");
        Stage = ERenderStage::Header;
        HeaderType::Headers = TStringBuf(BufferType::Pos(), size_t(0));
    }

    //THttpRenderer(TStringBuf protocol, TStringBuf version, TStringBuf status, TStringBuf message); // response
    void InitResponse(TStringBuf protocol, TStringBuf version, TStringBuf status, TStringBuf message) {
        Y_VERIFY_DEBUG(Stage == ERenderStage::Init);
        AppendParsedValue<&THttpResponse::Protocol>(protocol);
        Append('/');
        AppendParsedValue<&THttpResponse::Version>(version);
        Append(' ');
        AppendParsedValue<&THttpResponse::Status>(status);
        Append(' ');
        AppendParsedValue<&THttpResponse::Message>(message);
        Append("\r\n");
        Stage = ERenderStage::Header;
        HeaderType::Headers = TStringBuf(BufferType::Pos(), size_t(0));
    }

    void Append(TStringBuf text) {
        EnsureEnoughSpaceAvailable(text.size());
        BufferType::Append(text.data(), text.size());
    }

    void Append(char c) {
        EnsureEnoughSpaceAvailable(sizeof(c));
        BufferType::Append(c);
    }

    template <TStringBuf HeaderType::* string>
    void AppendParsedValue(TStringBuf value) {
        Append(value);
        static_cast<HeaderType*>(this)->*string = TStringBuf(BufferType::Pos() - value.size(), value.size());
    }

    template <TStringBuf HeaderType::* name>
    void Set(TStringBuf value) {
        Y_VERIFY_DEBUG(Stage == ERenderStage::Header);
        Append(HeaderType::template GetName<name>());
        Append(": ");
        AppendParsedValue<name>(value);
        Append("\r\n");
        HeaderType::Headers = TStringBuf(HeaderType::Headers.Data(), BufferType::Pos() - HeaderType::Headers.Data());
    }

    void Set(TStringBuf name, TStringBuf value) {
        Y_VERIFY_DEBUG(Stage == ERenderStage::Header);
        Append(name);
        Append(": ");
        auto data = BufferType::Pos();
        Append(value);
        auto cit = HeaderType::HeadersLocation.find(name);
        if (cit != HeaderType::HeadersLocation.end()) {
            (this->*cit->second) = TStringBuf(data, BufferType::Pos());
        }
        Append("\r\n");
        HeaderType::Headers = TStringBuf(HeaderType::Headers.Data(), BufferType::Pos() - HeaderType::Headers.Data());
    }

    void Set(const THeaders& headers) {
        Y_VERIFY_DEBUG(Stage == ERenderStage::Header);
        for (const auto& [name, value] : headers.Headers) {
            Set(name, value);
        }
        HeaderType::Headers = TStringBuf(HeaderType::Headers.Data(), BufferType::Pos() - HeaderType::Headers.Data());
    }

    static constexpr TStringBuf ALLOWED_CONTENT_ENCODINGS[] = {"deflate"};

    void SetContentEncoding(TStringBuf contentEncoding) {
        Y_VERIFY_DEBUG(Stage == ERenderStage::Header);
        if (Count(ALLOWED_CONTENT_ENCODINGS, contentEncoding) != 0) {
            Set("Content-Encoding", contentEncoding);
        }
    }

    void FinishHeader() {
        Append("\r\n");
        HeaderType::Headers = TStringBuf(HeaderType::Headers.Data(), BufferType::Pos() - HeaderType::Headers.Data());
        Stage = ERenderStage::Body;
    }

    void SetBody(TStringBuf body) {
        Y_VERIFY_DEBUG(Stage == ERenderStage::Header);
        if (HeaderType::ContentLength.empty()) {
            Set<&HeaderType::ContentLength>(ToString(body.size()));
        }
        FinishHeader();
        AppendParsedValue<&HeaderType::Body>(body);
        Stage = ERenderStage::Done;
    }

    void FinishBody() {
        Stage = ERenderStage::Done;
    }

    bool IsDone() const {
        return Stage == ERenderStage::Done;
    }

    void Finish() {
        switch (Stage) {
        case ERenderStage::Header:
            FinishHeader();
            FinishBody();
            break;
        case ERenderStage::Body:
            FinishBody();
            break;
        default:
            break;
        }
    }

    bool EnsureEnoughSpaceAvailable(size_t need = BufferType::BUFFER_MIN_STEP) {
        bool result = BufferType::EnsureEnoughSpaceAvailable(need);
        if (!result && !BufferType::Empty()) {
            Reparse();
        }
        return true;
    }

    void Clear() {
        BufferType::Clear();
        HeaderType::Clear();
    }

    void Reparse() {
        // move-magic
        size_t size = BufferType::Size();
        THttpParser<HeaderType, BufferType> parser;
        // move the buffer to parser
        static_cast<BufferType&>(parser) = std::move(static_cast<BufferType&>(*this));
        // reparse
        parser.Clear();
        parser.Advance(size);
        // move buffer and result back
        bool needReassignBody = (parser.Body.data() == parser.Content.data());
        static_cast<HeaderType&>(*this) = std::move(static_cast<HeaderType&>(parser));
        static_cast<BufferType&>(*this) = std::move(static_cast<BufferType&>(parser));
        if (needReassignBody) {
            Content = std::move(parser.Content);
            HeaderType::Body = Content;
        }
        switch (parser.Stage) {
        case THttpParser<HeaderType, BufferType>::EParseStage::Method:
        case THttpParser<HeaderType, BufferType>::EParseStage::URL:
        case THttpParser<HeaderType, BufferType>::EParseStage::Protocol:
        case THttpParser<HeaderType, BufferType>::EParseStage::Version:
        case THttpParser<HeaderType, BufferType>::EParseStage::Status:
        case THttpParser<HeaderType, BufferType>::EParseStage::Message:
            Stage = ERenderStage::Init;
            break;
        case THttpParser<HeaderType, BufferType>::EParseStage::Header:
            Stage = ERenderStage::Header;
            break;
        case THttpParser<HeaderType, BufferType>::EParseStage::Body:
        case THttpParser<HeaderType, BufferType>::EParseStage::ChunkLength:
        case THttpParser<HeaderType, BufferType>::EParseStage::ChunkData:
            Stage = ERenderStage::Body;
            break;
        case THttpParser<HeaderType, BufferType>::EParseStage::Done:
            Stage = ERenderStage::Done;
            break;
        case THttpParser<HeaderType, BufferType>::EParseStage::Error:
            Stage = ERenderStage::Error;
            break;
        }
        Y_ABORT_UNLESS(size == BufferType::Size());
    }

    TStringBuf GetRawData() const {
        return TStringBuf(BufferType::Data(), BufferType::Size());
    }
};

template <>
template <>
inline void THttpRenderer<THttpResponse, TSocketBuffer>::Set<&THttpResponse::Body>(TStringBuf value) {
    SetBody(value);
}

template <>
template <>
inline void THttpRenderer<THttpRequest, TSocketBuffer>::Set<&THttpRequest::Body>(TStringBuf value) {
    SetBody(value);
}

template <>
template <>
inline void THttpRenderer<THttpResponse, TSocketBuffer>::Set<&THttpResponse::ContentEncoding>(TStringBuf value) {
    SetContentEncoding(value);
}

struct THttpEndpointInfo {
    TString WorkerName;
    bool Secure = false;
    const std::vector<TString> CompressContentTypes; // content types, which will be automatically compressed on response

    THttpEndpointInfo() = default;

protected:
    THttpEndpointInfo(std::vector<TString> compressContentTypes)
        : CompressContentTypes(std::move(compressContentTypes))
    {}
};

class THttpIncomingRequest;
using THttpIncomingRequestPtr = TIntrusivePtr<THttpIncomingRequest>;

class THttpOutgoingResponse;
using THttpOutgoingResponsePtr = TIntrusivePtr<THttpOutgoingResponse>;

class THttpIncomingRequest :
        public THttpParser<THttpRequest, TSocketBuffer>,
        public TRefCounted<THttpIncomingRequest, TAtomicCounter> {
public:
    std::shared_ptr<THttpEndpointInfo> Endpoint;
    THttpConfig::SocketAddressType Address;
    THPTimer Timer;

    THttpIncomingRequest()
        : Endpoint(std::make_shared<THttpEndpointInfo>())
    {}

    THttpIncomingRequest(std::shared_ptr<THttpEndpointInfo> endpoint, const THttpConfig::SocketAddressType& address)
        : Endpoint(std::move(endpoint))
        , Address(address)
    {}

    THttpIncomingRequest(TStringBuf content, std::shared_ptr<THttpEndpointInfo> endpoint, const THttpConfig::SocketAddressType& address)
        : THttpParser(content)
        , Endpoint(std::move(endpoint))
        , Address(address)
    {}

    bool IsConnectionClose() const {
        if (Connection.empty()) {
            return Version == "1.0";
        } else {
            return TEqNoCase()(Connection, "close");
        }
    }

    TStringBuf GetConnection() const {
        if (!Connection.empty()) {
            if (TEqNoCase()(Connection, "keep-alive")) {
                return "keep-alive";
            }
            if (TEqNoCase()(Connection, "close")) {
                return "close";
            }
        }
        return Version == "1.0" ? "close" : "keep-alive";
    }

    THttpOutgoingResponsePtr CreateResponseOK(TStringBuf body, TStringBuf contentType = "text/html", TInstant lastModified = TInstant());
    THttpOutgoingResponsePtr CreateResponseString(TStringBuf data);
    THttpOutgoingResponsePtr CreateResponseBadRequest(TStringBuf html = TStringBuf(), TStringBuf contentType = "text/html"); // 400
    THttpOutgoingResponsePtr CreateResponseNotFound(TStringBuf html = TStringBuf(), TStringBuf contentType = "text/html"); // 404
    THttpOutgoingResponsePtr CreateResponseServiceUnavailable(TStringBuf html = TStringBuf(), TStringBuf contentType = "text/html"); // 503
    THttpOutgoingResponsePtr CreateResponseGatewayTimeout(TStringBuf html = TStringBuf(), TStringBuf contentType = "text/html"); // 504
    THttpOutgoingResponsePtr CreateResponse(TStringBuf status, TStringBuf message);
    THttpOutgoingResponsePtr CreateResponse(TStringBuf status, TStringBuf message, const THeaders& headers);
    THttpOutgoingResponsePtr CreateResponse(TStringBuf status, TStringBuf message, const THeaders& headers, TStringBuf body);
    THttpOutgoingResponsePtr CreateResponse(
            TStringBuf status,
            TStringBuf message,
            TStringBuf contentType,
            TStringBuf body = TStringBuf(),
            TInstant lastModified = TInstant());

    THttpOutgoingResponsePtr CreateIncompleteResponse(TStringBuf status, TStringBuf message, const THeaders& headers = {});
    THttpOutgoingResponsePtr CreateIncompleteResponse(TStringBuf status, TStringBuf message, const THeaders& headers, TStringBuf body);

    THttpIncomingRequestPtr Duplicate();

private:
    THttpOutgoingResponsePtr ConstructResponse(TStringBuf status, TStringBuf message);
    void FinishResponse(THttpOutgoingResponsePtr& response, TStringBuf body = TStringBuf());
};

class THttpIncomingResponse;
using THttpIncomingResponsePtr = TIntrusivePtr<THttpIncomingResponse>;

class THttpOutgoingRequest;
using THttpOutgoingRequestPtr = TIntrusivePtr<THttpOutgoingRequest>;

class THttpIncomingResponse :
        public THttpParser<THttpResponse, TSocketBuffer>,
        public TRefCounted<THttpIncomingResponse, TAtomicCounter> {
public:
    THttpIncomingResponse(THttpOutgoingRequestPtr request);

    THttpOutgoingRequestPtr GetRequest() const {
        return Request;
    }

    THttpIncomingResponsePtr Duplicate(THttpOutgoingRequestPtr request);
    THttpOutgoingResponsePtr Reverse(THttpIncomingRequestPtr request);

protected:
    THttpOutgoingRequestPtr Request;
};

class THttpOutgoingRequest :
        public THttpRenderer<THttpRequest, TSocketBuffer>,
        public TRefCounted<THttpOutgoingRequest, TAtomicCounter> {
public:
    THPTimer Timer;
    bool Secure = false;

    THttpOutgoingRequest() = default;
    THttpOutgoingRequest(TStringBuf method, TStringBuf url, TStringBuf protocol, TStringBuf version);
    THttpOutgoingRequest(TStringBuf method, TStringBuf scheme, TStringBuf host, TStringBuf uri, TStringBuf protocol, TStringBuf version);
    static THttpOutgoingRequestPtr CreateRequestString(TStringBuf data);
    static THttpOutgoingRequestPtr CreateRequestString(const TString& data);
    static THttpOutgoingRequestPtr CreateRequestGet(TStringBuf url);
    static THttpOutgoingRequestPtr CreateRequestGet(TStringBuf host, TStringBuf uri); // http only
    static THttpOutgoingRequestPtr CreateRequestPost(TStringBuf url, TStringBuf contentType = {}, TStringBuf body = {});
    static THttpOutgoingRequestPtr CreateRequestPost(TStringBuf host, TStringBuf uri, TStringBuf contentType, TStringBuf body); // http only
    static THttpOutgoingRequestPtr CreateRequest(TStringBuf method, TStringBuf url, TStringBuf contentType = TStringBuf(), TStringBuf body = TStringBuf());
    static THttpOutgoingRequestPtr CreateHttpRequest(TStringBuf method, TStringBuf host, TStringBuf uri, TStringBuf contentType = TStringBuf(), TStringBuf body = TStringBuf());
    THttpOutgoingRequestPtr Duplicate();
};

class THttpOutgoingResponse :
        public THttpRenderer<THttpResponse, TSocketBuffer>,
        public TRefCounted<THttpOutgoingResponse, TAtomicCounter> {
public:
    THttpOutgoingResponse(THttpIncomingRequestPtr request);
    THttpOutgoingResponse(THttpIncomingRequestPtr request, TStringBuf protocol, TStringBuf version, TStringBuf status, TStringBuf message);

    bool IsConnectionClose() const {
        if (!Connection.empty()) {
            return TEqNoCase()(Connection, "close");
        } else {
            return Request->IsConnectionClose();
        }
    }

    bool IsNeedBody() const {
        return GetRequest()->Method != "HEAD" && Status != "204";
    }

    bool EnableCompression() {
        TStringBuf acceptEncoding = Request->AcceptEncoding;
        std::vector<TStringBuf> encodings;
        TStringBuf encoding;
        while (acceptEncoding.NextTok(',', encoding)) {
            Trim(encoding, ' ');
            if (Count(ALLOWED_CONTENT_ENCODINGS, encoding) != 0) {
                encodings.push_back(encoding);
            }
        }
        if (!encodings.empty()) {
            // TODO: prioritize encodings
            SetContentEncoding(encodings.front());
            return true;
        }
        return false;
    }

    void SetBody(TStringBuf body) {
        if (ContentEncoding == "deflate") {
            TString compressedBody = CompressDeflate(body);
            THttpRenderer<THttpResponse, TSocketBuffer>::SetBody(compressedBody);
            Body = Content = body;
        } else {
            THttpRenderer<THttpResponse, TSocketBuffer>::SetBody(body);
        }
    }

    void SetBody(const TString& body) {
        if (ContentEncoding == "deflate") {
            TString compressedBody = CompressDeflate(body);
            THttpRenderer<THttpResponse, TSocketBuffer>::SetBody(compressedBody);
            Body = Content = body;
        } else {
            THttpRenderer<THttpResponse, TSocketBuffer>::SetBody(body);
        }
    }

    THttpIncomingRequestPtr GetRequest() const {
        return Request;
    }

    THttpOutgoingResponsePtr Duplicate(THttpIncomingRequestPtr request);

// it's temporary accessible for cleanup
//protected:
    THttpIncomingRequestPtr Request;
    std::unique_ptr<TSensors> Sensors;
};

}