github.com/whatap/golib@v0.0.22/util/urlutil/URL.go (about)

     1  package urlutil
     2  
     3  import (
     4  	//	"log"
     5  	"fmt"
     6  	_ "fmt"
     7  	"net/url"
     8  	"strconv"
     9  	"strings"
    10  )
    11  
    12  // java URL 처럼
    13  type URL struct {
    14  	Url      string
    15  	Protocol string
    16  	Host     string
    17  	RawPath  string
    18  	Path     string
    19  	RawPort  string
    20  	Port     int
    21  	RawQuery string
    22  	Query    string
    23  	File     string
    24  }
    25  
    26  func NewURL(url string) *URL {
    27  	p := new(URL)
    28  	p.Url = url
    29  
    30  	p.process()
    31  
    32  	return p
    33  }
    34  
    35  func (this *URL) process() {
    36  	var tmp string
    37  	var pos int
    38  	var err error
    39  
    40  	// query 먼저 분리
    41  	var urlStr string
    42  	if pos := strings.Index(this.Url, "?"); pos > -1 {
    43  		this.Query = string(this.Url[pos+1:])
    44  		urlStr = string(this.Url[0:pos])
    45  	} else {
    46  		urlStr = this.Url
    47  	}
    48  	if pos := strings.LastIndex(urlStr, "/"); pos > -1 {
    49  		this.File = string(urlStr[pos:])
    50  	}
    51  
    52  	// Protocol
    53  	tmp = urlStr
    54  	pos = strings.Index(tmp, "://")
    55  	if pos > -1 {
    56  		this.Protocol = tmp[0:pos]
    57  		tmp = tmp[pos+3:]
    58  	}
    59  
    60  	// Host, Port
    61  	pos = strings.Index(tmp, "/")
    62  	if pos > -1 {
    63  		this.Host = tmp[0:pos]
    64  		tmp = tmp[pos:]
    65  
    66  		// Port 분리
    67  		pos = strings.Index(this.Host, ":")
    68  		if pos > -1 {
    69  			this.RawPort = this.Host[pos+1:]
    70  			this.Port, _ = strconv.Atoi(this.Host[pos+1:])
    71  			this.Host = this.Host[0:pos]
    72  		} else {
    73  			if this.Protocol == "https" {
    74  				this.Port = 443
    75  			} else {
    76  				this.Port = 80
    77  			}
    78  		}
    79  
    80  	} else {
    81  		this.Host = tmp
    82  		if this.Protocol == "https" {
    83  			this.Port = 443
    84  		} else {
    85  			this.Port = 80
    86  		}
    87  	}
    88  
    89  	this.Path = tmp
    90  
    91  	// Path, Query URLDecode추가 (net/url)
    92  	this.RawPath = this.Path
    93  	this.Path, err = url.PathUnescape(this.RawPath)
    94  	if err != nil {
    95  		this.Path = this.RawPath
    96  	}
    97  	this.RawQuery = this.Query
    98  	this.Query, err = url.QueryUnescape(this.RawQuery)
    99  	if err != nil {
   100  		this.Query = this.RawQuery
   101  	}
   102  
   103  }
   104  
   105  // URL Decode 된  url 정보를 다시 조합해서 출력
   106  func (this *URL) String() string {
   107  	rt := ""
   108  	if this.Protocol != "" {
   109  		rt = rt + this.Protocol + "://"
   110  	}
   111  	rt = rt + this.Host
   112  
   113  	if this.RawPort != "" {
   114  		rt = rt + ":" + this.RawPort
   115  	}
   116  
   117  	rt = rt + this.Path
   118  
   119  	if this.Query != "" {
   120  		rt = rt + "?" + this.Query
   121  	}
   122  	return rt
   123  }
   124  func (this *URL) HostPort() string {
   125  	if this.Port > 0 && this.Port != 443 && this.Port != 80 {
   126  		return fmt.Sprintf("%s:%d", this.Host, this.Port)
   127  	}
   128  	return this.Host
   129  
   130  }
   131  func (this *URL) Domain() string {
   132  	rt := ""
   133  	if this.Protocol != "" {
   134  		rt = rt + this.Protocol + "://"
   135  	}
   136  	rt = rt + this.Host
   137  	return rt
   138  }
   139  
   140  func (this *URL) DomainPath() string {
   141  	rt := ""
   142  	if this.Protocol != "" {
   143  		rt = rt + this.Protocol + "://"
   144  	}
   145  	rt = rt + this.Host
   146  
   147  	if this.RawPort != "" {
   148  		rt = rt + ":" + this.RawPort
   149  	}
   150  
   151  	rt = rt + this.Path
   152  	return rt
   153  }
   154  func (this *URL) ParseQuery() map[string][]string {
   155  	if m, err := url.ParseQuery(this.Query); err == nil {
   156  		return map[string][]string(m)
   157  	} else {
   158  		return make(map[string][]string)
   159  	}
   160  }