|
| 1 | +package services |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/UzStack/bug-lang/internal/runtime/types" |
| 10 | + "github.com/UzStack/bug-lang/pkg/utils" |
| 11 | +) |
| 12 | + |
| 13 | +func ParsePostData(request *http.Request) any { |
| 14 | + body, err := io.ReadAll(request.Body) |
| 15 | + if err != nil { |
| 16 | + panic(err.Error()) |
| 17 | + } |
| 18 | + var data any |
| 19 | + if err := json.Unmarshal(body, &data); err != nil { |
| 20 | + return types.NewMap(nil) |
| 21 | + } |
| 22 | + return utils.DecodeBug(data) |
| 23 | +} |
| 24 | + |
| 25 | +func ParseGetData(request *http.Request) any { |
| 26 | + data := types.NewMap(make(map[string]any)) |
| 27 | + rawQuery := request.URL.RawQuery |
| 28 | + if len(rawQuery) == 0 { |
| 29 | + return data |
| 30 | + } |
| 31 | + for _, p := range strings.Split(rawQuery, "&") { |
| 32 | + param := strings.Split(p, "=") |
| 33 | + if len(param) >= 2 { |
| 34 | + data.Add(param[0], utils.DecodeBug(param[1])) |
| 35 | + } |
| 36 | + } |
| 37 | + return data |
| 38 | +} |
| 39 | + |
| 40 | +func ParseRequest(request *http.Request) any { |
| 41 | + headers := types.NewMap(make(map[string]any)) |
| 42 | + for key, values := range request.Header { |
| 43 | + if len(values) > 1 { |
| 44 | + value := types.NewArray([]any{}) |
| 45 | + for _, v := range values { |
| 46 | + value.Add(utils.DecodeBug(v)) |
| 47 | + } |
| 48 | + headers.Add(key, value) |
| 49 | + } else if len(values) == 1 { |
| 50 | + headers.Add(key, utils.DecodeBug(values[0])) |
| 51 | + } else { |
| 52 | + headers.Add(key, types.NewNull()) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + globals := types.NewMap(make(map[string]any)) |
| 57 | + globals.Add("RequestURI", types.NewString(request.RequestURI)) |
| 58 | + globals.Add("Host", types.NewString(request.Host)) |
| 59 | + globals.Add("Method", types.NewString(request.Method)) |
| 60 | + globals.Add("Headers", headers) |
| 61 | + globals.Add("Path", types.NewString(request.URL.Path)) |
| 62 | + return globals |
| 63 | +} |
| 64 | + |
| 65 | +func ParseGlobals(request *http.Request) any { |
| 66 | + globals := types.NewMap(make(map[string]any)) |
| 67 | + return globals |
| 68 | +} |
0 commit comments