Ejemplo n.º 1
0
func (this FakeProvider) Middleware() rack.Middleware {
	if this.middleware == nil {
		hostcept := interceptor.New()
		hostcept.Intercept("/auth", rack.Func(func(vars map[string]interface{}, next func()) {
			values := url.Values{}
			values.Set("state", parser.V(vars).FormValue("state"))
			values.Set("code", "c0D3")
			redirecter.V(vars).Redirect(parser.V(vars).FormValue("redirect_uri") + "?" + values.Encode())
		}))
		hostcept.Intercept("/token", rack.Func(func(vars map[string]interface{}, next func()) {
			if parser.V(vars).FormValue("code") == "c0D3" {
				httper.V(vars).GetRequest().Header.Set("content-type", "application/json")
				httper.V(vars).SetMessageString("{\"access_token\":\"tokendata\",\"refresh_token\":\"refreshtoken1\",\"expires_in\":3600}")
			}
		}))
		hostcept.Intercept("/data", rack.Func(func(vars map[string]interface{}, next func()) {
			if auth := httper.V(vars).GetRequest().Header.Get("Authorization"); auth != "Bearer tokendata" {
				httper.V(vars).SetMessageString("Invalid authorization: " + auth)
			}
			httper.V(vars).SetMessageString("payload")
		}))
		hostrackup := rack.New()
		hostrackup.Add(sessioner.Middleware)
		hostrackup.Add(hostcept)

		this.middleware = hostrackup
	}
	return this.middleware
}
Ejemplo n.º 2
0
func (this tokenGetter) Run(vars map[string]interface{}, next func()) {
	//Step 1: Ensure states match
	r := httper.V(vars).GetRequest()
	if r == nil {
		next()
		return
	}

	state1 := r.FormValue("state")
	state2, isstring := (sessioner.V)(vars).Clear("state").(string)

	//if states don't match, it's a potential CSRF attempt; we're just going to pass it on, and a 404 will probably be passed back (unless this happens to route somewhere else too)
	//perhaps we should just return a 401-Unauthorized, though
	if !isstring || state1 != state2 {
		//	Warning: Potential CSRF attempt : states don't match
		next()
		return
	}

	//Step 2: Exchange the code for the token
	code := r.FormValue("code")
	t := &oauth.Transport{Config: this.o.Config()}
	tok, _ := t.Exchange(code)

	//Step 3: Have some other middleware handle whatever they're doing with the token (probably logging a user in)
	process := this.t(this.o, tok)
	process.Run(vars, next)
}
Ejemplo n.º 3
0
/*
Render() is a vars operation that will render a template onto the response
*/
func (vars V) Render(s string) {
	w := httper.V(vars).BlankResponse()
	err := (templater.V)(vars).Render(s, w)
	w.Save()

	if err != nil {
		(logger.V)(vars).Println(err.Error())
	}
}
Ejemplo n.º 4
0
// Run implements the rack.Middleware interface
func (this Interceptor) Run(vars map[string]interface{}, next func()) {
	url := httper.V(vars).GetRequest().URL.Path
	exec := this[url]
	if exec != nil {
		exec.Run(vars, next)
	} else {
		next()
	}
}
Ejemplo n.º 5
0
func Example_NoError() {
	rackup := rack.New()
	rackup.Add(ErrorHandler)
	rackup.Add(rack.Func(func(vars map[string]interface{}, next func()) {
		httper.V(vars).SetMessageString("Just Fine!")
	}))

	conn := httper.HttpConnection(":3002")
	go conn.Go(rackup)

	GetFrom("http://localhost:3002/")
	//output: Just Fine!
}
Ejemplo n.º 6
0
func Example_BasicError() {
	rackup := rack.New()
	rackup.Add(ErrorHandler)
	rackup.Add(rack.Func(func(vars map[string]interface{}, next func()) {
		httper.V(vars).SetMessageString("Just Fine!")
		array := make([]byte, 0)
		array[1] = 0 //this action results in a runtime error; we are indexing past the range of the slice
	}))

	conn := httper.HttpConnection(":4001")
	go conn.Go(rackup)

	GetFrom("http://localhost:4001/")
	//output: runtime error: index out of range
}
Ejemplo n.º 7
0
// Implementation of rack.Middleware
func (this Encapsulator) Run(vars map[string]interface{}, next func()) {
	next()

	layout, ok := vars[this.LayoutVar].(string)

	if !ok {
		logger.V(vars).Println("Layout not set")
		return
	}

	location := this.Folder + "/" + layout

	if !templater.V(vars).Exists(location) {
		//no "layout", just let it through
		logger.V(vars).Println("Layout \"" + layout + "\" not found")
		return
	}

	vars[this.BodyVar] = template.HTML(httper.V(vars).ResetMessage())

	w := httper.V(vars).FilledResponse()
	templater.V(vars).Render(location, w)
	w.Save()
}
Ejemplo n.º 8
0
//Run implements the rack.Middleware interface
func (this Statuser) Run(vars map[string]interface{}, next func()) {
	next()

	status := httper.V(vars).GetStatus()

	layout := strconv.Itoa(status)
	if templater.V(vars).Exists(this.Folder + "/" + layout) {
		vars[this.LayoutVar] = layout
		return
	}

	layout = strconv.Itoa(status/100) + "xx"
	if templater.V(vars).Exists(this.Folder + "/" + layout) {
		vars[this.ErrorVar] = strconv.Itoa(status)
		vars[this.LayoutVar] = layout
	}
}
Ejemplo n.º 9
0
		fmt.Println(err.Error())
		return
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	fmt.Println(string(body))
}

var ErrorWare rack.Func = func(vars map[string]interface{}, next func()) {
	httper.V(vars).StatusError()
}

var ErrorWare2 rack.Func = func(vars map[string]interface{}, next func()) {
	(httper.V)(vars).Status(501)
}

func Example_General() {
	rackup := rack.New()
	rackup.Add(templater.GetTemplates("test_templates"))
	rackup.Add(encapsulator.AddLayout)
	rackup.Add(SetErrorLayout)
	rackup.Add(ErrorWare)

	conn := httper.HttpConnection(":4014")
	go conn.Go(rackup)
Ejemplo n.º 10
0
		fmt.Println(err.Error())
		return
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	fmt.Println(string(body))
}

var HttpWare rack.Func = func(vars map[string]interface{}, next func()) {
	h := httper.V(vars)
	p := V(vars)

	name, named := p.FormGetValue("Name")
	if named {
		h.SetMessageString("Welcome, " + name)
	} else {
		h.SetMessageString("You are nameless")
	}
}

var FileWare rack.Func = func(vars map[string]interface{}, next func()) {
	h := httper.V(vars)
	p := V(vars)

	file, err := p.GetFile("file")
Ejemplo n.º 11
0
*/
package errorhandler

import (
	"fmt"
	"github.com/ScruffyProdigy/TheRack/httper"
	"github.com/ScruffyProdigy/TheRack/rack"
)

func getErrorString(rec interface{}) string {
	err, isError := rec.(error)

	if isError {
		return err.Error()
	}
	return fmt.Sprint(rec)
}

//ErrorHandler is the middleware that you insert into your rack.
// if any downstream Middleware panics, ErrorHandler will catch it and recover
var ErrorHandler rack.Func = func(vars map[string]interface{}, next func()) {
	defer func() {
		rec := recover()
		if rec != nil {
			httper.V(vars).StatusError()
			httper.V(vars).SetMessageString(getErrorString(rec))
		}
	}()
	next()
}
Ejemplo n.º 12
0
/*
	methoder allows for POST requests to pose as PUT or DELETE requests when necessary
*/
package methoder

import (
	"github.com/ScruffyProdigy/TheRack/httper"
	"github.com/ScruffyProdigy/TheRack/rack"
	"strings"
)

var legal = map[string]bool{"GET": true, "POST": true, "PUT": true, "DELETE": true}

func isLegal(s string) bool {
	return legal[s]
}

/*
Override is a Middleware that will override the method used in the request if a _method argument is passed in
*/
var Override rack.Func = func(vars map[string]interface{}, next func()) {
	r := httper.V(vars).GetRequest()
	method := strings.ToUpper(r.Form.Get("_method"))
	if isLegal(method) {
		r.Method = method
	}
	next()
}
Ejemplo n.º 13
0
	if resp.StatusCode != 200 {
		fmt.Print(resp.StatusCode)
	}

	fmt.Println(string(body))
}

var coins = map[string]string{"penny": "useless", "nickel": "heavy and annoying", "dime": "light and annoying", "quarter": "not obsolete quite yet"}

var RootWare rack.Func = func(vars map[string]interface{}, next func()) {
	(httper.V)(vars).SetMessageString("<html>Check out <a href='/coins'>My Coins</a></html>")
}

var CoinCollectionWare rack.Func = func(vars map[string]interface{}, next func()) {
	v := httper.V(vars)
	v.SetMessageString("<html><ul>")
	coinnames := []string{}
	for coin, _ := range coins {
		coinnames = append(coinnames, coin)
	}
	sort.Strings(coinnames)
	for _, coin := range coinnames {
		v.AppendMessageString("<li><a href='/coins/" + coin + "'>")
		v.AppendMessageString(strings.ToUpper(coin[:1]) + coin[1:])
		v.AppendMessageString("</a></li>")
	}
	v.AppendMessageString("</ul></html>")
}

func init() {