Browse Source

added IsAjax and RemoteAddr for context, gofmt

tags/v0.4.8
fud 4 years ago
parent
commit
723c1c637b
11 changed files with 47 additions and 25 deletions
  1. +22
    -0
      context.go
  2. +1
    -1
      cookie.go
  3. +1
    -1
      cookie_test.go
  4. +2
    -2
      form.go
  5. +1
    -1
      form_test.go
  6. +1
    -1
      group.go
  7. +9
    -9
      param.go
  8. +1
    -1
      param_test.go
  9. +1
    -1
      pool.go
  10. +1
    -1
      return.go
  11. +7
    -7
      return_test.go

+ 22
- 0
context.go 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) RemoteAddr() 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


+ 1
- 1
cookie.go 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 {


+ 1
- 1
cookie_test.go 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")
}
}

+ 2
- 2
form.go 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...)
}
}

+ 1
- 1
form_test.go 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")
}
}

+ 1
- 1
group.go 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))
}
}

+ 9
- 9
param.go 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 {


+ 1
- 1
param_test.go 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")
}
}

+ 1
- 1
pool.go View File

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

+ 1
- 1
return.go View File

@@ -12,7 +12,7 @@ import (
)

type StatusResult struct {
Code int
Code int
Result interface{}
}



+ 7
- 7
return_test.go 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) {


Loading…
Cancel
Save