示例#1
0
文件: util.go 项目: erkl/relay
// writeResponse writes an HTTP response.
func writeResponse(w xo.Writer, resp *heat.Response, method string) error {
	if resp.Body != nil {
		defer resp.Body.Close()
	}

	size, err := heat.ResponseBodySize(resp, method)
	if err != nil {
		return err
	}

	if err = heat.WriteResponseHeader(w, resp); err != nil {
		return err
	}
	if err = w.Flush(); err != nil {
		return err
	}

	if size != 0 {
		if err = heat.WriteBody(w, resp.Body, size); err != nil {
			return err
		}
		if err = w.Flush(); err != nil {
			return err
		}
	}

	return nil
}
示例#2
0
文件: request.go 项目: erkl/heat
// WriteRequestHeader writes an HTTP request header to w.
func WriteRequestHeader(w xo.Writer, req *Request) error {
	buf, err := w.Reserve(len(req.Method) + len(req.URI) + 10 + 20 + 20)
	if err != nil {
		return err
	}

	n := copy(buf[0:], req.Method)
	n += copy(buf[n:], " ")
	n += copy(buf[n:], req.URI)
	n += copy(buf[n:], " HTTP/")
	n += itoa(buf[n:], int64(req.Major))
	n += copy(buf[n:], ".")
	n += itoa(buf[n:], int64(req.Minor))
	n += copy(buf[n:], "\r\n")

	if err := w.Commit(n); err != nil {
		return err
	}

	return writeHeader(w, req.Fields)
}
示例#3
0
文件: fields.go 项目: erkl/heat
func writeHeader(w xo.Writer, fields Fields) error {
	for _, f := range fields {
		buf, err := w.Reserve(len(f.Name) + len(f.Value) + 4)
		if err != nil {
			return err
		}

		n := copy(buf[0:], f.Name)
		n += copy(buf[n:], ": ")
		n += copy(buf[n:], f.Value)
		n += copy(buf[n:], "\r\n")

		if err := w.Commit(n); err != nil {
			return err
		}
	}

	_, err := w.Write(crlf)
	return err
}