本文整理汇总了Golang中net/http.Request.Referer方法的典型用法代码示例。如果您正苦于以下问题:Golang Request.Referer方法的具体用法?Golang Request.Referer怎么用?Golang Request.Referer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/http.Request
的用法示例。
在下文中一共展示了Request.Referer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: send
func (s *loggingSender) send(ctx context.Context, r *http.Request, message string) {
payload := map[string]interface{}{
"eventTime": time.Now().In(time.UTC).Format(time.RFC3339Nano),
"message": message,
"serviceContext": s.serviceContext,
}
if r != nil {
payload["context"] = map[string]interface{}{
"httpRequest": map[string]interface{}{
"method": r.Method,
"url": r.Host + r.RequestURI,
"userAgent": r.UserAgent(),
"referrer": r.Referer(),
"remoteIp": r.RemoteAddr,
},
}
}
e := logging.Entry{
Severity: logging.Error,
Payload: payload,
}
err := s.logger.LogSync(ctx, e)
if err != nil {
log.Println("Error writing error report:", err, "report:", payload)
}
}
示例2: EditTaskFunc
//EditTaskFunc is used to edit tasks, handles "/edit/" URL
func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Redirect(w, r, "/", http.StatusBadRequest)
return
}
id, err := strconv.Atoi(r.URL.Path[len("/edit/"):])
if err != nil {
log.Println(err)
http.Redirect(w, r, "/", http.StatusBadRequest)
return
}
redirectURL := utils.GetRedirectUrl(r.Referer())
username := sessions.GetCurrentUserName(r)
task, err := db.GetTaskByID(username, id)
categories := db.GetCategories(username)
task.Categories = categories
task.Referer = redirectURL
if err != nil {
task.Message = "Error fetching Tasks"
}
editTemplate.Execute(w, task)
}
示例3: ServeHTTP
func (handler *Handler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
logMsg := &RequestLog{actualResponseWriter: writer}
logMsg.RequestHeaders = request.Header
logMsg.Method = request.Method
logMsg.Url = request.URL.String()
logMsg.Host = request.Host
logMsg.RemoteAddr = request.RemoteAddr
logMsg.Referer = request.Referer()
logMsg.UserAgent = request.UserAgent()
handler.wrappedHandler.ServeHTTP(logMsg, request)
// Get the resulting values
if logMsg.ResponseCode == 0 {
logMsg.ResponseCode = http.StatusOK
}
logMsg.ResponseHeaders = writer.Header()
// Fire off the event to Forest Bus
jsonMsg, err := json.Marshal(logMsg)
if err == nil {
sendErr := handler.msgBatcher.AsyncSendMessage(jsonMsg, nil, nil, handler.blockIfFull)
if sendErr != nil {
log.Printf("Warning: Error from AsyncSendMessage: %v\n", sendErr)
}
} else {
log.Printf("Warning: Unable to marshal request information to JSON: %v\n", err)
}
}
示例4: Handle
func Handle(w http.ResponseWriter, r *http.Request) {
context := appengine.NewContext(r)
now := time.Now()
expire := now.AddDate(30, 0, 0)
zcookie, _ := r.Cookie("z")
if zcookie == nil {
zcookie = &http.Cookie{}
zcookie.Name = "z"
zcookie.Value = make_hash("127.0.0.1", r.RemoteAddr, now.UnixNano())
zcookie.Expires = expire
zcookie.Path = "/"
zcookie.Domain = config.DOMAIN
http.SetCookie(w, zcookie)
}
context.Infof("%s", zcookie.Value)
w.Header().Set("Content-type", "image/gif")
w.Header().Set("Cache-control", "no-cache, must-revalidate")
w.Header().Set("Expires", "Sat, 26 Jul 1997 05:00:00 GMT")
fmt.Fprintf(w, "%s", GIF)
channel.Send(context, "pi", zcookie.Value+"\n"+r.RemoteAddr+"\n"+r.Referer()+"\n"+r.FormValue("r")+"\n"+r.UserAgent())
}
示例5: Login
func Login(session sessions.Session, re render.Render, r *http.Request) {
client_id := common.Config.OAuth2Client_ID
letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, 10)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
session.AddFlash(string(b), "state")
redirectBack := r.URL.Query().Get("redirect_back")
ref := r.Referer()
if redirectBack == "true" && ref != "" {
session.Set("redirect_to", ref)
} else {
session.Set("redirect_to", nil)
}
query := url.Values{}
query.Set("client_id", client_id)
query.Set("state", string(b))
query.Set("scope", "repo")
dest := url.URL{
Scheme: "https",
Host: "github.com",
Path: "/login/oauth/authorize",
RawQuery: query.Encode(),
}
re.Redirect(dest.String())
}
示例6: RedirectBack
func RedirectBack(w http.ResponseWriter, r *http.Request) {
url := r.Referer()
if url == "" {
url = "/"
}
http.Redirect(w, r, url, http.StatusSeeOther)
}
示例7: loginPage
func loginPage(page *page, req *http.Request) {
page.SetField("redirect", "/")
page.SetField("success", false)
switch req.Method {
case "POST":
page.SetField("redirect", req.FormValue("redirect"))
if req.FormValue("password") != page.main.conf.Password() {
page.AddMessage("Feil passord.")
} else {
err := page.main.auth.NewSession()
if err != nil {
page.Fatalf("Failed to create new session: %s", err.Error())
}
http.SetCookie(page, &http.Cookie{
Name: "session-id",
Value: page.main.auth.SessionID(),
HttpOnly: true,
})
page.AddMessage("Innloggin vellykket.")
page.SetField("success", true)
}
case "GET":
referrer := req.URL.Query().Get("redirect")
if referrer == "" {
referrer = req.Referer()
}
if referrer != "" {
page.SetField("redirect", referrer)
}
}
page.ExecuteTemplate("login")
}
示例8: UserLoginHandler
func UserLoginHandler(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
if !cas.IsAuthenticated(r) {
// save the referrer
sessionmw.Set(ctx, CASReferrer, r.Referer())
// shut off rendering
dataRenderer := data.FromContext(ctx)
dataRenderer.Type = data.DataNoRender
// and redirect
cas.RedirectToLogin(rw, r)
} else {
// get the referrer
referrer, has := sessionmw.Get(ctx, CASReferrer)
sessionmw.Delete(ctx, CASReferrer)
// shut off rendering
dataRenderer := data.FromContext(ctx)
dataRenderer.Type = data.DataNoRender
// and redirect
if !has {
http.Redirect(rw, r, render.ConvertURL("/"), http.StatusTemporaryRedirect)
} else {
http.Redirect(rw, r, referrer.(string), http.StatusTemporaryRedirect)
}
}
}
示例9: processForm
func (p *post) processForm(r *http.Request) error {
p.Date = time.Now()
p.Subject = "Message submitted through " + r.Referer()
if r.Form.Get("email") != "" {
p.Replyto = r.Form.Get("email")
}
for k, v := range r.Form {
if k == "_replyto" {
p.Replyto = v[0]
continue
}
if k == "_next" {
p.Next = v[0]
continue
}
if k == "_subject" {
p.Subject = v[0]
continue
}
if k == "_cc" {
p.CC = v
continue
}
p.Content[k] = v
}
return nil
}
示例10: LoginHandler
func LoginHandler(w http.ResponseWriter, r *http.Request) {
ref := r.Referer()
if ref == "" {
ref = "localhost"
}
service := service.NewMembershipService()
service.Init(ref)
m := r.Method
if m != "POST" {
server.JsonErr(w, errors.New("must POST when logging in"))
}
dec := json.NewDecoder(r.Body)
var reg models.Registration
err := dec.Decode(reg)
if err != nil {
server.JsonErr(w, err)
return
}
reg, err = service.Login(reg)
if err != nil {
server.JsonErr(w, err)
}
server.Json(w, reg)
return
}
示例11: Log
// Log is show viwer log.
func Log(req *http.Request) {
var userIP string
if userIP = req.Header.Get("X-FORWARDED-FOR"); userIP == "" {
userIP = req.RemoteAddr
}
log.Println(req.URL, userIP, req.UserAgent(), req.Form, req.Referer())
}
示例12: logInternal
func (c *Client) logInternal(ctx context.Context, r *http.Request, isPanic bool, msg string) {
payload := map[string]interface{}{
"eventTime": time.Now().In(time.UTC).Format(time.RFC3339Nano),
}
// limit the stack trace to 16k.
var buf [16384]byte
stack := buf[0:runtime.Stack(buf[:], false)]
payload["message"] = msg + "\n" + chopStack(stack, isPanic)
if r != nil {
payload["context"] = map[string]interface{}{
"httpRequest": map[string]interface{}{
"method": r.Method,
"url": r.Host + r.RequestURI,
"userAgent": r.UserAgent(),
"referrer": r.Referer(),
"remoteIp": r.RemoteAddr,
},
}
}
if c == nil {
log.Println("Error report used nil client:", payload)
return
}
payload["serviceContext"] = c.serviceContext
e := logging.Entry{
Level: logging.Error,
Payload: payload,
}
err := c.loggingClient.LogSync(e)
if err != nil {
log.Println("Error writing error report:", err, "report:", payload)
}
}
示例13: NewContext
func NewContext(req *http.Request) (*Context, error) {
sess, err := store.Get(req, Settings.Session.Name)
sess.Options.Domain = Settings.Session.Domain
sess.Options.HttpOnly = true
var (
lastUid string
user *User
)
if v, ok := sess.Values[kLastUid]; ok {
lastUid = v.(string)
}
if v, ok := sess.Values[kUserOL]; ok {
user = v.(*User)
}
referer := req.FormValue("referer")
if referer == "" {
referer = req.Referer()
}
ctx := &Context{
Vars: mux.Vars(req),
Session: sess,
ResUrl: Settings.ResUrl,
Referer: referer,
Version: VERSION,
LastUid: lastUid,
User: user,
}
if err != nil {
log.Printf("new context error: %s", err)
return ctx, err
}
return ctx, err
}
示例14: NewContext
func NewContext(app *App, res http.ResponseWriter, req *http.Request) *Context {
c := new(Context)
c.app = app
c.IsSend = false
c.IsEnd = false
c.Request = req
c.Url = req.URL.Path
c.RequestUrl = req.RequestURI
c.Method = req.Method
c.Host = req.Host
c.Ip = strings.Split(req.RemoteAddr, ":")[0]
c.IsAjax = req.Header.Get("X-Requested-With") == "XMLHttpRequest"
c.IsSSH = req.TLS == nil
c.UserAgent = req.UserAgent()
c.Referer = req.Referer()
c.eventsFunc = make(map[string][]reflect.Value)
c.BaseUrl = "://" + c.Host + "/"
if c.IsSSH {
c.BaseUrl = "https" + c.BaseUrl
} else {
c.BaseUrl = "http" + c.BaseUrl
}
c.Response = res
c.Status = 200
c.Header = make(map[string]string)
c.Header["Content-Type"] = "text/html;charset=UTF-8"
req.ParseForm()
return c
}
示例15: assetView
// assetView handles the requested assets.
// request /doc/gopher/pkg.png
// referer http://localhost:8000/?view=https://golang.org/pkg/
// returns https://golang.org/doc/gopher/pkg.png
func assetView(r *http.Request) (v string) {
referer := r.Referer()
if referer == "" {
return
}
refererURL, err := url.Parse(referer)
if err != nil {
return
}
if refererURL.Host != r.Host {
return
}
view := refererURL.Query().Get("view")
if view == "" {
return
}
baseView, err := url.Parse(view)
if err != nil {
return
}
nextView, err := baseView.Parse(r.URL.Path)
if err != nil {
return
}
v = nextView.String()
return
}