Example #1
0
// Handler for /info/ to see a JSON view of some server context.
func Info(w http.ResponseWriter, r *http.Request) {
	context, err := context.FromRequest(r)
	if err != nil {
		view.Error(w, r, err)
		return
	}
	info := map[string]interface{}{
		"request": map[string]interface{}{
			"method": r.Method,
			"form":   r.Form,
			"url": map[string]interface{}{
				"path":  r.URL.Path,
				"query": r.URL.RawQuery,
			},
			"headers": headerMap(r.Header),
		},
		"context":    context,
		"pageTabURL": context.PageTabURL("/"),
		"canvasURL":  context.CanvasURL("/"),
		"sdkURL":     context.SdkURL(),
	}
	if version != "" {
		info["version"] = version
	}
	humanJSON(info, w, r)
}
Example #2
0
// Parse the Context and an Example.
func parse(r *http.Request) (*context.Context, *examples.Example, error) {
	context, err := context.FromRequest(r)
	if err != nil {
		return nil, nil, err
	}
	example, err := exampleStore.Load(context.Version, r.URL.Path)
	if err != nil {
		return nil, nil, err
	}
	return context, example, nil
}
Example #3
0
func SdkChannel(w http.ResponseWriter, r *http.Request) {
	const maxAge = 31536000 // 1 year
	context, err := context.FromRequest(r)
	if err != nil {
		view.Error(w, r, err)
		return
	}
	service.Stats.Inc("viewed channel")
	w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", maxAge))
	view.Write(w, r, &h.Script{Src: context.SdkURL()})
}
Example #4
0
func List(w http.ResponseWriter, r *http.Request) {
	context, err := context.FromRequest(r)
	if err != nil {
		view.Error(w, r, err)
		return
	}
	service.Stats.Inc("viewed examples listing")
	view.Write(w, r, &examplesList{
		Context: context,
		DB:      examples.GetDB(context.Version),
	})
}
Example #5
0
func fromValues(t *testing.T, values url.Values) *context.Context {
	req, err := http.NewRequest(
		"GET",
		"http://www.fbrell.com/?"+values.Encode(),
		nil)
	if err != nil {
		t.Fatalf("Failed to create request: %s", err)
	}
	ctx, err := context.FromRequest(req)
	if err != nil {
		t.Fatalf("Failed to create context: %s", err)
	}
	return ctx
}
Example #6
0
func Saved(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" && r.URL.Path == savedPath {
		c, err := context.FromRequest(r)
		if err != nil {
			view.Error(w, r, err)
			return
		}
		if !xsrf.Validate(r.FormValue(paramName), w, r, savedPath) {
			service.Stats.Inc(savedPath + " xsrf failure")
			view.Error(w, r, errTokenMismatch)
			return
		}
		content := bytes.TrimSpace([]byte(r.FormValue("code")))
		content = bytes.Replace(content, []byte{13}, nil, -1) // remove CR
		id := examples.ContentID(content)
		db := examples.GetDB(c.Version)
		example, ok := db.Reverse[id]
		if ok {
			http.Redirect(w, r, c.ViewURL(example.URL), 302)
			return
		}
		err = exampleStore.Save(id, content)
		if err != nil {
			view.Error(w, r, err)
			return
		}
		service.Stats.Inc("saved example")
		http.Redirect(w, r, c.ViewURL(savedPath+id), 302)
		return
	} else {
		context, example, err := parse(r)
		if err != nil {
			view.Error(w, r, err)
			return
		}
		service.Stats.Inc("viewed saved example")
		view.Write(w, r, &page{
			Writer:  w,
			Request: r,
			Context: context,
			Example: example,
		})
	}
}
Example #7
0
func Response(w http.ResponseWriter, r *http.Request) {
	c, err := context.FromRequest(r)
	if err != nil {
		view.Error(w, r, err)
		return
	}
	if r.FormValue("state") != state(w, r) {
		view.Error(w, r, errInvalidState)
		return
	}
	values := url.Values{}
	values.Set("client_id", strconv.FormatUint(fbapp.Default.ID(), 10))
	values.Set("client_secret", fbapp.Default.Secret())
	values.Set("redirect_uri", redirectURI(c))
	values.Set("code", r.FormValue("code"))
	res, err := fbapi.GetRaw("/oauth/access_token", values)
	if err != nil {
		view.Error(w, r, err)
		return
	}
	w.Write(res)
}
Example #8
0
func Start(w http.ResponseWriter, r *http.Request) {
	c, err := context.FromRequest(r)
	if err != nil {
		view.Error(w, r, err)
		return
	}
	values := url.Values{}
	values.Set("client_id", strconv.FormatUint(c.AppID, 10))
	if scope := r.FormValue("scope"); scope != "" {
		values.Set("scope", scope)
	}

	if c.ViewMode == context.Website {
		values.Set("redirect_uri", redirectURI(c))
		values.Set("state", state(w, r))
	} else {
		values.Set("redirect_uri", c.ViewURL("/auth/session"))
	}

	dialogURL := fburl.URL{
		Scheme:    "https",
		SubDomain: fburl.DWww,
		Env:       c.Env,
		Path:      "/dialog/oauth",
		Values:    values,
	}

	if c.ViewMode == context.Website {
		http.Redirect(w, r, dialogURL.String(), 302)
	} else {
		b, _ := json.Marshal(dialogURL.String())
		view.Write(w, r, &h.Script{
			Inner: h.Unsafe(fmt.Sprintf("top.location=%s", b)),
		})
	}
}