working commit
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
package rpccli
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
httpClient *http.Client
|
||||
userAgent string
|
||||
}
|
||||
|
||||
func NewClient(transport http.RoundTripper, mwFuncs ...MiddlewareFunc) *Client {
|
||||
if transport == nil {
|
||||
transport = NewDefaultTransport()
|
||||
}
|
||||
for _, mwFunc := range mwFuncs {
|
||||
transport = mwFunc(transport)
|
||||
}
|
||||
httpClient := &http.Client{
|
||||
Transport: transport,
|
||||
}
|
||||
|
||||
return &Client{
|
||||
httpClient: httpClient,
|
||||
userAgent: "ociClient/1.0",
|
||||
}
|
||||
}
|
||||
|
||||
func (cli *Client) SetTransport(transport http.RoundTripper) {
|
||||
cli.httpClient.Transport = transport
|
||||
}
|
||||
|
||||
type MiddlewareFunc func(next http.RoundTripper) http.RoundTripper
|
||||
|
||||
func (cli *Client) UseMiddleware(mwFunc MiddlewareFunc) {
|
||||
cli.httpClient.Transport = mwFunc(cli.httpClient.Transport)
|
||||
}
|
||||
|
||||
// BasicAuthMiddleware
|
||||
func NewBasicAuthMiddleware(user, pass string) MiddlewareFunc {
|
||||
return func(next http.RoundTripper) http.RoundTripper {
|
||||
return newBasicAuthMW(next, user, pass)
|
||||
}
|
||||
}
|
||||
|
||||
type BasicAuthMW struct {
|
||||
user, pass string
|
||||
next http.RoundTripper
|
||||
}
|
||||
|
||||
func newBasicAuthMW(next http.RoundTripper, user, pass string) *BasicAuthMW {
|
||||
return &BasicAuthMW{
|
||||
user: user,
|
||||
pass: pass,
|
||||
next: next,
|
||||
}
|
||||
}
|
||||
|
||||
func (tran BasicAuthMW) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
if tran.user != "" && tran.pass != "" {
|
||||
pair := base64.StdEncoding.EncodeToString([]byte(tran.user + ":" + tran.pass))
|
||||
req.Header.Set("Authorization", "Basic "+pair)
|
||||
}
|
||||
return tran.next.RoundTrip(req)
|
||||
}
|
||||
|
||||
// BearerAuthMiddleware
|
||||
func NewBearerAuthMiddleware(token string) MiddlewareFunc {
|
||||
return func(next http.RoundTripper) http.RoundTripper {
|
||||
return newBearerAuthMW(next, token)
|
||||
}
|
||||
}
|
||||
|
||||
type BearerAuthMW struct {
|
||||
token string
|
||||
next http.RoundTripper
|
||||
}
|
||||
|
||||
func newBearerAuthMW(next http.RoundTripper, token string) *BearerAuthMW {
|
||||
return &BearerAuthMW{
|
||||
token: token,
|
||||
next: next,
|
||||
}
|
||||
}
|
||||
|
||||
func (tran BearerAuthMW) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
req.Header.Set("Authorization", "Bearer "+tran.token)
|
||||
return tran.next.RoundTrip(req)
|
||||
}
|
||||
|
||||
// DefaultTransport
|
||||
type DefaultTransport struct {
|
||||
transport http.RoundTripper
|
||||
}
|
||||
|
||||
func NewDefaultTransport() *DefaultTransport {
|
||||
return &DefaultTransport{
|
||||
transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (wrap *DefaultTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return wrap.transport.RoundTrip(req)
|
||||
}
|
||||
|
||||
// ExampleMiddleware
|
||||
func NewExampleMiddleware() MiddlewareFunc {
|
||||
return func(next http.RoundTripper) http.RoundTripper {
|
||||
return newExampleTransport(next)
|
||||
}
|
||||
}
|
||||
|
||||
type ExampleTransport struct {
|
||||
next http.RoundTripper
|
||||
}
|
||||
|
||||
func newExampleTransport(next http.RoundTripper) *ExampleTransport {
|
||||
return &ExampleTransport{
|
||||
next: next,
|
||||
}
|
||||
}
|
||||
|
||||
func (tran ExampleTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return tran.next.RoundTrip(req)
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package rpccli
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Referer struct {
|
||||
urlobj *url.URL
|
||||
user, pass string
|
||||
obj, oper string
|
||||
}
|
||||
|
||||
func NewReferer(hostname, object, operation string) (*Referer, error) {
|
||||
repo := &Referer{
|
||||
obj: object,
|
||||
oper: operation,
|
||||
}
|
||||
if !strings.Contains(hostname, "://") {
|
||||
hostname = "https://" + hostname
|
||||
}
|
||||
urlobj, err := url.Parse(hostname)
|
||||
if err != nil {
|
||||
return repo, err
|
||||
}
|
||||
if urlobj.User != nil {
|
||||
repo.user = urlobj.User.Username()
|
||||
repo.pass, _ = urlobj.User.Password()
|
||||
urlobj.User = nil
|
||||
}
|
||||
repo.resource = path.Join("/", urlobj.Path)
|
||||
urlobj.Path = "/"
|
||||
repo.urlobj = urlobj
|
||||
return repo, err
|
||||
}
|
||||
|
||||
func (repo *Referer) Raw() string {
|
||||
return path.Join(repo.urlobj.Host, "/v3/api/", repo.obj, repo.oper)
|
||||
}
|
||||
|
||||
func (repo *Referer) URI(object, operation) string {
|
||||
curl := repo.urlobj.JoinPath("/v3/api/", object, operation)
|
||||
return curl.String()
|
||||
}
|
||||
|
||||
func (repo *Referer) Userinfo() (string, string) {
|
||||
return repo.user, repo.pass
|
||||
}
|
||||
|
||||
func (repo *Referer) SetUserinfo(user, pass string) {
|
||||
if user != "" && pass != "" {
|
||||
repo.user, repo.pass = user, pass
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user