xmlrpc

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2024 License: BSD-3-Clause Imports: 10 Imported by: 0

README

XML-RPC (server)

A small XML-RPC server.

The server does the bare minimum. It implements the net/http.Handler interface, does request decoding and response encoding, and handles errors.

See ./examples for two server examples. With the server-middleware example you can run the following commands:

curl --data @./testdata/getCurrentTimeRFC3339.xml http://127.0.0.1:4242/RPC2
curl --data @./testdata/getCurrentTimeUnix.xml http://127.0.0.1:4242/RPC2
curl --data @./testdata/getCurrentTimeWrongFormat.xml http://127.0.0.1:4242/RPC2
curl --data @./testdata/unknownMethod.xml http://127.0.0.1:4242/RPC2

This was part of a test and not completely finished. There's no client, not enough tests, and I think the middleware integration can be improved. But for now it's good enough.

Feel free to send a patch to the mailinglist.

Install with:

go get git.sr.ht/~fsx/xmlrpc-go@latest

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// -32000 to -32099: Reserved for implementation-defined server-errors.
	FaultParseError     = &Response{Fault: NewFault(-32700, "parse error")}
	FaultInvalidRequest = &Response{Fault: NewFault(-32600, "invalid request")}
	FaultMethodNotFound = &Response{Fault: NewFault(-32601, "method not found")}
	FaultInvalidParams  = &Response{Fault: NewFault(-32602, "invalid params")}
	FaultInternalError  = &Response{Fault: NewFault(-32603, "internal error")}
)

Functions

func GetLogger

func GetLogger(ctx context.Context) (*slog.Logger, bool)

func MustGetLogger

func MustGetLogger(ctx context.Context) *slog.Logger

func NewServer

func NewServer(handler Handler) http.Handler

func Ptr

func Ptr[T any](v T) *T

func SetLogger

func SetLogger(ctx context.Context, logger *slog.Logger) context.Context

Types

type DateTime

type DateTime struct {
	T time.Time
}

func (DateTime) MarshalXML

func (dt DateTime) MarshalXML(e *xml.Encoder, start xml.StartElement) error

func (*DateTime) UnmarshalXML

func (dt *DateTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

type Fault

type Fault struct {
	Members []Member `xml:"value>struct>member"`
}

func NewFault

func NewFault(code int32, message string) *Fault

func (*Fault) Code

func (f *Fault) Code() int32

func (*Fault) Error

func (f *Fault) Error() string

func (*Fault) Message

func (f *Fault) Message() string

func (*Fault) String

func (f *Fault) String() string

type Handler

type Handler func(context.Context, *Request) *Response

func LogRequestMiddleware

func LogRequestMiddleware(handler Handler) Handler

func LoggerMiddleware

func LoggerMiddleware(handler Handler, logger *slog.Logger) Handler

func RequestIDMiddleware

func RequestIDMiddleware(handler Handler) Handler

type Member

type Member struct {
	Name  string `xml:"name"`
	Value Value  `xml:"value"`
}

type Param

type Param struct {
	Value Value `xml:"value"`
}

type Params

type Params []Param

type Request

type Request struct {
	XMLName xml.Name `xml:"methodCall"`
	Method  string   `xml:"methodName"`
	Params  []Param  `xml:"params>param"`
}

type Response

type Response struct {
	XMLName xml.Name `xml:"methodResponse"`
	Params  Params   `xml:"params>param"`
	Fault   *Fault   `xml:"fault,omitempty"`
}

type Server

type Server struct {
	// contains filtered or unexported fields
}

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Value

type Value struct {
	Kind ValueKind `xml:"-"`

	Array    *[]Value  `xml:"array>data>value"`
	Struct   *[]Member `xml:"struct>member"`
	String   *string   `xml:"string"`
	Int      *int32    `xml:"int"`
	Int4     *int32    `xml:"i4"`
	Double   *float64  `xml:"double"`
	Boolean  *bool     `xml:"boolean"`
	DateTime *DateTime `xml:"dateTime.iso8601"`
	Base64   *string   `xml:"base64"`
	Raw      *string   `xml:",innerxml"`
}

func NewArray

func NewArray(values ...Value) Value

func NewBase64

func NewBase64(v string) Value

func NewBoolean

func NewBoolean(v bool) Value

func NewDateTime

func NewDateTime(v time.Time) Value

func NewDouble

func NewDouble(v float64) Value

func NewInt

func NewInt(v int32) Value

func NewInt4

func NewInt4(v int32) Value

func NewString

func NewString(v string) Value

func NewStruct

func NewStruct(members ...Member) Value

func (Value) Is

func (v Value) Is(k ValueKind) bool

type ValueKind

type ValueKind uint16
const (
	ValueUnknown ValueKind = 1 << iota
	ValueArray
	ValueStruct
	ValueString
	ValueInt
	ValueInt4
	ValueDouble
	ValueBoolean
	ValueDateTime
	ValueBase64
)

func (ValueKind) String

func (v ValueKind) String() string

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL