summaryrefslogtreecommitdiff
path: root/externals/lurlparser/LUrlParser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'externals/lurlparser/LUrlParser.cpp')
-rw-r--r--externals/lurlparser/LUrlParser.cpp265
1 files changed, 0 insertions, 265 deletions
diff --git a/externals/lurlparser/LUrlParser.cpp b/externals/lurlparser/LUrlParser.cpp
deleted file mode 100644
index 9c134e330..000000000
--- a/externals/lurlparser/LUrlParser.cpp
+++ /dev/null
@@ -1,265 +0,0 @@
1/*
2 * Lightweight URL & URI parser (RFC 1738, RFC 3986)
3 * https://github.com/corporateshark/LUrlParser
4 *
5 * The MIT License (MIT)
6 *
7 * Copyright (C) 2015 Sergey Kosarevsky (sk@linderdaum.com)
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in all
17 * copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28#include "LUrlParser.h"
29
30#include <algorithm>
31#include <cstring>
32#include <stdlib.h>
33
34// check if the scheme name is valid
35static bool IsSchemeValid( const std::string& SchemeName )
36{
37 for ( auto c : SchemeName )
38 {
39 if ( !isalpha( c ) && c != '+' && c != '-' && c != '.' ) return false;
40 }
41
42 return true;
43}
44
45bool LUrlParser::clParseURL::GetPort( int* OutPort ) const
46{
47 if ( !IsValid() ) { return false; }
48
49 int Port = atoi( m_Port.c_str() );
50
51 if ( Port <= 0 || Port > 65535 ) { return false; }
52
53 if ( OutPort ) { *OutPort = Port; }
54
55 return true;
56}
57
58// based on RFC 1738 and RFC 3986
59LUrlParser::clParseURL LUrlParser::clParseURL::ParseURL( const std::string& URL )
60{
61 LUrlParser::clParseURL Result;
62
63 const char* CurrentString = URL.c_str();
64
65 /*
66 * <scheme>:<scheme-specific-part>
67 * <scheme> := [a-z\+\-\.]+
68 * For resiliency, programs interpreting URLs should treat upper case letters as equivalent to lower case in scheme names
69 */
70
71 // try to read scheme
72 {
73 const char* LocalString = strchr( CurrentString, ':' );
74
75 if ( !LocalString )
76 {
77 return clParseURL( LUrlParserError_NoUrlCharacter );
78 }
79
80 // save the scheme name
81 Result.m_Scheme = std::string( CurrentString, LocalString - CurrentString );
82
83 if ( !IsSchemeValid( Result.m_Scheme ) )
84 {
85 return clParseURL( LUrlParserError_InvalidSchemeName );
86 }
87
88 // scheme should be lowercase
89 std::transform( Result.m_Scheme.begin(), Result.m_Scheme.end(), Result.m_Scheme.begin(), ::tolower );
90
91 // skip ':'
92 CurrentString = LocalString+1;
93 }
94
95 /*
96 * //<user>:<password>@<host>:<port>/<url-path>
97 * any ":", "@" and "/" must be normalized
98 */
99
100 // skip "//"
101 if ( *CurrentString++ != '/' ) return clParseURL( LUrlParserError_NoDoubleSlash );
102 if ( *CurrentString++ != '/' ) return clParseURL( LUrlParserError_NoDoubleSlash );
103
104 // check if the user name and password are specified
105 bool bHasUserName = false;
106
107 const char* LocalString = CurrentString;
108
109 while ( *LocalString )
110 {
111 if ( *LocalString == '@' )
112 {
113 // user name and password are specified
114 bHasUserName = true;
115 break;
116 }
117 else if ( *LocalString == '/' )
118 {
119 // end of <host>:<port> specification
120 bHasUserName = false;
121 break;
122 }
123
124 LocalString++;
125 }
126
127 // user name and password
128 LocalString = CurrentString;
129
130 if ( bHasUserName )
131 {
132 // read user name
133 while ( *LocalString && *LocalString != ':' && *LocalString != '@' ) LocalString++;
134
135 Result.m_UserName = std::string( CurrentString, LocalString - CurrentString );
136
137 // proceed with the current pointer
138 CurrentString = LocalString;
139
140 if ( *CurrentString == ':' )
141 {
142 // skip ':'
143 CurrentString++;
144
145 // read password
146 LocalString = CurrentString;
147
148 while ( *LocalString && *LocalString != '@' ) LocalString++;
149
150 Result.m_Password = std::string( CurrentString, LocalString - CurrentString );
151
152 CurrentString = LocalString;
153 }
154
155 // skip '@'
156 if ( *CurrentString != '@' )
157 {
158 return clParseURL( LUrlParserError_NoAtSign );
159 }
160
161 CurrentString++;
162 }
163
164 bool bHasBracket = ( *CurrentString == '[' );
165
166 // go ahead, read the host name
167 LocalString = CurrentString;
168
169 while ( *LocalString )
170 {
171 if ( bHasBracket && *LocalString == ']' )
172 {
173 // end of IPv6 address
174 LocalString++;
175 break;
176 }
177 else if ( !bHasBracket && ( *LocalString == ':' || *LocalString == '/' ) )
178 {
179 // port number is specified
180 break;
181 }
182
183 LocalString++;
184 }
185
186 Result.m_Host = std::string( CurrentString, LocalString - CurrentString );
187
188 CurrentString = LocalString;
189
190 // is port number specified?
191 if ( *CurrentString == ':' )
192 {
193 CurrentString++;
194
195 // read port number
196 LocalString = CurrentString;
197
198 while ( *LocalString && *LocalString != '/' ) LocalString++;
199
200 Result.m_Port = std::string( CurrentString, LocalString - CurrentString );
201
202 CurrentString = LocalString;
203 }
204
205 // end of string
206 if ( !*CurrentString )
207 {
208 Result.m_ErrorCode = LUrlParserError_Ok;
209
210 return Result;
211 }
212
213 // skip '/'
214 if ( *CurrentString != '/' )
215 {
216 return clParseURL( LUrlParserError_NoSlash );
217 }
218
219 CurrentString++;
220
221 // parse the path
222 LocalString = CurrentString;
223
224 while ( *LocalString && *LocalString != '#' && *LocalString != '?' ) LocalString++;
225
226 Result.m_Path = std::string( CurrentString, LocalString - CurrentString );
227
228 CurrentString = LocalString;
229
230 // check for query
231 if ( *CurrentString == '?' )
232 {
233 // skip '?'
234 CurrentString++;
235
236 // read query
237 LocalString = CurrentString;
238
239 while ( *LocalString && *LocalString != '#' ) LocalString++;
240
241 Result.m_Query = std::string( CurrentString, LocalString - CurrentString );
242
243 CurrentString = LocalString;
244 }
245
246 // check for fragment
247 if ( *CurrentString == '#' )
248 {
249 // skip '#'
250 CurrentString++;
251
252 // read fragment
253 LocalString = CurrentString;
254
255 while ( *LocalString ) LocalString++;
256
257 Result.m_Fragment = std::string( CurrentString, LocalString - CurrentString );
258
259 CurrentString = LocalString;
260 }
261
262 Result.m_ErrorCode = LUrlParserError_Ok;
263
264 return Result;
265}