Fedora 33 ships the new API so let's do the switch now. But... this would break older Fedoras because they only have the old API, right? We have the following options: 1) Ship xmlrpc compat package to Fedora 33+. This would mean that we delay the API switch till F32 EOL. This would be the most elegant solution, yet it has two issues: a) We will surely not be able to deliver the compat package before F33 Final Freeze. b) It's an extra and annoying work. 2) Downstream patch. No. 3) Use build constraints and have two versions of our code for both different API. I chose solution #3. It has an issue though: %gobuild macro already passes -tags argument to go build. Therefore the following line fails because it's not possible to use -tags more than once: %gobuild -tags kolo_xmlrpc_oldapi ... Therefore I had to come up with manual tinkering with the build constraints in the spec file. This is pretty ugly but I like that: 1) Go code is actually clean, no weird magic is happening there. 2) We can still ship our software to Fedora/RHEL as we used to (no downstream patches) 3) All downstreams can use the upstream spec file directly. Note that this doesn't affect RHEL in any way as it uses vendored libraries.
161 lines
3.2 KiB
Go
161 lines
3.2 KiB
Go
package xmlrpc
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/http/cookiejar"
|
|
"net/rpc"
|
|
"net/url"
|
|
"sync"
|
|
)
|
|
|
|
type Client struct {
|
|
*rpc.Client
|
|
}
|
|
|
|
// clientCodec is rpc.ClientCodec interface implementation.
|
|
type clientCodec struct {
|
|
// url presents url of xmlrpc service
|
|
url *url.URL
|
|
|
|
// httpClient works with HTTP protocol
|
|
httpClient *http.Client
|
|
|
|
// cookies stores cookies received on last request
|
|
cookies http.CookieJar
|
|
|
|
// responses presents map of active requests. It is required to return request id, that
|
|
// rpc.Client can mark them as done.
|
|
responses map[uint64]*http.Response
|
|
mutex sync.Mutex
|
|
|
|
response Response
|
|
|
|
// ready presents channel, that is used to link request and it`s response.
|
|
ready chan uint64
|
|
|
|
// close notifies codec is closed.
|
|
close chan uint64
|
|
}
|
|
|
|
func (codec *clientCodec) WriteRequest(request *rpc.Request, args interface{}) (err error) {
|
|
httpRequest, err := NewRequest(codec.url.String(), request.ServiceMethod, args)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if codec.cookies != nil {
|
|
for _, cookie := range codec.cookies.Cookies(codec.url) {
|
|
httpRequest.AddCookie(cookie)
|
|
}
|
|
}
|
|
|
|
var httpResponse *http.Response
|
|
httpResponse, err = codec.httpClient.Do(httpRequest)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if codec.cookies != nil {
|
|
codec.cookies.SetCookies(codec.url, httpResponse.Cookies())
|
|
}
|
|
|
|
codec.mutex.Lock()
|
|
codec.responses[request.Seq] = httpResponse
|
|
codec.mutex.Unlock()
|
|
|
|
codec.ready <- request.Seq
|
|
|
|
return nil
|
|
}
|
|
|
|
func (codec *clientCodec) ReadResponseHeader(response *rpc.Response) (err error) {
|
|
var seq uint64
|
|
select {
|
|
case seq = <-codec.ready:
|
|
case <-codec.close:
|
|
return errors.New("codec is closed")
|
|
}
|
|
response.Seq = seq
|
|
|
|
codec.mutex.Lock()
|
|
httpResponse := codec.responses[seq]
|
|
delete(codec.responses, seq)
|
|
codec.mutex.Unlock()
|
|
|
|
defer httpResponse.Body.Close()
|
|
|
|
if httpResponse.StatusCode < 200 || httpResponse.StatusCode >= 300 {
|
|
response.Error = fmt.Sprintf("request error: bad status code - %d", httpResponse.StatusCode)
|
|
return nil
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(httpResponse.Body)
|
|
if err != nil {
|
|
response.Error = err.Error()
|
|
return nil
|
|
}
|
|
|
|
resp := Response(body)
|
|
if err := resp.Err(); err != nil {
|
|
response.Error = err.Error()
|
|
return nil
|
|
}
|
|
|
|
codec.response = resp
|
|
|
|
return nil
|
|
}
|
|
|
|
func (codec *clientCodec) ReadResponseBody(v interface{}) (err error) {
|
|
if v == nil {
|
|
return nil
|
|
}
|
|
return codec.response.Unmarshal(v)
|
|
}
|
|
|
|
func (codec *clientCodec) Close() error {
|
|
if transport, ok := codec.httpClient.Transport.(*http.Transport); ok {
|
|
transport.CloseIdleConnections()
|
|
}
|
|
|
|
close(codec.close)
|
|
|
|
return nil
|
|
}
|
|
|
|
// NewClient returns instance of rpc.Client object, that is used to send request to xmlrpc service.
|
|
func NewClient(requrl string, transport http.RoundTripper) (*Client, error) {
|
|
if transport == nil {
|
|
transport = http.DefaultTransport
|
|
}
|
|
|
|
httpClient := &http.Client{Transport: transport}
|
|
|
|
jar, err := cookiejar.New(nil)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
u, err := url.Parse(requrl)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
codec := clientCodec{
|
|
url: u,
|
|
httpClient: httpClient,
|
|
close: make(chan uint64),
|
|
ready: make(chan uint64),
|
|
responses: make(map[uint64]*http.Response),
|
|
cookies: jar,
|
|
}
|
|
|
|
return &Client{rpc.NewClientWithCodec(&codec)}, nil
|
|
}
|