about summary refs log tree commit diff stats
path: root/README
diff options
context:
space:
mode:
authorAnselm R. Garbe <arg@suckless.org>2007-06-06 11:17:56 +0200
committerAnselm R. Garbe <arg@suckless.org>2007-06-06 11:17:56 +0200
commit4bf3b019535da3aa14cfccebd65ea726cbd15862 (patch)
treebfbb9877a5eb386f3d7e8870aa97edf7bbc00cab /README
parentff957d1eaca73800e59f640351dce35e3ab67f9e (diff)
downloaddwm-4bf3b019535da3aa14cfccebd65ea726cbd15862.tar.gz
I change the style of init somewhat, init as early as possible.
Diffstat (limited to 'README')
0 files changed, 0 insertions, 0 deletions
r/getwtxt/commit/http.go?h=v0.4.9&id=dbc93a011f442d09a18b7ae4ab34a8cfe64431dc'>dbc93a0 ^
1168570 ^
ec2c4ed ^
1168570 ^

df79157 ^
dbc93a0 ^





1168570 ^
d3d9df2 ^

1168570 ^
df1d1ef ^
747f4fb ^
1168570 ^


893123c ^
fc49c57 ^
df1d1ef ^
893123c ^
b9d16fb ^
1168570 ^

747f4fb ^
1168570 ^



b9d16fb ^
d3d9df2 ^
1168570 ^

b6e7bb3 ^
1168570 ^

b9d16fb ^
d3d9df2 ^
505c5d8 ^
37be25f ^
505c5d8 ^

37be25f ^
505c5d8 ^

1168570 ^

d2680fb ^
1168570 ^


d2680fb ^

1168570 ^

fc49c57 ^
1168570 ^


d6fbc25 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76


            
                 
             
             
                  
                 

 


                                                            
                                                                         
 

                                                
 





                                                                  
                                                  

 
                                                              
                                               
 


                                                                 
         
 
                               
 
 

                                                          
 



                                                                              
 
 

                              
 

                                                                    
 
 
                      
                                                                 

                                                                               
                                                                     

 

                                                                
 


                                                                               

 

                                                                
 


                                                                               
 
package main

import (
	"context"
	"log"
	"net"
	"net/http"
	"strings"
)

// Attaches a request's IP address to the request's context.
// If getwtxt is behind a reverse proxy, get the last entry
// in the X-Forwarded-For HTTP header as the user IP.
func newCtxUserIP(ctx context.Context, r *http.Request) context.Context {

	base := strings.Split(r.RemoteAddr, ":")
	uip := base[0]

	if _, ok := r.Header["X-Forwarded-For"]; ok {
		proxied := r.Header["X-Forwarded-For"]
		base = strings.Split(proxied[len(proxied)-1], ":")
		uip = base[0]
	}

	return context.WithValue(ctx, ctxKey, uip)
}

// Retrieves a request's IP address from the request's context
func getIPFromCtx(ctx context.Context) net.IP {

	uip, ok := ctx.Value(ctxKey).(string)
	if !ok {
		log.Printf("Couldn't retrieve IP from request\n")
	}

	return net.ParseIP(uip)
}

// Shim function to modify/pass context value to a handler
func ipMiddleware(hop http.Handler) http.Handler {

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := newCtxUserIP(r.Context(), r)
		hop.ServeHTTP(w, r.WithContext(ctx))
	})
}

// log output for 200s
func log200(r *http.Request) {

	uip := getIPFromCtx(r.Context())
	log.Printf("*** %v :: 200 :: %v %v\n", uip, r.Method, r.URL)
}

// log output for 400s
func log400(w http.ResponseWriter, r *http.Request, err string) {
	uip := getIPFromCtx(r.Context())
	log.Printf("*** %v :: 400 :: %v %v :: %v\n", uip, r.Method, r.URL, err)
	http.Error(w, "400 Bad Request: "+err, http.StatusBadRequest)
}

// log output for 404s
func log404(w http.ResponseWriter, r *http.Request, err error) {

	uip := getIPFromCtx(r.Context())
	log.Printf("*** %v :: 404 :: %v %v :: %v\n", uip, r.Method, r.URL, err)
	http.Error(w, err.Error(), http.StatusNotFound)
}

// log output for 500s
func log500(w http.ResponseWriter, r *http.Request, err error) {

	uip := getIPFromCtx(r.Context())
	log.Printf("*** %v :: 500 :: %v %v :: %v\n", uip, r.Method, r.URL, err)
	http.Error(w, err.Error(), http.StatusInternalServerError)
}