|
| 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