easyhttp

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2025 License: MIT Imports: 21 Imported by: 0

README

HTTPGolang

Go HTTP implementation as defined in protocol RFC 9112.

For now, it only supports HTTP 1.1

Documentation

Index

Constants

View Source
const (
	SAME_SITE_DEFAULT = iota
	SAME_SITE_LAX
	SAME_SITE_STRICT
	SAME_SITE_NONE
)
View Source
const (
	MethodGet     = "GET"
	MethodHead    = "HEAD"
	MethodPost    = "POST"
	MethodPut     = "PUT"
	MethodPatch   = "PATCH"
	MethodDelete  = "DELETE"
	MethodConnect = "CONNECT"
	MethodOptions = "OPTIONS"
	MethodTrace   = "TRACE"
)

HTTP Methods

View Source
const (
	STATUS_CONTINUE                      = 100
	STATUS_SWITCHING_PROTOCOL            = 101
	STATUS_OK                            = 200
	STATUS_CREATED                       = 201
	STATUS_ACCEPTED                      = 202
	STATUS_NON_AUTHORATIVE_INFORMATION   = 203
	STATUS_NO_CONTENT                    = 204
	STATUS_RESET_CONTENT                 = 205
	STATUS_PARTIAL_CONTENT               = 206
	STATUS_MULTIPLE_CHOICES              = 300
	STATUS_MOVED_PERMANENTLY             = 301
	STATUS_FOUND                         = 302
	STATUS_SEE_OTHER                     = 303
	STATUS_NOT_MODIFIED                  = 304
	STATUS_USE_PROXY                     = 305
	STATUS_UNUSED                        = 306
	STATUS_TEMPORARY_REDIRECT            = 307
	STATUS_PERMANENT_REDIRECT            = 308
	STATUS_BAD_REQUEST                   = 400
	STATUS_UNAUTHORIZED                  = 401
	STATUS_PAYMENT_REQUIRED              = 402
	STATUS_FORBIDDEN                     = 403
	STATUS_NOT_FOUND                     = 404
	STATUS_METHOD_NOT_ALLOWED            = 405
	STATUS_NOT_ACCEPTABLE                = 406
	STATUS_PROXY_AUTHENTICATION_REQUIRED = 407
	STATUS_REQUEST_TIMEOUT               = 408
	STATUS_CONFLICT                      = 409
	STATUS_GONE                          = 410
	STATUS_LENGTH_REQUIRED               = 411
	STATUS_PRECONDITION_FAILED           = 412
	STATUS_CONTENT_TOO_LARGE             = 413
	STATUS_URI_TOO_LONG                  = 414
	STATUS_UNSUPPORTED_MEDIA_TYPE        = 415
	STATUS_RANGE_NOT_SATISFIABLE         = 416
	STATUS_MISDIRECTED_REQUEST           = 421
	STATUS_UNPROCESSABLE_CONTENT         = 422
	STATUS_UPGRADE_REQUIRED              = 426
	STATUS_INTERNAL_ERROR                = 500
	STATUS_NOT_IMPLEMENTED               = 501
	STATUS_BAD_GATEWAY                   = 502
	STATUS_SERVICE_UNAVAILABLE           = 503
	STATUS_GATEWAY_TIMEOUT               = 504
	STATUS_HTTP_VERSION_NOT_SUPPORTED    = 505
)

HTTP Status

View Source
const KEEP_ALIVE_TIMEOUT = 5

Variables

View Source
var ErrBadRequest = errors.New("bad request")
View Source
var ErrClientTimeout = errors.New("client timeout")
View Source
var ErrInternalError = errors.New("internal error")
View Source
var ErrInvalidLength = errors.New("invalid content length")
View Source
var ErrInvalidMethod = errors.New("invalid method")
View Source
var ErrMethodNotAllowed = errors.New("method not allowed")
View Source
var ErrNotFound = errors.New("not found")
View Source
var ErrRequestTimeout = errors.New("request timeout")
View Source
var ErrVersionNotSupported = errors.New("version not supported")

Functions

func NewHTTPClient

func NewHTTPClient() httpClient

Types

type ClientChunkFunction

type ClientChunkFunction func([]byte, *ClientHTTPResponse) bool

Function that responds to HTTP Response Chunk on Client Requests

type ClientHTTPRequest

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

func NewRequest

func NewRequest(uri string) (ClientHTTPRequest, error)

func NewRequestWithBody

func NewRequestWithBody(uri string, body []byte) (ClientHTTPRequest, error)

func (*ClientHTTPRequest) AddHeader

func (r *ClientHTTPRequest) AddHeader(key string, value string)

func (*ClientHTTPRequest) Chunked

func (r *ClientHTTPRequest) Chunked()

func (*ClientHTTPRequest) CloseConnection

func (r *ClientHTTPRequest) CloseConnection()

func (*ClientHTTPRequest) Done

func (r *ClientHTTPRequest) Done()

func (*ClientHTTPRequest) GetHeader

func (r *ClientHTTPRequest) GetHeader(key string) []string

func (*ClientHTTPRequest) HasHeaderValue

func (r *ClientHTTPRequest) HasHeaderValue(key string, value string) bool

func (*ClientHTTPRequest) Headers

func (r *ClientHTTPRequest) Headers() Headers

func (*ClientHTTPRequest) OnChunkFunction

func (r *ClientHTTPRequest) OnChunkFunction(onChunk ClientChunkFunction)

func (*ClientHTTPRequest) SendChunk

func (r *ClientHTTPRequest) SendChunk(chunk []byte)

func (*ClientHTTPRequest) SetBody

func (r *ClientHTTPRequest) SetBody(body []byte)

func (*ClientHTTPRequest) SetHeader

func (r *ClientHTTPRequest) SetHeader(key string, value string)

func (*ClientHTTPRequest) SetTimeout

func (r *ClientHTTPRequest) SetTimeout(timeout_ms time.Duration)

func (*ClientHTTPRequest) SetURI

func (r *ClientHTTPRequest) SetURI(uri string) error

func (*ClientHTTPRequest) SetVersion

func (r *ClientHTTPRequest) SetVersion(version string) error

func (*ClientHTTPRequest) Version

func (r *ClientHTTPRequest) Version() string

type ClientHTTPResponse

type ClientHTTPResponse struct {
	StatusCode int
	// contains filtered or unexported fields
}

func (*ClientHTTPResponse) AddHeader

func (r *ClientHTTPResponse) AddHeader(key string, value string)

func (*ClientHTTPResponse) Cookies

func (r *ClientHTTPResponse) Cookies() []*Cookie

func (*ClientHTTPResponse) ExistsHeader

func (r *ClientHTTPResponse) ExistsHeader(key string) bool

func (*ClientHTTPResponse) GetBody

func (r *ClientHTTPResponse) GetBody() io.Reader

func (*ClientHTTPResponse) GetHeader

func (r *ClientHTTPResponse) GetHeader(key string) []string

func (*ClientHTTPResponse) HasBody

func (r *ClientHTTPResponse) HasBody() bool

func (*ClientHTTPResponse) HasHeaderValue

func (r *ClientHTTPResponse) HasHeaderValue(key string, value string) bool

func (*ClientHTTPResponse) Headers

func (r *ClientHTTPResponse) Headers() Headers

func (*ClientHTTPResponse) Read

func (r *ClientHTTPResponse) Read(buffer []byte) (int, error)

func (*ClientHTTPResponse) SetHeader

func (r *ClientHTTPResponse) SetHeader(key string, value string)

func (*ClientHTTPResponse) Version

func (r *ClientHTTPResponse) Version() string
type Cookie struct {
	Name     string
	Value    string
	Expires  time.Time
	MaxAge   int
	Domain   string
	Path     string
	Secure   bool
	HTTPOnly bool
	SameSite SameSite
	// contains filtered or unexported fields
}

func (*Cookie) String

func (c *Cookie) String() string

type CookieStorage

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

func (*CookieStorage) Cookies

func (cs *CookieStorage) Cookies(url *url.URL) []*Cookie

func (*CookieStorage) SetCookies

func (cs *CookieStorage) SetCookies(url *url.URL, cookies []*Cookie)

type HTTPServer

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

Struct that represent a HTTP Server

func NewHTTPServer

func NewHTTPServer(address string) (*HTTPServer, error)

Create a HTTP Server listening in address

func NewTLSHTTPServer

func NewTLSHTTPServer(address string, config *tls.Config) (*HTTPServer, error)

Create a HTTPS Server listening in address

func (*HTTPServer) Close

func (s *HTTPServer) Close() error

Closes server immediatly

func (*HTTPServer) GracefullShutdown

func (s *HTTPServer) GracefullShutdown() error

Gracefully shutdown server waiting for open connections to finish

func (*HTTPServer) HandleDELETE

func (s *HTTPServer) HandleDELETE(uriPattern string, handlerFunction ResponseFunction)

Add Handler for DELETE method to given uri pattern

func (*HTTPServer) HandleDELETEWithOptions

func (s *HTTPServer) HandleDELETEWithOptions(uriPattern string, handlerFunction ResponseFunction, options HandlerOptions)

Add Handler for DELETE method to given uri pattern with additional options

func (*HTTPServer) HandleGET

func (s *HTTPServer) HandleGET(uriPattern string, handlerFunction ResponseFunction)

Add Handler for GET method to given uri pattern

func (*HTTPServer) HandleGETWithOptions

func (s *HTTPServer) HandleGETWithOptions(uriPattern string, handlerFunction ResponseFunction, options HandlerOptions)

Add Handler for GET method to given uri pattern with additional options

func (*HTTPServer) HandlePATCH

func (s *HTTPServer) HandlePATCH(uriPattern string, handlerFunction ResponseFunction)

Add Handler for PATCH method to given uri pattern

func (*HTTPServer) HandlePATCHWithOptions

func (s *HTTPServer) HandlePATCHWithOptions(uriPattern string, handlerFunction ResponseFunction, options HandlerOptions)

Add Handler for PATCH method to given uri pattern with additional options

func (*HTTPServer) HandlePOST

func (s *HTTPServer) HandlePOST(uriPattern string, handlerFunction ResponseFunction)

Add Handler for POST method to given uri pattern

func (*HTTPServer) HandlePOSTWithOptions

func (s *HTTPServer) HandlePOSTWithOptions(uriPattern string, handlerFunction ResponseFunction, options HandlerOptions)

Add Handler for POST method to given uri pattern with additional options

func (*HTTPServer) HandlePUT

func (s *HTTPServer) HandlePUT(uriPattern string, handlerFunction ResponseFunction)

Add Handler for PUT method to given uri pattern

func (*HTTPServer) HandlePUTWithOptions

func (s *HTTPServer) HandlePUTWithOptions(uriPattern string, handlerFunction ResponseFunction, options HandlerOptions)

Add Handler for PUT method to given uri pattern with additional options

func (*HTTPServer) Run

func (s *HTTPServer) Run()

Start listening to requests. This method blocks until server is closed

func (*HTTPServer) SetTimeout

func (s *HTTPServer) SetTimeout(timeout_ms time.Duration)

Function that sets server request timeout

type HandlerOptions

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

Additional Options for Handlers

type Headers

type Headers map[string][]string

HTTP Headers

type ResponseFunction

type ResponseFunction func(ServerHTTPRequest, *ServerHTTPResponse)

Function that responds to HTTP Requests

func FileServer

func FileServer(fileName string) ResponseFunction

Response Function that responds to a request with the file indicated by fileName

func FileServerFromPath

func FileServerFromPath(filePrefix string) ResponseFunction

Response Function that responds to a request with the file indicated by filePrefix + lastPathElement

func PermaRedirect

func PermaRedirect(redirectURI string) ResponseFunction

Response Function that responds to a request with a perma redirect to given redirectURI

type SameSite

type SameSite int

type ServerChunkFunction

type ServerChunkFunction func([]byte, ServerHTTPRequest, *ServerHTTPResponse) bool

Function that responds to HTTP Request Chunk

type ServerHTTPRequest

type ServerHTTPRequest struct {
	Body []byte
	// contains filtered or unexported fields
}

func (*ServerHTTPRequest) AddHeader

func (r *ServerHTTPRequest) AddHeader(key string, value string)

func (*ServerHTTPRequest) Chunked

func (r *ServerHTTPRequest) Chunked()

func (*ServerHTTPRequest) Cookies

func (r *ServerHTTPRequest) Cookies() map[string]string

func (*ServerHTTPRequest) Done

func (r *ServerHTTPRequest) Done()

func (*ServerHTTPRequest) ExistsHeader

func (r *ServerHTTPRequest) ExistsHeader(key string) bool

func (*ServerHTTPRequest) GetHeader

func (r *ServerHTTPRequest) GetHeader(key string) []string

func (*ServerHTTPRequest) HasHeaderValue

func (r *ServerHTTPRequest) HasHeaderValue(key string, value string) bool

func (*ServerHTTPRequest) Headers

func (r *ServerHTTPRequest) Headers() Headers

func (*ServerHTTPRequest) ParseForm

func (r *ServerHTTPRequest) ParseForm() (map[string]string, error)

func (*ServerHTTPRequest) Path

func (r *ServerHTTPRequest) Path() string

func (*ServerHTTPRequest) QueryValues

func (r *ServerHTTPRequest) QueryValues() url.Values

func (*ServerHTTPRequest) SendChunk

func (r *ServerHTTPRequest) SendChunk(chunk []byte)

func (*ServerHTTPRequest) SetHeader

func (r *ServerHTTPRequest) SetHeader(key string, value string)

func (*ServerHTTPRequest) SetVersion

func (r *ServerHTTPRequest) SetVersion(version string) error

func (*ServerHTTPRequest) Version

func (r *ServerHTTPRequest) Version() string

type ServerHTTPResponse

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

func (*ServerHTTPResponse) AddHeader

func (r *ServerHTTPResponse) AddHeader(key string, value string)

func (*ServerHTTPResponse) ExistsHeader

func (r *ServerHTTPResponse) ExistsHeader(key string) bool

func (*ServerHTTPResponse) GetHeader

func (r *ServerHTTPResponse) GetHeader(key string) []string

func (*ServerHTTPResponse) HasBody

func (r *ServerHTTPResponse) HasBody() bool

func (*ServerHTTPResponse) Headers

func (r *ServerHTTPResponse) Headers() Headers

func (*ServerHTTPResponse) Read

func (r *ServerHTTPResponse) Read(buffer []byte) (int, error)

func (*ServerHTTPResponse) SendChunk

func (r *ServerHTTPResponse) SendChunk() (int, error)

func (*ServerHTTPResponse) SendFile

func (r *ServerHTTPResponse) SendFile(fileName string) error

func (*ServerHTTPResponse) SetCookie

func (r *ServerHTTPResponse) SetCookie(cookie *Cookie)

func (*ServerHTTPResponse) SetHeader

func (r *ServerHTTPResponse) SetHeader(key string, value string)

func (*ServerHTTPResponse) SetStatus

func (r *ServerHTTPResponse) SetStatus(status int)

func (*ServerHTTPResponse) Write

func (r *ServerHTTPResponse) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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