-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
173 lines (161 loc) · 4.68 KB
/
utils.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
package main
import (
"strconv"
"strings"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/widget"
)
// Generic printer to GUI-outbox
func myPrint(str string) {
glob.outtext.Append(time.Now().Format("15:04:05") + ": " + str + "\n")
}
// substring Knuth-Morris-Pratt (KMP)
// byte_index returns the index of the first occurrence of b in a
// It returns -1 if b is not found in a
func byte_index(a, b []byte) int {
// If b is empty, we consider it found at position 0
if len(b) == 0 {
return 0
}
// If b is larger than a, b cannot be found in a, return -1
if len(b) > len(a) {
return -1
}
for i := 0; i <= len(a)-len(b); i++ {
match := true // Assume we have a match, and check each byte
// Compare each byte of b with the slice of a starting from i
for j := 0; j < len(b); j++ {
if a[i+j] != b[j] {
match = false // If any byte does not match, break
break
}
}
// If all bytes match, return the starting index
if match {
return i
}
}
// If no match is found, return -1
return -1
}
// Helper function to get Content-Length from the HTTP headers
func getContentLength(headers []byte) int {
// Define the "Content-Length:" header prefix
contentLengthPrefix := []byte("Content-Length:")
// Iterate through headers manually
lineStart := 0
for i := 0; i < len(headers); i++ {
// Look for end of a line "\r\n"
if headers[i] == '\r' && i+1 < len(headers) && headers[i+1] == '\n' {
line := headers[lineStart:i]
lineStart = i + 2 // Skip "\r\n"
// Compare if the line starts with "Content-Length:"
if len(line) > len(contentLengthPrefix) && cmpBytes(line[:len(contentLengthPrefix)], contentLengthPrefix) {
// Skip the "Content-Length:" part and any spaces
startIdx := len(contentLengthPrefix)
for startIdx < len(line) && line[startIdx] == ' ' {
startIdx++ // Skip spaces
}
// Now parse the number (digits)
// not using strconv or bytes to avoid whole packages just to cmp bytes....
var length int
for ; startIdx < len(line) && line[startIdx] >= '0' && line[startIdx] <= '9'; startIdx++ {
length = length*10 + int(line[startIdx]-'0') // Build the number manually
}
return length
}
}
}
// Return 0 if Content-Length is not found
return 0
}
// Yeah comparing bytes by not using a whole package just for it
func cmpBytes(slice1, slice2 []byte) bool {
//fmt.Printf("%s %v", slice1, slice1)
if len(slice1) != len(slice2) {
return false
}
for i := range slice1 {
if slice1[i] != slice2[i] {
return false
}
}
return true
}
func monitorResize(isDead *bool, w *fyne.Window, text1, text2 *widget.Entry) {
var newSize fyne.Size
var factorH float32
for {
if *isDead {
return // killing this thread :)
}
if !text2.Hidden {
newSize = (*w).Canvas().Size()
factorH = (newSize.Height/800)*(newSize.Height/800)/10 + 0.66
text1.Resize(fyne.NewSize(newSize.Width*0.498, newSize.Height*factorH))
text2.Resize(fyne.NewSize(newSize.Width*0.498, newSize.Height*factorH))
}
time.Sleep(450 * time.Millisecond)
}
}
func sendFunc(sync *insync, hostEntry, requestText, responseText *widget.Entry, sendButton *widget.Button) func() {
return func() {
var start int64
if sendButton.Text == "Stop" {
sync.syncAbort = true
sendButton.Text = "Send"
hostEntry.Enable()
requestText.Enable()
myPrint("Tab" + strconv.Itoa(sync.id) + " interrupted")
return
}
sendButton.SetText("Stop")
hostEntry.Disable()
responseText.SetText("")
requestText.Disable()
requestChan := make(chan []byte)
var merr string
if hostEntry.Text == "" {
if requestText.Text == "" {
sendButton.SetText("Send")
sync.syncAbort = false
hostEntry.Enable()
requestText.Enable()
return
} else if _, ct1, ok := strings.Cut(requestText.Text, "Host: "); ok {
var result strings.Builder
for c := 0; c < len(ct1); c++ {
if ct1[c] == '\n' || ct1[c] == '\r' || ct1[c] == ' ' {
break
}
result.WriteByte(ct1[c])
}
hostEntry.SetText(result.String() + ":443")
}
}
size := len(requestText.Text)
if size > 0 && requestText.Text[size-4:] != "\r\n\r\n" && requestText.Text[size-2:] != "\n\n" {
requestText.Append("\r\n\r\n")
}
start = time.Now().UnixMilli()
go sendRequest(hostEntry.Text, requestText.Text, requestChan, &merr, sync)
go func() {
for data := range requestChan {
responseText.Append(string(data))
}
if merr != "" {
if len(merr) > 4 && merr[:3] == "err" {
responseText.Append("\nError: " + merr[4:])
}
}
if sync.tabs.isJitter {
myPrint("Tab" + strconv.Itoa(sync.id) + " " + strconv.Itoa(int(time.Now().UnixMilli()-start)) + "ms")
}
sendButton.SetText("Send")
sync.syncAbort = false
hostEntry.Enable()
requestText.Enable()
}()
}
}