about summary refs log tree commit diff stats
path: root/051scenario_test.mu
Commit message (Expand)AuthorAgeFilesLines
* 3385Kartik K. Agaram2016-09-171-6/+6
* 3379Kartik K. Agaram2016-09-171-3/+3
* 3015 - more symbolic names in testsKartik K. Agaram2016-05-261-15/+15
* 2987Kartik K. Agaram2016-05-201-0/+70
* 2971Kartik K. Agaram2016-05-171-70/+0
* 2467 - rename 'string' to 'text' everywhereKartik K. Agaram2015-11-211-1/+1
* 2466 - eliminate ':string' from scenariosKartik K. Agaram2015-11-211-1/+1
* 1990 - extra ingredient for 'trace' depthKartik K. Agaram2015-08-131-1/+1
* 1868 - start using naked literals everywhereKartik K. Agaram2015-07-281-9/+9
* 1363 - rename 'integer' to 'number'Kartik K. Agaram2015-05-131-6/+6
* 1276 - make C++ version the defaultKartik K. Agaram2015-05-051-0/+70
pre>4695425 ^
dbc93a0 ^

07872ef ^
1168570 ^
ec2c4ed ^
1168570 ^

df79157 ^
dbc93a0 ^





a6f47c0 ^


07872ef ^



1168570 ^
d3d9df2 ^

1168570 ^
df1d1ef ^
747f4fb ^
1168570 ^


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

747f4fb ^
1168570 ^



b9d16fb ^
d3d9df2 ^
1168570 ^
4fa97ee ^
b6e7bb3 ^
1168570 ^
4fa97ee ^
b9d16fb ^
d3d9df2 ^
824556a ^
4fa97ee ^
1168570 ^
fc49c57 ^
824556a ^

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
77
78
79
                                                      

        
                 
             
             
             
                  
                 

 









                                                          

                                                            
                                                                  
                                                                         
 

                                                
 





                                                                  


                                                       



                                                                  
                                                  

 
                                                              
                                               
 


                                                                 
         
 
                               
 
 

                                                          
 



                                                                              
 
 
                              
                                           
 
                                        
                                                                                     
 
 
                                                                           
                                           
                                        
 

                                                                                                             
 
package svc // import "github.com/getwtxt/getwtxt/svc"

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

// content-type consts
const txtutf8 = "text/plain; charset=utf-8"
const htmlutf8 = "text/html; charset=utf-8"
const cssutf8 = "text/css; charset=utf-8"

// ipCtxKey is the Context value key for user IP addresses
type ipCtxKey int

const ctxKey ipCtxKey = iota

// 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 or X-Real-IP 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]
	}

	xRealIP := http.CanonicalHeaderKey("X-Real-IP")
	if _, ok := r.Header[xRealIP]; ok {
		proxied := r.Header[xRealIP]
		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))
	})
}

func log200(r *http.Request) {
	useragent := r.Header["User-Agent"]

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

func errHTTP(w http.ResponseWriter, r *http.Request, err error, code int) {
	useragent := r.Header["User-Agent"]
	uip := getIPFromCtx(r.Context())

	log.Printf("*** %v :: %v :: %v %v :: %v :: %v\n", uip, code, r.Method, r.URL, useragent, err.Error())
	http.Error(w, fmt.Sprintf("Error %v: %v", code, err.Error()), code)
}