summaryrefslogtreecommitdiff
path: root/indra/llcommon/lluriparser.cpp
blob: 82d0dc8b4b25889b35c12b64f53bcf3bc094573a (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/** 
 * @file lluriparser.cpp
 * @author Protey
 * @date 2014-10-07
 * @brief Implementation of the LLUriParser class.
 *
 * $LicenseInfo:firstyear=2014&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2014, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "linden_common.h"
#include "lluriparser.h"

LLUriParser::LLUriParser(const std::string& u) : mTmpScheme(false), mNormalizedTmp(false), mRes(0)
{
	mState.uri = &mUri;

	if (u.find("://") == std::string::npos)
	{
		mNormalizedUri = "http://";
		mTmpScheme = true;
	}

	mNormalizedUri += u.c_str();

	mRes = parse();
}

LLUriParser::~LLUriParser()
{
	uriFreeUriMembersA(&mUri);
}

S32 LLUriParser::parse()
{
	mRes = uriParseUriA(&mState, mNormalizedUri.c_str());
	return mRes;
}

const char * LLUriParser::scheme() const
{
	return mScheme.c_str();
}

void LLUriParser::sheme(const std::string& s)
{
	mTmpScheme = !s.size();
	mScheme = s;
}

const char * LLUriParser::port() const
{
	return mPort.c_str();
}

void LLUriParser::port(const std::string& s)
{
	mPort = s;
}

const char * LLUriParser::host() const
{
	return mHost.c_str();
}

void LLUriParser::host(const std::string& s)
{
	mHost = s;
}

const char * LLUriParser::path() const
{
	return mPath.c_str();
}

void LLUriParser::path(const std::string& s)
{
	mPath = s;
}

const char * LLUriParser::query() const
{
	return mQuery.c_str();
}

void LLUriParser::query(const std::string& s)
{
	mQuery = s;
}

const char * LLUriParser::fragment() const
{
	return mFragment.c_str();
}

void LLUriParser::fragment(const std::string& s)
{
	mFragment = s;
}

void LLUriParser::textRangeToString(UriTextRangeA& textRange, std::string& str)
{
	if (textRange.first != NULL && textRange.afterLast != NULL && textRange.first < textRange.afterLast)
	{
		const ptrdiff_t len = textRange.afterLast - textRange.first;
		str.assign(textRange.first, static_cast<std::string::size_type>(len));
	}
	else
	{
		str = LLStringUtil::null;
	}
}

void LLUriParser::extractParts()
{
	if (mTmpScheme || mNormalizedTmp)
	{
		mScheme.clear();
	}
	else
	{
		textRangeToString(mUri.scheme, mScheme);
	}
	
	textRangeToString(mUri.hostText, mHost);
	textRangeToString(mUri.portText, mPort);
	textRangeToString(mUri.query, mQuery);
	textRangeToString(mUri.fragment, mFragment);

	UriPathSegmentA * pathHead = mUri.pathHead;
	while (pathHead)
	{
		std::string partOfPath;
		textRangeToString(pathHead->text, partOfPath);

		mPath += '/';
		mPath += partOfPath;

		pathHead = pathHead->next;
	}
}

S32 LLUriParser::normalize()
{
	mNormalizedTmp = mTmpScheme;
	if (!mRes)
	{
		mRes = uriNormalizeSyntaxExA(&mUri, URI_NORMALIZE_SCHEME | URI_NORMALIZE_HOST);

		if (!mRes)
		{
			S32 chars_required;
			mRes = uriToStringCharsRequiredA(&mUri, &chars_required);

			if (!mRes)
			{
				chars_required++;
				std::vector<char> label_buf(chars_required);
				mRes = uriToStringA(&label_buf[0], &mUri, chars_required, NULL);

				if (!mRes)
				{
					mNormalizedUri = &label_buf[mTmpScheme ? 7 : 0];
					mTmpScheme = false;
				}
			}
		}
	}

	if(mTmpScheme)
	{
		mNormalizedUri = mNormalizedUri.substr(7);
		mTmpScheme = false;
	}

	return mRes;
}

void LLUriParser::glue(std::string& uri) const
{
	std::string first_part;
	glueFirst(first_part);

	std::string second_part;
	glueSecond(second_part);

	uri = first_part + second_part;
}

void LLUriParser::glueFirst(std::string& uri) const
{
	if (mScheme.size())
	{
		uri = mScheme;
		uri += "://";
	}
	else
	{
		uri.clear();
	}

	uri += mHost;
}

void LLUriParser::glueSecond(std::string& uri) const
{
	if (mPort.size())
	{
		uri = ':';
		uri += mPort;
	}
	else
	{
		uri.clear();
	}

	uri += mPath;

	if (mQuery.size())
	{
		uri += '?';
		uri += mQuery;
	}

	if (mFragment.size())
	{
		uri += '#';
		uri += mFragment;
	}
}

bool LLUriParser::test() const
{
	std::string uri;
	glue(uri);

	return uri == mNormalizedUri;
}

const char * LLUriParser::normalizedUri() const
{
	return mNormalizedUri.c_str();
}