在golang中提供http中无限大小的内容

时间:2017-02-09 04:09:45

标签: http go

我有以下服务器代码:

package main

import (
    "fmt"
    "net/http"
    "time"
)

type serveData struct {

}

func (s *serveData) Read (p []byte)  (int, error) {
    l := len (p)
    fmt.Println ("p size is ", l);
    time.Sleep(200 * time.Millisecond);
    return l, nil;
}

func (s *serveData) Seek (offset int64, whence int)  (int64, error) {
    fmt.Println ("in seek ", offset);
    return offset, nil;
}

func handler(w http.ResponseWriter, r *http.Request) {
    reader := new (serveData)
    //w.WriteHeader(206);
    w.Header().Set("Content-type", "application/octet-stream");
    fmt.Println ("got request");
    http.ServeContent(w, r, "cool", time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), reader)
}

func main() {
    http.HandleFunc("/check", handler)
    http.ListenAndServe(":8080", nil)
}

当我将客户端连接到上面的服务器(curl -X GET http://127.0.0.1:8080/check)时,没有任何反应,卷曲只是退出。服务器调用Seek()函数2次,但从不调用Read()

我正在提供部分内容,因为大小无限制(伪实时数据)。此外,当我取消注释w.WriteHeader(206)时,服务器会抱怨" http: multiple response.WriteHeader calls"

这里可能出现什么问题?

0 个答案:

没有答案