about summary refs log tree commit diff stats
path: root/chessboard.mu
Commit message (Expand)AuthorAgeFilesLines
* 2260 - start tracing by depth rather than labelKartik K. Agaram2015-10-061-1/+1
* 2244Kartik K. Agaram2015-10-051-3/+3
* 2095Kartik K. Agaram2015-08-281-8/+0
* 2051Kartik K. Agaram2015-08-211-12/+12
* 1884Kartik K. Agaram2015-07-291-27/+0
* 1880 - switch .mu files to new type-deducing idiomKartik K. Agaram2015-07-291-156/+156
* 1869 - rename the /deref property to /lookupKartik K. Agaram2015-07-281-34/+34
* 1868 - start using naked literals everywhereKartik K. Agaram2015-07-281-133/+133
* 1808 - helper to print newlines during debuggingKartik K. Agaram2015-07-171-62/+20
* 1780 - now we always reclaim local scopesKartik K. Agaram2015-07-131-10/+10
* 1773 - update all mu recipes to new-default-spaceKartik K. Agaram2015-07-131-10/+10
* 1620Kartik K. Agaram2015-06-221-5/+18
* 1618Kartik K. Agaram2015-06-211-5/+5
* 1599Kartik K. Agaram2015-06-191-12/+12
* 1482Kartik K. Agaram2015-05-261-3/+2
* 1480Kartik K. Agaram2015-05-261-1/+1
* 1431Kartik K. Agaram2015-05-231-3/+3
* 1418 - starting trace-browser implementation in C++Kartik K. Agaram2015-05-221-1/+3
* 1408Kartik K. Agaram2015-05-191-4/+4
* 1401 - shave 80% off chessboard test timeKartik K. Agaram2015-05-181-0/+1
* 1397 - support unicode in screen checksKartik K. Agaram2015-05-181-2/+3
* 1375 - cleanupKartik K. Agaram2015-05-141-4/+3
* 1374 - chessboard end-to-end test passes!Kartik K. Agaram2015-05-141-16/+37
* 1372 - new chessboard test no longer hangingKartik K. Agaram2015-05-141-16/+23
* 1371Kartik K. Agaram2015-05-141-16/+0
* 1369Kartik K. Agaram2015-05-141-17/+17
* 1368 - alias carriage-return and newlineKartik K. Agaram2015-05-141-5/+62
* 1363 - rename 'integer' to 'number'Kartik K. Agaram2015-05-131-135/+135
* 1362Kartik K. Agaram2015-05-131-2/+2
* 1340Kartik K. Agaram2015-05-111-1/+4
* 1339Kartik K. Agaram2015-05-101-42/+42
* 1338Kartik K. Agaram2015-05-101-2/+2
* 1337Kartik K. Agaram2015-05-101-0/+2
* 1336Kartik K. Agaram2015-05-101-6/+18
* 1335Kartik K. Agaram2015-05-101-28/+2
* 1334Kartik K. Agaram2015-05-101-16/+18
* 1331Kartik K. Agaram2015-05-101-1/+1
* 1329Kartik K. Agaram2015-05-101-0/+1
* 1328 - another error messageKartik K. Agaram2015-05-101-0/+36
* 1327 - better error handling in chessboardKartik K. Agaram2015-05-101-43/+99
* 1326Kartik K. Agaram2015-05-101-23/+0
* 1323 - keyboard supports backspace and newlineKartik K. Agaram2015-05-101-0/+6
* 1321 - *finally*, fixed the chessboard sluggishnessKartik K. Agaram2015-05-101-3/+0
* 1316Kartik K. Agaram2015-05-091-4/+4
* 1315 - chessboard now working interactivelyKartik K. Agaram2015-05-091-2/+68
* 1312Kartik K. Agaram2015-05-081-35/+23
* 1311Kartik K. Agaram2015-05-081-0/+1
* 1306Kartik K. Agaram2015-05-071-0/+83
* 1305Kartik K. Agaram2015-05-071-2/+79
* 1301 - back to the chessboardKartik K. Agaram2015-05-071-0/+176
2019-06-12 21:11:48 -0400 committer Ben Morrison <ben@gbmor.dev> 2019-06-12 21:20:04 -0400 separate request and message log' href='/gbmor/getwtxt/commit/svc/http.go?h=v0.4.12&id=979e1f93d563637bcca99bcc56a03199807b92d8'>979e1f9 ^
b9d16fb ^
d3d9df2 ^
e53b843 ^

824556a ^
4fa97ee ^
1168570 ^
979e1f9 ^
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95


















                                                                    
                                                      

        
                 
             
             
             
                  
                 

 









                                                          

                                                            
                                                                  
                                                                         

                                                
 





                                                                  


                                                       



                                                                  
                                                  

 
                                                              
                                               


                                                                 
         
                               
 
 

                                                          



                                                                              
 
 
                                      
                              
                                           
                                        
                                                                                        
 
 

                                                          
                                                                           
                                           
                                        
                                                                                                                
                                                                           
 
/*
Copyright (c) 2019 Ben Morrison (gbmor)

This file is part of Getwtxt.

Getwtxt is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Getwtxt is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Getwtxt.  If not, see <https://www.gnu.org/licenses/>.
*/

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))
	})
}

// Appends a 200 OK to the request log
func log200(r *http.Request) {
	useragent := r.Header["User-Agent"]
	uip := getIPFromCtx(r.Context())
	reqLog.Printf("*** %v :: 200 :: %v %v :: %v\n", uip, r.Method, r.URL, useragent)
}

// Appends a request of a given status code to the request
// log. Intended for errors.
func errHTTP(w http.ResponseWriter, r *http.Request, err error, code int) {
	useragent := r.Header["User-Agent"]
	uip := getIPFromCtx(r.Context())
	reqLog.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)
}