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