mirror of
https://github.com/cubixle/groxy.git
synced 2026-04-30 09:48:42 +01:00
init
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package reverseproxy
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func ContentType(rsp *http.Response) string {
|
||||
mimetype, _, _ := mime.ParseMediaType(rsp.Header.Get("Content-Type"))
|
||||
if mimetype == "" {
|
||||
b := bufio.NewReader(rsp.Body)
|
||||
rsp.Body = ioutil.NopCloser(b)
|
||||
mimetype = peekContentType(b)
|
||||
}
|
||||
|
||||
return mimetype
|
||||
}
|
||||
|
||||
// peekContentType peeks at the first 512 bytes of p, and attempts to detect
|
||||
// the content type. Returns empty string if error occurs.
|
||||
func peekContentType(p *bufio.Reader) string {
|
||||
byt, err := p.Peek(512)
|
||||
if err != nil && !errors.Is(err, bufio.ErrBufferFull) && !errors.Is(err, io.EOF) {
|
||||
return ""
|
||||
}
|
||||
return http.DetectContentType(byt)
|
||||
}
|
||||
Reference in New Issue
Block a user