mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-02-03 00:41:10 +08:00
fix header writer after status issue
This commit is contained in:
parent
9a2aeef0d0
commit
896182417f
1 changed files with 5 additions and 5 deletions
10
context.go
10
context.go
|
|
@ -602,11 +602,6 @@ func (c *Context) GetReqQueryString() string {
|
|||
// SetBodyStream 设置响应体为一个 io.Reader,并指定内容长度
|
||||
// 如果 contentSize 为 -1,则表示内容长度未知,将使用 Transfer-Encoding: chunked
|
||||
func (c *Context) SetBodyStream(reader io.Reader, contentSize int) {
|
||||
// 确保在写入数据前设置状态码
|
||||
if !c.Writer.Written() {
|
||||
c.Writer.WriteHeader(http.StatusOK) // 默认 200 OK
|
||||
}
|
||||
|
||||
// 如果指定了内容长度且大于等于 0,则设置 Content-Length 头部
|
||||
if contentSize >= 0 {
|
||||
c.Writer.Header().Set("Content-Length", fmt.Sprintf("%d", contentSize))
|
||||
|
|
@ -615,6 +610,11 @@ func (c *Context) SetBodyStream(reader io.Reader, contentSize int) {
|
|||
c.Writer.Header().Del("Content-Length")
|
||||
}
|
||||
|
||||
// 确保在写入数据前设置状态码
|
||||
if !c.Writer.Written() {
|
||||
c.Writer.WriteHeader(http.StatusOK) // 默认 200 OK
|
||||
}
|
||||
|
||||
// 将 reader 的内容直接复制到 ResponseWriter
|
||||
// ResponseWriter 实现了 io.Writer 接口
|
||||
_, err := copyb.Copy(c.Writer, reader)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue