http_code_extractor.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "http_code_extractor.h"
  2. #include <library/cpp/http/io/stream.h>
  3. #include <library/cpp/http/misc/httpcodes.h>
  4. #include <util/generic/maybe.h>
  5. #include <util/generic/strbuf.h>
  6. #include <util/string/cast.h>
  7. namespace NRainCheck {
  8. TMaybe<HttpCodes> TryGetHttpCodeFromErrorDescription(const TStringBuf& errorMessage) {
  9. // Try to get HttpCode from library/cpp/neh response.
  10. // If response has HttpCode and it is not 200 OK, library/cpp/neh will send a message
  11. // "library/cpp/neh/http.cpp:<LINE>: request failed(<FIRST-HTTP-RESPONSE-LINE>)"
  12. // (see library/cpp/neh/http.cpp:625). So, we will try to parse this message and
  13. // find out HttpCode in it. It is bad temporary solution, but we have no choice.
  14. const TStringBuf SUBSTR = "request failed(";
  15. const size_t SUBSTR_LEN = SUBSTR.size();
  16. const size_t FIRST_LINE_LEN = TStringBuf("HTTP/1.X NNN").size();
  17. TMaybe<HttpCodes> httpCode;
  18. const size_t substrPos = errorMessage.find(SUBSTR);
  19. if (substrPos != TStringBuf::npos) {
  20. const TStringBuf firstLineStart = errorMessage.SubStr(substrPos + SUBSTR_LEN, FIRST_LINE_LEN);
  21. try {
  22. httpCode = static_cast<HttpCodes>(ParseHttpRetCode(firstLineStart));
  23. if (*httpCode < HTTP_CONTINUE || *httpCode >= HTTP_CODE_MAX) {
  24. httpCode = Nothing();
  25. }
  26. } catch (const TFromStringException& ex) {
  27. // Can't parse HttpCode: it is OK, because ErrorDescription can be random string.
  28. }
  29. }
  30. return httpCode;
  31. }
  32. }