func (f redirectFormatter) serveFromReader(reader io.Reader, writer http.ResponseWriter, request *http.Request) { contents, err := ioutil.ReadAll(reader) if err != nil { log.Warnf("%s %s: could not read from reader: %s", request.Method, request.URL, err) failer.ServeInternalServerError(writer, request) return } var matches = winUrlRegexp.FindSubmatch(contents) if matches == nil || len(matches) < 2 { matches = firstLineRegexp.FindSubmatch(contents) } if matches == nil || len(matches) < 2 { log.Warnf("%s %s: could not find a URL in file: %v", request.Method, request.URL, matches) failer.ServeInternalServerError(writer, request) return } url, err := url.Parse(string(matches[1])) if err != nil { log.Warnf("%s %s: could not parse file contents: %s", request.Method, request.URL, err) failer.ServeInternalServerError(writer, request) return } http.Redirect(writer, request, url.String(), http.StatusFound) }
func (e *TemplateDeliverer) ServeHTTP(writer http.ResponseWriter, request *http.Request) { if request.Method != "GET" { log.Printf("%s %s: wrong method for template handling", request.Method, request.URL) failer.ServeMethodNotAllowed(writer, request) return } readCloser, err := e.loader.LoadResource(request.URL.Path) if err != nil { log.Printf("%s %s: error while opening template resource: %s", request.Method, request.URL, err) failer.ServeNotFound(writer, request) return } defer readCloser.Close() writer.Header().Set("Content-Type", e.detectMimeType(request)) _, err = io.Copy(writer, readCloser) if err != nil { log.Printf("%s %s: error while writing template resource to output: %s", request.Method, request.URL, err) failer.ServeInternalServerError(writer, request) return } }
func (e *Editor) serveWriter(writer http.ResponseWriter, request *http.Request) { if !e.store.HasWriteAccessForRequest(request) { log.Printf("%s %s: no write permissions", request.Method, request.URL) failer.ServeUnauthorized(writer, request) return } var content = request.FormValue("content") if content == "" { log.Printf("%s %s: no valid content in request", request.Method, request.URL) failer.ServeBadRequest(writer, request) return } e.store.WriteString(request, content) if err := e.store.Err(); err != nil { log.Printf("%s %s: %s", request.Method, request.URL, err) failer.ServeInternalServerError(writer, request) return } log.Printf("%s %s: wrote %d bytes", request.Method, request.URL, len(content)) if request.FormValue("saveAndReturn") != "" { router.RedirectToViewMode(writer, request) return } router.RedirectToEditMode(writer, request) }
func (e *Editor) serveEditUI(writer http.ResponseWriter, request *http.Request) { if !e.store.HasWriteAccessForRequest(request) { log.Printf("%s %s: no write permissions", request.Method, request.URL) failer.ServeUnauthorized(writer, request) return } if !router.Is(router.ModeCreate, request) && !e.store.HasReadAccessForRequest(request) { log.Printf("%s %s: no read permissions", request.Method, request.URL) failer.ServeUnauthorized(writer, request) return } mimeType := e.store.MimeTypeForRequest(request) if err := e.assertEditableTextFile(request, mimeType); err != nil { log.Printf("%s %s: no editable text file: %s", request.Method, request.URL, err) failer.ServeUnsupportedMediaType(writer, request) return } var content = e.store.ReadString(request) if err := e.store.Err(); err != nil { if !store.IsPathNotFoundError(err) { log.Printf("%s %s: %s", request.Method, request.URL, err) failer.ServeInternalServerError(writer, request) return } else if router.Is(router.ModeEdit, request) { log.Printf("%s %s: file to be edited does not exist: %s", request.Method, request.URL, err) failer.ServeNotFound(writer, request) return } } else if router.Is(router.ModeCreate, request) { log.Printf("%s %s: file to be created already exists: %s", request.Method, request.URL, err) failer.ServeConflict(writer, request) return } err := e.renderer.Render(writer, request.URL, content, mimeType, router.Is(router.ModeEdit, request)) if err != nil { log.Printf("%s %s: %s", request.Method, request.URL, err) failer.ServeInternalServerError(writer, request) return } log.Printf("%s %s: served from template", request.Method, request.URL) }
func (v *Viewer) serveError(writer http.ResponseWriter, request *http.Request, err error) { v.log(request, err) if store.IsPathNotFoundError(err) { failer.ServeNotFound(writer, request) return } failer.ServeInternalServerError(writer, request) }
func (f rawFormatter) serveFromReader(reader io.Reader, writer http.ResponseWriter, request *http.Request) { writer.Header().Set("Content-Type", f.mimeType) // TODO: Use http.ServeContent instead _, err := io.Copy(writer, reader) if err != nil { log.Printf("%s %s: %s", request.Method, request.URL, err) failer.ServeInternalServerError(writer, request) return } }
func (f markdownFormatter) serveFromReader(reader io.Reader, writer http.ResponseWriter, request *http.Request) { writer.Header().Set("Content-Type", markdownFormatterOutputMimeType) markdown, err := ioutil.ReadAll(reader) if err != nil { log.Warnf("%s %s: %s", request.Method, request.URL, err) failer.ServeInternalServerError(writer, request) return } html := blackfriday.MarkdownCommon(markdown) if err := f.renderer.Render(writer, request.URL, string(html)); err != nil { log.Warnf("%s %s: %s", request.Method, request.URL, err) } }
func (e *Editor) serveDeleter(writer http.ResponseWriter, request *http.Request) { if !e.store.HasDeleteAccessForRequest(request) { log.Printf("%s %s: no delete permissions", request.Method, request.URL) failer.ServeUnauthorized(writer, request) return } e.store.Delete(request) if err := e.store.Err(); err != nil { log.Printf("%s %s: %s", request.Method, request.URL, err) failer.ServeInternalServerError(writer, request) return } log.Printf("%s %s: deleted", request.Method, request.URL) fmt.Fprintf(writer, "Successfully deleted") }