本文整理汇总了Golang中url.Parse函数的典型用法代码示例。如果您正苦于以下问题:Golang Parse函数的具体用法?Golang Parse怎么用?Golang Parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Parse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: buildTestRequest
func buildTestRequest(method string, path string, body string, headers map[string][]string, cookies []*http.Cookie) *http.Request {
host := "127.0.0.1"
port := "80"
rawurl := "http://" + host + ":" + port + path
url_, _ := url.Parse(rawurl)
proto := "HTTP/1.1"
if headers == nil {
headers = map[string][]string{}
}
headers["User-Agent"] = []string{"web.go test"}
if method == "POST" {
headers["Content-Length"] = []string{fmt.Sprintf("%d", len(body))}
if headers["Content-Type"] == nil {
headers["Content-Type"] = []string{"text/plain"}
}
}
req := http.Request{Method: method,
RawURL: rawurl,
URL: url_,
Proto: proto,
Host: host,
Header: http.Header(headers),
Body: ioutil.NopCloser(bytes.NewBufferString(body)),
}
for _, cookie := range cookies {
req.AddCookie(cookie)
}
return &req
}
示例2: getResponse
func getResponse(rawUrl string, req *http.Request) (*http.ClientConn, *http.Response, os.Error) {
url, err := url.Parse(rawUrl)
if url.Scheme == "" {
rawUrl = "http://" + rawUrl
url, err = url.Parse(rawUrl)
}
if err != nil {
return nil, nil, err
}
req.URL = url
if debugprint {
dump, err := http.DumpRequest(req, true)
if err != nil {
println(err.String())
}
print(string(dump))
}
conn, err := newConn(url)
if err != nil {
println(err.String())
return nil, nil, err
}
resp, err := conn.Do(req)
if err != nil {
if err != http.ErrPersistEOF {
return nil, nil, err
}
}
return conn, resp, nil
}
示例3: ResolveRelative
func ResolveRelative(basestr, relstr string) string {
u, _ := url.Parse(basestr)
rel, _ := url.Parse(relstr)
u = u.ResolveReference(rel)
us := u.String()
us = strings.Replace(us, "%7B", "{", -1)
us = strings.Replace(us, "%7D", "}", -1)
return us
}
示例4: resolveRelative
func resolveRelative(basestr, relstr string) string {
u, err := url.Parse(basestr)
if err != nil {
panicf("Error parsing base URL %q: %v", basestr, err)
}
rel, err := url.Parse(relstr)
if err != nil {
panicf("Error parsing relative URL %q: %v", relstr, err)
}
u = u.ResolveReference(rel)
return u.String()
}
示例5: RoundTrip
// RoundTrip implements the RoundTripper interface.
func (t *Transport) RoundTrip(req *Request) (resp *Response, err os.Error) {
if req.URL == nil {
if req.URL, err = url.Parse(req.RawURL); err != nil {
return
}
}
if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
t.lk.Lock()
var rt RoundTripper
if t.altProto != nil {
rt = t.altProto[req.URL.Scheme]
}
t.lk.Unlock()
if rt == nil {
return nil, &badStringError{"unsupported protocol scheme", req.URL.Scheme}
}
return rt.RoundTrip(req)
}
cm, err := t.connectMethodForRequest(req)
if err != nil {
return nil, err
}
// Get the cached or newly-created connection to either the
// host (for http or https), the http proxy, or the http proxy
// pre-CONNECTed to https server. In any case, we'll be ready
// to send it requests.
pconn, err := t.getConn(cm)
if err != nil {
return nil, err
}
return pconn.roundTrip(req)
}
示例6: Dial
/*
Dial opens a new client connection to a Web Socket.
A trivial example client:
package main
import (
"websocket"
"strings"
)
func main() {
ws, err := websocket.Dial("ws://localhost/ws", "", "http://localhost/");
if err != nil {
panic("Dial: " + err.String())
}
if _, err := ws.Write([]byte("hello, world!\n")); err != nil {
panic("Write: " + err.String())
}
var msg = make([]byte, 512);
if n, err := ws.Read(msg); err != nil {
panic("Read: " + err.String())
}
// use msg[0:n]
}
*/
func Dial(url_, protocol, origin string) (ws *Conn, err os.Error) {
var client net.Conn
parsedUrl, err := url.Parse(url_)
if err != nil {
goto Error
}
switch parsedUrl.Scheme {
case "ws":
client, err = net.Dial("tcp", parsedUrl.Host)
case "wss":
client, err = tls.Dial("tcp", parsedUrl.Host, nil)
default:
err = ErrBadScheme
}
if err != nil {
goto Error
}
ws, err = newClient(parsedUrl.RawPath, parsedUrl.Host, origin, url_, protocol, client, handshake)
if err != nil {
goto Error
}
return
Error:
return nil, &DialError{url_, protocol, origin, err}
}
示例7: OpenRaw
func OpenRaw(uarel string) (*Conn, os.Error) {
u, err := url.Parse(uarel)
if err != nil {
return nil, err
}
nc, err := net.Dial("tcp", u.Host)
if err != nil {
return nil, err
}
// TODO: use pass
user, _, err := url.UnescapeUserinfo(u.RawUserinfo)
if err != nil {
return nil, err
}
params := make(proto.Values)
params.Set("user", user)
if u.Path != "" {
params.Set("database", path.Base(u.Path))
}
return New(nc, params)
}
示例8: post
func post(theUrl string, oauthHeaders map[string]string) (r *http.Response, err os.Error) {
var req http.Request
var authorization string = "OAuth "
req.Method = "POST"
req.ProtoMajor = 1
req.ProtoMinor = 1
req.Close = true
req.Header = http.Header{}
req.TransferEncoding = []string{"chunked"}
first := true
for k, v := range oauthHeaders {
if first {
first = false
} else {
authorization += ",\n "
}
authorization += k + "=\"" + v + "\""
}
req.Header.Add("Authorization", authorization)
req.URL, err = url.Parse(theUrl)
if err != nil {
return nil, err
}
return send(&req)
}
示例9: CachePath
func CachePath(m *Module, cacheroot string) (string, error) {
source, err := url.Parse(m.Source)
if err == nil {
return filepath.Join(cacheroot, source.Host, source.Path), nil
}
return "", err
}
示例10: GetParameters
func GetParameters(urlToParse string) url.Values {
urlObj, err := url.Parse(urlToParse)
if err != nil {
log.Panic("Couldn't parse URL parameters from string")
}
return urlObj.Query()
}
示例11: post
func post(url_ string, oauthHeaders map[string]string) (r *http.Response, err os.Error) {
var req http.Request
req.Method = "POST"
req.ProtoMajor = 1
req.ProtoMinor = 1
req.Close = true
req.Header = map[string][]string{
"Authorization": {"OAuth "},
}
req.TransferEncoding = []string{"chunked"}
first := true
for k, v := range oauthHeaders {
if first {
first = false
} else {
req.Header["Authorization"][0] += ",\n "
}
req.Header["Authorization"][0] += k + "=\"" + v + "\""
}
req.URL, err = url.Parse(url_)
if err != nil {
return nil, err
}
return send(&req)
}
示例12: TestHostHandlers
func TestHostHandlers(t *testing.T) {
for _, h := range handlers {
Handle(h.pattern, stringHandler(h.msg))
}
ts := httptest.NewServer(nil)
defer ts.Close()
conn, err := net.Dial("tcp", ts.Listener.Addr().String())
if err != nil {
t.Fatal(err)
}
defer conn.Close()
cc := NewClientConn(conn, nil)
for _, vt := range vtests {
var r *Response
var req Request
if req.URL, err = url.Parse(vt.url); err != nil {
t.Errorf("cannot parse url: %v", err)
continue
}
if err := cc.Write(&req); err != nil {
t.Errorf("writing request: %v", err)
continue
}
r, err := cc.Read(&req)
if err != nil {
t.Errorf("reading response: %v", err)
continue
}
s := r.Header.Get("Result")
if s != vt.expected {
t.Errorf("Get(%q) = %q, want %q", vt.url, s, vt.expected)
}
}
}
示例13: NewRequest
// NewRequest returns a new Request given a method, URL, and optional body.
func NewRequest(method, urlStr string, body io.Reader) (*Request, os.Error) {
u, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
rc, ok := body.(io.ReadCloser)
if !ok && body != nil {
rc = ioutil.NopCloser(body)
}
req := &Request{
Method: method,
URL: u,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: make(Header),
Body: rc,
Host: u.Host,
}
if body != nil {
switch v := body.(type) {
case *strings.Reader:
req.ContentLength = int64(v.Len())
case *bytes.Buffer:
req.ContentLength = int64(v.Len())
}
}
return req, nil
}
示例14: TestLocationResponse
func TestLocationResponse(t *testing.T) {
for i, tt := range responseLocationTests {
res := new(Response)
res.Header = make(Header)
res.Header.Set("Location", tt.location)
if tt.requrl != "" {
res.Request = &Request{}
var err os.Error
res.Request.URL, err = url.Parse(tt.requrl)
if err != nil {
t.Fatalf("bad test URL %q: %v", tt.requrl, err)
}
}
got, err := res.Location()
if tt.wantErr != nil {
if err == nil {
t.Errorf("%d. err=nil; want %q", i, tt.wantErr)
continue
}
if g, e := err.String(), tt.wantErr.String(); g != e {
t.Errorf("%d. err=%q; want %q", i, g, e)
continue
}
continue
}
if err != nil {
t.Errorf("%d. err=%q", i, err)
continue
}
if g, e := got.String(), tt.want; g != e {
t.Errorf("%d. Location=%q; want %q", i, g, e)
}
}
}
示例15: defaultRequest
func (self *HTTPClient) defaultRequest() (req *http.Request, err os.Error) {
var h http.Header = map[string][]string{}
req = new(http.Request)
req.Method = self.method
if self.contentType != "" {
headers := map[string]string{"Content-Type": self.contentType}
for k, v := range headers {
h.Add(k, v)
}
}
//Request should have a Header, otherwise the
//transport will not work.
req.Header = h
req.ProtoMajor = 1
req.ProtoMinor = 1
if self.cookie.Name != "" && self.cookie.Value != "" {
req.AddCookie(&http.Cookie{Name: self.cookie.Name, Value: self.cookie.Value})
}
if req.URL, err = url.Parse(self.addr); err != nil {
return
}
return
}