Help us shape the future of Go! Take the 2026 Go Developer Survey

http

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package http provides the HTTP\Client and HTTP\Request bindings, the outbound HTTP surface a script reaches instead of PHP's curl_* family.

The types a script sees are facades over net/http rather than the net/http types themselves. Registration is not a method allowlist: every exported method and field of the value a constructor returns is reachable from PHP, so embedding *net/http.Request would publish the whole of net/http as methods on HTTP\Request. Each facade therefore holds its net/http value in a named unexported field, and the methods below are the whole surface.

Index

Constants

View Source
const DefaultTimeout = 30 * time.Second

DefaultTimeout is how long a client waits for a response when the script did not say. A request with no deadline at all is the one failure mode a script cannot recover from, so there is no way to ask for one.

Variables

This section is empty.

Functions

func NewRequest

func NewRequest(ctx context.Context, method, url string, body ...string) (*nethttp.Request, error)

NewRequest builds a request from $method and $url, with an optional $body. Building a request sends nothing: pass it to a client with $client->send($request).

The value is a net/http request, so a script reads and writes it the way Go does: $request->method, $request->host, and $request->header->set($name, $value) for headers. Methods are written uppercase, "GET" and "POST"; a lowercase one is upper-cased rather than sent as written, because a server treats the method as case-sensitive and would reject it.

func Register

func Register(rt *runner.Runtime)

Register installs the HTTP\Client and HTTP\Request bindings on rt.

Types

type Client

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

Client is the PHP-visible HTTP\Client. The net/http client is a named unexported field rather than an embedded one, so a script reaches the methods below and nothing else net/http exports.

func NewClient

func NewClient(ctx context.Context, options any) (*Client, error)

NewClient is an HTTP client configured by an associative array of $timeout, $base_url, $follow_redirects, $user_agent, $headers and $insecure. Every key is optional, and `new HTTP\Client` gives a client with a 30 second timeout that follows redirects.

$timeout is in seconds and covers the whole request. $headers are sent with every request the client makes, and a header set on a request replaces the client's. $insecure disables certificate verification, which is for a test server and not for a service. An unrecognised key throws.

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string) (*Response, error)

Get sends a GET request to $url and returns the response.

func (*Client) Parallel

func (c *Client) Parallel(ctx context.Context, requests any) (map[string]*Response, error)

Parallel sends every request in $requests at once and returns the responses under the same keys, so the call takes as long as the slowest request rather than the sum. $requests is an array of HTTP\Request keyed by a name the script chooses, each bounded by the client's timeout.

One request failing does not fail the others and does not throw: that response reports $response->ok() as false and $response->err() as the reason, so a script sees every outcome. A throw means the argument was not an array of requests.

func (*Client) Post

func (c *Client) Post(ctx context.Context, url string, body ...string) (*Response, error)

Post sends a POST request to $url with $body and returns the response.

func (*Client) Send

func (c *Client) Send(ctx context.Context, request *nethttp.Request) (*Response, error)

Send sends $request and returns the response. A transport failure or a timeout throws; an HTTP error status does not, so check $response->status().

type Mux added in v0.6.0

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

Mux is net/http's ServeMux with PHP callables as its handlers, answered on a worker runtime each. It is a facade rather than the ServeMux itself because nothing turns a PHP callable into the func(ResponseWriter, *Request) that HandleFunc declares. docs/use-cases/http-server.md is the surface.

func NewMux added in v0.6.0

func NewMux(rt *runner.Runtime) *Mux

NewMux returns a router with no routes on it.

func (*Mux) Handle added in v0.6.0

func (m *Mux) Handle(pattern string, handler any) error

Handle registers $handler for $pattern, which is ServeMux.HandleFunc's pattern, with {name} segments the handler reads through $r->path_value($name).

$handler is a callable: a closure, a method read off its receiver as $this->fnName, or the name of a declared function. It is called with the response writer and the request, what it echoes reaches the response, and a throw is answered with a 500. docs/use-cases/http-server.md is the contract.

func (*Mux) ServeHTTP added in v0.6.0

func (m *Mux) ServeHTTP(w nethttp.ResponseWriter, r *nethttp.Request)

ServeHTTP answers one request, so a Go host can mount a script's router without the script listening on anything.

func (*Mux) UsePoolForTest added in v0.6.0

func (m *Mux) UsePoolForTest(pool *runner.Pool)

UsePoolForTest installs the workers, for a test sizing them itself. The server calls usePool; this is the same thing with a name a test can reach.

type Response

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

Response is the PHP-visible result of sending a request.

This one is a facade rather than a net/http response, for two reasons. The body is read in full when the response is constructed, because a script has no stream to close and an unread net/http body leaks its connection. And ok() and json() are the two things a script does with a response that net/http has no equivalent for.

func (*Response) Body

func (r *Response) Body() string

Body returns the response body as a string.

func (*Response) Err

func (r *Response) Err() string

Err returns why the request failed, or an empty string when it did not. Only parallel() produces a failed response: send() throws instead, because there is one outcome to report rather than several.

func (*Response) Header

func (r *Response) Header(name string) string

Header returns the value of the named response header, or an empty string when it is not set. Header names are matched case-insensitively.

func (*Response) Headers

func (r *Response) Headers() map[string]string

Headers returns the response headers as an array of name to value. A header sent more than once is joined with ", ".

func (*Response) JSON

func (r *Response) JSON() (any, error)

JSON decodes the response body and returns it as arrays and scalars. It throws when the body is not valid JSON.

func (*Response) OK

func (r *Response) OK() bool

OK reports whether the request got a response with a 2xx status.

func (*Response) Status

func (r *Response) Status() int64

Status returns the HTTP status code. It is an int, so it compares against a literal: $response->status() == 200. A request that never got a response reports 0; see err().

type Server added in v0.6.0

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

Server listens and answers, which is what net/http's Server does and what a ServeMux does not. The vocabulary is Go's: listen(), then shutdown() to let the requests in flight finish, or close() to drop them.

func NewServer added in v0.6.0

func NewServer(rt *runner.Runtime, addr string, handler any, workers ...int64) (*Server, error)

NewServer builds a server for $addr answering through $handler, an HTTP\Mux or anything else that answers a request. Building one listens on nothing: listen() does that.

$workers is how many requests are answered at once, the number of cores when omitted, and the argument after it is how deep the queue behind them is, 1024 when omitted. docs/use-cases/http-server.md is what to size them by.

func (*Server) Addr added in v0.6.0

func (s *Server) Addr() string

addr answers the address the server bound, empty until it has listened.

func (*Server) Close added in v0.6.0

func (s *Server) Close() error

close stops the server at once, dropping whatever was in flight. shutdown() is the one to reach for; this is for a script that has decided the answers no longer matter.

func (*Server) Listen added in v0.6.0

func (s *Server) Listen() (string, error)

listen binds the address and starts answering, and returns the address it bound. It does not block: the script goes on, and calls wait() when it has nothing left to do.

func (*Server) Shutdown added in v0.6.0

func (s *Server) Shutdown()

Shutdown is http.Server.Shutdown with a second of grace, then Close.

It answers nothing and throws nothing: it is written into a register_shutdown_function callback, which runs when the script has already ended and has nowhere to put a failure. Calling it twice, or without having listened, is not an error either.

func (*Server) Wait added in v0.6.0

func (s *Server) Wait()

wait blocks until the script runs out of time, or the client that started it goes away, and returns. A script with no limit and no client waits until the host's own context ends, which for a command line run is never.

Nothing runs after it in a script the time limit ended: the limit is a fatal, as php's is, so the next statement is where the script stops. Cleanup belongs in a register_shutdown_function callback, which runs with the clock off.

Jump to

Keyboard shortcuts

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