Menu

[r331]: / trunk / framework / OSMF / org / osmf / utils / URL.as  Maximize  Restore  History

Download this file

493 lines (449 with data), 13.8 kB

  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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*****************************************************
*
* Copyright 2009 Adobe Systems Incorporated. All Rights Reserved.
*
*****************************************************
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
*
* The Initial Developer of the Original Code is Adobe Systems Incorporated.
* Portions created by Adobe Systems Incorporated are Copyright (C) 2009 Adobe Systems
* Incorporated. All Rights Reserved.
*
* Contributor(s): Akamai Technologies
*
*****************************************************/
package org.osmf.utils
{
[ExcludeClass]
/**
* @private
*
* URL parses a Uniform Resource Identifier (URI/URL) into individual properties and provides easy access
* to query string parameters. This also works with rtmp:// urls, but will assume the instance isn't specified.
* To use rtmp:// urls with an instance name, use the FMSURL class instead of URL.
*
* @see FMSURL
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public class URL
{
/**
* The constructor takes a URI/URL string and begins parsing it. The URI/URL can also
* be set via the <code>url</code> property.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function URL(url:String)
{
_rawUrl = url;
_protocol = "";
_userInfo = "";
_host = "";
_port = "";
_path = "";
_query = "";
_fragment = "";
if ((_rawUrl != null) && (_rawUrl.length > 0))
{
// Strip leading/trailing spaces.
_rawUrl = _rawUrl.replace(/^\s+|\s+$/g, "");
parseUrl();
}
}
/**
* The raw URI/URL string used by this class.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get rawUrl():String
{
return _rawUrl;
}
/**
* The protocol string, such as "rtmp", "http".
* <p>
* The protocol string is converted to lower case and the trailing <code>"://"</code>
* string, if it exists, is stripped off.</p>
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get protocol():String
{
return _protocol;
}
public function set protocol(value:String):void
{
if (value != null)
{
_protocol = value.replace(/:\/?\/?$/, "");
_protocol = _protocol.toLowerCase();
}
}
/**
* The user info if present.
* <p>An example of a URI/URL containing user info might be:
* <code>http://user:password&#38;#64host.com:80/foo.php</code></p>
* <p>
* This property contains a string formatted as "username:password".
* </p>
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get userInfo():String
{
return _userInfo;
}
public function set userInfo(value:String):void
{
if (value != null)
{
_userInfo = value.replace(/@$/, "");
}
}
/**
* The host name as it was specified in the URI/URL string supplied,
* without leading or trailing slashes.
* <p>
* For example, in this URL: <code>"http://hostname.com:80/foo/bar/index.html?a=1&#38;b=2"</code>
* host would be <code>"hostname.com"</code></p>
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get host():String
{
return _host;
}
public function set host(value:String):void
{
_host = value;
}
/**
* The port number as a string.
* <p>
* For example, in this URL: <code>"http://hostname.com:80/foo/bar/index.html?a=1&#38;b=2"</code>
* port would be <code>"80"</code></p>
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get port():String
{
return _port;
}
public function set port(value:String):void
{
if (value != null)
{
_port = value.replace(/(:)/, "");
}
}
/**
* The path is everything after the host but before any query string parameters, with no leading or trailing slashes.
* <p>
* For example, in this URL: <code>"http://host.com:80/foo/bar/index.html?a=1&#38;b=2"</code>
* path would be <code>"foo/bar/index.html"</code></p>
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get path():String
{
return _path;
}
public function set path(value:String):void
{
if (value != null)
{
_path = value.replace(/^\//, "");
}
}
/**
* The raw query string, everything after the first '?' character, and up to the '#' if it exists.
*
* <p>
* For example, in this URL: <code>"http://hostname.com/foo/bar/index.html?param1=abcdef&#38;param2=ghijkl#xyz"</code>
* query would be <code>"param1=abcdef&#38;param2=ghijkl"</code></p>
*
* @see #getParamValue()
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get query():String
{
return _query;
}
public function set query(value:String):void
{
if (value != null)
{
_query = value.replace(/^\?/, "");
}
}
/**
* The fragment, everything after the '#' to the end of the URL string.
* <p>
* For example, in this URL:<code>"http://host.com/foo/bar/index.html?p1=123&#38;p2=456#xyz"</code>,
* the fragment is <code>"xyz"</code></p>
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get fragment():String
{
return _fragment;
}
public function set fragment(value:String):void
{
if (value != null)
{
_fragment = value.replace(/^#/, "");
}
}
/**
* Returns the entire URL string.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function toString():String
{
return _rawUrl;
}
/**
* Given a query string parameter name, returns it's value. If not found, returns an empty string ("").
*
* <p>
* For example, if the URL constructor were handed this value:<code>"http://host.com/foo/bar/index.html?param1=123&#38;param2=456"</code>,
* calling <code>getParamValue("param1")</code> would return <code>"123"</code></p>
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function getParamValue(param:String):String
{
if (_query == null)
{
return "";
}
var pattern:RegExp = new RegExp("[\/?&]*" + param + "=([^&#]*)", "i");
var result:Array = _query.match(pattern);
var value:String = (result == null) ? "" : result[1];
return value;
}
/**
* The url is fully qualified.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get absolute():Boolean
{
return protocol != "";
}
/**
* Returns the file extension of the URL, the empty string if there is
* no extension.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get extension():String
{
var lastPathElement:int = path.lastIndexOf("/");
var lastDot:int = path.lastIndexOf(".");
if (lastDot != -1 && lastDot > lastPathElement)
{
return path.substr(lastDot+1);
}
return "";
}
/**
* Parses the url into properties.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
private function parseUrl():void
{
if ((_rawUrl == null) || (_rawUrl.length == 0))
{
return;
}
// Check to see if this is a relative path, meaning there is no protocol specified
if ( _rawUrl.search(/:\//) == -1
&& _rawUrl.indexOf(":") != _rawUrl.length - 1
)
{
path = _rawUrl;
}
else
{
// Handle the special case where the user is tring to connect to a dev server running on
// the same machine as the client with a url like this: "rtmp:/sudoku/room1"
var oneSlashRegExp:RegExp = /^(rtmp|rtmp[tse]|rtmpte)(:\/[^\/])/i;
var oneSlashResult:Array = _rawUrl.match(oneSlashRegExp);
var tempUrl:String = _rawUrl;
if (oneSlashResult != null)
{
tempUrl = _rawUrl.replace(/:\//, "://localhost/");
}
// We'll parse the url in two passes:
// 1) pull out the host name which might contain '@' and ':' characters;
// 2) parse the host name out into user info, host, and port.
//
// This makes the regular expressions simpler and should accommodate some irregular urls that might contain
// '@' and ':' in the path.
/*
* The regular expression below performs a match on a URL string, effectively breaking it up into
* the individual properites, such as protocol, host name, port, etc.
*
* Here are the individual patterns of the regular expression:
*
* protocol : ^([a-z+\w\+\.\-]+:\/?\/?)?
* The caret (^) means start at the beginnning of the string.
* The "[a-z+\w\+\.\-]+" pattern means match the following group of characters 1 or more times: letters from a to z, followed by any letter
* from a to z, a number from 0 to 9, an underscore (-), a plus sign (+), a period (.), or a dash (-).
* The ":\/?\/?" pattern means match the string ":" or ":/" or "://".
* The question mark '?' at the end means the entire sequence can appear once or not at all.
*
* path: (\/[^?#]*)
* This pattern matches a sequence starting with a slash (/) followed by any characters other than '?' or '#'.
*
* query: (\?[^#]*)
* This pattern matches a sequence starting with a '?' followed by any character other than a '#'.
*
* fragment: (\#.*)
* This pattern matches a sequence starting with a '#' character followed by any character zero or more times.
*/
var pattern:RegExp = /^([a-z+\w\+\.\-]+:\/?\/?)?([^\/?#]*)?(\/[^?#]*)?(\?[^#]*)?(\#.*)?/i;
var result:Array = tempUrl.match(pattern);
var hostName:String;
if (result != null)
{
protocol = result[1];
hostName = result[2];
path = result[3];
query = result[4];
fragment = result[5];
/*
* Now we'll parse the host name.
*
* user info: ([!-~]+@)?
* This pattern matches any charater from '!' to '~' in the ascii character set occurring 1 or more times followed by '@'.
*
* host and port number: ([^\/?#:]*)(:[\d]*)
* This pattern matches any sequence of characters other than '/', '?', '#', and ':', followed by an optional ':' and a
* digit representing the port number.
*/
pattern = /^([!-~]+@)?([^\/?#:]*)(:[\d]*)?/i;
result = hostName.match(pattern);
if (result != null)
{
this.userInfo = result[1];
this.host = result[2];
this.port = result[3];
}
}
}
}
public static function isAbsoluteURL(url:String):Boolean
{
var theURL:URL = new URL(url);
return theURL.absolute;
}
public static function getRootUrl(url:String):String
{
var path:String = url.substr(0, url.lastIndexOf("/"));
return path;
}
/**
* Normalizes a root URL. It adds a trailing slash (/) if not present.
* It is assumed that the passed url is an absolute root url. No checks will be performed to validate this.
*
* @param url The root URL to be normalized
*
*/
public static function normalizeRootURL(url:String):String
{
if (url != null && url.charAt(url.length - 1) != "/")
{
return url + "/";
}
else
{
return url;
}
}
/**
* Normalizes a relative URL. It removes the leading slash (/) if present.
* It is assumed that the passed url is a relative one. No checks will be performed to validate this.
*/
public static function normalizeRelativeURL(url:String):String
{
if (url.charAt(0) == "/")
{
return url.substr(1);
}
else
{
return url;
}
}
private var _rawUrl:String; // The raw URL string as it was supplied
private var _protocol:String; // The scheme or protocol, i.e., "http", "ftp", etc.
private var _userInfo:String; // User name : password. For example "http://user:password@host.com:80/foo.php"
private var _host:String; // Host name
private var _port:String; // The port name. Follows host, such as "http://host.com:80/index.html"
private var _path:String; // Path is everything after the host but before any params. For example
// in this URL: "http://host.com:80/foo/bar/index.html?a=1&#38;b=2", path would be "/foo/bar/index.html"
private var _query:String; // The entire query string, everything after the "?" up to the '#', if that exists
private var _fragment:String; // From the first # to the end of the url
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.