// TINYGO: The following is copied and modified from Go 1.26.2 official implementation. // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // HTTP client. See RFC 7230 through 7235. // // This is the high-level Client interface. // The low-level implementation is in transport.go. package http import ( "bufio" "crypto/tls" "encoding/base64" "errors" "fmt" "io" "net" "net/http/internal/ascii" "net/url" "strings" "time" "golang.org/x/net/http/httpguts" ) // A Client is an HTTP client. Its zero value ([DefaultClient]) is a // usable client that uses [DefaultTransport]. // // The [Client.Transport] typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. // // A Client is higher-level than a [RoundTripper] (such as [Transport]) // and additionally handles HTTP details such as cookies and // redirects. // // When following redirects, the Client will forward all headers set on the // initial [Request] except: // // - when forwarding sensitive headers like "Authorization", // "WWW-Authenticate", and "Cookie" to untrusted targets. // These headers will be ignored when following a redirect to a domain // that is not a subdomain match or exact match of the initial domain. // For example, a redirect from "foo.com" to either "foo.com" or "sub.foo.com" // will forward the sensitive headers, but a redirect to "bar.com" will not. // - when forwarding the "Cookie" header with a non-nil cookie Jar. // Since each redirect may mutate the state of the cookie jar, // a redirect may possibly alter a cookie set in the initial request. // When forwarding the "Cookie" header, any mutated cookies will be omitted, // with the expectation that the Jar will insert those mutated cookies // with the updated values (assuming the origin matches). // If Jar is nil, the initial cookies are forwarded without change. type Client struct { // Transport specifies the mechanism by which individual // HTTP requests are made. // If nil, DefaultTransport is used. Transport RoundTripper // CheckRedirect specifies the policy for handling redirects. // If CheckRedirect is not nil, the client calls it before // following an HTTP redirect. The arguments req and via are // the upcoming request and the requests made already, oldest // first. If CheckRedirect returns an error, the Client's Get // method returns both the previous Response (with its Body // closed) and CheckRedirect's error (wrapped in a url.Error) // instead of issuing the Request req. // As a special case, if CheckRedirect returns ErrUseLastResponse, // then the most recent response is returned with its body // unclosed, along with a nil error. // // If CheckRedirect is nil, the Client uses its default policy, // which is to stop after 10 consecutive requests. CheckRedirect func(req *Request, via []*Request) error // Jar specifies the cookie jar. // // The Jar is used to insert relevant cookies into every // outbound Request and is updated with the cookie values // of every inbound Response. The Jar is consulted for every // redirect that the Client follows. // // If Jar is nil, cookies are only sent if they are explicitly // set on the Request. Jar CookieJar // Timeout specifies a time limit for requests made by this // Client. The timeout includes connection time, any // redirects, and reading the response body. The timer remains // running after Get, Head, Post, or Do return and will // interrupt reading of the Response.Body. // // A Timeout of zero means no timeout. // // The Client cancels requests to the underlying Transport // as if the Request's Context ended. // // For compatibility, the Client will also use the deprecated // CancelRequest method on Transport if found. New // RoundTripper implementations should use the Request's Context // for cancellation instead of implementing CancelRequest. Timeout time.Duration } // DefaultClient is the default [Client] and is used by [Get], [Head], and [Post]. var DefaultClient = &Client{} // RoundTripper is an interface representing the ability to execute a // single HTTP transaction, obtaining the [Response] for a given [Request]. // // A RoundTripper must be safe for concurrent use by multiple // goroutines. type RoundTripper interface { // RoundTrip executes a single HTTP transaction, returning // a Response for the provided Request. // // RoundTrip should not attempt to interpret the response. In // particular, RoundTrip must return err == nil if it obtained // a response, regardless of the response's HTTP status code. // A non-nil err should be reserved for failure to obtain a // response. Similarly, RoundTrip should not attempt to // handle higher-level protocol details such as redirects, // authentication, or cookies. // // RoundTrip should not modify the request, except for // consuming and closing the Request's Body. RoundTrip may // read fields of the request in a separate goroutine. Callers // should not mutate or reuse the request until the Response's // Body has been closed. // // RoundTrip must always close the body, including on errors, // but depending on the implementation may do so in a separate // goroutine even after RoundTrip returns. This means that // callers wanting to reuse the body for subsequent requests // must arrange to wait for the Close call before doing so. // // The Request's URL and Header fields must be initialized. RoundTrip(*Request) (*Response, error) } // didTimeout is non-nil only if err != nil. func (c *Client) send(req *Request, deadline time.Time) (resp *Response, didTimeout func() bool, err error) { cookieURL := req.URL if req.Host != "" { cookieURL = cloneURL(cookieURL) cookieURL.Host = req.Host } if c.Jar != nil { for _, cookie := range c.Jar.Cookies(cookieURL) { req.AddCookie(cookie) } } resp, didTimeout, err = send(req, deadline) if err != nil { return nil, didTimeout, err } if c.Jar != nil { if rc := resp.Cookies(); len(rc) > 0 { c.Jar.SetCookies(cookieURL, rc) } } return resp, nil, nil } func (c *Client) deadline() time.Time { if c.Timeout > 0 { return time.Now().Add(c.Timeout) } return time.Time{} } // send issues an HTTP request. // Caller should close resp.Body when done reading from it. func send(req *Request, deadline time.Time) (resp *Response, didTimeout func() bool, err error) { // TINYGO: Removed round tripper if req.URL == nil { req.closeBody() return nil, alwaysFalse, errors.New("http: nil Request.URL") } if req.RequestURI != "" { req.closeBody() return nil, alwaysFalse, errors.New("http: Request.RequestURI can't be set in client requests") } // TINYGO: Removed forkReq stuff // Most the callers of send (Get, Post, et al) don't need // Headers, leaving it uninitialized. We guarantee to the // Transport that this has been initialized, though. if req.Header == nil { req.Header = make(Header) } if u := req.URL.User; u != nil && req.Header.Get("Authorization") == "" { username := u.Username() password, _ := u.Password() req.Header.Set("Authorization", "Basic "+basicAuth(username, password)) } resp, err = roundTrip(req) if err != nil { // TINYGO: Remove TLS error check return nil, didTimeout, err } if resp == nil { return nil, didTimeout, fmt.Errorf("http: sendit returned a nil *Response with a nil error") } // TINYGO: Skip check for resp.Body == nil since we'll set it in roundTrip return resp, nil, nil } func roundTrip(req *Request) (*Response, error) { // TINYGO: This is an approximation of Transport.roudTrip() if req.URL == nil { req.closeBody() return nil, errors.New("http: nil Request.URL") } if req.Header == nil { req.closeBody() return nil, errors.New("http: nil Request.Header") } scheme := req.URL.Scheme isHTTP := scheme == "http" || scheme == "https" if isHTTP { for k, vv := range req.Header { if !httpguts.ValidHeaderFieldName(k) { req.closeBody() return nil, fmt.Errorf("net/http: invalid header field name %q", k) } for _, v := range vv { if !httpguts.ValidHeaderFieldValue(v) { req.closeBody() // Don't include the value in the error, because it may be sensitive. return nil, fmt.Errorf("net/http: invalid header field value for %q", k) } } } } // TINYGO: Skipping alternate round tripper if !isHTTP { req.closeBody() return nil, badStringError("unsupported protocol scheme", scheme) } if req.Method != "" && !validMethod(req.Method) { req.closeBody() return nil, fmt.Errorf("net/http: invalid method %q", req.Method) } if req.URL.Host == "" { req.closeBody() return nil, errors.New("http: no Host in request URL") } // TINYGO: From here on just brute force dial a connection, // TINYGO: send the request, read and return the response. // TINYGO: The connection is closed when resp body is closed. var conn net.Conn var err error host := req.Host missingPort := !strings.Contains(host, ":") switch scheme { case "http": if missingPort { host = host + ":80" } conn, err = net.Dial("tcp", host) case "https": if missingPort { host = host + ":443" } conn, err = tls.Dial("tcp", host, nil) } if err != nil { req.closeBody() return nil, err } // TINYGO: TODO handle timeouts writer := bufio.NewWriter(conn) if err = req.Write(writer); err != nil { req.closeBody() return nil, err } req.closeBody() if err = writer.Flush(); err != nil { return nil, err } req.onEOF = func() { conn.Close() } reader := bufio.NewReader(conn) return ReadResponse(reader, req) } // See 2 (end of page 4) https://www.ietf.org/rfc/rfc2617.txt // "To receive authorization, the client sends the userid and password, // separated by a single colon (":") character, within a base64 // encoded string in the credentials." // It is not meant to be urlencoded. func basicAuth(username, password string) string { auth := username + ":" + password return base64.StdEncoding.EncodeToString([]byte(auth)) } // Get issues a GET to the specified URL. If the response is one of // the following redirect codes, Get follows the redirect, up to a // maximum of 10 redirects: // // 301 (Moved Permanently) // 302 (Found) // 303 (See Other) // 307 (Temporary Redirect) // 308 (Permanent Redirect) // // An error is returned if there were too many redirects or if there // was an HTTP protocol error. A non-2xx response doesn't cause an // error. Any returned error will be of type [*url.Error]. The url.Error // value's Timeout method will report true if the request timed out. // // When err is nil, resp always contains a non-nil resp.Body. // Caller should close resp.Body when done reading from it. // // Get is a wrapper around DefaultClient.Get. // // To make a request with custom headers, use [NewRequest] and // DefaultClient.Do. // // To make a request with a specified context.Context, use [NewRequestWithContext] // and DefaultClient.Do. func Get(url string) (resp *Response, err error) { return DefaultClient.Get(url) } // Get issues a GET to the specified URL. If the response is one of the // following redirect codes, Get follows the redirect after calling the // [Client.CheckRedirect] function: // // 301 (Moved Permanently) // 302 (Found) // 303 (See Other) // 307 (Temporary Redirect) // 308 (Permanent Redirect) // // An error is returned if the [Client.CheckRedirect] function fails // or if there was an HTTP protocol error. A non-2xx response doesn't // cause an error. Any returned error will be of type [*url.Error]. The // url.Error value's Timeout method will report true if the request // timed out. // // When err is nil, resp always contains a non-nil resp.Body. // Caller should close resp.Body when done reading from it. // // To make a request with custom headers, use [NewRequest] and [Client.Do]. // // To make a request with a specified context.Context, use [NewRequestWithContext] // and Client.Do. func (c *Client) Get(url string) (resp *Response, err error) { req, err := NewRequest("GET", url, nil) if err != nil { return nil, err } return c.Do(req) } func alwaysFalse() bool { return false } // urlErrorOp returns the (*url.Error).Op value to use for the // provided (*Request).Method value. func urlErrorOp(method string) string { if method == "" { return "Get" } if lowerMethod, ok := ascii.ToLower(method); ok { return method[:1] + lowerMethod[1:] } return method } // Do sends an HTTP request and returns an HTTP response, following // policy (such as redirects, cookies, auth) as configured on the // client. // // An error is returned if caused by client policy (such as // CheckRedirect), or failure to speak HTTP (such as a network // connectivity problem). A non-2xx status code doesn't cause an // error. // // If the returned error is nil, the [Response] will contain a non-nil // Body which the user is expected to close. If the Body is not both // read to EOF and closed, the [Client]'s underlying [RoundTripper] // (typically [Transport]) may not be able to re-use a persistent TCP // connection to the server for a subsequent "keep-alive" request. // // The request Body, if non-nil, will be closed by the underlying // Transport, even on errors. The Body may be closed asynchronously after // Do returns. // // On error, any Response can be ignored. A non-nil Response with a // non-nil error only occurs when CheckRedirect fails, and even then // the returned [Response.Body] is already closed. // // Generally [Get], [Post], or [PostForm] will be used instead of Do. // // If the server replies with a redirect, the Client first uses the // CheckRedirect function to determine whether the redirect should be // followed. If permitted, a 301, 302, or 303 redirect causes // subsequent requests to use HTTP method GET // (or HEAD if the original request was HEAD), with no body. // A 307 or 308 redirect preserves the original HTTP method and body, // provided that the [Request.GetBody] function is defined. // The [NewRequest] function automatically sets GetBody for common // standard library body types. // // Any returned error will be of type [*url.Error]. The url.Error // value's Timeout method will report true if the request timed out. func (c *Client) Do(req *Request) (*Response, error) { if c.Transport != nil { return c.Transport.RoundTrip(req) } return c.do(req) } func (c *Client) do(req *Request) (retres *Response, reterr error) { if req.URL == nil { req.closeBody() return nil, &url.Error{ Op: urlErrorOp(req.Method), Err: errors.New("http: nil Request.URL"), } } _ = *c // panic early if c is nil; see go.dev/issue/53521 var err error var didTimeout func() bool var resp *Response var deadline = c.deadline() // TINYGO: lots removed here, mostly handling multiple requests. // TINYGO: we just want simple GET, POST, etc. In and out. if resp, didTimeout, err = c.send(req, deadline); err != nil { // c.send() always closes req.Body if !deadline.IsZero() && didTimeout() { return nil, fmt.Errorf("%s (Client.Timeout exceeded while awaiting headers)", err.Error()) } return nil, err } return resp, nil } // Post issues a POST to the specified URL. // // Caller should close resp.Body when done reading from it. // // If the provided body is an [io.Closer], it is closed after the // request. // // Post is a wrapper around DefaultClient.Post. // // To set custom headers, use [NewRequest] and DefaultClient.Do. // // See the [Client.Do] method documentation for details on how redirects // are handled. // // To make a request with a specified context.Context, use [NewRequestWithContext] // and DefaultClient.Do. func Post(url, contentType string, body io.Reader) (resp *Response, err error) { return DefaultClient.Post(url, contentType, body) } // Post issues a POST to the specified URL. // // Caller should close resp.Body when done reading from it. // // If the provided body is an [io.Closer], it is closed after the // request. // // To set custom headers, use [NewRequest] and [Client.Do]. // // To make a request with a specified context.Context, use [NewRequestWithContext] // and [Client.Do]. // // See the [Client.Do] method documentation for details on how redirects // are handled. func (c *Client) Post(url, contentType string, body io.Reader) (resp *Response, err error) { req, err := NewRequest("POST", url, body) if err != nil { return nil, err } req.Header.Set("Content-Type", contentType) return c.Do(req) } // PostForm issues a POST to the specified URL, with data's keys and // values URL-encoded as the request body. // // The Content-Type header is set to application/x-www-form-urlencoded. // To set other headers, use [NewRequest] and DefaultClient.Do. // // When err is nil, resp always contains a non-nil resp.Body. // Caller should close resp.Body when done reading from it. // // PostForm is a wrapper around DefaultClient.PostForm. // // See the [Client.Do] method documentation for details on how redirects // are handled. // // To make a request with a specified [context.Context], use [NewRequestWithContext] // and DefaultClient.Do. func PostForm(url string, data url.Values) (resp *Response, err error) { return DefaultClient.PostForm(url, data) } // PostForm issues a POST to the specified URL, // with data's keys and values URL-encoded as the request body. // // The Content-Type header is set to application/x-www-form-urlencoded. // To set other headers, use [NewRequest] and [Client.Do]. // // When err is nil, resp always contains a non-nil resp.Body. // Caller should close resp.Body when done reading from it. // // See the [Client.Do] method documentation for details on how redirects // are handled. // // To make a request with a specified context.Context, use [NewRequestWithContext] // and Client.Do. func (c *Client) PostForm(url string, data url.Values) (resp *Response, err error) { return c.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode())) } // Head issues a HEAD to the specified URL. If the response is one of // the following redirect codes, Head follows the redirect, up to a // maximum of 10 redirects: // // 301 (Moved Permanently) // 302 (Found) // 303 (See Other) // 307 (Temporary Redirect) // 308 (Permanent Redirect) // // Head is a wrapper around DefaultClient.Head. // // To make a request with a specified [context.Context], use [NewRequestWithContext] // and DefaultClient.Do. func Head(url string) (resp *Response, err error) { return DefaultClient.Head(url) } // Head issues a HEAD to the specified URL. If the response is one of the // following redirect codes, Head follows the redirect after calling the // [Client.CheckRedirect] function: // // 301 (Moved Permanently) // 302 (Found) // 303 (See Other) // 307 (Temporary Redirect) // 308 (Permanent Redirect) // // To make a request with a specified [context.Context], use [NewRequestWithContext] // and [Client.Do]. func (c *Client) Head(url string) (resp *Response, err error) { req, err := NewRequest("HEAD", url, nil) if err != nil { return nil, err } return c.Do(req) }