github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/internal/httpx/content_type.go (about)

     1  package httpx
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  
     7  	"github.com/segakazzz/buffalo/internal/defaults"
     8  )
     9  
    10  func ContentType(req *http.Request) string {
    11  	ct := defaults.String(req.Header.Get("Content-Type"), req.Header.Get("Accept"))
    12  	ct = strings.TrimSpace(ct)
    13  	var cts []string
    14  	if strings.Contains(ct, ",") {
    15  		cts = strings.Split(ct, ",")
    16  	} else {
    17  		cts = strings.Split(ct, ";")
    18  	}
    19  	for _, c := range cts {
    20  		c = strings.TrimSpace(c)
    21  		if strings.HasPrefix(c, "*/*") {
    22  			continue
    23  		}
    24  		return strings.ToLower(c)
    25  	}
    26  	if ct == "*/*" {
    27  		return ""
    28  	}
    29  	return ct
    30  }