-
Notifications
You must be signed in to change notification settings - Fork 58
/
model.go
330 lines (294 loc) · 8.05 KB
/
model.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
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
package model
import (
"fmt"
"net/http"
"net/url"
"sync"
"github.com/therecipe/qt/core"
)
var History []*Row
// used to map id to actual rows in the table
var HashMapHistory = make(map[int64]int)
type Row struct {
ID int64
Req *Request
Resp *Response
EditedReq *Request
EditedResp *Response
}
// Request represents an HTTP request logged in the history
type Request struct {
ID int64
URL *url.URL
Proto string
Method string
Host string
Headers http.Header
ContentLength int64
Body []byte
Extension string
Params bool
IP string
}
// ToString returns a string representation of an HTTP request logged in the history
func (r *Request) ToString() string {
/*
Metho Path Proto
Host
Headers
Body
*/
if r == nil {
return ""
}
//u1 := fmt.Sprintf("%v", r.URL)
ret := fmt.Sprintf("%s %s %s\nHost: %s\n", r.Method, r.URL.Path, r.Proto, r.Host)
for k, v := range r.Headers {
values := ""
for _, s := range v {
values = values + s
}
ret = ret + fmt.Sprintf("%s: %s\n", k, values)
}
if len(r.Body) > 0 {
ret = ret + fmt.Sprintf("\n%s", string(r.Body))
}
return ret
}
// Response represents an HTTP response logged in the history
type Response struct {
ID int64
Proto string
Status string
StatusCode int
Headers http.Header
ContentLength int64
Body []byte
}
// ToString returns a string representation of an HTTP response logged in the history
func (r *Response) ToString() string {
/*
Proto Status
Headers
Body
*/
if r == nil {
return ""
}
ret := fmt.Sprintf("%s %s\n", r.Proto, r.Status)
for k, v := range r.Headers {
values := ""
for _, s := range v {
values = values + s
}
ret = ret + fmt.Sprintf("%s: %s\n", k, values)
}
ret = ret + fmt.Sprintf("Content-Length: %d\n", r.ContentLength)
if len(r.Body) > 0 {
ret = ret + fmt.Sprintf("\n%s", string(r.Body))
}
return ret
}
// HTTPItem represent an item in the history table
type HTTPItem struct {
core.QObject
ID int
Req *Request
Resp *Response
EditedReq *Request
EditedResp *Response
}
const (
ID = iota
Host
Method
Path
Params
Edit
Status
Length
Ip
)
/*func (m *CustomTableModel) row(i *HTTPItem) int {
for index, item := range m.modelData {
if item.Pointer() == i.Pointer() {
return index
}
}
return 0
}*/
// CustomTableModel represents a table model used to populate the history QtTableView
type CustomTableModel struct {
core.QAbstractTableModel
_ func() `constructor:"init"`
//modelData []HTTPItem
//hashMap map[int64]*HTTPItem
/* _ func(item *HTTPItem, i int64) `signal:"addItem,auto"`
_ func(item *HTTPItem, i int64) `signal:editItem,auto"`*/
_ func() `signal:clearHistory,auto"`
}
var mutex = &sync.Mutex{}
func (m *CustomTableModel) init() {
//m.modelData = []HTTPItem{}
//m.hashMap = make(map[int64]*HTTPItem)
//HashMapHistory = make(map[int64]int)
m.ConnectHeaderData(m.headerData)
m.ConnectRowCount(m.rowCount)
m.ConnectColumnCount(m.columnCount)
m.ConnectData(m.data)
}
func (m *CustomTableModel) headerData(section int, orientation core.Qt__Orientation, role int) *core.QVariant {
if role != int(core.Qt__DisplayRole) || orientation == core.Qt__Vertical {
return m.HeaderDataDefault(section, orientation, role)
}
switch section {
case ID:
return core.NewQVariant1("ID")
case Host:
return core.NewQVariant1("Host")
case Method:
return core.NewQVariant1("Method")
case Path:
return core.NewQVariant1("Path")
case Params:
return core.NewQVariant1("Params")
case Edit:
return core.NewQVariant1("Edit")
case Status:
return core.NewQVariant1("Status")
case Length:
return core.NewQVariant1("Length")
case Ip:
return core.NewQVariant1("IP")
}
return core.NewQVariant()
}
// GetReqResp retursn request, response, edited request and edited response for a given context.ID
func (m *CustomTableModel) GetReqResp(i int64) (*Request, *Request, *Response, *Response) {
if val, ok := HashMapHistory[i]; ok {
return History[val].Req, History[val].EditedReq, History[val].Resp, History[val].EditedResp
}else{
for row, item := range History{
//fmt.Printf("ID: %d\n", item.ID)
//fmt.Printf("i: %d\n", i)
if item.ID == i{
HashMapHistory[i] = row
return History[row].Req, History[row].EditedReq, History[row].Resp, History[row].EditedResp
}
}
}
//fmt.Println("here")
return nil, nil, nil, nil
}
func (m *CustomTableModel) clearHistory() {
mutex.Lock()
defer mutex.Unlock()
/*m.BeginRemoveRows(core.NewQModelIndex(), 0, len(m.modelData))
m.modelData = []HTTPItem{}
m.hashMap = make(map[int64]*HTTPItem)
m.EndRemoveRows()*/
}
// AddRequest adds a Request to the history
func (m *CustomTableModel) AddRequest(r *Request, id int64){
mutex.Lock()
defer mutex.Unlock()
m.BeginInsertRows(core.NewQModelIndex(), len(History), len(History))
// create a new row with the request
t := &Row{ID:id, Req:r}
History = append(History, t)
// save the i to a mapping with hasmap
HashMapHistory[id] = len(History)-1
m.EndInsertRows()
}
// AddEditedRequest adds an edited Request to the history
func (m *CustomTableModel) AddEditedRequest(r *Request, id int64){
mutex.Lock()
defer mutex.Unlock()
row := HashMapHistory[id]
History[row].EditedReq = r
m.DataChanged(m.Index(row, 2, core.NewQModelIndex()), m.Index(row, 2, core.NewQModelIndex()), []int{Edit, Status, Length})
}
// AddEditedResponse adds an edited Response to the history
func (m *CustomTableModel) AddEditedResponse(r *Response, id int64){
mutex.Lock()
defer mutex.Unlock()
row := HashMapHistory[id]
History[row].EditedResp = r
m.DataChanged(m.Index(row, 2, core.NewQModelIndex()), m.Index(row, 2, core.NewQModelIndex()), []int{Edit, Status, Length})
}
// AddResponse adds a Response to the history
func (m *CustomTableModel) AddResponse(r *Response, id int64){
mutex.Lock()
defer mutex.Unlock()
row := HashMapHistory[id]
History[row].Resp = r
m.DataChanged(m.Index(row, 2, core.NewQModelIndex()), m.Index(row, 2, core.NewQModelIndex()), []int{Edit, Status, Length})
}
/*func (m *CustomTableModel) addItem(item *HTTPItem, i int64) {
mutex.Lock()
defer mutex.Unlock()
m.BeginInsertRows(core.NewQModelIndex(), len(m.modelData), len(m.modelData))
m.hashMap[i] = item
m.modelData = append(m.modelData, *item)
m.EndInsertRows()
}
func (m *CustomTableModel) editItem(item *HTTPItem, i int64) {
mutex.Lock()
defer mutex.Unlock()
row := m.row(m.hashMap[i])
m.hashMap[i].Resp = item.Resp
m.hashMap[i].EditedResp = item.EditedResp
m.modelData[row].Resp = item.Resp
m.modelData[row].EditedResp = item.EditedResp
m.DataChanged(m.Index(row, 2, core.NewQModelIndex()), m.Index(row, 2, core.NewQModelIndex()), []int{Edit, Status, Length})
}*/
func (m *CustomTableModel) rowCount(*core.QModelIndex) int {
return len(History)
}
func (m *CustomTableModel) columnCount(*core.QModelIndex) int {
return 9
}
func (m *CustomTableModel) data(index *core.QModelIndex, role int) *core.QVariant {
if role == int(core.Qt__TextAlignmentRole) &&
(index.Column() == Method ||
index.Column() == Params ||
index.Column() == Edit ||
index.Column() == Length) {
return core.NewQVariant1(int64(core.Qt__AlignCenter))
}
if role != int(core.Qt__DisplayRole) {
return core.NewQVariant()
}
item := History[index.Row()]
switch index.Column() {
case ID:
return core.NewQVariant1(item.ID)
case Host:
return core.NewQVariant1(fmt.Sprintf("%s://%s",item.Req.URL.Scheme, item.Req.Host))
case Method:
return core.NewQVariant1(item.Req.Method)
case Path:
return core.NewQVariant1(item.Req.URL.Path)
case Params:
if item.Req.Params {
return core.NewQVariant1("✓")
}
return core.NewQVariant1("")
case Edit:
if item.EditedReq != nil || item.EditedResp != nil {
return core.NewQVariant1("✓")
}
return core.NewQVariant1("")
case Status:
if item.Resp != nil {
return core.NewQVariant1(item.Resp.Status)
}
case Length:
if item.Resp != nil {
return core.NewQVariant1(fmt.Sprintf("%d", item.Resp.ContentLength))
}
case Ip:
return core.NewQVariant1(item.Req.IP)
}
return core.NewQVariant()
}