GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fast-response/response.go at main · fast-response/fast-response

Expand file tree

/

response.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
208 lines (180 loc) · 5.2 KB
/

response.go

Copy path

File metadata and controls

208 lines (180 loc) · 5.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package fastresponse
import (
"fmt"
jsoniter "github.com/json-iterator/go"
"strconv"
"time"
"github.com/panjf2000/gnet/v2"
)
type Response struct {
// HTTP response code
Code string
// Headers contains the headers
headers map[string][]string
// Body contains the body of the response
body []byte
// Text contains the human-readable text of the response
text string
// Version of the HTTP protocol used in the response
version string
// Cookies contains the parsed cookies of the response
Cookies map[string]*Cookie
// Request is the *Request that generated this Response.
// It can be used for getting request specific values.
Req *Request
// Chunked controls whether chunked transfer encoding should be applied.
Chunked bool
// Connection of the response. This is the gnet.Conn object of the connection from the client.
Conn gnet.Conn
}
var Code = map[int]string{
100: "Continue",
101: "Switching Protocols",
200: "OK",
201: "Created",
202: "Accepted",
203: "Non-Authoritative Information",
204: "No Content",
205: "Reset Content",
206: "Partial Content",
300: "Multiple Choices",
301: "Moved Permanently",
302: "Found",
303: "See Other",
304: "Not Modified",
305: "Use Proxy",
306: "Unused",
307: "Temporary Redirect",
400: "Bad Request",
401: "Unauthorized",
402: "Payment Required",
403: "Forbidden",
404: "Not Found",
405: "Method Not Allowed",
406: "Not Acceptable",
407: "Proxy Authentication Required",
408: "Request Time-out",
409: "Conflict",
410: "Gone",
411: "Length Required",
412: "Precondition Failed",
413: "Request Entity Too Large",
414: "Request-URI Too Large",
415: "Unsupported Media Type",
416: "Requested range not satisfiable",
417: "Expectation Failed",
500: "Internal Server Error",
501: "Not Implemented",
502: "Bad Gateway",
503: "Service Unavailable",
504: "Gateway Time-out",
505: "HTTP Version not supported",
}
func Time2HttpDate() string {
l, _ := time.LoadLocation("Europe/London")
return time.Now().In(l).Format("Mon, 02 Jan 2006 15:04:05") + " GMT"
}
func (res *Response) SetHeader(name string, content string) string {
if name == "Date" || name == "Content-Length" {
return ""
}
if res.headers[name] == nil {
res.headers[name] = []string{content}
} else {
res.headers[name] = append(res.headers[name], content)
}
return ""
}
func (res *Response) SetBody(body []byte) {
res.body = body
}
func (res *Response) SetText(text string) {
res.text = text
res.SetBody(String2Slice(text))
}
func (res *Response) SetContentType(contentType string) {
res.headers["Content-Type"] = []string{contentType}
}
func (res *Response) SetCode(code int) {
if Code[code] == "" {
res.Code = "500 Internal Server Error"
}
res.Code = strconv.Itoa(code) + " " + Code[code]
}
func (res *Response) GetHeaders() map[string][]string {
return res.headers
}
func (res *Response) GetHeader(name string) []string {
if res.headers[name] == nil {
return []string{}
} else {
return res.headers[name]
}
}
func (res *Response) GetRaw() []byte {
res.headers["Content-Length"] = []string{strconv.Itoa(len(res.body))}
res.headers["Date"] = []string{Time2HttpDate()}
GenerateCookies(res)
headers := ""
for key, val := range res.headers {
for _, content := range val {
headers += key + ": " + content + "\r\n"
}
}
go fmt.Println("[" + time.Now().Format("2006-01-02 15:03:04") + "|" + res.Req.Path + "] " + res.Code + " | Body Length: " + strconv.Itoa(len(res.body)))
return BytesCombine2(String2Slice(res.version+" "+res.Code+"\r\n"+headers+"\r\n"), res.body)
}
func (res *Response) GetBody() []byte {
return res.body
}
func (res *Response) String() string {
return string(res.GetRaw())
}
func (res *Response) RemoveHeader(name string, content string) string {
if name == "Date" || name == "Content-Length" {
return ""
}
if res.headers[name] == nil {
res.headers[name] = []string{content}
} else {
for i := 0; i < len(res.headers[name]); i++ {
if res.headers[name][i] == content {
res.headers[name] = append(res.headers[name][:i], res.headers[name][(i+1):]...)
return ""
}
}
}
return ""
}
func (res *Response) PushBody(content []byte) {
if res.Chunked {
res.Conn.Write(BytesCombine2(String2Slice(strconv.FormatInt(int64(len(content)), 16)+"\r\n"), content, String2Slice("\r\n")))
} else {
res.headers["Date"] = []string{Time2HttpDate()}
headers := ""
for key, val := range res.headers {
for _, content := range val {
headers += key + ": " + content + "\r\n"
}
}
res.Conn.Write(BytesCombine2(String2Slice(res.version+" "+res.Code+"\r\n"+headers+"Transfer-Encoding: chunked\r\n"+"\r\n"+strconv.FormatInt(int64(len(content)), 16)+"\r\n"), content, String2Slice("\r\n")))
res.Chunked = true
}
}
func (res *Response) PushBodyEnd() {
if res.Chunked {
res.Conn.Write(String2Slice("0\r\n\r\n"))
}
}
func (res *Response) SetJson(data any) error {
res.SetHeader("Content-Type", "application/json; charset=utf-8")
result, err := jsoniter.Marshal(data)
if err == nil {
res.SetBody(result)
return nil
}
return err
}
func NewResponse(req *Request, Conn gnet.Conn) *Response {
return &Response{Code: "200 " + Code[200], headers: map[string][]string{"Server": {"FastResponse"}}, version: req.Version, Req: req, Chunked: false, Conn: Conn}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.