package response import( "fmt" "io" "httpfromtcp/internal/headers" ) type Response struct { } type Writer struct{ writer io.Writer } func NewWriter(writer io.Writer)*Writer{ return &Writer{writer:writer} } func (w *Writer) WriteStatusLine(statusCode StatusCode) error{ statusLine := []byte{} switch statusCode{ case StatusOK: statusLine = []byte("HTTP/1.1 200 OK\r\n") case StatusBadRequest: statusLine = []byte("HTTP/1.1 400 Bad Request\r\n") case StatusInternalServerError: statusLine = []byte("HTTP/1.1 500 Internal Server Error\r\n") default: return fmt.Errorf("unrecognized error code") } _,err := w.writer.Write(statusLine) return err } func (w *Writer) WriteHeaders(h headers.Headers) error{ b := []byte{} h.ForEach(func(n,v string){ b = fmt.Appendf(b, "%s: %s\r\n",n,v) }) b = fmt.Appendf(b,"\r\n") _,err := w.writer.Write(b) return err } func (w *Writer) WriteBody(p []byte) (int, error){ return w.writer.Write(p) } type StatusCode int const ( StatusOK StatusCode = 200 StatusBadRequest StatusCode = 400 StatusInternalServerError StatusCode = 500 ) func WriteHeaders(w io.Writer, h *headers.Headers) error{ b := []byte{} h.ForEach(func(n,v string){ b = fmt.Appendf(b, "%s: %s\r\n",n,v) }) b = fmt.Appendf(b,"\r\n") _,err := w.Write(b) return err } func GetDefaultHeaders(contentLen int) *headers.Headers{ h := headers.NewHeaders() h.Set("Content-Length", fmt.Sprintf("%d",contentLen)) h.Set("Connection", "close") h.Set("Content-Type","text/plain") return h } func WriteStatusLine(w io.Writer, statusCode StatusCode) error{ statusLine := []byte{} switch statusCode{ case StatusOK: statusLine = []byte("HTTP/1.1 200 OK\r\n") case StatusBadRequest: statusLine = []byte("HTTP/1.1 400 Bad Request\r\n") case StatusInternalServerError: statusLine = []byte("HTTP/1.1 500 Internal Server Error\r\n") default: return fmt.Errorf("unrecognized error code") } _,err := w.Write(statusLine) return err } //func (w *Writer) WriteChunkedBody(p []byte) (int,error){ //} //func (w *Writer) WriteChunkedBodyDone() (int,error){ //}