| 1 | package server |
| 2 | |
| 3 | import "strings" |
| 4 | |
| 5 | // ModuleName extracts the top-level module name from a URL path. |
| 6 | func ModuleName(path string) string { |
| 7 | path = strings.TrimPrefix(path, "/") |
| 8 | if i := strings.Index(path, "/"); i != -1 { |
| 9 | path = path[:i] |
| 10 | } |
| 11 | |
| 12 | return path |
| 13 | } |
| 14 | |
| 15 | // ParseProxyPath parses "/<module>/@v/<file>" paths. |
| 16 | // Returns the full module path and file component (e.g. "list", "v1.0.0.info"). |
| 17 | func ParseProxyPath(urlPath string) (modPath, file string, ok bool) { |
| 18 | path := strings.TrimPrefix(urlPath, "/") |
| 19 | |
| 20 | idx := strings.Index(path, "/@v/") |
| 21 | if idx == -1 { |
| 22 | return "", "", false |
| 23 | } |
| 24 | |
| 25 | modPath = path[:idx] |
| 26 | file = path[idx+len("/@v/"):] |
| 27 | |
| 28 | if modPath == "" || file == "" { |
| 29 | return "", "", false |
| 30 | } |
| 31 | |
| 32 | return modPath, file, true |
| 33 | } |
| 34 | |
| 35 | // ParseDocPath parses documentation URLs: "/<module>@<version>/<subpath>". |
| 36 | // Returns the module name, version (empty if not specified), and sub-path. |
| 37 | func ParseDocPath(urlPath string) (modName, version, subpath string) { |
| 38 | path := strings.TrimPrefix(urlPath, "/") |
| 39 | |
| 40 | // Split on first "/" to get the first segment (which may contain @version). |
| 41 | first, rest, _ := strings.Cut(path, "/") |
| 42 | |
| 43 | // Check for @version in first segment. |
| 44 | if mod, ver, ok := strings.Cut(first, "@"); ok { |
| 45 | return mod, ver, rest |
| 46 | } |
| 47 | |
| 48 | return first, "", rest |
| 49 | } |
| 50 | |
| 51 | // ParseLatestPath parses "/<module>/@latest" paths. |
| 52 | // Returns the escaped module path if the URL matches, or empty string if not. |
| 53 | func ParseLatestPath(urlPath string) (escapedModPath string, ok bool) { |
| 54 | path := strings.TrimPrefix(urlPath, "/") |
| 55 | |
| 56 | if !strings.HasSuffix(path, "/@latest") { |
| 57 | return "", false |
| 58 | } |
| 59 | |
| 60 | modPath := strings.TrimSuffix(path, "/@latest") |
| 61 | if modPath == "" { |
| 62 | return "", false |
| 63 | } |
| 64 | |
| 65 | return modPath, true |
| 66 | } |
| 67 | |
| 68 | // StorageKey returns the S3 key for a proxy artifact. |
| 69 | func StorageKey(modName, file string) string { |
| 70 | return modName + "/@v/" + file |
| 71 | } |
| 72 | |