PageRenderTime 31ms CodeModel.GetById 15ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/http/petar-boiler.go

http://github.com/petar/GoHTTP
Go | 146 lines | 133 code | 10 blank | 3 comment | 2 complexity | 52b3cfa7d4d2d6f88e0ac1ed637d0c33 MD5 | raw file
  1// Copyright 2009 The Go Authors. All rights reserved.
  2// Use of this source code is governed by a BSD-style
  3// license that can be found in the LICENSE file.
  4
  5package http
  6
  7func NewResponse200(req *Request) *Response {
  8	return &Response{
  9		Status:        "OK",
 10		StatusCode:    200,
 11		Proto:         "HTTP/1.1",
 12		ProtoMajor:    1,
 13		ProtoMinor:    1,
 14		Request:       req,
 15		Close:         false,
 16		ContentLength: 0,
 17	}
 18}
 19
 20func NewResponse200Bytes(req *Request, b []byte) *Response {
 21	if len(b) == 0 {
 22		return NewResponse200(req)
 23	}
 24	return &Response{
 25		Status:        "OK",
 26		StatusCode:    200,
 27		Proto:         "HTTP/1.1",
 28		ProtoMajor:    1,
 29		ProtoMinor:    1,
 30		Request:       req,
 31		Body:          NewBodyBytes(b),
 32		ContentLength: int64(len(b)),
 33		Close:         false,
 34	}
 35}
 36
 37func NewResponse200CONNECT(req *Request) *Response {
 38	return &Response{
 39		Status:        "Connection Established",
 40		StatusCode:    200,
 41		Proto:         "HTTP/1.1",
 42		ProtoMajor:    1,
 43		ProtoMinor:    1,
 44		Request:       req,
 45		Close:         false,
 46		Header:        Header{"Proxy-Agent": []string{"Go-HTTP-package"}},
 47	}
 48}
 49
 50func NewResponse500(req *Request) *Response {
 51	html := "<html>" +
 52		"<head><title>500 Internal Server Error</title></head>\n" +
 53		"<body bgcolor=\"white\">\n" +
 54		"<center><h1>500 Internal Server Error</h1></center>\n" +
 55		"<hr><center>Go HTTP package</center>\n" +
 56		"</body></html>"
 57	return &Response{
 58		Status:        "Internal Server Error",
 59		StatusCode:    500,
 60		Proto:         "HTTP/1.1",
 61		ProtoMajor:    1,
 62		ProtoMinor:    1,
 63		Request:       req,
 64		Body:          NewBodyString(html),
 65		ContentLength: int64(len(html)),
 66		Close:         false,
 67	}
 68}
 69
 70func NewResponse503(req *Request) *Response {
 71	html := "<html>" +
 72		"<head><title>503 Service Unavailable</title></head>\n" +
 73		"<body bgcolor=\"white\">\n" +
 74		"<center><h1>503 Service Unavailable</h1></center>\n" +
 75		"<hr><center>Go HTTP package</center>\n" +
 76		"</body></html>"
 77	return &Response{
 78		Status:        "Service Unavailable",
 79		StatusCode:    503,
 80		Proto:         "HTTP/1.1",
 81		ProtoMajor:    1,
 82		ProtoMinor:    1,
 83		Request:       req,
 84		Body:          NewBodyString(html),
 85		ContentLength: int64(len(html)),
 86		Close:         false,
 87	}
 88}
 89
 90func NewResponse400(req *Request) *Response {
 91	html := "<html>" +
 92		"<head><title>400 Bad Request</title></head>\n" +
 93		"<body bgcolor=\"white\">\n" +
 94		"<center><h1>400 Bad Request</h1></center>\n" +
 95		"<hr><center>Go HTTP package</center>\n" +
 96		"</body></html>"
 97	return &Response{
 98		Status:        "Bad Request",
 99		StatusCode:    400,
100		Proto:         "HTTP/1.1",
101		ProtoMajor:    1,
102		ProtoMinor:    1,
103		Request:       req,
104		Body:          NewBodyString(html),
105		ContentLength: int64(len(html)),
106		Close:         false,
107	}
108}
109
110func NewResponse400String(req *Request, body string) *Response {
111	return &Response{
112		Status:        "Bad Request",
113		StatusCode:    400,
114		Proto:         "HTTP/1.1",
115		ProtoMajor:    1,
116		ProtoMinor:    1,
117		Request:       req,
118		Body:          NewBodyString(body),
119		ContentLength: int64(len(body)),
120		Close:         false,
121	}
122}
123
124func NewResponse404(req *Request) *Response {
125	html := "<html>" +
126		"<head><title>404 Not found</title></head>\n" +
127		"<body bgcolor=\"white\">\n" +
128		"<center><h1>404 Not found</h1></center>\n" +
129		"<hr><center>Go HTTP package</center>\n" +
130		"</body></html>"
131	return NewResponse404String(req, html)
132}
133
134func NewResponse404String(req *Request, s string) *Response {
135	return &Response{
136		Status:        "Not found",
137		StatusCode:    404,
138		Proto:         "HTTP/1.1",
139		ProtoMajor:    1,
140		ProtoMinor:    1,
141		Request:       req,
142		Body:          NewBodyString(s),
143		ContentLength: int64(len(s)),
144		Close:         false,
145	}
146}