aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/uri/location.cpp
blob: c6c0d4b5298254e6252a892143f02de1d40f99f9 (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
#include "location.h"
#include "uri.h"

namespace NUri {
    static const ui64 URI_PARSE_FLAGS =
        (TFeature::FeaturesRecommended | TFeature::FeatureConvertHostIDN | TFeature::FeatureEncodeExtendedDelim | TFeature::FeatureEncodePercent) & ~TFeature::FeatureHashBangToEscapedFragment;

    TString ResolveRedirectLocation(const TStringBuf& baseUrl,
                                    const TStringBuf& location) {
        TUri baseUri;
        TUri locationUri;

        // Parse base URL.
        if (baseUri.Parse(baseUrl, URI_PARSE_FLAGS) != NUri::TState::ParsedOK) {
            return "";
        }
        // Parse location with respect to the base URL.
        if (locationUri.Parse(location, baseUri, URI_PARSE_FLAGS) != NUri::TState::ParsedOK) {
            return "";
        }
        // Inherit fragment.
        if (!locationUri.GetField(NUri::TField::FieldFragment)) {
            NUri::TUriUpdate update(locationUri);
            update.Set(NUri::TField::FieldFragment, baseUri.GetField(NUri::TField::FieldFragment));
        }
        TString res;
        locationUri.Print(res, NUri::TField::FlagAllFields);
        return res;
    }

}