-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
res_header.go
167 lines (147 loc) · 4.7 KB
/
res_header.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package minima
/**
* Minima is a free and open source software under Mit license
Copyright (c) 2024 gominima
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
* Authors @apoorvcodes @megatank58
* Maintainers @Panquesito7 @savioxavier @Shubhaankar-Sharma @apoorvcodes @megatank58
* Thank you for showing interest in minima and for this beautiful community
*/
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 Unavailable": 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
}