File: README.md

package info (click to toggle)
golang-github-sebest-xff 0.0~git20160910.6c115e0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, buster-backports
  • size: 88 kB
  • sloc: makefile: 2
file content (44 lines) | stat: -rw-r--r-- 1,114 bytes parent folder | download | duplicates (4)
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
# X-Forwarded-For middleware fo Go [![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/sebest/xff) [![Build Status](https://travis-ci.org/sebest/xff.svg?branch=master)](https://travis-ci.org/sebest/xff)

Package `xff` is a `net/http` middleware/handler to parse [Forwarded HTTP Extension](http://tools.ietf.org/html/rfc7239) in Golang.

## Example usage

Install `xff`:

    go get github.com/sebest/xff

Edit `server.go`:

```go
package main

import (
  "net/http"

  "github.com/sebest/xff"
)

func main() {
  handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("hello from " + r.RemoteAddr + "\n"))
  })

  xffmw, _ := xff.Default()
  http.ListenAndServe(":8080", xffmw.Handler(handler))
}
```

Then run your server:

    go run server.go

The server now runs on `localhost:8080`:

    $ curl -D - -H 'X-Forwarded-For: 42.42.42.42' http://localhost:8080/
    HTTP/1.1 200 OK
    Date: Fri, 20 Feb 2015 20:03:02 GMT
    Content-Length: 29
    Content-Type: text/plain; charset=utf-8

    hello from 42.42.42.42:52661