2019-10-07 16:19:01 +00:00
|
|
|
package httpd
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
import (
|
2020-01-31 22:26:56 +00:00
|
|
|
"context"
|
2021-02-01 18:04:15 +00:00
|
|
|
"errors"
|
2019-07-20 10:26:52 +00:00
|
|
|
"net/http"
|
2019-12-27 22:12:44 +00:00
|
|
|
"os"
|
2021-02-01 18:04:15 +00:00
|
|
|
"strconv"
|
2019-07-20 10:26:52 +00:00
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/go-chi/render"
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
"github.com/drakkan/sftpgo/common"
|
2019-07-20 10:26:52 +00:00
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
)
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
func sendAPIResponse(w http.ResponseWriter, r *http.Request, err error, message string, code int) {
|
|
|
|
var errorString string
|
|
|
|
if err != nil {
|
|
|
|
errorString = err.Error()
|
|
|
|
}
|
|
|
|
resp := apiResponse{
|
2020-09-08 07:45:21 +00:00
|
|
|
Error: errorString,
|
|
|
|
Message: message,
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
2020-01-31 22:26:56 +00:00
|
|
|
ctx := context.WithValue(r.Context(), render.StatusCtxKey, code)
|
|
|
|
render.JSON(w, r.WithContext(ctx), resp)
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getRespStatus(err error) int {
|
|
|
|
if _, ok := err.(*dataprovider.ValidationError); ok {
|
|
|
|
return http.StatusBadRequest
|
|
|
|
}
|
|
|
|
if _, ok := err.(*dataprovider.MethodDisabledError); ok {
|
|
|
|
return http.StatusForbidden
|
|
|
|
}
|
2020-06-20 10:38:04 +00:00
|
|
|
if _, ok := err.(*dataprovider.RecordNotFoundError); ok {
|
|
|
|
return http.StatusNotFound
|
|
|
|
}
|
2019-12-27 22:12:44 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return http.StatusBadRequest
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
return http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func handleCloseConnection(w http.ResponseWriter, r *http.Request) {
|
|
|
|
connectionID := getURLParam(r, "connectionID")
|
|
|
|
if connectionID == "" {
|
|
|
|
sendAPIResponse(w, r, nil, "connectionID is mandatory", http.StatusBadRequest)
|
|
|
|
return
|
2020-06-07 21:30:18 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
if common.Connections.Close(connectionID) {
|
|
|
|
sendAPIResponse(w, r, nil, "Connection closed", http.StatusOK)
|
2020-06-07 21:30:18 +00:00
|
|
|
} else {
|
2021-01-17 21:29:08 +00:00
|
|
|
sendAPIResponse(w, r, nil, "Not Found", http.StatusNotFound)
|
2020-09-01 14:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-01 18:04:15 +00:00
|
|
|
|
|
|
|
func getSearchFilters(w http.ResponseWriter, r *http.Request) (int, int, string, error) {
|
|
|
|
var err error
|
|
|
|
limit := 100
|
|
|
|
offset := 0
|
|
|
|
order := dataprovider.OrderASC
|
|
|
|
if _, ok := r.URL.Query()["limit"]; ok {
|
|
|
|
limit, err = strconv.Atoi(r.URL.Query().Get("limit"))
|
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
err = errors.New("invalid limit")
|
2021-02-01 18:04:15 +00:00
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return limit, offset, order, err
|
|
|
|
}
|
|
|
|
if limit > 500 {
|
|
|
|
limit = 500
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := r.URL.Query()["offset"]; ok {
|
|
|
|
offset, err = strconv.Atoi(r.URL.Query().Get("offset"))
|
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
err = errors.New("invalid offset")
|
2021-02-01 18:04:15 +00:00
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return limit, offset, order, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := r.URL.Query()["order"]; ok {
|
|
|
|
order = r.URL.Query().Get("order")
|
|
|
|
if order != dataprovider.OrderASC && order != dataprovider.OrderDESC {
|
2021-03-21 18:15:47 +00:00
|
|
|
err = errors.New("invalid order")
|
2021-02-01 18:04:15 +00:00
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return limit, offset, order, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return limit, offset, order, err
|
|
|
|
}
|