Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:lafenghu
go-httplib.go
httplib.go-weekly-fixes.patch
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File httplib.go-weekly-fixes.patch of Package go-httplib.go
diff -ru a/httplib.go b/httplib.go --- a/httplib.go 2011-12-13 15:24:59.776471311 +0100 +++ b/httplib.go 2011-12-13 15:33:00.687482771 +0100 @@ -1,15 +1,16 @@ package httplib import ( - "bytes" - "crypto/tls" - "http" - "io" - "io/ioutil" - "net" - "os" - "strings" - "url" + "bytes" + "crypto/tls" + "io" + "io/ioutil" + "net" + "net/http" + "net/http/httputil" + "net/url" + "os" + "strings" ) var defaultUserAgent = "httplib.go" @@ -17,239 +18,239 @@ var debugprint = false type Client struct { - conn *http.ClientConn - lastURL *url.URL + conn *httputil.ClientConn + lastURL *url.URL } type nopCloser struct { - io.Reader + io.Reader } -func (nopCloser) Close() os.Error { return nil } +func (nopCloser) Close() error { return nil } func getNopCloser(buf *bytes.Buffer) nopCloser { - return nopCloser{buf} + return nopCloser{buf} } func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") } -func newConn(url *url.URL) (*http.ClientConn, os.Error) { - addr := url.Host - //just set the default scheme to http - if url.Scheme == "" { - url.Scheme = "http" - } - if !hasPort(addr) { - addr += ":" + url.Scheme - } - var conn net.Conn - var err os.Error - if url.Scheme == "http" { - conn, err = net.Dial("tcp", addr) - if err != nil { - return nil, err - } - } else { // https - conn, err = tls.Dial("tcp", addr, nil) - if err != nil { - return nil, err - } - h := url.Host - if hasPort(h) { - h = h[0:strings.LastIndex(h, ":")] - } - if err := conn.(*tls.Conn).VerifyHostname(h); err != nil { - return nil, err - } - } - - return http.NewClientConn(conn, nil), nil -} - -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 +func newConn(url *url.URL) (*httputil.ClientConn, error) { + addr := url.Host + //just set the default scheme to http + if url.Scheme == "" { + url.Scheme = "http" + } + if !hasPort(addr) { + addr += ":" + url.Scheme + } + var conn net.Conn + var err error + if url.Scheme == "http" { + conn, err = net.Dial("tcp", addr) + if err != nil { + return nil, err + } + } else { // https + conn, err = tls.Dial("tcp", addr, nil) + if err != nil { + return nil, err + } + h := url.Host + if hasPort(h) { + h = h[0:strings.LastIndex(h, ":")] + } + if err := conn.(*tls.Conn).VerifyHostname(h); err != nil { + return nil, err + } + } + + return httputil.NewClientConn(conn, nil), nil +} + +func getResponse(rawUrl string, req *http.Request) (*httputil.ClientConn, *http.Response, 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 := httputil.DumpRequest(req, true) + if err != nil { + println(err.Error()) + } + print(string(dump)) + } + + conn, err := newConn(url) + if err != nil { + println(err.Error()) + return nil, nil, err + } + + resp, err := conn.Do(req) + if err != nil { + if err != httputil.ErrPersistEOF { + return nil, nil, err + } + } + return conn, resp, nil } func Get(url string) *HttpRequestBuilder { - var req http.Request - req.Method = "GET" - req.Header = http.Header{} - req.Header.Set("User-Agent", defaultUserAgent) - return &HttpRequestBuilder{url, &req, nil, map[string]string{}} + var req http.Request + req.Method = "GET" + req.Header = http.Header{} + req.Header.Set("User-Agent", defaultUserAgent) + return &HttpRequestBuilder{url, &req, nil, map[string]string{}} } func Post(url string) *HttpRequestBuilder { - var req http.Request - req.Method = "POST" - req.Header = http.Header{} - req.Header.Set("User-Agent", defaultUserAgent) - return &HttpRequestBuilder{url, &req, nil, map[string]string{}} + var req http.Request + req.Method = "POST" + req.Header = http.Header{} + req.Header.Set("User-Agent", defaultUserAgent) + return &HttpRequestBuilder{url, &req, nil, map[string]string{}} } func Put(url string) *HttpRequestBuilder { - var req http.Request - req.Method = "PUT" - req.Header = http.Header{} - req.Header.Set("User-Agent", defaultUserAgent) - return &HttpRequestBuilder{url, &req, nil, map[string]string{}} + var req http.Request + req.Method = "PUT" + req.Header = http.Header{} + req.Header.Set("User-Agent", defaultUserAgent) + return &HttpRequestBuilder{url, &req, nil, map[string]string{}} } func Delete(url string) *HttpRequestBuilder { - var req http.Request - req.Method = "DELETE" - req.Header = http.Header{} - req.Header.Set("User-Agent", defaultUserAgent) - return &HttpRequestBuilder{url, &req, nil, map[string]string{}} + var req http.Request + req.Method = "DELETE" + req.Header = http.Header{} + req.Header.Set("User-Agent", defaultUserAgent) + return &HttpRequestBuilder{url, &req, nil, map[string]string{}} } type HttpRequestBuilder struct { - url string - req *http.Request - clientConn *http.ClientConn - params map[string]string -} - -func (b *HttpRequestBuilder) getResponse() (*http.Response, os.Error) { - var paramBody string - if b.params != nil && len(b.params) > 0 { - var buf bytes.Buffer - for k, v := range b.params { - buf.WriteString(url.QueryEscape(k)) - buf.WriteByte('=') - buf.WriteString(url.QueryEscape(v)) - buf.WriteByte('&') - } - paramBody = buf.String() - paramBody = paramBody[0 : len(paramBody)-1] - } - if b.req.Method == "GET" && len(paramBody) > 0 { - if strings.Index(b.url, "?") != -1 { - b.url += "&" + paramBody - } else { - b.url = b.url + "?" + paramBody - } - } else if b.req.Method == "POST" && b.req.Body == nil && len(paramBody) > 0 { - b.Header("Content-Type", "application/x-www-form-urlencoded") - b.req.Body = nopCloser{bytes.NewBufferString(paramBody)} - b.req.ContentLength = int64(len(paramBody)) - } - - conn, resp, err := getResponse(b.url, b.req) - b.clientConn = conn - return resp, err + url string + req *http.Request + clientConn *httputil.ClientConn + params map[string]string +} + +func (b *HttpRequestBuilder) getResponse() (*http.Response, error) { + var paramBody string + if b.params != nil && len(b.params) > 0 { + var buf bytes.Buffer + for k, v := range b.params { + buf.WriteString(url.QueryEscape(k)) + buf.WriteByte('=') + buf.WriteString(url.QueryEscape(v)) + buf.WriteByte('&') + } + paramBody = buf.String() + paramBody = paramBody[0 : len(paramBody)-1] + } + if b.req.Method == "GET" && len(paramBody) > 0 { + if strings.Index(b.url, "?") != -1 { + b.url += "&" + paramBody + } else { + b.url = b.url + "?" + paramBody + } + } else if b.req.Method == "POST" && b.req.Body == nil && len(paramBody) > 0 { + b.Header("Content-Type", "application/x-www-form-urlencoded") + b.req.Body = nopCloser{bytes.NewBufferString(paramBody)} + b.req.ContentLength = int64(len(paramBody)) + } + + conn, resp, err := getResponse(b.url, b.req) + b.clientConn = conn + return resp, err } func (b *HttpRequestBuilder) Header(key, value string) *HttpRequestBuilder { - b.req.Header.Set(key, value) - return b + b.req.Header.Set(key, value) + return b } func (b *HttpRequestBuilder) Param(key, value string) *HttpRequestBuilder { - b.params[key] = value - return b + b.params[key] = value + return b } func (b *HttpRequestBuilder) Body(data interface{}) *HttpRequestBuilder { - switch t := data.(type) { - case string: - b.req.Body = getNopCloser(bytes.NewBufferString(t)) - b.req.ContentLength = int64(len(t)) - case []byte: - b.req.Body = getNopCloser(bytes.NewBuffer(t)) - b.req.ContentLength = int64(len(t)) - } - return b -} - -func (b *HttpRequestBuilder) AsString() (string, os.Error) { - resp, err := b.getResponse() - if err != nil { - return "", err - } - if resp.Body == nil { - return "", nil - } - data, err := ioutil.ReadAll(resp.Body) - if err != nil { - return "", err - } - - return string(data), nil -} - -func (b *HttpRequestBuilder) AsBytes() ([]byte, os.Error) { - resp, err := b.getResponse() - if err != nil { - return nil, err - } - if resp.Body == nil { - return nil, nil - } - data, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - return data, nil -} - -func (b *HttpRequestBuilder) AsFile(filename string) os.Error { - f, err := os.Create(filename) - if err != nil { - return err - } - defer f.Close() - - resp, err := b.getResponse() - if err != nil { - return err - } - if resp.Body == nil { - return nil - } - _, err = io.Copy(f, resp.Body) - if err != nil { - return err - } - return nil + switch t := data.(type) { + case string: + b.req.Body = getNopCloser(bytes.NewBufferString(t)) + b.req.ContentLength = int64(len(t)) + case []byte: + b.req.Body = getNopCloser(bytes.NewBuffer(t)) + b.req.ContentLength = int64(len(t)) + } + return b +} + +func (b *HttpRequestBuilder) AsString() (string, error) { + resp, err := b.getResponse() + if err != nil { + return "", err + } + if resp.Body == nil { + return "", nil + } + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + + return string(data), nil +} + +func (b *HttpRequestBuilder) AsBytes() ([]byte, error) { + resp, err := b.getResponse() + if err != nil { + return nil, err + } + if resp.Body == nil { + return nil, nil + } + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + return data, nil +} + +func (b *HttpRequestBuilder) AsFile(filename string) error { + f, err := os.Create(filename) + if err != nil { + return err + } + defer f.Close() + + resp, err := b.getResponse() + if err != nil { + return err + } + if resp.Body == nil { + return nil + } + _, err = io.Copy(f, resp.Body) + if err != nil { + return err + } + return nil } -func (b *HttpRequestBuilder) AsResponse() (*http.Response, os.Error) { - return b.getResponse() +func (b *HttpRequestBuilder) AsResponse() (*http.Response, error) { + return b.getResponse() } func (b *HttpRequestBuilder) Close() { - if b.clientConn != nil { - b.clientConn.Close() - } + if b.clientConn != nil { + b.clientConn.Close() + } } Only in b: .httplib.go.swp diff -ru a/httplib_test.go b/httplib_test.go --- a/httplib_test.go 2011-12-13 15:24:59.776471311 +0100 +++ b/httplib_test.go 2011-12-13 15:33:28.449483433 +0100 @@ -7,7 +7,7 @@ func TestFluidGet(t *testing.T) { query, err := Get("www.google.com/search").AsString() if err != nil { - println(err.String()) + println(err.Error()) } println(query); @@ -20,4 +20,4 @@ t.Fatalf("Expected an error!") } } -*/ \ No newline at end of file +*/ Only in b: .httplib_test.go.swp
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor