| 1 |
/* |
|---|
| 2 |
xinf is not flash. |
|---|
| 3 |
Copyright (c) 2006, Daniel Fischer. |
|---|
| 4 |
|
|---|
| 5 |
This library is free software; you can redistribute it and/or |
|---|
| 6 |
modify it under the terms of the GNU Lesser General Public |
|---|
| 7 |
License as published by the Free Software Foundation; either |
|---|
| 8 |
version 2.1 of the License, or (at your option) any later version. |
|---|
| 9 |
|
|---|
| 10 |
This library is distributed in the hope that it will be useful, |
|---|
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 13 |
Lesser General Public License or the LICENSE file for more details. |
|---|
| 14 |
*/ |
|---|
| 15 |
|
|---|
| 16 |
package xinf.ony; |
|---|
| 17 |
|
|---|
| 18 |
class URL { |
|---|
| 19 |
public var protocol:String; |
|---|
| 20 |
public var host:String; |
|---|
| 21 |
public var port:Int; |
|---|
| 22 |
public var path:String; |
|---|
| 23 |
// fragments (#foo) and GET parameters (?foo=bar) are just part of the path (for now) |
|---|
| 24 |
|
|---|
| 25 |
public function new( s:String ) :Void { |
|---|
| 26 |
parse(s); |
|---|
| 27 |
} |
|---|
| 28 |
|
|---|
| 29 |
private function parse( s:String ) :Void { |
|---|
| 30 |
var r:EReg = ~/([a-z]+):\/\/([a-zA-Z-\.]+)(:([0-9]+))?(.*)/; |
|---|
| 31 |
if( r.match( s ) ) { |
|---|
| 32 |
protocol = r.matched(1); |
|---|
| 33 |
host = r.matched(2); |
|---|
| 34 |
port = Std.parseInt(r.matched(4)); |
|---|
| 35 |
if( port==0 ) { |
|---|
| 36 |
switch(protocol) { |
|---|
| 37 |
case "http": port=80; |
|---|
| 38 |
case "https": port=443; |
|---|
| 39 |
case "ftp": port=21; |
|---|
| 40 |
default: port=0; |
|---|
| 41 |
} |
|---|
| 42 |
} |
|---|
| 43 |
path = r.matched(5); |
|---|
| 44 |
if( protocol=="file" ) { |
|---|
| 45 |
if( path!="" ) path=host+"/"+path; |
|---|
| 46 |
else path=host; |
|---|
| 47 |
host=""; |
|---|
| 48 |
} |
|---|
| 49 |
} else { |
|---|
| 50 |
protocol="file"; |
|---|
| 51 |
host=null; |
|---|
| 52 |
port=0; |
|---|
| 53 |
path=s; |
|---|
| 54 |
} |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
public function getRelativeURL( rel:String ) :URL { |
|---|
| 58 |
var url = new URL( this.toString() ); |
|---|
| 59 |
if( url.path.charAt(url.path.length-1)!="/" ) { |
|---|
| 60 |
var p = url.path.split("/"); |
|---|
| 61 |
p.pop(); |
|---|
| 62 |
url.path = p.join("/"); |
|---|
| 63 |
if( p.length>1 ) url.path+="/"; |
|---|
| 64 |
} |
|---|
| 65 |
trace("self: "+this+", rel: "+rel ); |
|---|
| 66 |
url.path+=rel; |
|---|
| 67 |
return url; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
public function fetch( onData:String->Void, ?onError:String->Void ) { |
|---|
| 71 |
try { |
|---|
| 72 |
|
|---|
| 73 |
#if neko |
|---|
| 74 |
if( protocol=="file" ) { |
|---|
| 75 |
var data = neko.io.File.getContent( if( host!=null ) host+path else path ); |
|---|
| 76 |
onData( data ); |
|---|
| 77 |
return; |
|---|
| 78 |
} |
|---|
| 79 |
#end |
|---|
| 80 |
|
|---|
| 81 |
var request = new haxe.Http(toString()); |
|---|
| 82 |
request.onError = onError; |
|---|
| 83 |
request.onData = onData; |
|---|
| 84 |
request.request(false); |
|---|
| 85 |
|
|---|
| 86 |
} catch( e:Dynamic ) { |
|---|
| 87 |
var msg = "Could not load document '"+this+"': "+e; |
|---|
| 88 |
if( onError!=null ) onError(msg); |
|---|
| 89 |
else throw(msg); |
|---|
| 90 |
} |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
public function toString() :String { |
|---|
| 94 |
var h = ""; |
|---|
| 95 |
if( host!=null ) { |
|---|
| 96 |
h = host; |
|---|
| 97 |
} |
|---|
| 98 |
if( port!=0 ) { |
|---|
| 99 |
h = h+":"+port; |
|---|
| 100 |
} |
|---|
| 101 |
return( protocol+"://"+h+path ); |
|---|
| 102 |
} |
|---|
| 103 |
} |
|---|