github.com/searKing/golang/go@v1.2.117/net/mux/internal/http/request.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package http 6 7 import ( 8 "bufio" 9 "bytes" 10 "io" 11 "net/http" 12 ) 13 14 const maxHTTPRead = 4096 15 16 // ReadRequestLine reads first line of HTTP request 17 func ReadRequestLine(r io.Reader) *http.Request { 18 br := bufio.NewReader(&io.LimitedReader{R: r, N: maxHTTPRead}) 19 l, part, err := br.ReadLine() 20 if err != nil || part { 21 return nil 22 } 23 // padding with http header tailer bytes \r\n\r\n 24 l = append(l, []byte("\r\n\r\n")...) 25 26 req, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(l))) 27 if err != nil { 28 return nil 29 } 30 return req 31 } 32 33 // MatchHTTPHeader reads first line of HTTP request 34 // returns true if headers matches 35 func MatchHTTPHeader(r io.Reader, matches func(parsedHeader http.Header) bool) (matched bool) { 36 req, err := http.ReadRequest(bufio.NewReader(r)) 37 if err != nil { 38 return false 39 } 40 41 return matches(req.Header) 42 }