当前位置: 首页>>代码示例>>Golang>>正文


Golang Request.UserAgent方法代码示例

本文整理汇总了Golang中net/http.Request.UserAgent方法的典型用法代码示例。如果您正苦于以下问题:Golang Request.UserAgent方法的具体用法?Golang Request.UserAgent怎么用?Golang Request.UserAgent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在net/http.Request的用法示例。


在下文中一共展示了Request.UserAgent方法的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)
	}
}
开发者ID:trythings,项目名称:trythings,代码行数:26,代码来源:errors.go

示例2: 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)
	}
}
开发者ID:trythings,项目名称:trythings,代码行数:33,代码来源:errors.go

示例3: buildLogLine

// buildLogLine creates a common log format
// in addittion to the common fields, we also append referrer, user agent and request ID
func buildLogLine(l *responseLogger, r *http.Request, start time.Time) string {
	username := parseUsername(r)

	host, _, err := net.SplitHostPort(r.RemoteAddr)

	if err != nil {
		host = r.RemoteAddr
	}

	uri := r.URL.RequestURI()

	referer := r.Referer()

	userAgent := r.UserAgent()

	fields := []string{
		host,
		"-",
		detect(username, "-"),
		fmt.Sprintf("[%s]", start.Format("02/Jan/2006:15:04:05 -0700")),
		r.Method,
		uri,
		r.Proto,
		detect(strconv.Itoa(l.Status()), "-"),
		strconv.Itoa(l.Size()),
		detect(referer, "-"),
		detect(userAgent, "-"),
		r.Header.Get("Request-Id"),
	}

	return strings.Join(fields, " ")
}
开发者ID:pcn,项目名称:influxdb,代码行数:34,代码来源:response_logger.go

示例4: ServeHTTP

func (b *BouncerHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	reqParams := BouncerParamsFromValues(req.URL.Query())

	if reqParams.Product == "" {
		http.Redirect(w, req, "http://www.mozilla.org/", 302)
		return
	}

	if reqParams.OS == "" {
		reqParams.OS = DefaultOS
	}
	if reqParams.Lang == "" {
		reqParams.Lang = DefaultLang
	}

	isWinXpClient := isWindowsXPUserAgent(req.UserAgent())

	// If the client is not WinXP and attribution_code is set, redirect to the stub service
	if b.StubRootURL != "" &&
		reqParams.AttributionCode != "" &&
		reqParams.AttributionSig != "" &&
		!isWinXpClient {

		stubURL := b.stubAttributionURL(reqParams)
		http.Redirect(w, req, stubURL, 302)
		return
	}

	// HACKS
	// If the user is coming from windows xp or vista, send a sha1
	// signed product
	// HACKS
	if reqParams.OS == "win" && isWinXpClient {
		reqParams.Product = sha1Product(reqParams.Product)
	}

	url, err := b.URL(reqParams.Lang, reqParams.OS, reqParams.Product)
	if err != nil {
		http.Error(w, "Internal Server Error.", http.StatusInternalServerError)
		log.Println(err)
		return
	}
	if url == "" {
		http.NotFound(w, req)
		return
	}

	if b.CacheTime > 0 {
		w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d", b.CacheTime/time.Second))
	}

	// If ?print=yes, print the resulting URL instead of 302ing
	if reqParams.PrintOnly {
		w.Header().Set("Content-Type", "text/plain")
		w.Write([]byte(url))
		return
	}

	http.Redirect(w, req, url, 302)
}
开发者ID:mozilla-services,项目名称:go-bouncer,代码行数:60,代码来源:handlers.go

示例5: 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)
	}
}
开发者ID:owlfish,项目名称:forestbus-server,代码行数:26,代码来源:handler.go

示例6: HandlerEiffageQueryPost

func HandlerEiffageQueryPost(w http.ResponseWriter, r *http.Request) {
	log.Printf("Content-Type %s & Content-Length %d \n", r.Header.Get("Content-Type"), r.ContentLength)
	if r.Method == "POST" {
		b, err := ioutil.ReadAll(r.Body)
		if err != nil {
			log.Fatal("error handler: ", err)
		}
		//dont' write until you have read all the elements in the request
		log.Printf("User-Agent %s\n", r.UserAgent())
		log.Printf("Body %s\n", string(b))
		defer r.Body.Close()

		var resultFakeXml []byte
		if strings.Contains(string(b), "FROM Opportunity") {
			resultFakeXml = []byte(`
<result><records><sf:Id>123456</sf:Id><sf:OpportunityNumber__c>789012</sf:OpportunityNumber__c><sf:VP_ID__c>C3587C35-AFE8-4434-AD69-104C84025163</sf:VP_ID__c><sf:Name>Golang Salesforce Affaire</sf:Name></records></result>
				`)
		} else if strings.Contains(string(b), "FROM Ville__c") {
			resultFakeXml = []byte(`
<result><records><sf:Id>555666</sf:Id><sf:Name>NANTES</sf:Name><sf:Departement__c>44 - Loire-Atlantique</sf:Departement__c></records></result>
				`)
		}

		w.Write(resultFakeXml)
		log.Printf("xml_data %s\n", resultFakeXml)
		return
	}
}
开发者ID:jlandure,项目名称:go_server_mock,代码行数:28,代码来源:eiffagetp.go

示例7: ServeHTTP

// ServeHTTP wraps the http.Request and http.ResponseWriter to log to standard
// output and pass through to the underlying http.Handler.
func (al *ApacheLogger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	aw := &apacheLoggerResponseWriter{ResponseWriter: w}
	al.handler.ServeHTTP(aw, r)
	remoteAddr := r.RemoteAddr
	if index := strings.LastIndex(remoteAddr, ":"); index != -1 {
		remoteAddr = remoteAddr[:index]
	}
	referer := r.Referer()
	if "" == referer {
		referer = "-"
	}
	userAgent := r.UserAgent()
	if "" == userAgent {
		userAgent = "-"
	}
	username, _, _ := httpBasicAuth(r.Header)
	if "" == username {
		username = "-"
	}
	al.Printf(
		"%s %s %s [%v] \"%s %s %s\" %d %d \"%s\" \"%s\"\n",
		remoteAddr,
		"-", // We're not supporting identd, sorry.
		username,
		time.Now().Format("02/Jan/2006:15:04:05 -0700"),
		r.Method,
		r.RequestURI,
		r.Proto,
		aw.StatusCode,
		aw.Size,
		referer,
		userAgent,
	)
}
开发者ID:glycerine,项目名称:rmq,代码行数:36,代码来源:logger.go

示例8: IsMobile

func IsMobile(r *http.Request) bool {
	ua := r.UserAgent()
	if strings.Index(ua, "Mobile") != -1 || strings.Index(ua, "Android") != -1 {
		return true
	}
	return false
}
开发者ID:yulon,项目名称:gotor,代码行数:7,代码来源:device.go

示例9: ServeHTTP

func (h *ApacheLoggingHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
	clientIP := r.RemoteAddr
	if colon := strings.LastIndex(clientIP, ":"); colon != -1 {
		clientIP = clientIP[:colon]
	}

	record := &ApacheLogRecord{
		ResponseWriter: rw,
		ip:             clientIP,
		time:           time.Time{},
		method:         r.Method,
		uri:            r.RequestURI,
		protocol:       r.Proto,
		status:         http.StatusOK,
		referrer:       r.Referer(),
		userAgent:      r.UserAgent(),
		elapsedTime:    time.Duration(0),
	}

	startTime := time.Now()
	h.handler.ServeHTTP(record, r)
	finishTime := time.Now()

	record.time = finishTime.UTC()
	record.elapsedTime = finishTime.Sub(startTime)

	record.Log(h.out)
}
开发者ID:reedobrien,项目名称:dserve,代码行数:28,代码来源:main.go

示例10: isBanned

func isBanned(r *http.Request) bool {
	ctx := req2ctx(r)
	cdb := complaintdb.NewDB(ctx)
	u := user.Current(cdb.Ctx())
	userWhitelist := map[string]int{
		"[email protected]":    1,
		"[email protected]": 1,
		"[email protected]":  1,
		"[email protected]": 1,
	}

	reqBytes, _ := httputil.DumpRequest(r, true)

	cdb.Infof("remoteAddr: '%v'", r.RemoteAddr)
	cdb.Infof("user: '%v' (%s)", u, u.Email)
	cdb.Infof("inbound IP determined as: '%v'", getIP(r))
	cdb.Infof("HTTP req:-\n%s", string(reqBytes))

	if strings.HasPrefix(r.UserAgent(), "python") {
		cdb.Infof("User-Agent rejected")
		return true
	}
	if _, exists := userWhitelist[u.Email]; !exists {
		cdb.Infof("user not found in whitelist")
		return true
	}
	return false
}
开发者ID:skypies,项目名称:complaints,代码行数:28,代码来源:summary.go

示例11: SetValueFromRequest

func (n *Notice) SetValueFromRequest(req *http.Request) {
	if n.Request == nil {
		n.Request = &Request{}
	}
	n.Request.Url = req.URL.String()
	n.Request.AddCgiKeyValue("SERVER_SOFTWARE", "go")
	n.Request.AddCgiKeyValue("PATH_INFO", req.URL.Path)
	n.Request.AddCgiKeyValue("HTTP_HOST", req.Host)
	n.Request.AddCgiKeyValue("GATEWAY_INTERFACE", "CGI/1.1")
	n.Request.AddCgiKeyValue("REQUEST_METHOD", req.Method)
	n.Request.AddCgiKeyValue("QUERY_STRING", req.URL.RawQuery)
	n.Request.AddCgiKeyValue("REQUEST_URI", req.URL.RequestURI())
	n.Request.AddCgiKeyValue("REMOTE_ADDR", req.RemoteAddr)
	n.Request.AddCgiKeyValue("REMOTE_HOST", req.RemoteAddr)
	n.Request.AddCgiKeyValue("HTTP_ACCEPT", req.Header.Get("Accept-Encoding"))
	n.Request.AddCgiKeyValue("HTTP_USER_AGENT", req.UserAgent())
	env := os.Environ()
	for _, val := range env {
		pair := strings.Split(val, "=")
		if len(pair) < 2 {
			continue
		}
		if strings.Index(pair[0], "_KEY") >= 0 {
			continue
		}
		n.Request.AddCgiKeyValue(pair[0], pair[1])
	}
}
开发者ID:sunfmin,项目名称:goairbrake,代码行数:28,代码来源:notice.go

示例12: ServeHTTP

func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// From https://en.wikipedia.org/wiki/Common_Log_Format
	//
	// 127.0.0.1 user-identifier frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326
	const format = "%s - - [%s] \"%s %s %s\" %d %d \"%s\" \"%s\""
	const layout = "2/Jan/2006:15:04:05 -0700"

	userAgent := "-"
	if agent := r.UserAgent(); agent != "" {
		userAgent = agent
	}
	referer := "-"
	if ref := r.Referer(); ref != "" {
		referer = ref
	}

	shim := responseWriter{ResponseWriter: w}

	//start := time.Now()
	h.handler.ServeHTTP(&shim, r)
	end := time.Now()

	h.logf(format,
		strings.Split(r.RemoteAddr, ":")[0],
		end.Format(layout),
		r.Method,
		r.URL.RequestURI(),
		r.Proto,
		shim.status,
		shim.n,
		referer,
		userAgent)
}
开发者ID:danzilio,项目名称:meta-graphite,代码行数:33,代码来源:accesslog.go

示例13: LoggerMiddleware

// LoggerMiddleware log each request using
// comman log format
func LoggerMiddleware(c *Container, rw http.ResponseWriter, r *http.Request, next func()) {
	rw.(ResponseWriterExtra).SetLogger(c.MustGetLogger())
	start := time.Now()
	next()

	// @see https://en.wikipedia.org/wiki/Common_Log_Format for log format
	// @see http://httpd.apache.org/docs/1.3/logs.html#combined
	c.MustGetLogger().Info(
		fmt.Sprintf("%s %s %s [%s] \"%s %s %s\" %d %d \"%s\" \"%s\"",
			r.RemoteAddr,
			func() string {
				if c.CurrentUser() != nil {
					return fmt.Sprintf("%d", c.CurrentUser().ID)
				}
				return "-"
			}(),
			func() string {
				if c.CurrentUser() != nil {
					return c.CurrentUser().Username
				}
				return "-"
			}(),
			start.Format("Jan/02/2006:15:04:05 -0700 MST"),
			r.Method,
			r.RequestURI,
			r.Proto,
			c.ResponseWriter().Status(),
			rw.(ResponseWriterExtra).GetCurrentSize(),
			r.Referer(),
			r.UserAgent(),
		))

}
开发者ID:Mparaiso,项目名称:gonews,代码行数:35,代码来源:middlewares.go

示例14: 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())
}
开发者ID:668Jerry,项目名称:gogrs,代码行数:8,代码来源:main.go

示例15: PrefersHTML

// PrefersHTML returns true if the request was made by something that looks like a browser, or can receive HTML
func PrefersHTML(req *http.Request) bool {
	accepts := goautoneg.ParseAccept(req.Header.Get("Accept"))
	acceptsHTML := false
	acceptsJSON := false
	for _, accept := range accepts {
		if accept.Type == "text" && accept.SubType == "html" {
			acceptsHTML = true
		} else if accept.Type == "application" && accept.SubType == "json" {
			acceptsJSON = true
		}
	}

	// If HTML is accepted, return true
	if acceptsHTML {
		return true
	}

	// If JSON was specifically requested, return false
	// This gives browsers a way to make requests and add an "Accept" header to request JSON
	if acceptsJSON {
		return false
	}

	// In Intranet/Compatibility mode, IE sends an Accept header that does not contain "text/html".
	if strings.HasPrefix(req.UserAgent(), "Mozilla") {
		return true
	}

	return false
}
开发者ID:juanluisvaladas,项目名称:origin,代码行数:31,代码来源:httprequest.go


注:本文中的net/http.Request.UserAgent方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。