forked from gominima/minima
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathres_header.go
More file actions
139 lines (125 loc) · 3.31 KB
/
Copy pathres_header.go
File metadata and controls
139 lines (125 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package minima
import (
"net/http"
)
/**
@info The Outgoing header structure
@property {http.Request} [req] The net/http request instance
@property {http.ResponseWriter} [res] The net/http response instance
@property {bool} [body] Whether body has been sent or not
@property {int} [status] response status code
*/
type OutgoingHeader struct {
req *http.Request
res http.ResponseWriter
}
var statusCodes = map[string]int{
"OK": 200,
"Created": 201,
"Accepted": 202,
"No Content": 204,
"Reset Content": 205,
"Partial Content": 206,
"Moved Permanently": 301,
"Found": 302,
"Not Modified": 304,
"Use Proxy": 305,
"Switch Proxy": 306,
"Temporary Redirect": 307,
"Permanent Redirect": 308,
"Bad Request": 400,
"Unauthorized": 401,
"Forbidden": 403,
"NOT FOUND": 404,
"Method Not Allowed": 405,
"Payload Too Large": 413,
"URI Too Long": 414,
"Internal Server Error": 500,
"Not Implemented": 501,
"Bad Gateway": 502,
"Service Unavailaible": 503,
"Gateway Timeout": 504,
"HTTP Version Not Supported": 505,
}
/**
@info Make a new default request header instance
@param {http.Request} [req] The net/http request instance
@param {http.ResponseWriter} [res] The net/http response instance
@returns {OutgoingHeader}
*/
func NewResHeader(res http.ResponseWriter, req *http.Request) *OutgoingHeader {
return &OutgoingHeader{req, res}
}
/**
@info Sets and new header to response
@param {string} [key] Key of the new header
@param {string} [value] Value of the new header
@returns {OutgoingHeader}
*/
func (h *OutgoingHeader) Set(key string, value string) *OutgoingHeader {
h.res.Header().Set(key, value)
return h
}
/**
@info Gets the header from response headers
@param {string} [key] Key of the header
@returns {string}
*/
func (h *OutgoingHeader) Get(key string) string {
return h.res.Header().Get(key)
}
/**
@info Deletes header from respose
@param {string} [key] Key of the header
@returns {OutgoingHeader}
*/
func (h *OutgoingHeader) Del(key string) *OutgoingHeader {
h.res.Header().Del(key)
return h
}
/**
@info Clones all headers from response
@returns {OutgoingHeader}
*/
func (h *OutgoingHeader) Clone() http.Header {
return h.res.Header().Clone()
}
/**
@info Sets content lenght
@param {string} [len] The lenght of the content
@returns {OutgoingHeader}
*/
func (h *OutgoingHeader) Setlength(len string) *OutgoingHeader {
h.Set("Content-length", len)
return h
}
/**
@info Sets response status
@param {int} [code] The status code for the response
@returns {OutgoingHeader}
*/
func (h *OutgoingHeader) Status(code int) *OutgoingHeader {
h.res.WriteHeader(code)
return h
}
/**
@info Sends good stack of base headers
@returns {}
*/
func (h *OutgoingHeader) BaseHeaders() {
h.Set("transfer-encoding", "chunked")
h.Set("connection", "keep-alive")
}
/**
@info Flushes and writes header to route
@returns {bool}
*/
func (h *OutgoingHeader) Flush() bool {
if h.Get("Content-Type") == "" {
h.Set("Content-Type", "text/html;charset=utf-8")
}
if f, ok := h.res.(http.Flusher); ok {
f.Flush()
}
return true
}