Beispiel #1
0
// 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
}
Beispiel #2
0
// 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)
}
Beispiel #3
0
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
}