http_headers.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #include <util/generic/strbuf.h>
  3. #include <util/stream/output.h>
  4. #include <util/string/ascii.h>
  5. namespace NNeh {
  6. namespace NHttp {
  7. template <typename Port>
  8. void WriteHostHeader(IOutputStream& out, TStringBuf host, Port port) {
  9. out << TStringBuf("Host: ") << host;
  10. if (port) {
  11. out << TStringBuf(":") << port;
  12. }
  13. out << TStringBuf("\r\n");
  14. }
  15. class THeaderSplitter {
  16. public:
  17. THeaderSplitter(TStringBuf headers)
  18. : Headers_(headers)
  19. {
  20. }
  21. bool Next(TStringBuf& header) {
  22. while (Headers_.ReadLine(header)) {
  23. if (!header.Empty()) {
  24. return true;
  25. }
  26. }
  27. return false;
  28. }
  29. private:
  30. TStringBuf Headers_;
  31. };
  32. inline bool HasHostHeader(TStringBuf headers) {
  33. THeaderSplitter splitter(headers);
  34. TStringBuf header;
  35. while (splitter.Next(header)) {
  36. if (AsciiHasPrefixIgnoreCase(header, "Host:")) {
  37. return true;
  38. }
  39. }
  40. return false;
  41. }
  42. template <typename Port>
  43. void WriteHostHeaderIfNot(IOutputStream& out, TStringBuf host, Port port, TStringBuf headers) {
  44. if (!NNeh::NHttp::HasHostHeader(headers)) {
  45. NNeh::NHttp::WriteHostHeader(out, host, port);
  46. }
  47. }
  48. }
  49. }