-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest.go
186 lines (154 loc) · 4.47 KB
/
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
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
// cui: http request/response tui
// Copyright 2022-2023 Mario Finelli
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/alecthomas/chroma/v2/formatters"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles"
"github.com/rivo/tview"
)
type cuiRequest struct {
Method string `json:"method"`
URL string `json:"url"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
Parameters map[string]string `json:"parameters"`
}
type cuiStoredRequest struct {
CuiVersion string `json:"version"`
StatusCode int `json:"status"`
Timestamp int64 `json:"timestamp"`
Method string `json:"method"`
URL string `json:"url"`
Parameters map[string]string `json:"parameters"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
}
func sendRequest(app *tview.Application, req *cuiRequest, cui *cuiApp) error {
// we assume this directory exists because we created it on startup
// for the logfile
cacheDir, err := os.UserCacheDir()
if err != nil {
return err
}
cacheDir = filepath.Join(cacheDir, "cui")
client := &http.Client{}
cui.ResponseBody.Clear()
cui.ResponseHeaders.Clear()
highlight := false
highlightLang := ""
r, err := http.NewRequest(req.Method, req.URL, nil)
if err != nil {
return err
}
// query param handling
qParams := r.URL.Query()
for key, value := range req.Parameters {
qParams.Add(key, value)
}
r.URL.RawQuery = qParams.Encode()
// header handling
for header, value := range req.Headers {
// special handling for "host" header if set
if strings.EqualFold("host", header) {
r.Host = value
} else {
r.Header.Set(header, value)
}
}
if req.Body != "" {
r.Body = io.NopCloser(strings.NewReader(req.Body))
}
res, err := client.Do(r)
if err != nil {
return err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
cui.ResponseStatus.SetText(fmt.Sprintf("Status: %d,", res.StatusCode))
cui.ResponseSize.SetText(fmt.Sprintf("Response Size: %d bytes", len(body)))
cui.ResponseHeaders.SetCell(0, 0, tview.NewTableCell("Header"))
cui.ResponseHeaders.SetCell(0, 1, tview.NewTableCell("Value"))
i := 1
for k, v := range res.Header {
for _, vv := range v {
if strings.EqualFold("content-type", k) {
if strings.HasPrefix(strings.ToLower(vv), "text/html") {
highlight = true
highlightLang = "html"
} else if strings.HasPrefix(strings.ToLower(vv), "application/json") {
highlight = true
highlightLang = "json"
}
}
cui.ResponseHeaders.SetCell(i, 0, tview.NewTableCell(k))
cui.ResponseHeaders.SetCell(i, 1, tview.NewTableCell(vv))
i += 1
}
}
if highlight {
// TODO: need to call app.Draw() in a SetChangedFunc?
cui.ResponseBody.SetDynamicColors(true)
lexer := lexers.Get(highlightLang)
style := styles.Get("fruity")
formatter := formatters.Get("terminal")
iterator, err := lexer.Tokenise(nil, string(body))
if err != nil {
return err
}
w := tview.ANSIWriter(cui.ResponseBody)
err = formatter.Format(w, style, iterator)
if err != nil {
return err
}
cui.ResponseBody.ScrollToBeginning()
} else {
cui.ResponseBody.SetText(string(body)).ScrollToBeginning()
}
cui.ViewHasResponse = true
store := cuiStoredRequest{
CuiVersion: version,
StatusCode: res.StatusCode,
Timestamp: time.Now().Unix(),
Method: req.Method,
URL: req.URL,
Parameters: req.Parameters,
Headers: req.Headers,
Body: req.Body,
}
jsonBytes, err := json.Marshal(store)
if err != nil {
return err
}
err = os.WriteFile(filepath.Join(cacheDir, fmt.Sprintf("request-%d.json", store.Timestamp)), jsonBytes, 0o644)
if err != nil {
return err
}
insertHistoryItem(app, &store, cui, req)
cui.RequestHistory.SetCurrentItem(0)
return nil
}