func newServerConn(rwc io.ReadWriteCloser, buf *bufio.ReadWriter, req *http.Request) (conn *Conn, err os.Error) { config := new(Config) var hs serverHandshaker = &hybiServerHandshaker{Config: config} code, err := hs.ReadHandshake(buf.Reader, req) if err == ErrBadWebSocketVersion { fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code)) fmt.Fprintf(buf, "Sec-WebSocket-Version: %s\r\n", SupportedProtocolVersion) buf.WriteString("\r\n") buf.WriteString(err.String()) return } if err != nil { hs = &hixie76ServerHandshaker{Config: config} code, err = hs.ReadHandshake(buf.Reader, req) } if err != nil { hs = &hixie75ServerHandshaker{Config: config} code, err = hs.ReadHandshake(buf.Reader, req) } if err != nil { fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code)) buf.WriteString("\r\n") buf.WriteString(err.String()) return } config.Protocol = nil err = hs.AcceptHandshake(buf.Writer) if err != nil { return } conn = hs.NewServerConn(buf, rwc, req) return }
// Provide a canned HTTP status response, including content body func (c *Conn) HTTPStatusResponse(status int) { c.SetHeader("Content-Type", "text/plain; charset=utf-8") c.SetStatus(status) writer := c.NewContentWriter() if writer == nil { // There was an application error, but we still want to send our // status response, so break the existing pipe and close it reader := c.NewContentReader() reader.Close() // Continue as normal, inserting our own content writer writer = c.NewContentWriter() } statusText := http.StatusText(status) if statusText == "" { statusText = "status code " + strconv.Itoa(status) } content := fmt.Sprintf("%s\n", statusText) go func(writer io.WriteCloser, content string) { io.WriteString(writer, content) writer.Close() }(writer, content) }
// Makes an unsuccessful standardResponse object with the specified settings func makeFailureStandardResponse(context string, statusCode int) *standardResponse { response := makeStandardResponse() response.C = context response.S = statusCode response.E = []string{http.StatusText(statusCode)} return response }
func (r *response) WriteHeader(code int) { if r.headerSent { // Note: explicitly using Stderr, as Stdout is our HTTP output. fmt.Fprintf(os.Stderr, "CGI attempted to write header twice on request for %s", r.req.URL) return } r.headerSent = true fmt.Fprintf(r.bufw, "Status: %d %s\r\n", code, http.StatusText(code)) // Set a default Content-Type if _, hasType := r.header["Content-Type"]; !hasType { r.header.Add("Content-Type", "text/html; charset=utf-8") } // TODO: add a method on http.Header to write itself to an io.Writer? // This is duplicated code. for k, vv := range r.header { for _, v := range vv { v = strings.Replace(v, "\n", "", -1) v = strings.Replace(v, "\r", "", -1) v = strings.TrimSpace(v) fmt.Fprintf(r.bufw, "%s: %s\r\n", k, v) } } r.bufw.Write([]byte("\r\n")) r.bufw.Flush() }
func (r *response) WriteHeader(code int) { r.wroteHeader = true r.code = code if code == http.StatusNotModified { // must not have body r.header["Content-Type"] = "", false r.header["Transfer-Encoding"] = "", false } codestring := strconv.Itoa(code) statusText := http.StatusText(code) io.WriteString(&r.buf, "Status: "+codestring+" "+statusText+"\r\n") for k, v := range r.header { io.WriteString(&r.buf, k+": "+v+"\r\n") } io.WriteString(&r.buf, "\r\n") }
func (r *response) WriteHeader(code int) { if r.headerSent { // Note: explicitly using Stderr, as Stdout is our HTTP output. fmt.Fprintf(os.Stderr, "CGI attempted to write header twice on request for %s", r.req.URL) return } r.headerSent = true fmt.Fprintf(r.bufw, "Status: %d %s\r\n", code, http.StatusText(code)) // Set a default Content-Type if _, hasType := r.header["Content-Type"]; !hasType { r.header.Add("Content-Type", "text/html; charset=utf-8") } r.header.Write(r.bufw) r.bufw.WriteString("\r\n") r.bufw.Flush() }
func (r *response) WriteHeader(code int) { if r.wroteHeader { return } r.wroteHeader = true if code == http.StatusNotModified { // Must not have body. r.header.Del("Content-Type") r.header.Del("Content-Length") r.header.Del("Transfer-Encoding") } else if r.header.Get("Content-Type") == "" { r.header.Set("Content-Type", "text/html; charset=utf-8") } if r.header.Get("Date") == "" { r.header.Set("Date", time.UTC().Format(http.TimeFormat)) } fmt.Fprintf(r.w, "Status: %d %s\r\n", code, http.StatusText(code)) r.header.Write(r.w) r.w.WriteString("\r\n") }
func statusCodeToText(code int) string { if t := http.StatusText(code); t != "" { return t } return strconv.Itoa(code) }
func (s status) XML(b *bytes.Buffer) { b.WriteString(fmt.Sprintf("<status>HTTP/1.1 %d %s</status>", s, template.HTMLEscapeString(http.StatusText(int(s))))) }
// Responds with the specified HTTP status code defined in RFC 2616 // and adds the description to the errors list // see http://golang.org/src/pkg/http/status.go for options func (c *Context) RespondWithError(statusCode int) os.Error { return c.RespondWithErrorMessage(http.StatusText(statusCode), statusCode) }