Added to input.go: AcceptHtml, AcceptsXml and AcceptsJson functions which check …
…the header agains a regex for simpler mult-content-type handling.
Showing
1 changed file
with
24 additions
and
0 deletions
| ... | @@ -21,12 +21,21 @@ import ( | ... | @@ -21,12 +21,21 @@ import ( |
| 21 | "net/http" | 21 | "net/http" |
| 22 | "net/url" | 22 | "net/url" |
| 23 | "reflect" | 23 | "reflect" |
| 24 | "regexp" | ||
| 24 | "strconv" | 25 | "strconv" |
| 25 | "strings" | 26 | "strings" |
| 26 | 27 | ||
| 27 | "github.com/astaxie/beego/session" | 28 | "github.com/astaxie/beego/session" |
| 28 | ) | 29 | ) |
| 29 | 30 | ||
| 31 | // Regexes for checking the accept headers | ||
| 32 | // TODO make sure these are correct | ||
| 33 | var ( | ||
| 34 | acceptsHtmlRegex = regexp.MustCompile(`(text/html|application/xhtml\+xml)(?:,|$)`) | ||
| 35 | acceptsXmlRegex = regexp.MustCompile(`(application/xml|text/xml)(?:,|$)`) | ||
| 36 | acceptsJsonRegex = regexp.MustCompile(`(application/json)(?:,|$)?`) | ||
| 37 | ) | ||
| 38 | |||
| 30 | // BeegoInput operates the http request header, data, cookie and body. | 39 | // BeegoInput operates the http request header, data, cookie and body. |
| 31 | // it also contains router params and current session. | 40 | // it also contains router params and current session. |
| 32 | type BeegoInput struct { | 41 | type BeegoInput struct { |
| ... | @@ -163,6 +172,21 @@ func (input *BeegoInput) IsUpload() bool { | ... | @@ -163,6 +172,21 @@ func (input *BeegoInput) IsUpload() bool { |
| 163 | return strings.Contains(input.Header("Content-Type"), "multipart/form-data") | 172 | return strings.Contains(input.Header("Content-Type"), "multipart/form-data") |
| 164 | } | 173 | } |
| 165 | 174 | ||
| 175 | // Checks if request accepts html response | ||
| 176 | func (input *BeegoInput) AcceptsHtml() bool { | ||
| 177 | return acceptsHtmlRegex.MatchString(input.Header("Accept")) | ||
| 178 | } | ||
| 179 | |||
| 180 | // Checks if request accepts xml response | ||
| 181 | func (input *BeegoInput) AcceptsXml() bool { | ||
| 182 | return acceptsXmlRegex.MatchString(input.Header("Accept")) | ||
| 183 | } | ||
| 184 | |||
| 185 | // Checks if request accepts json response | ||
| 186 | func (input *BeegoInput) AcceptsJson() bool { | ||
| 187 | return acceptsJsonRegex.MatchString(input.Header("Accept")) | ||
| 188 | } | ||
| 189 | |||
| 166 | // IP returns request client ip. | 190 | // IP returns request client ip. |
| 167 | // if in proxy, return first proxy id. | 191 | // if in proxy, return first proxy id. |
| 168 | // if error, return 127.0.0.1. | 192 | // if error, return 127.0.0.1. | ... | ... |
-
Please register or sign in to post a comment