Ejemplo n.º 1
0
Archivo: Viewer.go Proyecto: fxnn/gone
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)
}
Ejemplo n.º 2
0
func TestReadNonExistantFileReturnsPathNotFoundError(t *testing.T) {
	sut := sutNotAuthenticated(t)

	readCloser := sut.OpenReader(requestGET("/thisFileShouldNeverExist"))
	closed(readCloser)
	if err := sut.Err(); err == nil {
		t.Fatalf("expected error, but got nil")
	} else if !store.IsPathNotFoundError(err) {
		t.Fatalf("expected PathNotFoundError, but got %v", err)
	}
}
Ejemplo n.º 3
0
func (s *errStore) prependErr(prefix string) {
	if s.err != nil {
		var msg = fmt.Sprintf("%s: %s", prefix, s.err.Error())
		switch {
		case store.IsPathNotFoundError(s.err):
			s.err = store.NewPathNotFoundError(msg)
		case store.IsAccessDeniedError(s.err):
			s.err = store.NewAccessDeniedError(msg)
		default:
			s.err = fmt.Errorf(msg)
		}
	}
}
Ejemplo n.º 4
0
// https://github.com/fxnn/gone/issues/15
func TestCreateFileInHiddenDirReturnsPathNotFoundError(t *testing.T) {
	tempDir := createPrefixedTempDirInCurrentwd(t, 0777, ".")
	defer removeTempDirFromCurrentwd(t, tempDir)

	sut := sutNotAuthenticated(t)

	readCloser := sut.OpenWriter(requestGET("/" + tempDir + "/newFile"))
	closed(readCloser)
	if err := sut.Err(); err == nil {
		t.Fatalf("expected error, but got nil")
	} else if !store.IsPathNotFoundError(err) {
		t.Fatalf("expected PathNotFoundError, but got %v", err)
	}
}
Ejemplo n.º 5
0
func TestReadHiddenFileReturnsPathNotFoundError(t *testing.T) {
	skipOnWindows(t)

	tempFile := createPrefixedTempFileInCurrentwd(t, 0777, ".")
	defer removeTempFileFromCurrentwd(t, tempFile)

	sut := sutNotAuthenticated(t)

	readCloser := sut.OpenReader(requestGET("/" + tempFile))
	closed(readCloser)
	if err := sut.Err(); err == nil {
		t.Fatalf("expected error, but got nil")
	} else if !store.IsPathNotFoundError(err) {
		t.Fatalf("expected PathNotFoundError, but got %v", err)
	}
}
Ejemplo n.º 6
0
Archivo: editor.go Proyecto: fxnn/gone
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)
}
Ejemplo n.º 7
0
func TestAccessToParentDirDenied(t *testing.T) {
	skipOnWindows(t)

	tempFile := createTempFileInCurrentwd(t, 0777)
	defer removeTempFileFromCurrentwd(t, tempFile)

	tempWd := createTempWdInCurrentwd(t, 0777)
	defer removeTempWdFromCurrentwd(t, tempWd)

	sut := sutAuthenticated(t)

	readCloser := sut.OpenReader(requestGET("/../" + tempFile))
	closed(readCloser)
	if err := sut.Err(); err == nil {
		t.Fatalf("could open reader for parent dir of working directory %s", getwd(t))
	} else if !store.IsPathNotFoundError(err) {
		t.Fatalf("expected PathNotFoundError: %s", err)
	}
}
Ejemplo n.º 8
0
Archivo: editor.go Proyecto: fxnn/gone
func (e *Editor) assertEditableTextFile(request *http.Request, mimeType string) error {
	bytes := e.store.FileSizeForRequest(request)
	if err := e.store.Err(); err != nil && store.IsPathNotFoundError(err) {
		// HINT: doesn't exist; that's pretty editable
		return nil
	}

	if bytes > maxEditableBytes {
		return fmt.Errorf(
			"the file size of %d bytes is larger than the allowed %d bytes",
			bytes, maxEditableBytes)
	}

	if e.isKnownEditableMimeType(mimeType) {
		return nil
	}

	return fmt.Errorf("the mime type %s doesn't represent editable text", mimeType)
}
Ejemplo n.º 9
0
func (s *errStore) hasPathNotFoundError() bool {
	return s.hasErr() && store.IsPathNotFoundError(s.err)
}