aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/rain_check/http/client.cpp
blob: d2e36093dc1fb5abbd34cbca5ec121d5a6c1cc89 (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
#include "client.h"

#include "http_code_extractor.h"

#include <library/cpp/http/io/stream.h>
#include <library/cpp/neh/factory.h>
#include <library/cpp/neh/http_common.h>
#include <library/cpp/neh/location.h>
#include <library/cpp/neh/neh.h>

#include <util/generic/ptr.h> 
#include <util/generic/strbuf.h>
#include <util/network/socket.h>
#include <util/stream/str.h>

namespace NRainCheck {
    class THttpCallback: public NNeh::IOnRecv {
    public:
        THttpCallback(NRainCheck::THttpFuture* future)
            : Future(future)
        {
            Y_VERIFY(!!future, "future is NULL");
        }

        void OnRecv(NNeh::THandle& handle) override {
            THolder<THttpCallback> self(this);
            NNeh::TResponseRef response = handle.Get();
            Future->SetDoneAndSchedule(response);
        }

    private:
        NRainCheck::THttpFuture* const Future;
    };

    THttpFuture::THttpFuture()
        : Task(nullptr)
        , ErrorCode(THttpFuture::NoError)
    {
    }

    THttpFuture::~THttpFuture() {
    }

    bool THttpFuture::HasError() const {
        return (ErrorCode != THttpFuture::NoError);
    }

    THttpFuture::EError THttpFuture::GetErrorCode() const {
        return ErrorCode;
    }

    TString THttpFuture::GetErrorDescription() const {
        return ErrorDescription;
    }

    THttpClientService::THttpClientService()
        : GetProtocol(NNeh::ProtocolFactory()->Protocol("http"))
        , FullProtocol(NNeh::ProtocolFactory()->Protocol("full"))
    {
        Y_VERIFY(!!GetProtocol, "GET protocol is NULL.");
        Y_VERIFY(!!FullProtocol, "POST protocol is NULL.");
    }

    THttpClientService::~THttpClientService() {
    }

    void THttpClientService::SendPost(TString addr, const TString& data, const THttpHeaders& headers, THttpFuture* future) {
        Y_VERIFY(!!future, "future is NULL.");

        TTaskRunnerBase* current = TTaskRunnerBase::CurrentTask();
        future->SetRunning(current);
        future->Task = current;

        THolder<THttpCallback> callback(new THttpCallback(future));
        NNeh::TServiceStatRef stat;
        try {
            NNeh::TMessage msg(addr.replace(0, NNeh::TParsedLocation(addr).Scheme.size(), "post"), data);
            TStringStream headersText;
            headers.OutTo(&headersText);
            NNeh::NHttp::MakeFullRequest(msg, headersText.Str(), TString());
            FullProtocol->ScheduleRequest(msg, callback.Get(), stat);
            Y_UNUSED(callback.Release());
        } catch (const TNetworkResolutionError& err) {
            future->SetFail(THttpFuture::CantResolveNameError, err.AsStrBuf());
        } catch (const yexception& err) {
            future->SetFail(THttpFuture::OtherError, err.AsStrBuf());
        }
    }

    void THttpClientService::Send(const TString& request, THttpFuture* future) {
        Y_VERIFY(!!future, "future is NULL.");

        TTaskRunnerBase* current = TTaskRunnerBase::CurrentTask();
        future->SetRunning(current);
        future->Task = current;

        THolder<THttpCallback> callback(new THttpCallback(future));
        NNeh::TServiceStatRef stat;
        try {
            GetProtocol->ScheduleRequest(NNeh::TMessage::FromString(request),
                                         callback.Get(),
                                         stat);
            Y_UNUSED(callback.Release());
        } catch (const TNetworkResolutionError& err) {
            future->SetFail(THttpFuture::CantResolveNameError, err.AsStrBuf());
        } catch (const yexception& err) {
            future->SetFail(THttpFuture::OtherError, err.AsStrBuf());
        }
    }

    bool THttpFuture::HasHttpCode() const {
        return !!HttpCode;
    }

    bool THttpFuture::HasResponseBody() const {
        return !!Response;
    }

    ui32 THttpFuture::GetHttpCode() const {
        Y_ASSERT(IsDone());
        Y_ASSERT(HasHttpCode());

        return static_cast<ui32>(*HttpCode);
    }

    TString THttpFuture::GetResponseBody() const {
        Y_ASSERT(IsDone());
        Y_ASSERT(HasResponseBody());

        return Response->Data;
    }

    void THttpFuture::SetDoneAndSchedule(TAutoPtr<NNeh::TResponse> response) {
        if (!response->IsError()) {
            ErrorCode = THttpFuture::NoError;
            HttpCode = HttpCodes::HTTP_OK;
        } else {
            ErrorCode = THttpFuture::BadHttpCodeError;
            ErrorDescription = response->GetErrorText();

            HttpCode = TryGetHttpCodeFromErrorDescription(ErrorDescription);
        }
        Response.Reset(response);
        SetDone();
    }

    void THttpFuture::SetFail(THttpFuture::EError errorCode, const TStringBuf& errorDescription) {
        ErrorCode = errorCode;
        ErrorDescription = errorDescription;
        Response.Destroy();
        SetDone();
    }

}