implemented responses for PUT and OPTIONS

This commit is contained in:
Altair-sh
2026-11-06 03:09:01 +01:00
parent 7a43e7f8c9
commit 2098082aa6
11 changed files with 206 additions and 33 deletions
+1 -1
View File
@@ -17,7 +17,7 @@ type Shares []*SharedLocation
type Config struct {
Addr string `toml:"address"`
MaxUpload int64 `toml:"max_upload"`
MaxUpload uint64 `toml:"max_upload"`
HtmlTitle string `toml:"html_title"`
// sorted by Url in reverse to allow this pattern:
// shared/private
+5 -5
View File
@@ -9,16 +9,16 @@ import (
"time"
)
func (conn *TcpConnection) serveFileOrDir(req *http.Request) *http.Response {
func (conn *TcpConnection) GET_HEAD(req *http.Request) *http.Response {
path, share, urlStatus := conn.srv.Conf.Share.MapUrl(req.Url)
// handle authorization
if share != nil {
access := checkSharePasswords(share, req)
if access < AccessReadOnly {
return conn.createErrorResponse(req, false, http.StatusUnauthorized,
"authorization required").
return conn.createErrorResponse(req, false,
http.StatusUnauthorized, "reader password required").
WithHeader("www-authenticate",
`Basic realm="`+share.Url+`", charset="UTF-8"`)
`Basic realm="`+share.Url+` (read)", charset="UTF-8"`)
}
}
@@ -27,7 +27,7 @@ func (conn *TcpConnection) serveFileOrDir(req *http.Request) *http.Response {
return conn.serveFile(req, path)
case UrlDir:
return conn.serveDirListing(req, path)
case UrlNotFound:
case UrlShareNotFound, UrlDoesntExistInShare:
return conn.createErrorResponse(req, false, http.StatusNotFound, req.Url)
case UrlForbidden:
return conn.createErrorResponse(req, false, http.StatusForbidden, req.Url)
+162 -8
View File
@@ -1,12 +1,166 @@
package server
import "serveg/internal/http"
import (
"errors"
"fmt"
"io"
"os"
"serveg/internal/http"
"strconv"
)
// TODO implement file uploading
func (conn *TcpConnection) handleUpload(req *http.Request) *http.Response {
// TODO: use os.OpenFile(filePath + ".upload", os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o644)
// if such file exists, send response like
// "somebody is uploading such file right now"
// TODO: defer { if !renamed { os.Remove(tmp) } }
panic("unimplemented")
func DirOrFileExists(path string) (exists bool, fi os.FileInfo, err error) {
fi, err = os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
err = nil
} else if err == nil {
exists = true
}
return
}
var ErrIsUploadingAlready = http.NewHttpError(http.StatusConflict,
"somebody is already uploading such file")
var ErrFileIsDir = http.NewHttpError(http.StatusConflict,
"there is a directory with such name already")
// PUT: accept request body and write it to filePath+".uploading"
// OPTIONS: check if file can be uploaded and return without reading body
// returns StatusCreated if upload is a new file, and StatusOK if it is replacing existing file
func (conn *TcpConnection) PUT_OPTIONS(req *http.Request) *http.Response {
reqHasBody := req.Method == "PUT"
path, share, urlStatus := conn.srv.Conf.Share.MapUrl(req.Url)
// handle authorization
if share != nil {
if share.WritePassword == "" {
return conn.createErrorResponse(req, reqHasBody, http.StatusForbidden, "share is read-only")
}
access := checkSharePasswords(share, req)
if access < AccessReadWrite {
return conn.createErrorResponse(req, reqHasBody,
http.StatusUnauthorized, "writer password required").
WithHeader("www-authenticate",
`Basic realm="`+share.Url+` (write)", charset="UTF-8"`)
}
}
switch urlStatus {
case UrlFile:
return conn.handleUpload(req, path, true)
case UrlDir:
return conn.http.CreateErrorResponse(req, reqHasBody, ErrFileIsDir)
case UrlDoesntExistInShare:
// TODO: allow users to create directories
return conn.handleUpload(req, path, false)
case UrlShareNotFound:
return conn.createErrorResponse(req, reqHasBody, http.StatusNotFound, req.Url)
case UrlForbidden:
return conn.createErrorResponse(req, reqHasBody, http.StatusForbidden, req.Url)
case UrlIncorrect:
return conn.createErrorResponse(req, reqHasBody, http.StatusBadRequest,
"invalid url '"+req.Url+"'")
default:
panic(fmt.Sprintf("unsupported UrlStatus: %d", int32(urlStatus)))
}
}
func (conn *TcpConnection) handleUpload(req *http.Request, filePath string, fileExists bool,
) *http.Response {
reqHasBody := req.Method == "PUT"
maxUpload := conn.srv.Conf.MaxUpload
var resultStatus http.StatusCode
if fileExists {
resultStatus = http.StatusOK
} else {
resultStatus = http.StatusCreated
}
errToResponse := func(err error) *http.Response {
httpErr, ok := errors.AsType[*http.HttpError](err)
if !ok {
httpErr = http.NewHttpError(http.StatusInternalServerError, err.Error())
}
return conn.http.CreateErrorResponse(req, reqHasBody, httpErr)
}
createResponseMsg := func(status http.StatusCode, msg string) *http.Response {
return conn.http.CreateResponse(status, req, false).
WithBodyStr(msg, http.TextPlain)
}
tmpPath := filePath + ".uploading"
tmpFileExists, fi, err := DirOrFileExists(tmpPath)
if err != nil {
return errToResponse(err)
}
if tmpFileExists {
var httpErr *http.HttpError
if fi.IsDir() {
httpErr = ErrFileIsDir
} else {
httpErr = ErrIsUploadingAlready
}
return conn.http.CreateErrorResponse(req, reqHasBody, httpErr)
}
// OPTIONS
if !reqHasBody {
uploadSize, err := strconv.ParseUint(req.Headers["x-upload-size"], 10, 64)
if err != nil {
return createResponseMsg(http.StatusBadRequest,
"OPTIONS request must have valid header x-upload-size")
}
if uploadSize > maxUpload {
return createResponseMsg(http.StatusRequestEntityTooLarge,
fmt.Sprintf("x-upload-size (%v) > maxUpload (%v)",
uploadSize, maxUpload))
}
return conn.http.CreateResponse(resultStatus, req, false)
}
// PUT
f, err := os.OpenFile(tmpPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o644)
if errors.Is(err, os.ErrExist) {
return errToResponse(ErrIsUploadingAlready)
}
if err != nil {
return errToResponse(err)
}
renamed := false
defer func() {
_ = f.Close()
// remove temp file if upload failed
if !renamed {
// TODO: log this error
_ = os.Remove(tmpPath)
}
}()
// read request body
reqBody, err := req.GetBodyReader(int64(maxUpload))
if err != nil {
return errToResponse(err)
}
n, err := io.Copy(f, reqBody)
if err != nil {
return errToResponse(err)
}
if n != req.ContentLength {
return conn.createErrorResponse(req, true, http.StatusBadRequest,
"uploaded file size != content-length")
}
// move tmpPath to filePath
if err = f.Sync(); err != nil {
return errToResponse(err)
}
if err = f.Close(); err != nil {
return errToResponse(err)
}
if err = os.Rename(tmpPath, filePath); err != nil {
return errToResponse(err)
}
renamed = true
// TODO: is it right status?
return conn.http.CreateResponse(resultStatus, req, false)
}
+4 -3
View File
@@ -90,7 +90,8 @@ type UrlStatus uint32
const (
UrlFile = UrlStatus(iota)
UrlDir
UrlNotFound
UrlDoesntExistInShare
UrlShareNotFound
UrlForbidden
UrlIncorrect
)
@@ -115,7 +116,7 @@ func (shares Shares) MapUrl(url string) (string, *SharedLocation, UrlStatus) {
fi, err := os.Stat(pathAbs)
switch {
case errors.Is(err, fs.ErrNotExist):
return pathAbs, sh, UrlNotFound
return pathAbs, sh, UrlDoesntExistInShare
case err != nil:
return pathAbs, sh, UrlForbidden
case fi.IsDir():
@@ -124,5 +125,5 @@ func (shares Shares) MapUrl(url string) (string, *SharedLocation, UrlStatus) {
return pathAbs, sh, UrlFile
}
}
return url, nil, UrlNotFound
return url, nil, UrlShareNotFound
}
+11 -6
View File
@@ -28,17 +28,15 @@ func (conn *TcpConnection) Start() {
conn.http.Start()
}
func (conn *TcpConnection) handleHttpRequest(req *http.Request) (res *http.Response) {
func (conn *TcpConnection) handleHttpRequest(req *http.Request) *http.Response {
switch req.Method {
case "GET", "HEAD":
res = conn.serveFileOrDir(req)
return conn.GET_HEAD(req)
case "PUT", "OPTIONS":
res = conn.handleUpload(req)
return conn.PUT_OPTIONS(req)
default:
res = conn.createErrorResponse(req, false, http.StatusMethodNotAllowed, req.Method)
res.Headers["allow"] = "GET, HEAD, PUT, OPTIONS"
return conn.responseMethodNotAllowed(req, "GET, HEAD, PUT, OPTIONS")
}
return
}
func (conn *TcpConnection) createErrorResponse(
@@ -46,3 +44,10 @@ func (conn *TcpConnection) createErrorResponse(
) *http.Response {
return conn.http.CreateErrorResponse(req, wantsClose, http.NewHttpError(status, msg))
}
func (conn *TcpConnection) responseMethodNotAllowed(req *http.Request, allowedMethods string,
) *http.Response {
return conn.createErrorResponse(req, false,
http.StatusMethodNotAllowed, req.Method).
WithHeader("allow", allowedMethods)
}
+3 -3
View File
@@ -11,7 +11,7 @@ import (
type ShareAccess uint8
const (
AccessDenied ShareAccess = iota
AccessIncorrectPassword ShareAccess = iota
AccessReadOnly
AccessReadWrite
)
@@ -22,7 +22,7 @@ func checkSharePasswords(share *SharedLocation, req *http.Request,
) ShareAccess {
reqPass := getBasicAuthPassword(req)
canWrite := share.WritePassword != "" &&
(reqPass != "" && passwordMatches(reqPass, share.WritePassword))
reqPass != "" && passwordMatches(reqPass, share.WritePassword)
if canWrite {
return AccessReadWrite
}
@@ -31,7 +31,7 @@ func checkSharePasswords(share *SharedLocation, req *http.Request,
if canRead {
return AccessReadOnly
}
return AccessDenied
return AccessIncorrectPassword
}
func getBasicAuthPassword(req *http.Request) string {