func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { base := httputil.PathBase(req) subPath := httputil.PathSuffix(req) switch req.Method { case "GET", "HEAD": switch subPath { case "": http.Redirect(rw, req, base+"camli/sig/discovery", http.StatusFound) return case h.pubKeyBlobRefServeSuffix: h.pubKeyHandler.ServeHTTP(rw, req) return case "camli/sig/sign": fallthrough case "camli/sig/verify": http.Error(rw, "POST required", 400) return case "camli/sig/discovery": httputil.ReturnJSON(rw, h.DiscoveryMap(base)) return } case "POST": switch subPath { case "camli/sig/sign": h.handleSign(rw, req) return case "camli/sig/verify": h.handleVerify(rw, req) return } } http.Error(rw, "Unsupported path or method.", http.StatusBadRequest) }
func (ui *UIHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { suffix := httputil.PathSuffix(req) rw.Header().Set("Vary", "Accept") switch { case wantsDiscovery(req): ui.root.serveDiscovery(rw, req) case wantsUploadHelper(req): ui.serveUploadHelper(rw, req) case strings.HasPrefix(suffix, "download/"): ui.serveDownload(rw, req) case strings.HasPrefix(suffix, "thumbnail/"): ui.serveThumbnail(rw, req) case strings.HasPrefix(suffix, "tree/"): ui.serveFileTree(rw, req) case strings.HasPrefix(suffix, "qr/"): ui.serveQR(rw, req) case getSuffixMatches(req, closurePattern): ui.serveClosure(rw, req) case getSuffixMatches(req, lessPattern): ui.serveFromDiskOrStatic(rw, req, lessPattern, ui.fileLessHandler, lessstatic.Files) case getSuffixMatches(req, reactPattern): ui.serveFromDiskOrStatic(rw, req, reactPattern, ui.fileReactHandler, reactstatic.Files) case getSuffixMatches(req, glitchPattern): ui.serveFromDiskOrStatic(rw, req, glitchPattern, ui.fileGlitchHandler, glitchstatic.Files) case getSuffixMatches(req, fontawesomePattern): ui.serveFromDiskOrStatic(rw, req, fontawesomePattern, ui.fileFontawesomeHandler, fontawesomestatic.Files) default: file := "" if m := staticFilePattern.FindStringSubmatch(suffix); m != nil { file = m[1] } else { switch { case wantsBlobRef(req): file = "index.html" case wantsPermanode(req): file = "permanode.html" case wantsBlobInfo(req): file = "blobinfo.html" case wantsFileTreePage(req): file = "filetree.html" case req.URL.Path == httputil.PathBase(req): file = "index.html" default: http.Error(rw, "Illegal URL.", http.StatusNotFound) return } } if file == "deps.js" { serveDepsJS(rw, req, ui.uiDir) return } ServeStaticFile(rw, req, uistatic.Files, file) } }
func (ui *UIHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { suffix := httputil.PathSuffix(req) rw.Header().Set("Vary", "Accept") switch { case wantsDiscovery(req): ui.root.serveDiscovery(rw, req) case wantsUploadHelper(req): ui.serveUploadHelper(rw, req) case strings.HasPrefix(suffix, "download/"): ui.serveDownload(rw, req) case strings.HasPrefix(suffix, "thumbnail/"): ui.serveThumbnail(rw, req) case strings.HasPrefix(suffix, "tree/"): ui.serveFileTree(rw, req) case wantsClosure(req): ui.serveClosure(rw, req) default: file := "" if m := staticFilePattern.FindStringSubmatch(suffix); m != nil { file = m[1] } else { switch { case wantsPermanode(req): file = "permanode.html" case wantsBlobInfo(req): file = "blobinfo.html" case wantsFileTreePage(req): file = "filetree.html" case req.URL.Path == httputil.PathBase(req): file = "index.html" default: http.Error(rw, "Illegal URL.", http.StatusNotFound) return } } if file == "deps.js" { envVar := uiFiles.OverrideEnv if envVar != "" && os.Getenv(envVar) != "" { serveDepsJS(rw, req) return } } serveStaticFile(rw, req, uiFiles, file) } }
func (ph *publishHandler) NewRequest(rw http.ResponseWriter, req *http.Request) (*publishRequest, error) { // splits a path request into its suffix and subresource parts. // e.g. /blog/foo/camli/res/file/xxx -> ("foo", "file/xxx") suffix, res := httputil.PathSuffix(req), "" if strings.HasPrefix(suffix, "-/") { suffix, res = "", suffix[2:] } else if s := strings.SplitN(suffix, "/-/", 2); len(s) == 2 { suffix, res = s[0], s[1] } return &publishRequest{ ph: ph, rw: rw, req: req, suffix: suffix, base: httputil.PathBase(req), subres: res, rootpn: ph.rootNode, inSubjectChain: make(map[string]bool), subjectBasePath: "", }, nil }
func (ph *PublishHandler) NewRequest(rw http.ResponseWriter, req *http.Request) *publishRequest { // splits a path request into its suffix and subresource parts. // e.g. /blog/foo/camli/res/file/xxx -> ("foo", "file/xxx") suffix, res := httputil.PathSuffix(req), "" if strings.HasPrefix(suffix, "-/") { suffix, res = "", suffix[2:] } else if s := strings.SplitN(suffix, "/-/", 2); len(s) == 2 { suffix, res = s[0], s[1] } rootpn, _ := ph.rootPermanode() return &publishRequest{ ph: ph, rw: rw, req: req, suffix: suffix, base: httputil.PathBase(req), subres: res, rootpn: rootpn, dr: ph.Search.NewDescribeRequest(), inSubjectChain: make(map[string]bool), subjectBasePath: "", resizeSem: ph.resizeSem, } }
// PathPrefix returns the app's prefix on the app handler if the request was proxied // through Camlistore, or "/" if the request went directly to the app. func PathPrefix(r *http.Request) string { if prefix := httputil.PathBase(r); prefix != "" { return prefix } return "/" }