Commit 1a6ea693 authored by Donal Byrne's avatar Donal Byrne Committed by astaxie

Added to input.go: AcceptHtml, AcceptsXml and AcceptsJson functions which check…

Added to input.go: AcceptHtml, AcceptsXml and AcceptsJson functions which check the header agains a regex for simpler mult-content-type handling.
parent 2e51c124
...@@ -21,12 +21,21 @@ import ( ...@@ -21,12 +21,21 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"reflect" "reflect"
"regexp"
"strconv" "strconv"
"strings" "strings"
"github.com/astaxie/beego/session" "github.com/astaxie/beego/session"
) )
// Regexes for checking the accept headers
// TODO make sure these are correct
var (
acceptsHtmlRegex = regexp.MustCompile(`(text/html|application/xhtml\+xml)(?:,|$)`)
acceptsXmlRegex = regexp.MustCompile(`(application/xml|text/xml)(?:,|$)`)
acceptsJsonRegex = regexp.MustCompile(`(application/json)(?:,|$)?`)
)
// BeegoInput operates the http request header, data, cookie and body. // BeegoInput operates the http request header, data, cookie and body.
// it also contains router params and current session. // it also contains router params and current session.
type BeegoInput struct { type BeegoInput struct {
...@@ -163,6 +172,21 @@ func (input *BeegoInput) IsUpload() bool { ...@@ -163,6 +172,21 @@ func (input *BeegoInput) IsUpload() bool {
return strings.Contains(input.Header("Content-Type"), "multipart/form-data") return strings.Contains(input.Header("Content-Type"), "multipart/form-data")
} }
// Checks if request accepts html response
func (input *BeegoInput) AcceptsHtml() bool {
return acceptsHtmlRegex.MatchString(input.Header("Accept"))
}
// Checks if request accepts xml response
func (input *BeegoInput) AcceptsXml() bool {
return acceptsXmlRegex.MatchString(input.Header("Accept"))
}
// Checks if request accepts json response
func (input *BeegoInput) AcceptsJson() bool {
return acceptsJsonRegex.MatchString(input.Header("Accept"))
}
// IP returns request client ip. // IP returns request client ip.
// if in proxy, return first proxy id. // if in proxy, return first proxy id.
// if error, return 127.0.0.1. // if error, return 127.0.0.1.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment