forked from gramework/gramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendfile.go
53 lines (46 loc) · 1.44 KB
/
sendfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package gramework
// // Sendfile sends count bytes from f to remote a TCP connection.
// // f offset is always relative to the current offset.
// func Sendfile(conn *net.TCPConn, f *os.File, count int64) (n int64, err error) {
// lr := &io.LimitedReader{N: count, R: f}
// n, err = conn.ReadFrom(lr)
// return
// }
// // Sendfile tries to serve a file with sendfile()
// // otherwise fallbacks to slower copyZeroAlloc
// func (ctx *Context) Sendfile(filepath string) {
// ctx.Hijack(sendFileFunc(filepath))
// }
// var (
// internalServerErrorBytes = []byte("HTTP/1.1 500 Internal Server error\r\nConnection: close\r\n\r\nInternal Server Error")
// )
// func sendFileFunc(filepath string) func(conn net.Conn) {
// return func(conn net.Conn) {
// f, err := os.Open(filepath)
// if err != nil {
// conn.Write(internalServerErrorBytes)
// }
// fi, err := f.Stat()
// if err != nil || fi.IsDir() {
// conn.Write(internalServerErrorBytes)
// }
// if tcpConn, ok := conn.(*net.TCPConn); ok {
// Sendfile(tcpConn, f, fi.Size())
// log.Printf("Wow! So sendfile, much fast!")
// return
// }
// copyZeroAlloc(conn, f)
// }
// }
// func copyZeroAlloc(w io.Writer, r io.Reader) (int64, error) {
// vbuf := copyBufPool.Get()
// buf := vbuf.([]byte)
// n, err := io.CopyBuffer(w, r, buf)
// copyBufPool.Put(vbuf)
// return n, err
// }
// var copyBufPool = sync.Pool{
// New: func() interface{} {
// return make([]byte, 4096)
// },
// }