Documentation
¶
Overview ¶
Package nethttp is a pure-Go (no cgo) reimplementation of the deterministic, interpreter-independent core of Ruby's Net::HTTP: the HTTP/1.1 *message* codec. It builds request bytes exactly as MRI 4.0.5 writes them to the socket and parses a raw response byte stream into MRI's Net::HTTPResponse subclass model — without any Ruby runtime, and without performing any I/O itself.
The TCP socket and TLS are a host-side seam: the host supplies the byte transport, this library supplies build-request-bytes + parse-response-bytes + the header / response object model.
Index ¶
- func EncodeWWWForm(pairs [][2]string) string
- func EncodeWWWFormComponent(s string) string
- type BadResponse
- type Header
- func (h *Header) AddField(key, val string) error
- func (h *Header) Chunked() bool
- func (h *Header) ConnectionClose() bool
- func (h *Header) ConnectionKeepAlive() bool
- func (h *Header) ContentLength() (int, bool, error)
- func (h *Header) ContentType() string
- func (h *Header) Delete(key string) []string
- func (h *Header) EachCapitalized(fn func(key, value string))
- func (h *Header) EachHeader(fn func(key, value string))
- func (h *Header) Get(key string) (string, bool)
- func (h *Header) GetFields(key string) []string
- func (h *Header) Key(key string) bool
- func (h *Header) MainType() string
- func (h *Header) Set(key, val string) error
- func (h *Header) SetContentLength(n int)
- func (h *Header) SetContentType(typ string, params ...[2]string)
- func (h *Header) SubType() string
- type HeaderSyntaxError
- type Request
- func (r *Request) BasicAuth(account, password string)
- func (r *Request) Body() []byte
- func (r *Request) Bytes(version string) ([]byte, error)
- func (r *Request) Method() string
- func (r *Request) Path() string
- func (r *Request) ProxyBasicAuth(account, password string)
- func (r *Request) RequestBodyPermitted() bool
- func (r *Request) ResponseBodyPermitted() bool
- func (r *Request) SetBody(body []byte)
- func (r *Request) SetFormData(params [][2]string, sep ...string)
- type Response
- func (r *Response) Body() []byte
- func (r *Response) BodyPermitted() bool
- func (r *Response) Category() string
- func (r *Response) Class() string
- func (r *Response) Code() string
- func (r *Response) HTTPVersion() string
- func (r *Response) IsClientError() bool
- func (r *Response) IsInformation() bool
- func (r *Response) IsRedirection() bool
- func (r *Response) IsServerError() bool
- func (r *Response) IsSuccess() bool
- func (r *Response) Message() string
- func (r *Response) ReadBody() []byte
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EncodeWWWForm ¶
EncodeWWWForm joins key/value pairs into an application/x-www-form-urlencoded query string, mirroring URI.encode_www_form. Each key and value is encoded with EncodeWWWFormComponent and joined with '='; pairs are joined with '&'. A pair whose value is the empty string is emitted as bare "key" only when it was given as a value-less entry; here every pair has a value, so an empty value yields "key=" — matching MRI for the [][2]string form.
func EncodeWWWFormComponent ¶
EncodeWWWFormComponent percent-encodes s as URI.encode_www_form_component does: unreserved bytes pass through, space becomes '+', and every other byte becomes %XX with uppercase hex.
Types ¶
type BadResponse ¶
type BadResponse struct{ Msg string }
BadResponse corresponds to Net::HTTPBadResponse: a protocol-level error while parsing the status line, a header line, or a chunk-size line.
func (*BadResponse) Error ¶
func (e *BadResponse) Error() string
type Header ¶
type Header struct {
// contains filtered or unexported fields
}
Header is the field store shared by requests and responses — the pure-Go port of the Net::HTTPHeader mixin. Like MRI it stores keys downcased, each mapping to an ordered list of raw string values, and preserves first-insertion order.
func (*Header) AddField ¶
AddField appends val to key, preserving any existing values (Net::HTTPHeader#add_field).
func (*Header) Chunked ¶
Chunked reports whether Transfer-Encoding requests chunked encoding (Net::HTTPHeader#chunked?). MRI uses /(?:\A|[^\-\w])chunked(?![\-\w])/i.
func (*Header) ConnectionClose ¶
ConnectionClose reports whether Connection (or Proxy-Connection) requests the connection be closed (Net::HTTPHeader#connection_close?).
func (*Header) ConnectionKeepAlive ¶
ConnectionKeepAlive reports whether Connection (or Proxy-Connection) requests keep-alive (Net::HTTPHeader#connection_keep_alive?).
func (*Header) ContentLength ¶
ContentLength returns the parsed Content-Length, ok=false when the field is absent (Net::HTTPHeader#content_length). A present-but-malformed value yields a *HeaderSyntaxError.
func (*Header) ContentType ¶
ContentType returns "main/sub" (or just "main", or "") mirroring Net::HTTPHeader#content_type.
func (*Header) Delete ¶
Delete removes key and returns its prior value list (Net::HTTPHeader#delete).
func (*Header) EachCapitalized ¶
EachCapitalized calls fn for each field in insertion order with the canonicalised key and the joined value (Net::HTTPHeader#each_capitalized) — this is the order and casing written to the wire by write_header.
func (*Header) EachHeader ¶
EachHeader calls fn for each field in insertion order with the downcased key and the values joined by ", " (Net::HTTPHeader#each_header / #each).
func (*Header) Get ¶
Get returns the field for key joined by ", " (Net::HTTPHeader#[]), and "" with ok=false when the field is absent.
func (*Header) GetFields ¶
GetFields returns a copy of the raw value list for key, or nil if absent (Net::HTTPHeader#get_fields).
func (*Header) MainType ¶
MainType returns the part of Content-Type before '/' (Net::HTTPHeader#main_type), or "" if there is no Content-Type.
func (*Header) Set ¶
Set replaces the field for key (Net::HTTPHeader#[]=). A nil-equivalent is expressed by Delete; Set always stores a value.
func (*Header) SetContentLength ¶
SetContentLength sets Content-Length to len (Net::HTTPHeader#content_length=).
func (*Header) SetContentType ¶
SetContentType sets Content-Type to type plus "; k=v" params in the given order (Net::HTTPHeader#set_content_type / #content_type=).
type HeaderSyntaxError ¶
type HeaderSyntaxError struct{ Msg string }
HeaderSyntaxError corresponds to Net::HTTPHeaderSyntaxError: a malformed Content-Length / Content-Range field value.
func (*HeaderSyntaxError) Error ¶
func (e *HeaderSyntaxError) Error() string
type Request ¶
type Request struct {
*Header
// contains filtered or unexported fields
}
Request is the pure-Go port of Net::HTTPGenericRequest (the parent of every Net::HTTP::Get/Post/... request). It carries the method, the request path, the header model, and an optional body, and serialises itself to the exact HTTP/1.1 request bytes MRI writes to the socket.
func NewRequest ¶
NewRequest builds a request for the given uppercase method ("GET", "POST", …) targeting path. path is the request-target as written on the request line (e.g. URI#request_uri: "/p?q=1"); host, when non-empty, supplies the default Host header (URI#authority). initHeader is the ordered list of caller-supplied initial fields; MRI preserves the initheader hash's insertion order on the wire, so the order of these pairs is the order they are emitted.
It mirrors Net::HTTPGenericRequest#initialize: seeding Accept-Encoding (unless the caller's initheader already set accept-encoding or range) appended after the caller's fields, then the Accept, User-Agent and Host defaults.
func (*Request) BasicAuth ¶
BasicAuth sets the Authorization header to a Basic credential for account/password (Net::HTTPHeader#basic_auth / #basic_encode).
func (*Request) Bytes ¶
Bytes returns the exact HTTP/1.1 request byte stream MRI writes to the socket for this request at the given version (e.g. "1.1"). This is the host-side seam's payload: write these bytes to the connected socket.
It mirrors Net::HTTPGenericRequest#exec: when a body is present (explicitly, or defaulted to "" for a body-permitted method), it sets Content-Length, deletes Transfer-Encoding, writes the header block, then the body — exactly send_request_with_body. Otherwise it writes the header block alone.
func (*Request) ProxyBasicAuth ¶
ProxyBasicAuth sets the Proxy-Authorization header (Net::HTTPHeader#proxy_basic_auth).
func (*Request) RequestBodyPermitted ¶
RequestBodyPermitted reports whether the request may carry a body (Net::HTTPGenericRequest#request_body_permitted?).
func (*Request) ResponseBodyPermitted ¶
ResponseBodyPermitted reports whether a response to this request may carry a body (Net::HTTPGenericRequest#response_body_permitted?).
func (*Request) SetFormData ¶
SetFormData sets an application/x-www-form-urlencoded body from params, mirroring Net::HTTPHeader#set_form_data: it URL-encodes the pairs (default separator "&"), sets the body, and sets Content-Type.
type Response ¶
type Response struct {
*Header
// contains filtered or unexported fields
}
Response is the pure-Go port of Net::HTTPResponse: the parsed status line, the response header model (it embeds *Header, so all Net::HTTPHeader getters apply), the decoded body, and the selected subclass identity. The subclass is not a Go type but the Class / Category names MRI would have instantiated (e.g. "HTTPOK" in category "HTTPSuccess"), exposed via Class, Category and the kind helpers.
func ParseResponse ¶
ParseResponse parses a complete raw HTTP/1.1 response byte stream into a Response: the status line, the header block (with obs-fold continuation and multi-value fields), and the body decoded per Content-Length or chunked Transfer-Encoding. It is the inverse of Request.Bytes and the payload side of the host's read seam: hand it everything read from the socket for one response.
It mirrors Net::HTTPResponse.read_new (status line + headers) followed by read_body (body framing). A response whose status permits no body (HAS_BODY false: 1xx, 204, 205, 304) yields a nil body and ignores any trailing bytes, exactly as MRI does.
func (*Response) Body ¶
Body returns the decoded response body (Net::HTTPResponse#body), or nil for a status that permits no body. It is populated by ReadBody during ParseResponse.
func (*Response) BodyPermitted ¶
BodyPermitted reports whether the status permits a body (Net::HTTPResponse.body_permitted? / HAS_BODY).
func (*Response) Category ¶
Category returns the response category class name (e.g. "HTTPSuccess", "HTTPClientError") — the per-first-digit parent class.
func (*Response) Class ¶
Class returns the Net::HTTPResponse subclass name MRI selects for this code (e.g. "HTTPOK", "HTTPNotFound", "HTTPUnknownResponse").
func (*Response) HTTPVersion ¶
HTTPVersion returns the server's HTTP version string (Net::HTTPResponse#http_version).
func (*Response) IsClientError ¶
IsClientError reports a 4xx response (kind_of? Net::HTTPClientError).
func (*Response) IsInformation ¶
IsInformation reports a 1xx response (kind_of? Net::HTTPInformation).
func (*Response) IsRedirection ¶
IsRedirection reports a 3xx response (kind_of? Net::HTTPRedirection).
func (*Response) IsServerError ¶
IsServerError reports a 5xx response (kind_of? Net::HTTPServerError).
