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.

320 lines
8.0 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 tmplProxy = 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. User string // username or organization, includes forward slash
  64. Pkg string // repository name
  65. VersionStr string // version string ("v1")
  66. Path string // path inside repository
  67. Compat bool // when true, use old format
  68. Version Version // requested version (major only)
  69. Versions VersionList // available versions
  70. }
  71. func (repo *Repo) GitRoot() string {
  72. return "https://" + repo.PkgRoot()
  73. }
  74. func (repo *Repo) HubRoot() string {
  75. if len(repo.User) == 0 {
  76. return "https://github.com/go-" + repo.Pkg + "/" + repo.Pkg
  77. }
  78. return "https://github.com/" + repo.User + repo.Pkg
  79. }
  80. func (repo *Repo) PkgBase() string {
  81. return "gopkg.in/" + repo.User + repo.Pkg
  82. }
  83. func (repo *Repo) PkgRoot() string {
  84. if repo.Compat {
  85. return "gopkg.in/" + repo.User + repo.VersionStr + "/" + repo.Pkg
  86. }
  87. return repo.PkgBase() + "." + repo.VersionStr
  88. }
  89. func (repo *Repo) PkgPath() string {
  90. return repo.PkgRoot() + repo.Path
  91. }
  92. 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]*)*)$`)
  93. 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]*)*)$`)
  94. func handler(resp http.ResponseWriter, req *http.Request) {
  95. if req.URL.Path == "/health-check" {
  96. resp.Write([]byte("ok"))
  97. return
  98. }
  99. log.Printf("%s requested %s", req.RemoteAddr, req.URL)
  100. if req.URL.Path == "/" {
  101. resp.Header().Set("Location", "http://labix.org/gopkg.in")
  102. resp.WriteHeader(http.StatusTemporaryRedirect)
  103. return
  104. }
  105. m := patternNew.FindStringSubmatch(req.URL.Path)
  106. compat := false
  107. if m == nil {
  108. m = patternOld.FindStringSubmatch(req.URL.Path)
  109. if m == nil {
  110. sendNotFound(resp, "Unsupported URL pattern; see the documentation at gopkg.in for details.")
  111. return
  112. }
  113. m[2], m[3] = m[3], m[2]
  114. compat = true
  115. }
  116. if strings.Contains(m[3], ".") {
  117. sendNotFound(resp, "Import paths take the major version only (.%s instead of .%s); see docs at gopkg.in for the reasoning.",
  118. m[3][:strings.Index(m[3], ".")], m[3])
  119. return
  120. }
  121. repo := &Repo{
  122. User: m[1],
  123. Pkg: m[2],
  124. VersionStr: m[3],
  125. Path: m[4],
  126. Compat: compat,
  127. }
  128. var ok bool
  129. repo.Version, ok = parseVersion(repo.VersionStr)
  130. if !ok {
  131. sendNotFound(resp, "Version %q improperly considered invalid; please warn the service maintainers.", m[3])
  132. return
  133. }
  134. var err error
  135. var refs []byte
  136. refs, repo.Versions, err = hackedRefs(repo)
  137. switch err {
  138. case nil:
  139. // all ok
  140. case ErrNoRepo:
  141. // if <-nameVersioned {
  142. // v := repo.Version.String()
  143. // repo.GitRoot += "-" + v
  144. // repo.HubRoot += "-" + v
  145. // break
  146. // }
  147. sendNotFound(resp, "GitHub repository not found at %s", repo.HubRoot())
  148. return
  149. case ErrNoVersion:
  150. v := repo.Version.String()
  151. if repo.Version.Minor == -1 {
  152. sendNotFound(resp, `GitHub repository at %s has no branch or tag "%s", "%s.N" or "%s.N.M"`, repo.HubRoot(), v, v, v)
  153. } else if repo.Version.Patch == -1 {
  154. sendNotFound(resp, `GitHub repository at %s has no branch or tag "%s" or "%s.N"`, repo.HubRoot(), v, v)
  155. } else {
  156. sendNotFound(resp, `GitHub repository at %s has no branch or tag "%s"`, repo.HubRoot(), v)
  157. }
  158. return
  159. default:
  160. resp.WriteHeader(http.StatusBadGateway)
  161. resp.Write([]byte(fmt.Sprintf("Cannot obtain refs from GitHub: %v", err)))
  162. return
  163. }
  164. if m[4] == "/git-upload-pack" {
  165. resp.Header().Set("Location", repo.HubRoot()+"/git-upload-pack")
  166. resp.WriteHeader(http.StatusMovedPermanently)
  167. return
  168. }
  169. if m[4] == "/info/refs" {
  170. resp.Header().Set("Content-Type", "application/x-git-upload-pack-advertisement")
  171. resp.Write(refs)
  172. return
  173. }
  174. resp.Header().Set("Content-Type", "text/html")
  175. if req.FormValue("go-get") == "1" {
  176. // execute simple template when this is a go-get request
  177. err = tmplProxy.Execute(resp, repo)
  178. if err != nil {
  179. log.Printf("error executing tmplProxy: %s\n", err)
  180. }
  181. return
  182. }
  183. renderInterface(resp, req, repo)
  184. }
  185. func sendNotFound(resp http.ResponseWriter, msg string, args ...interface{}) {
  186. if len(args) > 0 {
  187. msg = fmt.Sprintf(msg, args...)
  188. }
  189. resp.WriteHeader(http.StatusNotFound)
  190. resp.Write([]byte(msg))
  191. }
  192. // TODO Timeouts for these http interactions. Use the new support coming in 1.3.
  193. const refsSuffix = ".git/info/refs?service=git-upload-pack"
  194. var ErrNoRepo = errors.New("repository not found in github")
  195. var ErrNoVersion = errors.New("version reference not found in github")
  196. func hackedRefs(repo *Repo) (data []byte, versions []Version, err error) {
  197. resp, err := http.Get(repo.HubRoot() + refsSuffix)
  198. if err != nil {
  199. return nil, nil, fmt.Errorf("cannot talk to GitHub: %v", err)
  200. }
  201. switch resp.StatusCode {
  202. case 200:
  203. defer resp.Body.Close()
  204. case 401, 404:
  205. return nil, nil, ErrNoRepo
  206. default:
  207. return nil, nil, fmt.Errorf("error from GitHub: %v", resp.Status)
  208. }
  209. data, err = ioutil.ReadAll(resp.Body)
  210. if err != nil {
  211. return nil, nil, fmt.Errorf("error reading from GitHub: %v", err)
  212. }
  213. var mrefi, mrefj int
  214. var vrefi, vrefj int
  215. var vrefv = InvalidVersion
  216. var unversioned = true
  217. versions = make([]Version, 0)
  218. sdata := string(data)
  219. for i, j := 0, 0; i < len(data); i = j {
  220. size, err := strconv.ParseInt(sdata[i:i+4], 16, 32)
  221. if err != nil {
  222. return nil, nil, fmt.Errorf("cannot parse refs line size: %s", string(data[i:i+4]))
  223. }
  224. if size == 0 {
  225. size = 4
  226. }
  227. j = i + int(size)
  228. if j > len(sdata) {
  229. return nil, nil, fmt.Errorf("incomplete refs data received from GitHub")
  230. }
  231. if sdata[0] == '#' {
  232. continue
  233. }
  234. hashi := i + 4
  235. hashj := strings.IndexByte(sdata[hashi:j], ' ')
  236. if hashj < 0 || hashj != 40 {
  237. continue
  238. }
  239. hashj += hashi
  240. namei := hashj + 1
  241. namej := strings.IndexAny(sdata[namei:j], "\n\x00")
  242. if namej < 0 {
  243. namej = j
  244. } else {
  245. namej += namei
  246. }
  247. name := sdata[namei:namej]
  248. if name == "refs/heads/master" {
  249. mrefi = hashi
  250. mrefj = hashj
  251. }
  252. if strings.HasPrefix(name, "refs/heads/v") || strings.HasPrefix(name, "refs/tags/v") {
  253. v, ok := parseVersion(name[strings.IndexByte(name, 'v'):])
  254. if ok && repo.Version.Contains(v) && (!vrefv.IsValid() || vrefv.Less(v)) {
  255. vrefv = v
  256. vrefi = hashi
  257. vrefj = hashj
  258. }
  259. if ok {
  260. unversioned = false
  261. versions = append(versions, v)
  262. }
  263. }
  264. }
  265. // If there were absolutely no versions, and v0 was requested, accept the master as-is.
  266. if unversioned && repo.Version == (Version{0, -1, -1}) {
  267. return data, nil, nil
  268. }
  269. if mrefi == 0 || vrefi == 0 {
  270. return nil, nil, ErrNoVersion
  271. }
  272. copy(data[mrefi:mrefj], data[vrefi:vrefj])
  273. return data, versions, nil
  274. }