Skip to content

Commit a2e9817

Browse files
committed
Cleanup code
1 parent 0252df0 commit a2e9817

25 files changed

+878
-1138
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ linters:
66
enable:
77
- govet
88
- ineffassign
9-
- paralleltest
109
- staticcheck
1110
settings:
1211
staticcheck:

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ build:
55
go run -v ./cmd/build-naive build $(TARGET_FLAG)
66
go run -v ./cmd/build-naive package --local $(TARGET_FLAG)
77

8+
generate_net_errors:
9+
go run ./cmd/build-naive generate-net-errors
10+
811
test: make
912
go test -v .
1013

bidirectional_conn.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111

1212
// BidirectionalConn is a wrapper from BidirectionalStream to net.Conn
1313
type BidirectionalConn struct {
14-
stream BidirectionalStream
15-
cancelOnce sync.Once // Ensures Cancel is called at most once
16-
destroyOnce sync.Once // Ensures Destroy is called at most once
14+
stream BidirectionalStream
15+
cancelOnce sync.Once // Ensures Cancel is called at most once
16+
destroyOnce sync.Once // Ensures Destroy is called at most once
1717
readWaitHeaders bool
1818
writeWaitHeaders bool
1919
access sync.Mutex

bidirectional_stream_cgo.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ func (c BidirectionalStream) Start(method string, url string, headers map[string
8181
if headerLen > 0 {
8282
cHeadersPtr := C.malloc(C.size_t(int(C.sizeof_struct_bidirectional_stream_header) * headerLen))
8383
defer C.free(cHeadersPtr)
84-
var cType *C.bidirectional_stream_header
85-
cType = (*C.bidirectional_stream_header)(cHeadersPtr)
84+
cType := (*C.bidirectional_stream_header)(cHeadersPtr)
8685
cHeaders := unsafe.Slice(cType, headerLen)
8786
var index int
8887
for key, value := range headers {
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"fmt"
7+
"go/format"
8+
"log"
9+
"os"
10+
"path/filepath"
11+
"regexp"
12+
"strconv"
13+
"strings"
14+
"unicode"
15+
16+
"github.com/spf13/cobra"
17+
)
18+
19+
var commandGenerateNetErrors = &cobra.Command{
20+
Use: "generate-net-errors",
21+
Short: "Generate Go error constants from Chromium's net_error_list.h",
22+
Run: runGenerateNetErrors,
23+
}
24+
25+
func init() {
26+
mainCommand.AddCommand(commandGenerateNetErrors)
27+
}
28+
29+
type netErrorEntry struct {
30+
name string
31+
code int
32+
description string
33+
message string
34+
}
35+
36+
func runGenerateNetErrors(cmd *cobra.Command, args []string) {
37+
sourceFile := filepath.Join(srcRoot, "net", "base", "net_error_list.h")
38+
outputFile := filepath.Join(projectRoot, "net_error_generated.go")
39+
40+
errors, err := parseNetErrorList(sourceFile)
41+
if err != nil {
42+
log.Fatalf("failed to parse %s: %v", sourceFile, err)
43+
}
44+
45+
err = generateNetErrorGoFile(errors, outputFile)
46+
if err != nil {
47+
log.Fatalf("failed to generate %s: %v", outputFile, err)
48+
}
49+
50+
log.Printf("generated %s with %d error codes", outputFile, len(errors))
51+
}
52+
53+
var netErrorRegex = regexp.MustCompile(`NET_ERROR\(\s*(\w+)\s*,\s*(-?\d+)\s*\)`)
54+
55+
func parseNetErrorList(filename string) ([]netErrorEntry, error) {
56+
file, err := os.Open(filename)
57+
if err != nil {
58+
return nil, err
59+
}
60+
defer file.Close()
61+
62+
var errors []netErrorEntry
63+
var commentLines []string
64+
scanner := bufio.NewScanner(file)
65+
66+
for scanner.Scan() {
67+
line := scanner.Text()
68+
trimmed := strings.TrimSpace(line)
69+
70+
if strings.HasPrefix(trimmed, "//") {
71+
comment := strings.TrimPrefix(trimmed, "//")
72+
comment = strings.TrimSpace(comment)
73+
if !strings.Contains(comment, "no-include-guard") &&
74+
!strings.Contains(comment, "NOLINT") &&
75+
comment != "" {
76+
commentLines = append(commentLines, comment)
77+
}
78+
continue
79+
}
80+
81+
if matches := netErrorRegex.FindStringSubmatch(line); matches != nil {
82+
name := matches[1]
83+
code, _ := strconv.Atoi(matches[2])
84+
85+
if strings.HasSuffix(name, "_END") {
86+
commentLines = nil
87+
continue
88+
}
89+
90+
description := buildNetErrorDescription(commentLines)
91+
message := netErrorNameToMessage(name)
92+
93+
errors = append(errors, netErrorEntry{
94+
name: name,
95+
code: code,
96+
description: description,
97+
message: message,
98+
})
99+
commentLines = nil
100+
continue
101+
}
102+
103+
if trimmed == "" || (!strings.HasPrefix(trimmed, "//") && !strings.HasPrefix(trimmed, "NET_ERROR")) {
104+
if trimmed != "" && !strings.HasPrefix(trimmed, "#") {
105+
commentLines = nil
106+
}
107+
}
108+
}
109+
110+
return errors, scanner.Err()
111+
}
112+
113+
func buildNetErrorDescription(comments []string) string {
114+
if len(comments) == 0 {
115+
return ""
116+
}
117+
118+
var filtered []string
119+
for _, c := range comments {
120+
if strings.HasPrefix(c, "Ranges:") ||
121+
regexp.MustCompile(`^\d+-\d+\s+`).MatchString(c) ||
122+
strings.HasPrefix(c, "0-") {
123+
continue
124+
}
125+
if strings.Contains(c, "was removed") {
126+
continue
127+
}
128+
if strings.Contains(c, "is reserved") {
129+
continue
130+
}
131+
filtered = append(filtered, c)
132+
}
133+
134+
return strings.Join(filtered, " ")
135+
}
136+
137+
func netErrorNameToMessage(name string) string {
138+
words := strings.Split(strings.ToLower(name), "_")
139+
if len(words) > 0 && words[0] == "err" {
140+
words = words[1:]
141+
}
142+
return strings.Join(words, " ")
143+
}
144+
145+
func netErrorNameToGoName(name string) string {
146+
parts := strings.Split(strings.ToLower(name), "_")
147+
var result strings.Builder
148+
for _, part := range parts {
149+
if part == "" {
150+
continue
151+
}
152+
switch strings.ToUpper(part) {
153+
case "IO", "IP", "SSL", "TLS", "HTTP", "DNS", "URL", "TCP", "UDP", "QUIC", "CT", "PAC", "PKCS", "RST", "FIN", "ACK", "SOCKS", "MAC", "DH", "ECH", "CSP", "ORB":
154+
result.WriteString(strings.ToUpper(part))
155+
case "HTTP2":
156+
result.WriteString("HTTP2")
157+
default:
158+
runes := []rune(part)
159+
runes[0] = unicode.ToUpper(runes[0])
160+
result.WriteString(string(runes))
161+
}
162+
}
163+
return result.String()
164+
}
165+
166+
func generateNetErrorGoFile(errors []netErrorEntry, filename string) error {
167+
var buffer bytes.Buffer
168+
169+
buffer.WriteString(`// Code generated by cmd/build-naive generate-net-errors. DO NOT EDIT.
170+
// Source: naiveproxy/src/net/base/net_error_list.h
171+
172+
package cronet
173+
174+
// NetError constants from Chromium's net_error_list.h
175+
const (
176+
`)
177+
178+
for _, entry := range errors {
179+
goName := "NetError" + netErrorNameToGoName(entry.name)
180+
buffer.WriteString(fmt.Sprintf("\t%s NetError = %d\n", goName, entry.code))
181+
}
182+
183+
buffer.WriteString(`)
184+
185+
type netErrorEntry struct {
186+
name string
187+
message string
188+
description string
189+
}
190+
191+
var netErrorInfo = map[NetError]netErrorEntry{
192+
`)
193+
194+
for _, entry := range errors {
195+
goName := "NetError" + netErrorNameToGoName(entry.name)
196+
name := fmt.Sprintf("ERR_%s", entry.name)
197+
buffer.WriteString(fmt.Sprintf("\t%s: {%q, %q, %q},\n", goName, name, entry.message, entry.description))
198+
}
199+
200+
buffer.WriteString(`}
201+
`)
202+
203+
formatted, err := format.Source(buffer.Bytes())
204+
if err != nil {
205+
os.WriteFile(filename+".unformatted", buffer.Bytes(), 0o644)
206+
return fmt.Errorf("failed to format generated code: %w", err)
207+
}
208+
209+
return os.WriteFile(filename, formatted, 0o644)
210+
}

0 commit comments

Comments
 (0)