-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
408 lines (350 loc) · 11 KB
/
Copy pathhandler.go
File metadata and controls
408 lines (350 loc) · 11 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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"github.com/nikolalohinski/gonja/v2/exec"
)
func createHandler(app *App, path string, pathParams []string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
app.hitsProcessed.Add(1)
// Trim the leading slash for consistency with cache keys
trimmedPath := strings.TrimPrefix(path, "/")
content, ok := app.sqlCache[trimmedPath]
if !ok {
http.Error(w, "Route not found in cache: "+trimmedPath, http.StatusNotFound)
return
}
conn, err := app.DB.Conn(r.Context())
if err != nil { /* handle error */
http.Error(w, "Error connecting database: "+err.Error(), http.StatusInternalServerError)
return
}
defer conn.Close()
_, err = conn.ExecContext(r.Context(), "ATTACH DATABASE ':memory:' AS wtfhttpd;")
if err != nil { /* handle error */
http.Error(w, "Error attaching in-memory database: "+err.Error(), http.StatusInternalServerError)
return
}
defer func() {
if _, err := conn.ExecContext(r.Context(), "DETACH DATABASE wtfhttpd;"); err != nil {
log.Printf("Error detaching in-memory database: %v", err)
}
}()
// Create a transaction to work with temporary tables
tx, err := conn.BeginTx(r.Context(), nil)
if err != nil {
http.Error(w, "Error starting transaction: "+err.Error(), http.StatusInternalServerError)
return
}
defer tx.Rollback() // Will be ignored if transaction is committed
if err := setupTemporaryTables(tx); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := populateTemporaryTables(tx, r, pathParams, app.Config); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
varsMap := make(map[string]interface{})
// First add path parameters (highest precedence)
for _, param := range pathParams {
varsMap[param] = r.PathValue(param)
}
// Parse form data (including multipart forms)
if err := r.ParseForm(); err == nil {
// Add form parameters (second precedence)
for key, values := range r.Form {
// Check if this is an array parameter
if strings.HasSuffix(key, "[]") {
// Remove the [] suffix and store as JSON string
arrayKey := key[:len(key)-2]
if _, exists := varsMap[arrayKey]; !exists {
jsonData, err := json.Marshal(values)
if err == nil {
varsMap[arrayKey] = string(jsonData)
}
}
} else if _, exists := varsMap[key]; !exists {
// For non-array parameters, use the first value if not already set by path params
if len(values) > 0 {
varsMap[key] = values[0]
}
}
}
}
// Add query parameters (lowest precedence)
for key, values := range r.URL.Query() {
// Check if this is an array parameter
if strings.HasSuffix(key, "[]") {
// Remove the [] suffix and store as JSON string
arrayKey := key[:len(key)-2]
if _, exists := varsMap[arrayKey]; !exists {
jsonData, err := json.Marshal(values)
if err == nil {
varsMap[arrayKey] = string(jsonData)
}
}
} else if _, exists := varsMap[key]; !exists {
// For non-array parameters, use the first value if not already set by path or form params
if len(values) > 0 {
varsMap[key] = values[0]
}
}
}
parsedQueries := ParseQueries(string(content))
results := make(map[string][]map[string]any)
for _, query := range parsedQueries {
validations := make(map[string]any)
// log.Printf("Processing query: %s\n", query.Query)
// if len(query.Directives) > 0 {
// log.Printf("Query directives: %+v\n", query.Directives)
// }
for _, directive := range query.Directives {
if directive.name == "validate" && len(directive.params) >= 2 {
validations[directive.params[0]] = directive.params[1]
}
}
if len(validations) > 0 {
errs := app.vd.ValidateMap(varsMap, validations)
if len(errs) > 0 {
// Validation failed
// log.Printf("%+v", errs)
http.Error(w, fmt.Sprintf("Validation error: %v", errs), http.StatusBadRequest)
return
}
// log.Printf("Validation passed for fields: %v", validations)
}
result, err := executeQuery(tx, query.Query, varsMap)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Check for store directive
storeDirectiveFound := false
for _, directive := range query.Directives {
if directive.name == "store" && len(directive.params) > 0 {
// Store the result under the specified key
storeKey := directive.params[0]
results[storeKey] = result
// log.Printf("Stored query result in '%s'", storeKey)
storeDirectiveFound = true
break
}
}
// If no store directive was found, store the result in the "ctx" key
if !storeDirectiveFound {
results["ctx"] = result
// log.Printf("No store directive found, stored query result in 'ctx'")
}
for _, directive := range query.Directives {
if directive.name == "capture" && len(directive.params) > 0 {
varName := directive.params[0]
isScalar := false
if len(directive.params) == 2 && directive.params[1] == "single" {
isScalar = true
}
if isScalar && len(result) > 0 {
// For scalar, get the first value of the first row
for _, val := range result[0] {
varsMap[varName] = val
break
}
} else {
// Otherwise store the entire result set as JSON
jsonData, err := json.Marshal(result)
if err == nil {
varsMap[varName] = string(jsonData)
}
}
}
}
}
statusCode := http.StatusOK
responseHeaders := make(map[string]string)
rows, err := tx.Query("SELECT name, value FROM response_meta")
tplName := ""
if err == nil {
defer rows.Close()
for rows.Next() {
var name, value string
if err := rows.Scan(&name, &value); err != nil {
continue
}
if strings.ToLower(name) == "status" {
if code, err := strconv.Atoi(value); err == nil && code >= 100 && code < 600 {
statusCode = code
}
} else if strings.ToLower(name) == "wtf-tpl" {
tplName = value
} else {
responseHeaders[name] = value
}
}
}
// Handle response cookies
rows, err = tx.Query("SELECT name, value, max_age, expires, path, domain, secure, http_only, same_site FROM response_cookies")
if err != nil {
log.Printf("Error querying response cookies: %v", err)
} else {
defer rows.Close()
// log.Printf("Processing response cookies")
for rows.Next() {
var name, value, path, domain, sameSite string
var maxAge, secure, httpOnly sql.NullInt64
var expires sql.NullTime
if err := rows.Scan(&name, &value, &maxAge, &expires, &path, &domain, &secure, &httpOnly, &sameSite); err != nil {
log.Printf("Error scanning cookie row: %v", err)
continue
}
cookie := http.Cookie{
Name: name,
Value: value,
Path: path,
}
if maxAge.Valid {
cookie.MaxAge = int(maxAge.Int64)
}
if expires.Valid {
cookie.Expires = expires.Time
}
if domain != "" {
cookie.Domain = domain
}
if secure.Valid && secure.Int64 > 0 {
cookie.Secure = true
}
if httpOnly.Valid && httpOnly.Int64 > 0 {
cookie.HttpOnly = true
}
if sameSite != "" {
switch sameSite {
case "Strict":
cookie.SameSite = http.SameSiteStrictMode
case "Lax":
cookie.SameSite = http.SameSiteLaxMode
case "None":
cookie.SameSite = http.SameSiteNoneMode
default:
log.Printf("Unknown SameSite value: %s, defaulting to Lax", sameSite)
cookie.SameSite = http.SameSiteLaxMode
}
}
// If domain is blank, set it according to the host header
if cookie.Domain == "" {
host := r.Host
// Strip port number if present
if colonIndex := strings.Index(host, ":"); colonIndex != -1 {
host = host[:colonIndex]
}
cookie.Domain = host
}
http.SetCookie(w, &cookie)
}
}
if err := tx.Commit(); err != nil {
http.Error(w, "Error committing transaction: "+err.Error(), http.StatusInternalServerError)
return
}
for name, value := range responseHeaders {
w.Header().Set(name, value)
}
w.WriteHeader(statusCode)
if tplName != "" {
w.Header().Set("Content-Type", "text/html")
template, ok := app.tpl[tplName]
if !ok {
http.Error(w, "Template not found: "+tplName, http.StatusInternalServerError)
return
}
// Convert results to the expected type for exec.NewContext
contextData := make(map[string]interface{})
for key, value := range results {
contextData[key] = value
}
data := exec.NewContext(contextData)
if err = template.Execute(w, data); err != nil {
http.Error(w, "Error rendering template: "+err.Error(), http.StatusInternalServerError)
return
}
} else {
// this should probably depend on the content type set into response meta?
w.Header().Set("Content-Type", "application/json")
// If results only contains ctx, output just that, otherwise output all results
var outputData any
outputData = results
if len(results) == 1 {
if ctx, exists := results["ctx"]; exists {
outputData = ctx
}
} else {
// Remove "ctx" from the results if it exists
if _, exists := results["ctx"]; exists {
// Create a copy of results without the "ctx" key
filteredResults := make(map[string]interface{})
for key, value := range results {
if key != "ctx" {
filteredResults[key] = value
}
}
outputData = filteredResults
}
}
if err := json.NewEncoder(w).Encode(outputData); err != nil {
http.Error(w, "Error encoding JSON: "+err.Error(), http.StatusInternalServerError)
return
}
}
}
}
func namedParamsToArgs(varsMap map[string]interface{}) []interface{} {
args := make([]interface{}, 0, len(varsMap))
for name, value := range varsMap {
args = append(args, sql.Named(name, value))
}
return args
}
// executeQuery runs the SQL query and returns the results
func executeQuery(tx *sql.Tx, query string, varsMap map[string]interface{}) ([]map[string]interface{}, error) {
rows, err := tx.Query(query, namedParamsToArgs(varsMap)...)
if err != nil {
return nil, fmt.Errorf("Error executing SQL: %v", err)
}
defer rows.Close()
columns, err := rows.Columns()
if err != nil {
return nil, fmt.Errorf("Error getting columns: %v", err)
}
values := make([]interface{}, len(columns))
valuePtrs := make([]interface{}, len(columns))
for i := range values {
valuePtrs[i] = &values[i]
}
var results []map[string]interface{}
for rows.Next() {
if err := rows.Scan(valuePtrs...); err != nil {
return nil, fmt.Errorf("Error scanning row: %v", err)
}
// Create a map for this row
row := make(map[string]interface{})
for i, col := range columns {
var v interface{}
val := values[i]
if b, ok := val.([]byte); ok {
v = string(b)
} else {
v = val
}
row[col] = v
}
results = append(results, row)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("Error iterating rows: %v", err)
}
return results, nil
}