Stable APIs for Go. https://go.code.as
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

313 lines
7.7 KiB

  1. package main
  2. import (
  3. "errors"
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "os"
  10. "regexp"
  11. "strconv"
  12. "strings"
  13. "text/template"
  14. )
  15. var httpFlag = flag.String("http", ":8080", "Serve HTTP at given address")
  16. var httpsFlag = flag.String("https", "", "Serve HTTPS at given address")
  17. var certFlag = flag.String("cert", "", "Use the provided TLS certificate")
  18. var keyFlag = flag.String("key", "", "Use the provided TLS key")
  19. func main() {
  20. if err := run(); err != nil {
  21. fmt.Fprintf(os.Stderr, "error: %v\n", err)
  22. os.Exit(1)
  23. }
  24. }
  25. func run() error {
  26. flag.Parse()
  27. if len(flag.Args()) > 0 {
  28. return fmt.Errorf("too many arguments: %s", flag.Args()[0])
  29. }
  30. http.HandleFunc("/", handler)
  31. if *httpFlag == "" && *httpsFlag == "" {
  32. return fmt.Errorf("must provide -http and/or -https")
  33. }
  34. if (*httpsFlag != "" || *certFlag != "" || *keyFlag != "") && (*httpsFlag == "" || *certFlag == "" || *keyFlag == "") {
  35. return fmt.Errorf("-https -cert and -key must be used together")
  36. }
  37. ch := make(chan error, 2)
  38. if *httpFlag != "" {
  39. go func() {
  40. ch <- http.ListenAndServe(*httpFlag, nil)
  41. }()
  42. }
  43. if *httpsFlag != "" {
  44. go func() {
  45. ch <- http.ListenAndServeTLS(*httpsFlag, *certFlag, *keyFlag, nil)
  46. }()
  47. }
  48. return <-ch
  49. }
  50. var tmpl = template.Must(template.New("").Parse(`
  51. <html>
  52. <head>
  53. <meta name="go-import" content="{{.PkgRoot}} git {{.GitRoot}}">
  54. </head>
  55. <body>
  56. <script>
  57. window.location = "http://godoc.org/{{.PkgPath}}" + window.location.hash;
  58. </script>
  59. </body>
  60. </html>
  61. `))
  62. type Repo struct {
  63. GitRoot string
  64. HubRoot string
  65. PkgRoot string
  66. PkgPath string
  67. Version Version
  68. }
  69. var patternOld = regexp.MustCompile(`^/([a-z0-9][-a-z0-9]+/)?((?:v0|v[1-9][0-9]*)(?:\.0|\.[1-9][0-9]*){0,2})/([a-zA-Z][-a-zA-Z0-9]*)(?:\.git)?((?:/[a-zA-Z][-a-zA-Z0-9]*)*)$`)
  70. var patternNew = regexp.MustCompile(`^/([a-z0-9][-a-z0-9]+/)?([a-zA-Z][-a-zA-Z0-9]*)\.((?:v0|v[1-9][0-9]*)(?:\.0|\.[1-9][0-9]*){0,2})(?:\.git)?((?:/[a-zA-Z][-a-zA-Z0-9]*)*)$`)
  71. func handler(resp http.ResponseWriter, req *http.Request) {
  72. if req.URL.Path == "/health-check" {
  73. resp.Write([]byte("ok"))
  74. return
  75. }
  76. log.Printf("%s requested %s", req.RemoteAddr, req.URL)
  77. if req.URL.Path == "/" {
  78. resp.Header().Set("Location", "http://labix.org/gopkg.in")
  79. resp.WriteHeader(http.StatusTemporaryRedirect)
  80. return
  81. }
  82. m := patternNew.FindStringSubmatch(req.URL.Path)
  83. compat := false
  84. if m == nil {
  85. m = patternOld.FindStringSubmatch(req.URL.Path)
  86. if m == nil {
  87. sendNotFound(resp, "Unsupported URL pattern; see the documentation at gopkg.in for details.")
  88. return
  89. }
  90. m[2], m[3] = m[3], m[2]
  91. compat = true
  92. }
  93. if strings.Contains(m[3], ".") {
  94. sendNotFound(resp, "Import paths take the major version only (.%s instead of .%s); see docs at gopkg.in for the reasoning.",
  95. m[3][:strings.Index(m[3], ".")], m[3])
  96. return
  97. }
  98. var repo *Repo
  99. if m[1] == "" {
  100. repo = &Repo{
  101. GitRoot: "https://github.com/go-" + m[2] + "/" + m[2],
  102. PkgRoot: "gopkg.in/" + m[2] + "." + m[3],
  103. PkgPath: "gopkg.in/" + m[2] + "." + m[3] + m[4],
  104. }
  105. if compat {
  106. repo.PkgRoot = "gopkg.in/" + m[3] + "/" + m[2]
  107. repo.PkgPath = "gopkg.in/" + m[3] + "/" + m[2] + m[4]
  108. }
  109. } else {
  110. repo = &Repo{
  111. GitRoot: "https://github.com/" + m[1] + m[2],
  112. PkgRoot: "gopkg.in/" + m[1] + m[2] + "." + m[3],
  113. PkgPath: "gopkg.in/" + m[1] + m[2] + "." + m[3] + m[4],
  114. }
  115. if compat {
  116. repo.PkgRoot = "gopkg.in/" + m[1] + m[3] + "/" + m[2]
  117. repo.PkgPath = "gopkg.in/" + m[1] + m[3] + "/" + m[2] + m[4]
  118. }
  119. }
  120. var ok bool
  121. repo.Version, ok = parseVersion(m[3])
  122. if !ok {
  123. sendNotFound(resp, "Version %q improperly considered invalid; please warn the service maintainers.", m[3])
  124. }
  125. repo.HubRoot = repo.GitRoot
  126. // Run this concurrently to avoid waiting later.
  127. nameVersioned := nameHasVersion(repo)
  128. refs, err := hackedRefs(repo)
  129. switch err {
  130. case nil:
  131. repo.GitRoot = "https://" + repo.PkgRoot
  132. case ErrNoRepo:
  133. if <-nameVersioned {
  134. v := repo.Version.String()
  135. repo.GitRoot += "-" + v
  136. repo.HubRoot += "-" + v
  137. break
  138. }
  139. sendNotFound(resp, "GitHub repository not found at %s", repo.HubRoot)
  140. return
  141. case ErrNoVersion:
  142. v := repo.Version.String()
  143. if repo.Version.Minor == -1 {
  144. sendNotFound(resp, `GitHub repository at %s has no branch or tag "%s", "%s.N" or "%s.N.M"`, repo.HubRoot, v, v, v)
  145. } else if repo.Version.Patch == -1 {
  146. sendNotFound(resp, `GitHub repository at %s has no branch or tag "%s" or "%s.N"`, repo.HubRoot, v, v)
  147. } else {
  148. sendNotFound(resp, `GitHub repository at %s has no branch or tag "%s"`, repo.HubRoot, v)
  149. }
  150. return
  151. default:
  152. resp.WriteHeader(http.StatusBadGateway)
  153. resp.Write([]byte(fmt.Sprintf("Cannot obtain refs from GitHub: %v", err)))
  154. return
  155. }
  156. if m[4] == "/git-upload-pack" {
  157. resp.Header().Set("Location", repo.HubRoot+"/git-upload-pack")
  158. resp.WriteHeader(http.StatusMovedPermanently)
  159. return
  160. }
  161. if m[4] == "/info/refs" {
  162. resp.Header().Set("Content-Type", "application/x-git-upload-pack-advertisement")
  163. resp.Write(refs)
  164. return
  165. }
  166. resp.Header().Set("Content-Type", "text/html")
  167. tmpl.Execute(resp, repo)
  168. }
  169. func sendNotFound(resp http.ResponseWriter, msg string, args ...interface{}) {
  170. if len(args) > 0 {
  171. msg = fmt.Sprintf(msg, args...)
  172. }
  173. resp.WriteHeader(http.StatusNotFound)
  174. resp.Write([]byte(msg))
  175. }
  176. // TODO Timeouts for these http interactions. Use the new support coming in 1.3.
  177. const refsSuffix = ".git/info/refs?service=git-upload-pack"
  178. // Obsolete. Drop this.
  179. func nameHasVersion(repo *Repo) chan bool {
  180. ch := make(chan bool, 1)
  181. go func() {
  182. resp, err := http.Head(repo.HubRoot + "-" + repo.Version.String() + refsSuffix)
  183. if err != nil {
  184. ch <- false
  185. }
  186. if resp.Body != nil {
  187. resp.Body.Close()
  188. }
  189. ch <- resp.StatusCode == 200
  190. }()
  191. return ch
  192. }
  193. var ErrNoRepo = errors.New("repository not found in github")
  194. var ErrNoVersion = errors.New("version reference not found in github")
  195. func hackedRefs(repo *Repo) (data []byte, err error) {
  196. resp, err := http.Get(repo.HubRoot + refsSuffix)
  197. if err != nil {
  198. return nil, fmt.Errorf("cannot talk to GitHub: %v", err)
  199. }
  200. switch resp.StatusCode {
  201. case 200:
  202. defer resp.Body.Close()
  203. case 401, 404:
  204. return nil, ErrNoRepo
  205. default:
  206. return nil, fmt.Errorf("error from GitHub: %v", resp.Status)
  207. }
  208. data, err = ioutil.ReadAll(resp.Body)
  209. if err != nil {
  210. return nil, fmt.Errorf("error reading from GitHub: %v", err)
  211. }
  212. var mrefi, mrefj int
  213. var vrefi, vrefj int
  214. var vrefv = InvalidVersion
  215. var unversioned = true
  216. sdata := string(data)
  217. for i, j := 0, 0; i < len(data); i = j {
  218. size, err := strconv.ParseInt(sdata[i:i+4], 16, 32)
  219. if err != nil {
  220. return nil, fmt.Errorf("cannot parse refs line size: %s", string(data[i:i+4]))
  221. }
  222. if size == 0 {
  223. size = 4
  224. }
  225. j = i + int(size)
  226. if j > len(sdata) {
  227. return nil, fmt.Errorf("incomplete refs data received from GitHub")
  228. }
  229. if sdata[0] == '#' {
  230. continue
  231. }
  232. hashi := i + 4
  233. hashj := strings.IndexByte(sdata[hashi:j], ' ')
  234. if hashj < 0 || hashj != 40 {
  235. continue
  236. }
  237. hashj += hashi
  238. namei := hashj + 1
  239. namej := strings.IndexAny(sdata[namei:j], "\n\x00")
  240. if namej < 0 {
  241. namej = j
  242. } else {
  243. namej += namei
  244. }
  245. name := sdata[namei:namej]
  246. if name == "refs/heads/master" {
  247. mrefi = hashi
  248. mrefj = hashj
  249. }
  250. if strings.HasPrefix(name, "refs/heads/v") || strings.HasPrefix(name, "refs/tags/v") {
  251. v, ok := parseVersion(name[strings.IndexByte(name, 'v'):])
  252. if ok && repo.Version.Contains(v) && (!vrefv.IsValid() || vrefv.Less(v)) {
  253. vrefv = v
  254. vrefi = hashi
  255. vrefj = hashj
  256. }
  257. if ok {
  258. unversioned = false
  259. }
  260. }
  261. }
  262. // If there were absolutely no versions, and v0 was requested, accept the master as-is.
  263. if unversioned && repo.Version == (Version{0, -1, -1}) {
  264. return data, nil
  265. }
  266. if mrefi == 0 || vrefi == 0 {
  267. return nil, ErrNoVersion
  268. }
  269. copy(data[mrefi:mrefj], data[vrefi:vrefj])
  270. return data, nil
  271. }