This repository has been archived by the owner on Apr 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrack_request.go
82 lines (63 loc) · 1.55 KB
/
rack_request.go
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
package gorack
import (
"fmt"
"io"
"net/http"
"sort"
"strings"
)
type RackRequest struct {
Request *http.Request
SERVER_NAME string
SERVER_PORT string
}
func NewRackRequest(r *http.Request, serverName, serverPort string) *RackRequest {
return &RackRequest{
Request: r,
SERVER_NAME: serverName,
SERVER_PORT: serverPort,
}
}
func (rr *RackRequest) headers() http.Header {
r := rr.Request
headers := http.Header{
"SCRIPT_NAME": []string{""}, // TODO: build properly
"REQUEST_METHOD": []string{r.Method},
"PATH_INFO": []string{r.URL.Path},
"QUERY_STRING": []string{r.URL.RawQuery},
"SERVER_NAME": []string{rr.SERVER_NAME},
"SERVER_PORT": []string{rr.SERVER_PORT},
}
for k, val := range r.Header {
headers[http_header(k)] = val
}
return headers
}
func (rr *RackRequest) writeHeaders(out io.Writer, headers http.Header) error {
// sort keys so order is predictable
keys := make([]string, 0)
for k, _ := range headers {
keys = append(keys, k)
}
sort.Strings(keys)
for _, sort_key := range keys {
k, val := sort_key, headers[sort_key]
fmt.Fprintf(out, "%s: %s%s", k, strings.Join(val, "; "), delim)
}
_, err := out.Write([]byte(delim))
return err
}
func (r *RackRequest) WriteTo(w io.WriteCloser) error {
if err := r.writeHeaders(w, r.headers()); err != nil {
return err
}
if _, err := io.Copy(w, r.Request.Body); err != nil {
return err
}
w.Close()
return nil
}
func http_header(name string) string {
name = strings.Replace(name, "-", "_", -1)
return "HTTP_" + strings.ToUpper(name)
}