Documentation
¶
Overview ¶
Package session manages tuned HTTP transport, strict host-boundary redirect validation, IServ form/OIDC authentication handoffs, and authenticated requests.
Index ¶
- Constants
- func BuildTargetURL(baseURL, relPath string) string
- func DrainAndClose(body io.ReadCloser)
- func DrainBody(body io.Reader)
- func HasKind(err error, kinds ...ErrorKind) bool
- func IsForbidden(err error) bool
- func IsNotFound(err error) bool
- func IsProtocolOrValidation(err error) bool
- func IsUnauthorized(err error) bool
- func MapStatus(code int, targetURL string) error
- func NewDefaultTransport() *http.Transport
- func NewRedirectPolicy(allowedHost string, allowInsecureHTTP bool) func(req *http.Request, via []*http.Request) error
- func ReadLimited(r io.Reader, limit int64) ([]byte, error)
- type ErrorKind
- type Factory
- type FactoryConfig
- type Session
- func (s *Session) Get(ctx context.Context, relPath string) (*http.Response, error)
- func (s *Session) GetBytes(ctx context.Context, relPath string, limitBytes int64) ([]byte, string, error)
- func (s *Session) GetHTML(ctx context.Context, relPath string) (*goquery.Document, error)
- func (s *Session) GetJSON(ctx context.Context, relPath string, dst any) error
- func (s *Session) GetJSONBytes(ctx context.Context, relPath string) ([]byte, string, error)
- func (s *Session) Log() *slog.Logger
- func (s *Session) MaxBody() int64
- func (s *Session) PostJSON(ctx context.Context, relPath string, payload []byte) ([]byte, string, error)
- type UpstreamError
Constants ¶
const ( // DefaultTimeout is the default per-request HTTP timeout. DefaultTimeout = 20 * time.Second // DefaultMaxBodyBytes is the default maximum buffered response size (10 MiB). DefaultMaxBodyBytes int64 = 10 * 1024 * 1024 // DefaultUserAgent is the default User-Agent header sent by the SDK. DefaultUserAgent = "iserv-go/0.2" )
Variables ¶
This section is empty.
Functions ¶
func BuildTargetURL ¶
BuildTargetURL resolves relPath against baseURL while avoiding duplicate subpath mount prefixes (e.g., baseURL "https://host/school" + "/school/iserv/...").
func DrainAndClose ¶
func DrainAndClose(body io.ReadCloser)
DrainAndClose drains a bounded remainder of body and closes it so HTTP/1.1 keep-alive connections remain reusable.
func DrainBody ¶
DrainBody drains a bounded remainder of body so HTTP/1.1 keep-alive connections remain reusable before the caller closes the body.
func HasKind ¶
HasKind reports whether err contains an *UpstreamError matching any of kinds.
func IsForbidden ¶
IsForbidden reports whether err represents an HTTP 403 forbidden error.
func IsNotFound ¶
IsNotFound reports whether err represents an HTTP 404 resource not found error.
func IsProtocolOrValidation ¶
IsProtocolOrValidation reports whether err represents an upstream protocol or validation rejection.
func IsUnauthorized ¶
IsUnauthorized reports whether err represents a session-expired / unauthorized condition.
func NewDefaultTransport ¶
NewDefaultTransport constructs a production-tuned HTTP transport with TLS 1.2+, elevated per-host idle connections, and bounded handshake/header timeouts.
func NewRedirectPolicy ¶
func NewRedirectPolicy(allowedHost string, allowInsecureHTTP bool) func(req *http.Request, via []*http.Request) error
NewRedirectPolicy enforces same-host redirect boundaries, prevents HTTPS-to-HTTP downgrades, caps redirect depth, and strips Authorization headers across origin changes.
Types ¶
type ErrorKind ¶
type ErrorKind int
ErrorKind categorizes upstream transport and protocol errors without coupling internal packages to the root package.
const ( KindUnknown ErrorKind = iota KindValidation KindInvalidCredentials KindForbidden KindNotFound KindProtocol KindTooLarge )
Canonical ErrorKind classifications returned by UpstreamError.
type Factory ¶
type Factory struct {
// contains filtered or unexported fields
}
Factory constructs per-session HTTP clients sharing a tuned transport and performs form + OIDC authentication against IServ portals.
func NewFactory ¶
func NewFactory(cfg FactoryConfig) *Factory
NewFactory creates a Factory with secure TLS, connection pooling, and silent default logging via slog.DiscardHandler.
func (*Factory) Authenticate ¶
func (f *Factory) Authenticate(ctx context.Context, creds credentials.Credentials) (*Session, error)
Authenticate signs into IServ using the web form flow (plus any OIDC handoff steps) and returns a live Session.
type FactoryConfig ¶
type FactoryConfig struct {
Transport http.RoundTripper
Timeout time.Duration
MaxBodyBytes int64
UserAgent string
Logger *slog.Logger
}
FactoryConfig configures a Factory.
type Session ¶
type Session struct {
ID string
Credentials credentials.Credentials
HTTPClient *http.Client
MaxBodyBytes int64
UserAgent string
Logger *slog.Logger
CreatedAt time.Time
}
Session represents an authenticated IServ upstream session with its own isolated cookie jar and HTTP client.
func (*Session) Get ¶
Get sends an authenticated GET request for HTML or binary resources. Callers must close resp.Body when finished.
func (*Session) GetBytes ¶
func (s *Session) GetBytes(ctx context.Context, relPath string, limitBytes int64) ([]byte, string, error)
GetBytes performs an authenticated GET and reads up to limitBytes (or MaxBodyBytes).
func (*Session) GetHTML ¶
GetHTML performs an authenticated GET and parses the HTML response body into a goquery document.
func (*Session) GetJSONBytes ¶
GetJSONBytes performs an authenticated AJAX GET (Accept: application/json + X-Requested-With).
type UpstreamError ¶
type UpstreamError struct {
Kind ErrorKind
Code string
Message string
StatusCode int
URL string
Details map[string]string
Cause error
}
UpstreamError captures structured upstream HTTP, protocol, or auth failures.
func (*UpstreamError) Error ¶
func (e *UpstreamError) Error() string
Error implements the error interface.
func (*UpstreamError) Unwrap ¶
func (e *UpstreamError) Unwrap() error
Unwrap returns the underlying cause when present.