2014-02-26 19:12:37 +00:00
|
|
|
module Typertext.Http {
|
|
|
|
export class HttpUrl {
|
|
|
|
private _Domain:string;
|
|
|
|
private _Path:string;
|
|
|
|
private _Port:number;
|
|
|
|
private _Protocol:HttpProtocol;
|
|
|
|
private _QueryString:{
|
|
|
|
[index:string]:string
|
|
|
|
};
|
|
|
|
|
|
|
|
public static DefaultPort(protocol:HttpProtocol) {
|
|
|
|
return ((protocol == HttpProtocol.http) ? 80 : 443)
|
|
|
|
}
|
|
|
|
|
2014-02-26 21:20:10 +00:00
|
|
|
public static FromUrl(location:string):HttpUrl {
|
|
|
|
var l = document.createElement("a");
|
|
|
|
l.href = location;
|
|
|
|
return new HttpUrl(l.hostname, HttpProtocol[l.protocol], l.pathname, HttpUrl.DecodeQueryString(l.search))
|
|
|
|
}
|
|
|
|
|
|
|
|
public static DecodeQueryString(queryString:string):HttpQueryString {
|
|
|
|
if (queryString.length == 0 || queryString == "?") {
|
2014-02-26 21:56:26 +00:00
|
|
|
return {};
|
2014-02-26 21:20:10 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 21:56:26 +00:00
|
|
|
return HttpUrl.UrlDecodeString(queryString);
|
2014-02-26 21:20:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static EncodeQueryString(query:HttpQueryString) {
|
2014-02-26 21:56:26 +00:00
|
|
|
var rs = "?" + HttpUrl.UrlEncodeObject(query);
|
2014-02-26 19:12:37 +00:00
|
|
|
return ((rs.length == 1) ? "" : rs);
|
|
|
|
}
|
|
|
|
|
2014-02-26 21:56:26 +00:00
|
|
|
public static UrlEncodeObject(data:HttpQueryString):string {
|
2014-02-26 19:12:37 +00:00
|
|
|
var rs:string = "";
|
|
|
|
var temp:string;
|
|
|
|
|
|
|
|
for (temp in data) {
|
|
|
|
rs += +encodeURIComponent(temp) + "=" + encodeURIComponent(data[temp]) + "&";
|
|
|
|
}
|
|
|
|
|
|
|
|
return rs.slice(0, -1);
|
|
|
|
}
|
|
|
|
|
2014-02-26 21:56:26 +00:00
|
|
|
public static UrlDecodeString(queryString:string):HttpQueryString {
|
|
|
|
var returnValue:HttpQueryString = {}, params:string[] = HttpUrl.splitString(queryString, "&");
|
|
|
|
for (var i:number = 0; i < params.length; i++) {
|
|
|
|
var param = HttpUrl.splitString(params[i], "=", 2);
|
|
|
|
if (param.length == 1) {
|
|
|
|
returnValue[param[0]] = "";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
returnValue[param[0]] = param[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
2014-02-26 21:20:10 +00:00
|
|
|
private static splitString(input:string, separator:string, limit:number = 0):string[] {
|
|
|
|
limit++;
|
|
|
|
var chunks:string[] = input.split(separator);
|
|
|
|
if (limit > 0 && chunks.length > limit) {
|
|
|
|
var ret = chunks.splice(0, limit);
|
|
|
|
ret.push(chunks.join(separator));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return chunks;
|
|
|
|
}
|
|
|
|
|
2014-02-26 21:56:26 +00:00
|
|
|
constructor(domain:string, protocol:HttpProtocol = HttpProtocol.http, path:string = "/", queryString:HttpQueryString = {}, port:number = 0) {
|
2014-02-26 19:12:37 +00:00
|
|
|
if (port < 1 || port > 65535) {
|
|
|
|
port = HttpUrl.DefaultPort(protocol);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.indexOf("/") != 0) {
|
|
|
|
path = "/" + path;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._Domain = domain;
|
|
|
|
this._Protocol = protocol;
|
|
|
|
this._Path = path;
|
|
|
|
this._QueryString = queryString;
|
|
|
|
this._Port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ToString():string {
|
|
|
|
return HttpProtocol[this._Protocol] + "://" + this._Domain +
|
|
|
|
((this._Port == HttpUrl.DefaultPort(this._Protocol)) ? "" : ":" + this._Port) + this._Path +
|
|
|
|
HttpUrl.EncodeQueryString(this._QueryString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|