aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/http/server/options.cpp
blob: 3aed3757ed1eb9610f9bcfc90b0fe4b88d28395f (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
#include "options.h"

#include <util/string/cast.h>
#include <util/digest/numeric.h>
#include <util/network/ip.h>
#include <util/network/socket.h>
#include <util/generic/hash_set.h>
#include <util/generic/yexception.h>

using TAddr = THttpServerOptions::TAddr;

static inline TString AddrToString(const TAddr& addr) {
    return addr.Addr + ":" + ToString(addr.Port);
}

static inline TNetworkAddress ToNetworkAddr(const TString& address, ui16 port) {
    if (address.empty() || address == TStringBuf("*")) {
        return TNetworkAddress(port);
    }

    return TNetworkAddress(address, port);
}

void THttpServerOptions::BindAddresses(TBindAddresses& ret) const {
    THashSet<TString> check;

    for (auto addr : BindSockaddr) { 
        if (!addr.Port) {
            addr.Port = Port;
        }

        const TString straddr = AddrToString(addr);

        if (check.find(straddr) == check.end()) {
            check.insert(straddr);
            ret.push_back(ToNetworkAddr(addr.Addr, addr.Port));
        }
    }

    if (ret.empty()) {
        ret.push_back(Host ? TNetworkAddress(Host, Port) : TNetworkAddress(Port));
    }
}