forked from beego/beego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
154 lines (128 loc) · 5.2 KB
/
output.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
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package context
import (
"github.com/beego/beego/v2/server/web/context"
)
// BeegoOutput does work for sending response header.
type BeegoOutput context.BeegoOutput
// NewOutput returns new BeegoOutput.
// it contains nothing now.
func NewOutput() *BeegoOutput {
return (*BeegoOutput)(context.NewOutput())
}
// Reset init BeegoOutput
func (output *BeegoOutput) Reset(ctx *Context) {
(*context.BeegoOutput)(output).Reset((*context.Context)(ctx))
}
// Header sets response header item string via given key.
func (output *BeegoOutput) Header(key, val string) {
(*context.BeegoOutput)(output).Header(key, val)
}
// Body sets response body content.
// if EnableGzip, compress content string.
// it sends out response body directly.
func (output *BeegoOutput) Body(content []byte) error {
return (*context.BeegoOutput)(output).Body(content)
}
// Cookie sets cookie value via given key.
// others are ordered as cookie's max age time, path,domain, secure and httponly.
func (output *BeegoOutput) Cookie(name string, value string, others ...interface{}) {
(*context.BeegoOutput)(output).Cookie(name, value, others)
}
// JSON writes json to response body.
// if encoding is true, it converts utf-8 to \u0000 type.
func (output *BeegoOutput) JSON(data interface{}, hasIndent bool, encoding bool) error {
return (*context.BeegoOutput)(output).JSON(data, hasIndent, encoding)
}
// YAML writes yaml to response body.
func (output *BeegoOutput) YAML(data interface{}) error {
return (*context.BeegoOutput)(output).YAML(data)
}
// JSONP writes jsonp to response body.
func (output *BeegoOutput) JSONP(data interface{}, hasIndent bool) error {
return (*context.BeegoOutput)(output).JSONP(data, hasIndent)
}
// XML writes xml string to response body.
func (output *BeegoOutput) XML(data interface{}, hasIndent bool) error {
return (*context.BeegoOutput)(output).XML(data, hasIndent)
}
// ServeFormatted serve YAML, XML OR JSON, depending on the value of the Accept header
func (output *BeegoOutput) ServeFormatted(data interface{}, hasIndent bool, hasEncode ...bool) {
(*context.BeegoOutput)(output).ServeFormatted(data, hasIndent, hasEncode...)
}
// Download forces response for download file.
// it prepares the download response header automatically.
func (output *BeegoOutput) Download(file string, filename ...string) {
(*context.BeegoOutput)(output).Download(file, filename...)
}
// ContentType sets the content type from ext string.
// MIME type is given in mime package.
func (output *BeegoOutput) ContentType(ext string) {
(*context.BeegoOutput)(output).ContentType(ext)
}
// SetStatus sets response status code.
// It writes response header directly.
func (output *BeegoOutput) SetStatus(status int) {
(*context.BeegoOutput)(output).SetStatus(status)
}
// IsCachable returns boolean of this request is cached.
// HTTP 304 means cached.
func (output *BeegoOutput) IsCachable() bool {
return (*context.BeegoOutput)(output).IsCachable()
}
// IsEmpty returns boolean of this request is empty.
// HTTP 201,204 and 304 means empty.
func (output *BeegoOutput) IsEmpty() bool {
return (*context.BeegoOutput)(output).IsEmpty()
}
// IsOk returns boolean of this request runs well.
// HTTP 200 means ok.
func (output *BeegoOutput) IsOk() bool {
return (*context.BeegoOutput)(output).IsOk()
}
// IsSuccessful returns boolean of this request runs successfully.
// HTTP 2xx means ok.
func (output *BeegoOutput) IsSuccessful() bool {
return (*context.BeegoOutput)(output).IsSuccessful()
}
// IsRedirect returns boolean of this request is redirection header.
// HTTP 301,302,307 means redirection.
func (output *BeegoOutput) IsRedirect() bool {
return (*context.BeegoOutput)(output).IsRedirect()
}
// IsForbidden returns boolean of this request is forbidden.
// HTTP 403 means forbidden.
func (output *BeegoOutput) IsForbidden() bool {
return (*context.BeegoOutput)(output).IsForbidden()
}
// IsNotFound returns boolean of this request is not found.
// HTTP 404 means not found.
func (output *BeegoOutput) IsNotFound() bool {
return (*context.BeegoOutput)(output).IsNotFound()
}
// IsClientError returns boolean of this request client sends error data.
// HTTP 4xx means client error.
func (output *BeegoOutput) IsClientError() bool {
return (*context.BeegoOutput)(output).IsClientError()
}
// IsServerError returns boolean of this server handler errors.
// HTTP 5xx means server internal error.
func (output *BeegoOutput) IsServerError() bool {
return (*context.BeegoOutput)(output).IsServerError()
}
// Session sets session item value with given key.
func (output *BeegoOutput) Session(name interface{}, value interface{}) {
(*context.BeegoOutput)(output).Session(name, value)
}