// Given the payload of a transport layer packet NewHttpHdr will return a
// *HttpHdr struct is the bytes contain a valid HTTP header, or nil otherwise.
Given the payload of a transport layer packet NewHttpHdr will return a
*HttpHdr struct is the bytes contain a valid HTTP header, or nil otherwise.
// Given the payload of a transport layer packet NewHttpHdr will return a // * HttpHdr struct is the bytes contain a valid HTTP header or nil otherwise .
func NewHttpHdr(b []byte) *HttpHdr {
	h := &HttpHdr{}
	s := string(b)
	ln := strings.SplitN(s, "\r\n", 2)
	if len(ln) < 2 {
		return nil
	}
	lnp := strings.SplitN(ln[0], " ", 3)
	if len(lnp) < 3 {
		return nil
	}
	m := strings.ToUpper(lnp[0])
	switch m {
	case "GET", "POST", "HEAD", "PUT", "DELETE", "TRACE", "OPTIONS", "CONNECT", "PATCH":
		h.Method = m
		h.RequestURI = lnp[1]
		p := strings.ToUpper(lnp[2])
		if strings.HasPrefix(p, "HTTP/") {
			h.Proto = p
		}
	default:
		if strings.HasPrefix(m, "HTTP/") {
			h.Proto = m
		} else {
			return nil
		}
		var err error
		h.StatusCode, err = strconv.ParseInt(lnp[1], 10, 0)
		if err != nil {
			return nil
		}
		h.Status = lnp[2]
	}
	return h
}
func NewHttpHdr ( b [ ] byte ) * HttpHdr { h := & HttpHdr { } s := string ( b ) ln := strings . SplitN ( s , " \r \n " , 2 ) if len ( ln ) < 2 { return nil } lnp := strings . SplitN ( ln [ 0 ] , " " , 3 ) if len ( lnp ) < 3 { return nil } m := strings . ToUpper ( lnp [ 0 ] ) switch m { case " " , " " , " " , " " , " " , " " , " " , " " , " " : h . Method = m h . RequestURI = lnp [ 1 ] p := strings . ToUpper ( lnp [ 2 ] ) if strings . HasPrefix ( p , " " ) { h . Proto = p } default : if strings . HasPrefix ( m , " " ) { h . Proto = m } else { return nil } var err error h . StatusCode , err = strconv . ParseInt ( lnp [ 1 ] , 10 , 0 ) if err != nil { return nil } h . Status = lnp [ 2 ] } return h }
Given the payload of a transport layer packet NewHttpHdr will return a
HttpHdr struct is the bytes contain a valid HTTP header, or nil otherwise.
Given the payload of a transport layer packet NewHttpHdr will return a HttpHdr struct is the bytes contain a valid HTTP header or nil otherwise .
//NOTE: If we want to implement parsing the HTTP headers and the // payload then we will need to make sure that this method fails fast so // that we don't spend too much time trying to reconstruct a incomplete // HTTP request.