aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/uri/location.cpp
blob: 2eb099bedace7a7368b7aab7c87a61da40e11802 (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 int 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; 
    }

}