pax_global_header00006660000000000000000000000064134371507650014525gustar00rootroot0000000000000052 comment=b33132ba322246f94c9014de03af2176f00743bb go-xmlrpc-0.0.3/000077500000000000000000000000001343715076500134355ustar00rootroot00000000000000go-xmlrpc-0.0.3/.travis.yml000066400000000000000000000000651343715076500155470ustar00rootroot00000000000000language: go go: - tip before_install: - go test go-xmlrpc-0.0.3/LICENSE000066400000000000000000000020751343715076500144460ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2017 Yasuhiro Matsumoto Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. go-xmlrpc-0.0.3/README.md000066400000000000000000000011061343715076500147120ustar00rootroot00000000000000# go-xmlrpc xmlrpc interface for go ## Usage ```go package main import ( "github.com/mattn/go-xmlrpc" "fmt" "log" ) func main() { res, e := xmlrpc.Call( "http://your-blog.example.com/xmlrpc.php", "metaWeblog.getRecentPosts", "blog-id", "user-id", "password", 10) if e != nil { log.Fatal(e) } for _, p := range res.(xmlrpc.Array) { for k, v := range p.(xmlrpc.Struct) { fmt.Printf("%s=%v\n", k, v) } fmt.Println() } } ``` ## Installation ``` $ go get github.com/mattn/go-xmlrpc ``` ## License MIT ## Author Yasuhiro Matsumoto (a.k.a. mattn) go-xmlrpc-0.0.3/_example/000077500000000000000000000000001343715076500152275ustar00rootroot00000000000000go-xmlrpc-0.0.3/_example/example.go000066400000000000000000000006171343715076500172150ustar00rootroot00000000000000package main import ( "github.com/mattn/go-xmlrpc" "fmt" "log" ) func main() { res, e := xmlrpc.Call( "http://your-blog.example.com/xmlrpc.php", "metaWeblog.getRecentPosts", "blog-id", "user-id", "password", 10) if e != nil { log.Fatal(e) } for _, p := range res.(xmlrpc.Array) { for k, v := range p.(xmlrpc.Struct) { fmt.Printf("%s=%v\n", k, v) } fmt.Println() } } go-xmlrpc-0.0.3/go.mod000066400000000000000000000000531343715076500145410ustar00rootroot00000000000000module github.com/mattn/go-xmlrpc go 1.10 go-xmlrpc-0.0.3/xmlrpc.go000066400000000000000000000203371343715076500152760ustar00rootroot00000000000000package xmlrpc import ( "bytes" "encoding/base64" "encoding/xml" "errors" "fmt" "io" "io/ioutil" "net/http" "reflect" "strconv" "strings" "time" ) type Array []interface{} type Struct map[string]interface{} var xmlSpecial = map[byte]string{ '<': "<", '>': ">", '"': """, '\'': "'", '&': "&", } func xmlEscape(s string) string { var b bytes.Buffer for i := 0; i < len(s); i++ { c := s[i] if s, ok := xmlSpecial[c]; ok { b.WriteString(s) } else { b.WriteByte(c) } } return b.String() } type valueNode struct { Type string `xml:"attr"` Body string `xml:"chardata"` } func next(p *xml.Decoder) (xml.Name, interface{}, error) { se, e := nextStart(p) if e != nil { return xml.Name{}, nil, e } var nv interface{} switch se.Name.Local { case "string": var s string if e = p.DecodeElement(&s, &se); e != nil { return xml.Name{}, nil, e } return xml.Name{}, s, nil case "boolean": var s string if e = p.DecodeElement(&s, &se); e != nil { return xml.Name{}, nil, e } s = strings.TrimSpace(s) var b bool switch s { case "true", "1": b = true case "false", "0": b = false default: e = errors.New("invalid boolean value") } return xml.Name{}, b, e case "int", "i1", "i2", "i4", "i8": var s string var i int if e = p.DecodeElement(&s, &se); e != nil { return xml.Name{}, nil, e } i, e = strconv.Atoi(strings.TrimSpace(s)) return xml.Name{}, i, e case "double": var s string var f float64 if e = p.DecodeElement(&s, &se); e != nil { return xml.Name{}, nil, e } f, e = strconv.ParseFloat(strings.TrimSpace(s), 64) return xml.Name{}, f, e case "dateTime.iso8601": var s string if e = p.DecodeElement(&s, &se); e != nil { return xml.Name{}, nil, e } t, e := time.Parse("20060102T15:04:05", s) if e != nil { t, e = time.Parse("2006-01-02T15:04:05-07:00", s) if e != nil { t, e = time.Parse("2006-01-02T15:04:05", s) } } return xml.Name{}, t, e case "base64": var s string if e = p.DecodeElement(&s, &se); e != nil { return xml.Name{}, nil, e } if b, e := base64.StdEncoding.DecodeString(s); e != nil { return xml.Name{}, nil, e } else { return xml.Name{}, b, nil } case "member": nextStart(p) return next(p) case "value": nextStart(p) return next(p) case "name": nextStart(p) return next(p) case "struct": st := Struct{} se, e = nextStart(p) for e == nil && se.Name.Local == "member" { // name se, e = nextStart(p) if se.Name.Local != "name" { return xml.Name{}, nil, errors.New("invalid response") } if e != nil { break } var name string if e = p.DecodeElement(&name, &se); e != nil { return xml.Name{}, nil, e } se, e = nextStart(p) if e != nil { break } // value _, value, e := next(p) if se.Name.Local != "value" { return xml.Name{}, nil, errors.New("invalid response") } if e != nil { break } st[name] = value se, e = nextStart(p) if e != nil { break } } return xml.Name{}, st, nil case "array": var ar Array nextStart(p) // data nextStart(p) // top of value for { _, value, e := next(p) if e != nil { break } ar = append(ar, value) if reflect.ValueOf(value).Kind() != reflect.Map { nextStart(p) } } return xml.Name{}, ar, nil case "nil": return xml.Name{}, nil, nil } if e = p.DecodeElement(nv, &se); e != nil { return xml.Name{}, nil, e } return se.Name, nv, e } func nextStart(p *xml.Decoder) (xml.StartElement, error) { for { t, e := p.Token() if e != nil { return xml.StartElement{}, e } switch t := t.(type) { case xml.StartElement: return t, nil } } panic("unreachable") } func toXml(v interface{}, typ bool) (s string) { if v == nil { return "" } r := reflect.ValueOf(v) t := r.Type() k := t.Kind() if b, ok := v.([]byte); ok { return "" + base64.StdEncoding.EncodeToString(b) + "" } switch k { case reflect.Invalid: panic("unsupported type") case reflect.Bool: return fmt.Sprintf("%v", v) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: if typ { return fmt.Sprintf("%v", v) } return fmt.Sprintf("%v", v) case reflect.Uintptr: panic("unsupported type") case reflect.Float32, reflect.Float64: if typ { return fmt.Sprintf("%v", v) } return fmt.Sprintf("%v", v) case reflect.Complex64, reflect.Complex128: panic("unsupported type") case reflect.Array: s = "" for n := 0; n < r.Len(); n++ { s += "" s += toXml(r.Index(n).Interface(), typ) s += "" } s += "" return s case reflect.Chan: panic("unsupported type") case reflect.Func: panic("unsupported type") case reflect.Interface: return toXml(r.Elem(), typ) case reflect.Map: s = "" for _, key := range r.MapKeys() { s += "" s += "" + xmlEscape(key.Interface().(string)) + "" s += "" + toXml(r.MapIndex(key).Interface(), typ) + "" s += "" } s += "" return s case reflect.Ptr: panic("unsupported type") case reflect.Slice: s = "" for n := 0; n < r.Len(); n++ { s += "" s += toXml(r.Index(n).Interface(), typ) s += "" } s += "" return s case reflect.String: if typ { return fmt.Sprintf("%v", xmlEscape(v.(string))) } return xmlEscape(v.(string)) case reflect.Struct: s = "" for n := 0; n < r.NumField(); n++ { s += "" s += "" + t.Field(n).Name + "" s += "" + toXml(r.FieldByIndex([]int{n}).Interface(), true) + "" s += "" } s += "" return s case reflect.UnsafePointer: return toXml(r.Elem(), typ) } return } // Client is client of XMLRPC type Client struct { HttpClient *http.Client url string } // NewClient create new Client func NewClient(url string) *Client { return &Client{ HttpClient: &http.Client{Transport: http.DefaultTransport, Timeout: 10 * time.Second}, url: url, } } func makeRequest(name string, args ...interface{}) *bytes.Buffer { buf := new(bytes.Buffer) buf.WriteString(``) buf.WriteString("" + xmlEscape(name) + "") buf.WriteString("") for _, arg := range args { buf.WriteString("") buf.WriteString(toXml(arg, true)) buf.WriteString("") } buf.WriteString("") return buf } func call(client *http.Client, url, name string, args ...interface{}) (v interface{}, e error) { r, e := client.Post(url, "text/xml", makeRequest(name, args...)) if e != nil { return nil, e } // Since we do not always read the entire body, discard the rest, which // allows the http transport to reuse the connection. defer io.Copy(ioutil.Discard, r.Body) defer r.Body.Close() if r.StatusCode/100 != 2 { return nil, errors.New(http.StatusText(http.StatusBadRequest)) } p := xml.NewDecoder(r.Body) se, e := nextStart(p) // methodResponse if se.Name.Local != "methodResponse" { return nil, errors.New("invalid response: missing methodResponse") } se, e = nextStart(p) // params if se.Name.Local != "params" { return nil, errors.New("invalid response: missing params") } se, e = nextStart(p) // param if se.Name.Local != "param" { return nil, errors.New("invalid response: missing param") } se, e = nextStart(p) // value if se.Name.Local != "value" { return nil, errors.New("invalid response: missing value") } _, v, e = next(p) return v, e } // Call call remote procedures function name with args func (c *Client) Call(name string, args ...interface{}) (v interface{}, e error) { return call(c.HttpClient, c.url, name, args...) } // Global httpClient allows us to pool/reuse connections and not wastefully // re-create transports for each request. var httpClient = &http.Client{Transport: http.DefaultTransport, Timeout: 10 * time.Second} // Call call remote procedures function name with args func Call(url, name string, args ...interface{}) (v interface{}, e error) { return call(httpClient, url, name, args...) } go-xmlrpc-0.0.3/xmlrpc_test.go000066400000000000000000000173541343715076500163420ustar00rootroot00000000000000package xmlrpc import ( "encoding/xml" "errors" "fmt" "net/http" "net/http/httptest" "testing" ) func createServer(path, name string, f func(args ...interface{}) (interface{}, error)) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != path { http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) return } p := xml.NewDecoder(r.Body) se, _ := nextStart(p) // methodResponse if se.Name.Local != "methodCall" { http.Error(w, "missing methodCall", http.StatusBadRequest) return } se, _ = nextStart(p) // params if se.Name.Local != "methodName" { http.Error(w, "missing methodName", http.StatusBadRequest) return } var s string if err := p.DecodeElement(&s, &se); err != nil { http.Error(w, "wrong function name", http.StatusBadRequest) return } if s != name { http.Error(w, fmt.Sprintf("want function name %q but got %q", name, s), http.StatusBadRequest) return } se, _ = nextStart(p) // params if se.Name.Local != "params" { http.Error(w, "missing params", http.StatusBadRequest) return } var args []interface{} for { se, _ = nextStart(p) // param if se.Name.Local == "" { break } if se.Name.Local != "param" { http.Error(w, "missing param", http.StatusBadRequest) return } se, _ = nextStart(p) // value if se.Name.Local != "value" { http.Error(w, "missing value", http.StatusBadRequest) return } _, v, err := next(p) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } args = append(args, v) } ret, err := f(args...) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } w.Write([]byte(` ` + toXml(ret, true) + ` `)) } } func TestAddInt(t *testing.T) { ts := httptest.NewServer(createServer("/api", "AddInt", func(args ...interface{}) (interface{}, error) { if len(args) != 2 { return nil, errors.New("bad number of arguments") } switch args[0].(type) { case int: default: return nil, errors.New("args[0] should be int") } switch args[1].(type) { case int: default: return nil, errors.New("args[1] should be int") } return args[0].(int) + args[1].(int), nil })) defer ts.Close() client := NewClient(ts.URL + "/api") v, err := client.Call("AddInt", 1, 2) if err != nil { t.Fatal(err) } i, ok := v.(int) if !ok { t.Fatalf("want int but got %T: %v", v, v) } if i != 3 { t.Fatalf("want %v but got %v", 3, v) } } func TestAddString(t *testing.T) { ts := httptest.NewServer(createServer("/api", "AddString", func(args ...interface{}) (interface{}, error) { if len(args) != 2 { return nil, errors.New("bad number of arguments") } switch args[0].(type) { case string: default: return nil, errors.New("args[0] should be string") } switch args[1].(type) { case string: default: return nil, errors.New("args[1] should be string") } return args[0].(string) + args[1].(string), nil })) defer ts.Close() client := NewClient(ts.URL + "/api") v, err := client.Call("AddString", "hello", "world") if err != nil { t.Fatal(err) } s, ok := v.(string) if !ok { t.Fatalf("want string but got %T: %v", v, v) } if s != "helloworld" { t.Fatalf("want %q but got %q", "helloworld", v) } } type ParseStructArrayHandler struct { } func (h *ParseStructArrayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Write([]byte(` test1 a test2 2 test1 b test2 2 test1 c test2 2 `)) } func TestParseStructArray(t *testing.T) { ts := httptest.NewServer(&ParseStructArrayHandler{}) defer ts.Close() res, err := NewClient(ts.URL + "/").Call("Irrelevant") if err != nil { t.Fatal(err) } if len(res.(Array)) != 3 { t.Fatal("expected array with 3 entries") } } type ParseIntArrayHandler struct { } func (h *ParseIntArrayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Write([]byte(` 2 3 4 `)) } func TestParseIntArray(t *testing.T) { ts := httptest.NewServer(&ParseIntArrayHandler{}) defer ts.Close() res, err := NewClient(ts.URL + "/").Call("Irrelevant") if err != nil { t.Fatal(err) } if len(res.(Array)) != 3 { t.Fatal("expected array with 3 entries") } } type ParseMixedArrayHandler struct { } func (h *ParseMixedArrayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Write([]byte(` test1 a test2 2 2 test1 b test2 2 4 `)) } func TestParseMixedArray(t *testing.T) { ts := httptest.NewServer(&ParseMixedArrayHandler{}) defer ts.Close() res, err := NewClient(ts.URL + "/").Call("Irrelevant") if err != nil { t.Fatal(err) } if len(res.(Array)) != 4 { t.Fatal("expected array with 4 entries") } }