-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathspark.go
More file actions
468 lines (406 loc) · 12.4 KB
/
spark.go
File metadata and controls
468 lines (406 loc) · 12.4 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
var (
address = flag.String("address", "0.0.0.0", "Listening address")
port = flag.String("port", "8080", "Listening port")
sslPort = flag.String("sslPort", "10433", "SSL listening port")
path = flag.String("path", "/", "URL path")
deny = flag.String("deny", "", "Sensitive directory or file patterns to be denied when serving directory (comma separated)")
status = flag.Int("status", 200, "Returned HTTP status code")
cert = flag.String("cert", "cert.pem", "SSL certificate path")
key = flag.String("key", "key.pem", "SSL private Key path")
proxy = flag.String("proxy", "", "URL prefixes to be proxied to another server e.g. /api=>http://localhost:3000 will forward all requests starting with /api to http://localhost:3000 (comma separated)")
corsOrigin = flag.String("corsOrigin", "", "Allow CORS requests from this origin (can be '*')")
corsMethods = flag.String("corsMethods", "POST, GET, OPTIONS, PUT, DELETE", "Allowed CORS methods")
corsHeaders = flag.String("corsHeaders", "Content-Type, Authorization, X-Requested-With", "Allowed CORS headers")
contentType = flag.String("contentType", "", "Set response Content-Type")
mock = flag.String("mock", "", "Directory containing mock responses")
versionFlag = flag.Bool("version", false, "prints current version")
)
var (
version = "dev"
commit = "none"
date = "unknown"
builtBy = "goreleaser"
)
type bytesHandler []byte
func (h bytesHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(*status)
w.Write(h)
}
type proxyHandler struct {
prefix string
proxyURL string
}
func middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if *contentType != "" {
w.Header().Set("Content-Type", *contentType)
}
if corsOrigin != nil && *corsOrigin != "" {
// Set CORS headers
w.Header().Set("Access-Control-Allow-Origin", *corsOrigin)
w.Header().Set("Access-Control-Allow-Methods", *corsMethods)
w.Header().Set("Access-Control-Allow-Headers", *corsHeaders)
// Check if the request is a preflight request and handle it.
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
}
// Call the next handler, which can be another middleware in the chain, or the final handler.
next.ServeHTTP(w, r)
})
}
type echoHandler struct{}
func (eh *echoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var buffer bytes.Buffer
multiWriter := io.MultiWriter(w, &buffer)
// Buffer body
var bodyBytes []byte
if r.Body != nil {
bodyBytes, _ = io.ReadAll(r.Body)
r.Body.Close()
}
// Write REQUEST section
fmt.Fprintf(multiWriter, "=== REQUEST ===\n")
fmt.Fprintf(multiWriter, "Method: %s\n", r.Method)
fmt.Fprintf(multiWriter, "Path: %s\n", r.URL.Path)
if r.URL.RawQuery != "" {
fmt.Fprintf(multiWriter, "Query: %s\n", r.URL.RawQuery)
}
fmt.Fprintf(multiWriter, "Host: %s\n", r.Host)
fmt.Fprintf(multiWriter, "Protocol: %s\n", r.Proto)
fmt.Fprintf(multiWriter, "\n")
// Write HEADERS section
fmt.Fprintf(multiWriter, "=== HEADERS ===\n")
for name, values := range r.Header {
for _, value := range values {
fmt.Fprintf(multiWriter, "%s: %s\n", name, value)
}
}
fmt.Fprintf(multiWriter, "\n")
// Write BODY section
fmt.Fprintf(multiWriter, "=== BODY ===\n")
if len(bodyBytes) > 0 {
multiWriter.Write(bodyBytes)
fmt.Fprintf(multiWriter, "\n")
} else {
fmt.Fprintf(multiWriter, "(empty)\n")
}
// Log everything that was written
log.Print(buffer.String())
}
type mockHandler struct {
mockDir string
endpointPath string
}
func (mh *mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Construct the directory path for this endpoint
dirPath := filepath.Join(mh.mockDir, mh.endpointPath)
// Check if directory exists
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
http.NotFound(w, r)
return
}
// Read directory contents
entries, err := os.ReadDir(dirPath)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
log.Printf("Error reading mock directory %s: %v", dirPath, err)
return
}
// Find matching method file (case-insensitive)
var matchedFile string
var statusCode int
allowedMethods := []string{}
for _, entry := range entries {
if entry.IsDir() {
continue
}
fileName := entry.Name()
// Parse METHOD or METHOD_STATUS format
parts := strings.Split(fileName, "_")
method := strings.ToUpper(parts[0])
allowedMethods = append(allowedMethods, method)
if strings.ToUpper(r.Method) == method {
matchedFile = fileName
// Parse status code if present
if len(parts) > 1 {
if code, err := filepath.Match("[0-9]*", parts[1]); err == nil && code {
fmt.Sscanf(parts[1], "%d", &statusCode)
}
}
if statusCode == 0 {
statusCode = 200 // Default status code
}
break
}
}
// If no matching method found, return 405
if matchedFile == "" {
w.Header().Set("Allow", strings.Join(allowedMethods, ", "))
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
// Read file content
filePath := filepath.Join(dirPath, matchedFile)
content, err := os.ReadFile(filePath)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
log.Printf("Error reading mock file %s: %v", filePath, err)
return
}
// Detect content type from file extension if present
ext := filepath.Ext(matchedFile)
if ext == "" {
// Check for common extensions after the method name
nameParts := strings.Split(matchedFile, ".")
if len(nameParts) > 1 {
ext = "." + nameParts[len(nameParts)-1]
}
}
// Set content type based on extension
switch ext {
case ".json":
w.Header().Set("Content-Type", "application/json")
case ".xml":
w.Header().Set("Content-Type", "application/xml")
case ".html":
w.Header().Set("Content-Type", "text/html")
case ".txt":
w.Header().Set("Content-Type", "text/plain")
default:
// Try to detect from content
contentType := http.DetectContentType(content)
w.Header().Set("Content-Type", contentType)
}
// Write response
w.WriteHeader(statusCode)
w.Write(content)
}
func (ph *proxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
client := &http.Client{
Timeout: 5 * time.Second,
}
// Construct target URL with proper prefix removal and query parameter preservation
targetPath := strings.TrimPrefix(req.URL.Path, ph.prefix)
targetURL := ph.proxyURL + targetPath
if req.URL.RawQuery != "" {
targetURL += "?" + req.URL.RawQuery
}
if req.URL.Fragment != "" {
targetURL += "#" + req.URL.Fragment
}
request, err := http.NewRequest(req.Method, targetURL, req.Body)
if err != nil {
log.Println("error creating proxy request: ", err)
http.Error(w, "Bad Gateway", http.StatusBadGateway)
return
}
request.Header = req.Header
response, err := client.Do(request)
if err != nil {
log.Print("error response from proxy: ", err)
http.Error(w, "Bad Gateway", http.StatusBadGateway)
return
}
defer response.Body.Close()
// Copy all headers from proxied response
for key, values := range response.Header {
w.Header().Del(key)
for _, value := range values {
w.Header().Add(key, value)
}
}
// Override with our CORS settings if configured
if *corsOrigin != "" {
w.Header().Set("Access-Control-Allow-Origin", *corsOrigin)
w.Header().Set("Access-Control-Allow-Methods", *corsMethods)
w.Header().Set("Access-Control-Allow-Headers", *corsHeaders)
}
// Write status code from proxied response
w.WriteHeader(response.StatusCode)
// Copy response body
if _, err := io.Copy(w, response.Body); err != nil {
log.Print("error copying the response from proxy: ", err)
}
}
func isDenied(path, denyList string) bool {
if len(denyList) == 0 {
return false
}
for _, pathElement := range strings.Split(path, string(filepath.Separator)) {
for _, denyElement := range strings.Split(denyList, ",") {
match, err := filepath.Match(strings.TrimSpace(denyElement), pathElement)
if err != nil {
log.Print("error matching file path element: ", err)
}
if match {
return true
}
}
}
return false
}
func parseProxy(flagStr string) (handlers []*proxyHandler) {
proxyList := strings.Split(flagStr, ",")
for _, proxyRedirect := range proxyList {
proxyElements := strings.Split(proxyRedirect, "=>")
if len(proxyElements) == 2 {
prefix := strings.TrimSpace(proxyElements[0])
proxyURL := strings.TrimSpace(proxyElements[1])
if strings.HasPrefix(prefix, "/") && strings.HasPrefix(proxyURL, "http") {
handlers = append(handlers, &proxyHandler{
prefix: prefix,
proxyURL: proxyURL,
})
} else {
log.Printf("bad proxy pair: %s=>%s", prefix, proxyURL)
}
}
}
return
}
func walkMockDir(mockDir string) (handlers []*mockHandler) {
// Check if mock directory exists
if _, err := os.Stat(mockDir); os.IsNotExist(err) {
log.Printf("Warning: mock directory does not exist: %s", mockDir)
return
}
// Walk the directory tree
err := filepath.Walk(mockDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Skip the root directory itself
if path == mockDir {
return nil
}
// Only process directories
if !info.IsDir() {
return nil
}
// Check if this directory contains any HTTP verb files
entries, err := os.ReadDir(path)
if err != nil {
return nil // Skip directories we can't read
}
hasVerbFiles := false
for _, entry := range entries {
if !entry.IsDir() {
// Check if filename starts with a common HTTP verb
name := strings.ToUpper(entry.Name())
if strings.HasPrefix(name, "GET") ||
strings.HasPrefix(name, "POST") ||
strings.HasPrefix(name, "PUT") ||
strings.HasPrefix(name, "DELETE") ||
strings.HasPrefix(name, "PATCH") ||
strings.HasPrefix(name, "HEAD") ||
strings.HasPrefix(name, "OPTIONS") {
hasVerbFiles = true
break
}
}
}
if hasVerbFiles {
// Calculate the endpoint path relative to mock directory
relPath, err := filepath.Rel(mockDir, path)
if err != nil {
return nil
}
// Convert to URL path (use forward slashes)
endpointPath := "/" + filepath.ToSlash(relPath)
handlers = append(handlers, &mockHandler{
mockDir: mockDir,
endpointPath: relPath,
})
log.Printf("Registered mock endpoint: %s", endpointPath)
}
return nil
})
if err != nil {
log.Printf("Error walking mock directory: %v", err)
}
return
}
type protectedFileSystem struct {
fs http.FileSystem
}
func (pfs protectedFileSystem) Open(path string) (http.File, error) {
if isDenied(path, *deny) {
return nil, os.ErrPermission
}
return pfs.fs.Open(path)
}
func main() {
flag.Parse()
if *versionFlag {
fmt.Printf("spark version %s, commit %s, built at %s by %s\n", version, commit, date, builtBy)
os.Exit(0)
}
listen := *address + ":" + *port
listenTLS := *address + ":" + *sslPort
body := flag.Arg(0)
if body == "" {
body = "."
}
var handler http.Handler
if fi, err := os.Stat(body); err == nil {
switch mode := fi.Mode(); {
case mode.IsDir():
if *deny == "" {
log.Print("Warning: serving files without any filter!")
}
handler = http.StripPrefix(*path, http.FileServer(protectedFileSystem{http.Dir(body)}))
case mode.IsRegular():
if content, err := os.ReadFile(body); err != nil {
log.Fatal("Error reading file: ", err)
} else {
handler = bytesHandler(content)
}
}
} else {
handler = bytesHandler(body)
}
// Register mock handlers first (if mock mode is enabled) to ensure priority
if mock != nil && *mock != "" {
mockHandlers := walkMockDir(*mock)
for _, mh := range mockHandlers {
// Convert endpoint path to URL path with forward slashes
urlPath := "/" + filepath.ToSlash(mh.endpointPath)
http.Handle(urlPath, middleware(mh))
}
}
http.Handle(*path, middleware(handler))
http.Handle("/echo", middleware(&echoHandler{}))
if proxy != nil {
proxyHandlers := parseProxy(*proxy)
for _, ph := range proxyHandlers {
log.Printf("sending %s to %s", ph.prefix, ph.proxyURL)
http.Handle(ph.prefix, middleware(ph))
}
}
go func() {
if _, err := os.Stat(*cert); err != nil {
return
}
if _, err := os.Stat(*key); err != nil {
return
}
log.Fatal(http.ListenAndServeTLS(listenTLS, *cert, *key, nil))
}()
log.Printf("Serving %s on %s%s...", body, listen, *path)
log.Fatal(http.ListenAndServe(listen, nil))
}