aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/http/server/response.h
blob: a75cb85605f3f52eea7cd8114b19f266c58253ec (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
#pragma once

#include <library/cpp/http/misc/httpcodes.h>
#include <library/cpp/http/io/stream.h>

#include <util/generic/strbuf.h>
#include <util/string/cast.h>

class THttpHeaders;
class IOutputStream;

class THttpResponse {
public:
    THttpResponse() noexcept
        : Code(HTTP_OK)
    {
    }

    explicit THttpResponse(HttpCodes code) noexcept
        : Code(code)
    {
    }

    template <typename ValueType>
    THttpResponse& AddHeader(const TString& name, const ValueType& value) {
        return AddHeader(THttpInputHeader(name, ToString(value)));
    }

    THttpResponse& AddHeader(const THttpInputHeader& header) {
        Headers.AddHeader(header);

        return *this;
    }

    THttpResponse& AddMultipleHeaders(const THttpHeaders& headers);

    const THttpHeaders& GetHeaders() const {
        return Headers;
    }

    THttpResponse& SetContentType(const TStringBuf& contentType);

    /**
     * @note If @arg content isn't empty its size is automatically added as a
     * "Content-Length" header during output to IOutputStream.
     * @see IOutputStream& operator << (IOutputStream&, const THttpResponse&)
     */
    THttpResponse& SetContent(const TString& content) {
        Content = content;

        return *this;
    }

    TString GetContent() const {
        return Content;
    }

    /**
     * @note If @arg content isn't empty its size is automatically added as a
     * "Content-Length" header during output to IOutputStream.
     * @see IOutputStream& operator << (IOutputStream&, const THttpResponse&)
     */
    THttpResponse& SetContent(const TString& content, const TStringBuf& contentType) {
        return SetContent(content).SetContentType(contentType);
    }

    HttpCodes HttpCode() const {
        return Code;
    }

    THttpResponse& SetHttpCode(HttpCodes code) {
        Code = code;
        return *this;
    }

    void OutTo(IOutputStream& out) const;

private:
    HttpCodes Code;
    THttpHeaders Headers;
    TString Content;
};