aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/neh/http_headers.h
diff options
context:
space:
mode:
authormonster <monster@ydb.tech>2022-07-07 14:41:37 +0300
committermonster <monster@ydb.tech>2022-07-07 14:41:37 +0300
commit06e5c21a835c0e923506c4ff27929f34e00761c2 (patch)
tree75efcbc6854ef9bd476eb8bf00cc5c900da436a2 /library/cpp/neh/http_headers.h
parent03f024c4412e3aa613bb543cf1660176320ba8f4 (diff)
downloadydb-06e5c21a835c0e923506c4ff27929f34e00761c2.tar.gz
fix ya.make
Diffstat (limited to 'library/cpp/neh/http_headers.h')
-rw-r--r--library/cpp/neh/http_headers.h55
1 files changed, 55 insertions, 0 deletions
diff --git a/library/cpp/neh/http_headers.h b/library/cpp/neh/http_headers.h
new file mode 100644
index 0000000000..70cf3a9fbe
--- /dev/null
+++ b/library/cpp/neh/http_headers.h
@@ -0,0 +1,55 @@
+#pragma once
+
+#include <util/generic/strbuf.h>
+#include <util/stream/output.h>
+#include <util/string/ascii.h>
+
+namespace NNeh {
+ namespace NHttp {
+ template <typename Port>
+ void WriteHostHeader(IOutputStream& out, TStringBuf host, Port port) {
+ out << TStringBuf("Host: ") << host;
+ if (port) {
+ out << TStringBuf(":") << port;
+ }
+ out << TStringBuf("\r\n");
+ }
+
+ class THeaderSplitter {
+ public:
+ THeaderSplitter(TStringBuf headers)
+ : Headers_(headers)
+ {
+ }
+
+ bool Next(TStringBuf& header) {
+ while (Headers_.ReadLine(header)) {
+ if (!header.Empty()) {
+ return true;
+ }
+ }
+ return false;
+ }
+ private:
+ TStringBuf Headers_;
+ };
+
+ inline bool HasHostHeader(TStringBuf headers) {
+ THeaderSplitter splitter(headers);
+ TStringBuf header;
+ while (splitter.Next(header)) {
+ if (AsciiHasPrefixIgnoreCase(header, "Host:")) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ template <typename Port>
+ void WriteHostHeaderIfNot(IOutputStream& out, TStringBuf host, Port port, TStringBuf headers) {
+ if (!NNeh::NHttp::HasHostHeader(headers)) {
+ NNeh::NHttp::WriteHostHeader(out, host, port);
+ }
+ }
+ }
+}