2013-06-18 19:02:56 +00:00
|
|
|
package httplog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-07-03 15:16:13 +00:00
|
|
|
var (
|
|
|
|
/* This default icon is empty with a long lived cache */
|
|
|
|
DefaultFavIcon FavIcon = defaultFavIcon{}
|
|
|
|
)
|
|
|
|
|
|
|
|
type defaultFavIcon struct {
|
|
|
|
}
|
|
|
|
|
2013-07-03 15:18:35 +00:00
|
|
|
func (dfi defaultFavIcon) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2013-07-03 15:16:13 +00:00
|
|
|
LogRequest(r, 200)
|
|
|
|
w.Header().Set("Cache-Control", "max-age=315360000")
|
|
|
|
}
|
|
|
|
|
|
|
|
/* simple interface for a favicon */
|
|
|
|
type FavIcon interface {
|
2013-07-03 15:18:35 +00:00
|
|
|
ServeHTTP(w http.ResponseWriter, r *http.Request)
|
2013-07-03 15:16:13 +00:00
|
|
|
}
|
|
|
|
|
2013-06-18 19:20:14 +00:00
|
|
|
// for debugging request headers
|
|
|
|
func LogHeaders(r *http.Request) {
|
|
|
|
fmt.Printf("HEADERS:\n")
|
|
|
|
for k, v := range r.Header {
|
|
|
|
fmt.Printf("\t%s\n", k)
|
|
|
|
for i, _ := range v {
|
|
|
|
fmt.Printf("\t\t%s\n", v[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-18 19:02:56 +00:00
|
|
|
/* kindof a common log type output */
|
|
|
|
func LogRequest(r *http.Request, statusCode int) {
|
|
|
|
var addr string
|
|
|
|
var user_agent string
|
|
|
|
|
|
|
|
user_agent = ""
|
2013-06-18 19:09:12 +00:00
|
|
|
addr = RealIP(r)
|
2013-06-18 19:02:56 +00:00
|
|
|
|
|
|
|
for k, v := range r.Header {
|
|
|
|
if k == "User-Agent" {
|
|
|
|
user_agent = strings.Join(v, " ")
|
|
|
|
}
|
|
|
|
if k == "X-Forwarded-For" {
|
|
|
|
addr = strings.Join(v, " ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%s - - [%s] \"%s %s\" \"%s\" %d %d\n",
|
|
|
|
addr,
|
2013-07-10 17:52:10 +00:00
|
|
|
time.Now().Format(time.RFC1123Z),
|
2013-06-18 19:02:56 +00:00
|
|
|
r.Method,
|
|
|
|
r.URL.String(),
|
|
|
|
user_agent,
|
|
|
|
statusCode,
|
|
|
|
r.ContentLength)
|
|
|
|
}
|
2013-06-18 19:09:12 +00:00
|
|
|
|
|
|
|
func RealIP(r *http.Request) (ip string) {
|
|
|
|
ip = r.RemoteAddr
|
|
|
|
|
|
|
|
port_pos := strings.LastIndex(ip, ":")
|
|
|
|
if port_pos != -1 {
|
|
|
|
ip = ip[0:port_pos]
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range r.Header {
|
|
|
|
if k == "X-Forwarded-For" {
|
|
|
|
ip = strings.Join(v, " ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ip
|
|
|
|
}
|