// Package httpsrv parses HTTP/1.1 requests. // // It used to be a small server built directly on net.Listener, because TinyGo's // net/http closes a hijacked connection when the handler returns. The domain // split made that server unused: the reader domain owns the socket, the writer // domain writes to it, and neither of them can hand a connection to a value that // wants both directions. What is left here is the parser and the status table. // // The Server type is gone on purpose. A second, unmanaged per-connection // goroutine has no place in the spawn tree. package httpsrv import ( "bufio" "errors" "io" "strconv" "strings" ) // Headers maps lowercase header names to their combined values. type Headers map[string]string // Request is one parsed HTTP request. type Request struct { Method string Path string RawQuery string Proto string Header Headers Body []byte RemoteAddr string } var errClosed = errors.New("closed") // StatusText returns the reason phrase for common codes. func StatusText(code int) string { switch code { case 200: return "OK" case 201: return "Created" case 202: return "Accepted" case 204: return "No Content" case 304: return "Not Modified" case 400: return "Bad Request" case 401: return "Unauthorized" case 403: return "Forbidden" case 404: return "Not Found" case 405: return "Method Not Allowed" case 409: return "Conflict" case 411: return "Length Required" case 413: return "Payload Too Large" case 415: return "Unsupported Media Type" case 429: return "Too Many Requests" case 500: return "Internal Server Error" case 501: return "Not Implemented" case 503: return "Service Unavailable" default: return "Status" } } func ReadRequest(br *bufio.Reader, remote string, maxHeader int, maxBody int64, onContinue func() error) (*Request, error) { line, n, err := readLine(br, maxHeader) if err != nil { return nil, err } if line == "" { return nil, nil } headerBytes := n parts := strings.Split(line, " ") if len(parts) != 3 { return nil, errors.New("malformed request line") } req := &Request{ Method: parts[0], Proto: parts[2], Header: make(Headers, 12), RemoteAddr: remote, } target := parts[1] if i := strings.IndexByte(target, '?'); i >= 0 { req.Path = target[:i] req.RawQuery = target[i+1:] } else { req.Path = target } for { line, n, err := readLine(br, maxHeader) if err != nil { return nil, err } headerBytes += n if headerBytes > maxHeader { return nil, errors.New("headers too large") } if line == "" { break } i := strings.IndexByte(line, ':') if i <= 0 { return nil, errors.New("malformed header") } name := strings.ToLower(strings.TrimSpace(line[:i])) value := strings.TrimSpace(line[i+1:]) if old, ok := req.Header[name]; ok && old != "" { req.Header[name] = old + ", " + value } else { req.Header[name] = value } } if te := req.Header["transfer-encoding"]; te != "" && !strings.EqualFold(te, "identity") { return nil, errors.New("chunked requests are not supported") } length := int64(0) if cl := req.Header["content-length"]; cl != "" { v, err := strconv.ParseInt(cl, 10, 64) if err != nil || v < 0 { return nil, errors.New("bad content-length") } length = v } if length > maxBody { return nil, errors.New("request body too large") } if strings.EqualFold(req.Header["expect"], "100-continue") { if onContinue != nil { if err := onContinue(); err != nil { return nil, err } } } if length > 0 { body := make([]byte, length) if _, err := io.ReadFull(br, body); err != nil { return nil, err } req.Body = body } return req, nil } func readLine(br *bufio.Reader, max int) (string, int, error) { line, err := br.ReadString('\n') if err != nil { if err == io.EOF && line == "" { return "", 0, errClosed } if err != io.EOF { return "", 0, err } } if len(line) > max { return "", len(line), errors.New("line too long") } line = strings.TrimSuffix(line, "\n") line = strings.TrimSuffix(line, "\r") return line, len(line) + 1, nil }