client.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "client.h"
  2. #include "http_code_extractor.h"
  3. #include <library/cpp/http/io/stream.h>
  4. #include <library/cpp/neh/factory.h>
  5. #include <library/cpp/neh/http_common.h>
  6. #include <library/cpp/neh/location.h>
  7. #include <library/cpp/neh/neh.h>
  8. #include <util/generic/ptr.h>
  9. #include <util/generic/strbuf.h>
  10. #include <util/network/socket.h>
  11. #include <util/stream/str.h>
  12. namespace NRainCheck {
  13. class THttpCallback: public NNeh::IOnRecv {
  14. public:
  15. THttpCallback(NRainCheck::THttpFuture* future)
  16. : Future(future)
  17. {
  18. Y_ABORT_UNLESS(!!future, "future is NULL");
  19. }
  20. void OnRecv(NNeh::THandle& handle) override {
  21. THolder<THttpCallback> self(this);
  22. NNeh::TResponseRef response = handle.Get();
  23. Future->SetDoneAndSchedule(response);
  24. }
  25. private:
  26. NRainCheck::THttpFuture* const Future;
  27. };
  28. THttpFuture::THttpFuture()
  29. : Task(nullptr)
  30. , ErrorCode(THttpFuture::NoError)
  31. {
  32. }
  33. THttpFuture::~THttpFuture() {
  34. }
  35. bool THttpFuture::HasError() const {
  36. return (ErrorCode != THttpFuture::NoError);
  37. }
  38. THttpFuture::EError THttpFuture::GetErrorCode() const {
  39. return ErrorCode;
  40. }
  41. TString THttpFuture::GetErrorDescription() const {
  42. return ErrorDescription;
  43. }
  44. THttpClientService::THttpClientService()
  45. : GetProtocol(NNeh::ProtocolFactory()->Protocol("http"))
  46. , FullProtocol(NNeh::ProtocolFactory()->Protocol("full"))
  47. {
  48. Y_ABORT_UNLESS(!!GetProtocol, "GET protocol is NULL.");
  49. Y_ABORT_UNLESS(!!FullProtocol, "POST protocol is NULL.");
  50. }
  51. THttpClientService::~THttpClientService() {
  52. }
  53. void THttpClientService::SendPost(TString addr, const TString& data, const THttpHeaders& headers, THttpFuture* future) {
  54. Y_ABORT_UNLESS(!!future, "future is NULL.");
  55. TTaskRunnerBase* current = TTaskRunnerBase::CurrentTask();
  56. future->SetRunning(current);
  57. future->Task = current;
  58. THolder<THttpCallback> callback(new THttpCallback(future));
  59. NNeh::TServiceStatRef stat;
  60. try {
  61. NNeh::TMessage msg(addr.replace(0, NNeh::TParsedLocation(addr).Scheme.size(), "post"), data);
  62. TStringStream headersText;
  63. headers.OutTo(&headersText);
  64. NNeh::NHttp::MakeFullRequest(msg, headersText.Str(), TString());
  65. FullProtocol->ScheduleRequest(msg, callback.Get(), stat);
  66. Y_UNUSED(callback.Release());
  67. } catch (const TNetworkResolutionError& err) {
  68. future->SetFail(THttpFuture::CantResolveNameError, err.AsStrBuf());
  69. } catch (const yexception& err) {
  70. future->SetFail(THttpFuture::OtherError, err.AsStrBuf());
  71. }
  72. }
  73. void THttpClientService::Send(const TString& request, THttpFuture* future) {
  74. Y_ABORT_UNLESS(!!future, "future is NULL.");
  75. TTaskRunnerBase* current = TTaskRunnerBase::CurrentTask();
  76. future->SetRunning(current);
  77. future->Task = current;
  78. THolder<THttpCallback> callback(new THttpCallback(future));
  79. NNeh::TServiceStatRef stat;
  80. try {
  81. GetProtocol->ScheduleRequest(NNeh::TMessage::FromString(request),
  82. callback.Get(),
  83. stat);
  84. Y_UNUSED(callback.Release());
  85. } catch (const TNetworkResolutionError& err) {
  86. future->SetFail(THttpFuture::CantResolveNameError, err.AsStrBuf());
  87. } catch (const yexception& err) {
  88. future->SetFail(THttpFuture::OtherError, err.AsStrBuf());
  89. }
  90. }
  91. bool THttpFuture::HasHttpCode() const {
  92. return !!HttpCode;
  93. }
  94. bool THttpFuture::HasResponseBody() const {
  95. return !!Response;
  96. }
  97. ui32 THttpFuture::GetHttpCode() const {
  98. Y_ASSERT(IsDone());
  99. Y_ASSERT(HasHttpCode());
  100. return static_cast<ui32>(*HttpCode);
  101. }
  102. TString THttpFuture::GetResponseBody() const {
  103. Y_ASSERT(IsDone());
  104. Y_ASSERT(HasResponseBody());
  105. return Response->Data;
  106. }
  107. void THttpFuture::SetDoneAndSchedule(TAutoPtr<NNeh::TResponse> response) {
  108. if (!response->IsError()) {
  109. ErrorCode = THttpFuture::NoError;
  110. HttpCode = HttpCodes::HTTP_OK;
  111. } else {
  112. ErrorCode = THttpFuture::BadHttpCodeError;
  113. ErrorDescription = response->GetErrorText();
  114. HttpCode = TryGetHttpCodeFromErrorDescription(ErrorDescription);
  115. }
  116. Response.Reset(response);
  117. SetDone();
  118. }
  119. void THttpFuture::SetFail(THttpFuture::EError errorCode, const TStringBuf& errorDescription) {
  120. ErrorCode = errorCode;
  121. ErrorDescription = errorDescription;
  122. Response.Destroy();
  123. SetDone();
  124. }
  125. }