summaryrefslogtreecommitdiff
path: root/server/http.go
blob: 6e9c91e6b4838c5453deeb39628ca915ecfa2a86 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package server

import (
	"github.com/spiral/roadrunner"
	"net/http"
	"strings"
	"path"
	"github.com/sirupsen/logrus"
	"os"
	"path/filepath"
	"encoding/json"
	"fmt"
)

var (
	excludeFiles = []string{".php", ".htaccess"}
)

type Request struct {
	Protocol string            `json:"protocol"`
	Uri      string            `json:"uri"`
	Method   string            `json:"method"`
	Headers  http.Header       `json:"headers"`
	Cookies  map[string]string `json:"cookies"`
	RawQuery string            `json:"rawQuery"`
}

// Configures http rr
type HTTPConfig struct {
	// ServeStatic enables static file serving from desired root directory.
	ServeStatic bool

	// Root directory, required when ServeStatic set to true.
	Root string
}

// HTTP serves http connections to underlying PHP application using PSR-7 protocol. Context will include request headers,
// parsed files and query, payload will include parsed form data (if any) - todo: do we need to do that?.
type HTTP struct {
	cfg  HTTPConfig
	root http.Dir
	rr   *roadrunner.Server
}

func NewHTTP(cfg HTTPConfig, server *roadrunner.Server) *HTTP {
	h := &HTTP{cfg: cfg, rr: server}
	if cfg.ServeStatic {
		h.root = http.Dir(h.cfg.Root)
	}

	return h
}

// ServeHTTP serve using PSR-7 requests passed to underlying application.
func (h *HTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) () {
	if h.cfg.ServeStatic && h.serveStatic(w, r) {
		// serving static files
		return
	}

	// WHAT TO PUT TO BODY?
	p, err := h.buildPayload(r)
	rsp, err := h.rr.Exec(p)

	if err != nil {
		w.Write([]byte(err.Error()))
		return
	}

	// wrapping the response

	w.Header().Add("content-type", "text/html;charset=UTF-8")
	w.Write(rsp.Body)
}

// serveStatic attempts to serve static file and returns true in case of success, will return false in case if file not
// found, not allowed or on read error.
func (h *HTTP) serveStatic(w http.ResponseWriter, r *http.Request) bool {
	fpath := r.URL.Path
	if !strings.HasPrefix(fpath, "/") {
		fpath = "/" + fpath
	}
	fpath = path.Clean(fpath)

	if isForbidden(fpath) {
		logrus.Warningf("attempt to access forbidden file %s", fpath)
		return false
	}

	f, err := h.root.Open(fpath)
	if err != nil {
		if !os.IsNotExist(err) {
			// rr or access error
			logrus.Error(err)
		}

		return false
	}
	defer f.Close()

	d, err := f.Stat()
	if err != nil {
		// rr error
		logrus.Error(err)
		return false
	}

	if d.IsDir() {
		// we are not serving directories
		return false
	}

	http.ServeContent(w, r, d.Name(), d.ModTime(), f)
	return true
}

// todo: add files support
func (h *HTTP) buildPayload(r *http.Request) (*roadrunner.Payload, error) {
	request := Request{
		Protocol: r.Proto,
		Uri:      fmt.Sprintf("%s%s", r.Host, r.URL.String()),
		Method:   r.Method,
		Headers:  r.Header,
		Cookies:  make(map[string]string),
		RawQuery: r.URL.RawQuery,
	}

	logrus.Print(parseData(r))

	//logrus.Print(r.MultipartForm.File["kkk"][0].Header)
	//logrus.Print(r.MultipartForm.File["kkk"][0].Filename)

	// cookies
	for _, c := range r.Cookies() {
		request.Cookies[c.Name] = c.Value
	}

	data, _ := json.Marshal(request)

	logrus.Info(string(data))

	return &roadrunner.Payload{
		Context: data,
		Body:    []byte("lol"),
	}, nil
}

// isForbidden returns true if file has forbidden extension.
func isForbidden(path string) bool {
	ext := strings.ToLower(filepath.Ext(path))
	for _, exl := range excludeFiles {
		if ext == exl {
			return true
		}
	}

	return false
}

type postData map[string]interface{}

func (d postData) push(k string, v []string) {
	if len(v) == 0 {
		// doing nothing
		return
	}

	chunks := make([]string, 0)
	for _, chunk := range strings.Split(k, "[") {
		chunks = append(chunks, strings.Trim(chunk, "]"))
	}

	d.pushChunk(chunks, v)
}

func (d postData) pushChunk(k []string, v []string) {
	if len(v) == 0 {
		return
	}

	head := k[0]
	tail := k[1:]
	if len(k) == 1 {
		d[head] = v[0]
		return
	}

	// unnamed array
	if len(tail) == 1 && tail[0] == "" {
		d[head] = v
		return
	}

	if p, ok := d[head]; !ok {
		d[head] = make(postData)
		d[head].(postData).pushChunk(tail, v)
	} else {
		p.(postData).pushChunk(tail, v)
	}
}

// parse incoming data request into JSON (including multipart form data)
func parseData(r *http.Request) (*postData, error) {
	if r.Method != "POST" && r.Method != "PUT" && r.Method != "PATCH" {
		return nil, nil
	}

	r.ParseMultipartForm(32 << 20)

	data := make(postData)
	for k, v := range r.MultipartForm.Value {
		data.push(k, v)
	}

	jd, _ := json.Marshal(data)
	logrus.Warning(string(jd))

	return nil, nil
}