added IsAjax and RemoteAddr for context, gofmt #15

Merged
lunny merged 2 commits from fuxiaohei/master into master 2015-10-26 05:49:29 +00:00
11 changed files with 47 additions and 25 deletions

View File

@ -15,6 +15,7 @@ import (
"os"
"path/filepath"
"reflect"
"strings"
)
type Handler interface {
@ -57,6 +58,10 @@ func (ctx *Context) Req() *http.Request {
return ctx.req
}
func (ctx *Context) IsAjax() bool {
return ctx.Req().Header.Get("X-Requested-With") == "XMLHttpRequest"
}
func (ctx *Context) SecureCookies(secret string) Cookies {
return &secureCookies{
(*cookies)(ctx),
@ -82,6 +87,23 @@ func (ctx *Context) Params() *Params {
return &ctx.params
}
func (ctx *Context) IP() string {
proxy := []string{}
if ips := ctx.Req().Header.Get("X-Forwarded-For"); ips != "" {
proxy = strings.Split(ips, ",")
}
if len(proxy) > 0 && proxy[0] != "" {
return proxy[0]
}
ip := strings.Split(ctx.Req().RemoteAddr, ":")
if len(ip) > 0 {
if ip[0] != "[" {
return ip[0]
}
}
return "127.0.0.1"
}
func (ctx *Context) Action() interface{} {
ctx.newAction()
return ctx.action

View File

@ -10,12 +10,12 @@ import (
"crypto/sha1"
"encoding/base64"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
"html/template"
)
func isValidCookieValue(p []byte) bool {

View File

@ -1670,4 +1670,4 @@ func TestCookie60(t *testing.T) {
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "1")
}
}

View File

@ -2,9 +2,9 @@ package tango
import (
"errors"
"html/template"
"net/http"
"strconv"
"html/template"
)
type Forms http.Request
@ -226,4 +226,4 @@ func (ctx *Context) FormFloat64(key string, defaults ...float64) float64 {
func (ctx *Context) FormBool(key string, defaults ...bool) bool {
return (*Forms)(ctx.req).MustBool(key, defaults...)
}
}

View File

@ -846,4 +846,4 @@ func TestForm30(t *testing.T) {
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "1")
}
}

View File

@ -106,4 +106,4 @@ func (t *Tango) addGroup(p string, g *Group) {
func (t *Tango) Group(p string, o interface{}) {
t.addGroup(p, getGroup(o))
}
}

View File

@ -6,8 +6,8 @@ package tango
import (
"errors"
"strconv"
"html/template"
"strconv"
)
type (
@ -25,7 +25,7 @@ func (p *Params) Get(key string) string {
return ""
}
if key[0] != ':' && key[0] != '*' {
key = ":"+ key
key = ":" + key
}
for _, v := range *p {
@ -41,7 +41,7 @@ func (p *Params) String(key string) (string, error) {
return "", errors.New("not exist")
}
if key[0] != ':' && key[0] != '*' {
key = ":"+ key
key = ":" + key
}
for _, v := range *p {
@ -57,7 +57,7 @@ func (p *Params) Strings(key string) ([]string, error) {
return nil, errors.New("not exist")
}
if key[0] != ':' && key[0] != '*' {
key = ":"+ key
key = ":" + key
}
var s = make([]string, 0)
@ -77,7 +77,7 @@ func (p *Params) Escape(key string) (string, error) {
return "", errors.New("not exist")
}
if key[0] != ':' && key[0] != '*' {
key = ":"+ key
key = ":" + key
}
for _, v := range *p {
@ -133,7 +133,7 @@ func (p *Params) MustString(key string, defaults ...string) string {
return ""
}
if key[0] != ':' && key[0] != '*' {
key = ":"+ key
key = ":" + key
}
for _, v := range *p {
@ -152,7 +152,7 @@ func (p *Params) MustStrings(key string, defaults ...[]string) []string {
return []string{}
}
if key[0] != ':' && key[0] != '*' {
key = ":"+ key
key = ":" + key
}
var s = make([]string, 0)
@ -175,7 +175,7 @@ func (p *Params) MustEscape(key string, defaults ...string) string {
return ""
}
if key[0] != ':' && key[0] != '*' {
key = ":"+ key
key = ":" + key
}
for _, v := range *p {
@ -316,7 +316,7 @@ func (p *Params) Set(key, value string) {
return
}
if key[0] != ':' && key[0] != '*' {
key = ":"+ key
key = ":" + key
}
for i, v := range *p {

View File

@ -1005,4 +1005,4 @@ func TestParams36(t *testing.T) {
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "1")
}
}

View File

@ -39,4 +39,4 @@ func (p *pool) New() reflect.Value {
p.cur++
p.lock.Unlock()
return res
}
}

View File

@ -12,7 +12,7 @@ import (
)
type StatusResult struct {
Code int
Code int
Result interface{}
}

View File

@ -218,16 +218,16 @@ func TestReturnJson3(t *testing.T) {
}
type JsonReturn3 struct {
Json
Json
}
func (JsonReturn3) Get() (int, interface{}) {
if true {
return 201, map[string]string{
"say": "Hello tango!",
}
}
return 500, errors.New("something error")
if true {
return 201, map[string]string{
"say": "Hello tango!",
}
}
return 500, errors.New("something error")
}
func TestReturnJson4(t *testing.T) {