2019-10-07 18:19:01 +02:00
|
|
|
package httpd
|
2019-07-20 12:26:52 +02:00
|
|
|
|
|
|
|
|
import (
|
2020-01-31 23:26:56 +01:00
|
|
|
"context"
|
2019-07-20 12:26:52 +02:00
|
|
|
"net/http"
|
2019-12-27 23:12:44 +01:00
|
|
|
"os"
|
2019-07-20 12:26:52 +02:00
|
|
|
|
2020-05-06 19:36:34 +02:00
|
|
|
"github.com/go-chi/render"
|
|
|
|
|
|
2020-07-24 23:39:38 +02:00
|
|
|
"github.com/drakkan/sftpgo/common"
|
2019-07-20 12:26:52 +02:00
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
|
)
|
|
|
|
|
|
2019-10-07 18:19:01 +02: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 09:45:21 +02:00
|
|
|
Error: errorString,
|
|
|
|
|
Message: message,
|
2019-10-07 18:19:01 +02:00
|
|
|
}
|
2020-01-31 23:26:56 +01:00
|
|
|
ctx := context.WithValue(r.Context(), render.StatusCtxKey, code)
|
|
|
|
|
render.JSON(w, r.WithContext(ctx), resp)
|
2019-10-07 18:19:01 +02: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 12:38:04 +02:00
|
|
|
if _, ok := err.(*dataprovider.RecordNotFoundError); ok {
|
|
|
|
|
return http.StatusNotFound
|
|
|
|
|
}
|
2019-12-27 23:12:44 +01:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
return http.StatusBadRequest
|
|
|
|
|
}
|
2019-10-07 18:19:01 +02:00
|
|
|
return http.StatusInternalServerError
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 22:29:08 +01: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 23:30:18 +02:00
|
|
|
}
|
2021-01-17 22:29:08 +01:00
|
|
|
if common.Connections.Close(connectionID) {
|
|
|
|
|
sendAPIResponse(w, r, nil, "Connection closed", http.StatusOK)
|
2020-06-07 23:30:18 +02:00
|
|
|
} else {
|
2021-01-17 22:29:08 +01:00
|
|
|
sendAPIResponse(w, r, nil, "Not Found", http.StatusNotFound)
|
2020-09-01 16:10:26 +02:00
|
|
|
}
|
|
|
|
|
}
|