Standard library / net/http/httputil
httputil.ServerConn.Read()
Read the next incoming request. A valid request may accompany ErrPersistEOF. Legacy-only: use net/http Client or Server for new code. Expected stdout: '/a\n'.
Formal Syntax & Structure
go syntaxgo
func (sc *ServerConn) Read() (*http.Request, error)Return Value
(*http.Request, error)
Next request and error; a valid request can accompany ErrPersistEOF.
Working Example
Example Snippetgo
package main
import ( "fmt" "io" "net" "net/http/httputil" "time")
func main() { c, s := net.Pipe() defer c.Close() defer s.Close() check(c.SetDeadline(time.Now().Add(3 * time.Second))) check(s.SetDeadline(time.Now().Add(3 * time.Second))) sc := httputil.NewServerConn(s, nil) done := make(chan error, 1) go func() { _, e := io.WriteString(c, "GET /a HTTP/1.1\r\nHost: demo.invalid\r\n\r\n"); done <- e }() r := must(sc.Read()) fmt.Println(r.URL.Path) check(<-done) check(sc.Close())}
func must[T any](v T, e error) T { if e != nil { panic(e) } return v}func check(e error) { if e != nil { panic(e) }}