moved Read/Send code to http.Connection

This commit is contained in:
Altair-sh
2026-11-04 03:32:36 +01:00
parent 59f7d873be
commit 0ecbb629ea
10 changed files with 297 additions and 274 deletions
+89
View File
@@ -0,0 +1,89 @@
package server
import (
"fmt"
"os"
"path"
"serveg/internal/html"
"serveg/internal/http"
"time"
)
func (conn *TcpConnection) serveFileOrDir(req *http.Request) *http.Response {
path, share, urlStatus := conn.srv.Conf.Share.MapUrl(req.Url)
// TODO implement authorization
_ = share
switch urlStatus {
case UrlFile:
return conn.serveFile(req, path)
case UrlDir:
return conn.serveDirListing(req, path)
case UrlNotFound:
return conn.createErrorResponse(req, false, http.StatusNotFound, path)
case UrlForbidden:
return conn.createErrorResponse(req, false, http.StatusForbidden, path)
case UrlIncorrect:
return conn.createErrorResponse(req, false, http.StatusBadRequest,
"invalid url '"+req.Url+"'")
default:
panic(fmt.Sprintf("unsupported UrlStatus: %d", int32(urlStatus)))
}
}
func (conn *TcpConnection) serveDirListing(req *http.Request, dirPath string) *http.Response {
htmlBytes, err := html.GenDirListingHtml(&conn.log, dirPath,
req.Url, conn.srv.Conf.HtmlTitle)
if err != nil {
return conn.createErrorResponse(req, false, http.StatusInternalServerError, err.Error())
}
return conn.http.CreateResponse(http.StatusOK, req, false).
WithBodyBuf(htmlBytes, http.TextHtml)
}
func (conn *TcpConnection) serveFile(req *http.Request, filePath string) *http.Response {
f, err := os.Open(filePath)
if err != nil {
return conn.createErrorResponse(req, false, http.StatusInternalServerError, err.Error())
}
var fileOwnershipTransferred bool
defer func() {
if !fileOwnershipTransferred {
_ = f.Close()
}
}()
finf, err := f.Stat()
if err != nil {
return conn.createErrorResponse(req, false, http.StatusInternalServerError, err.Error())
}
size := finf.Size()
// time is received with second precision
// if not truncate, mtime will always be > ifModifiedSince
mtime := finf.ModTime().UTC().Truncate(time.Second)
ifModifiedSince, errTime := time.Parse(http.HeaderTimeFormat, req.Headers["if-modified-since"])
notModified := errTime == nil && !mtime.After(ifModifiedSince)
if notModified {
return conn.http.CreateResponse(http.StatusNotModified, req, false)
}
res := conn.http.CreateResponse(http.StatusOK, req, false)
fileName := path.Base(filePath)
if req.Method != "HEAD" {
// file will be closed by SendResponse()
fileOwnershipTransferred = true
res.WithBodyReader(f, size, http.GetMimeType(fileName))
}
// Tell browser to not use its own caching, remember last-modified
// On next request browser will send if-modified-since and let server check if file changed.
res.Headers["cache-control"] = "no-cache"
res.Headers["last-modified"] = mtime.Format(http.HeaderTimeFormat)
res.Headers["content-disposition"] = http.ContentDisposition("inline", fileName)
//TODO: accept "range" header and return StatusPartialContent
// res.Headers["accept-ranges"] = "bytes"
return res
}
+12
View File
@@ -0,0 +1,12 @@
package server
import "serveg/internal/http"
// 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")
}
+6 -12
View File
@@ -5,25 +5,19 @@ import (
"net"
"serveg/internal/log"
"sync"
"time"
)
type Server struct {
Conf *Config
logger log.LoggerContext
// used in sockets to sed read/write deadline
timeout time.Duration
Conf *Config
logger log.LoggerContext
listener net.Listener
wg sync.WaitGroup
}
const defaultTimeout = 30 * time.Second
func NewServer(conf *Config) *Server {
return &Server{
Conf: conf,
logger: log.LoggerContext{Name: "server"},
timeout: defaultTimeout,
Conf: conf,
logger: log.LoggerContext{Name: "server"},
}
}
@@ -48,8 +42,8 @@ func (srv *Server) serve() {
srv.logger.Error(err)
continue
}
conn := newConnection(srv, i, accepted.(*net.TCPConn))
go conn.handle()
conn := NewTcpConnection(srv, accepted.(*net.TCPConn), i)
go conn.Start()
}
}
+48
View File
@@ -0,0 +1,48 @@
package server
import (
"net"
"serveg/internal/http"
"serveg/internal/log"
"strconv"
)
type TcpConnection struct {
srv *Server
socket *net.TCPConn
log log.LoggerContext
http *http.Connection
}
func NewTcpConnection(srv *Server, socket *net.TCPConn, n int64) *TcpConnection {
conn := &TcpConnection{
srv: srv,
socket: socket,
log: log.LoggerContext{Name: "conn-" + strconv.FormatInt(n, 10)},
}
conn.http = http.NewConnection(socket, &conn.log, conn.handleHttpRequest)
return conn
}
func (conn *TcpConnection) Start() {
conn.http.Start()
}
func (conn *TcpConnection) handleHttpRequest(req *http.Request) (res *http.Response) {
switch req.Method {
case "GET", "HEAD":
res = conn.serveFileOrDir(req)
case "PUT", "OPTIONS":
res = conn.handleUpload(req)
default:
res = conn.createErrorResponse(req, false, http.StatusMethodNotAllowed, req.Method)
res.Headers["allow"] = "GET, HEAD, PUT, OPTIONS"
}
return
}
func (conn *TcpConnection) createErrorResponse(
req *http.Request, wantsClose bool, status http.StatusCode, msg string,
) *http.Response {
return conn.http.CreateErrorResponse(req, wantsClose, http.NewHttpError(status, msg))
}
-212
View File
@@ -1,212 +0,0 @@
package server
import (
"errors"
"fmt"
"io"
"net"
"os"
"path"
"serveg/internal/html"
"serveg/internal/http"
"serveg/internal/log"
"strconv"
"syscall"
"time"
)
type connection struct {
srv *Server
n int64
socket *net.TCPConn
log log.LoggerContext
http *http.Connection
req *http.Request
}
func newConnection(srv *Server, n int64, socket *net.TCPConn) *connection {
return &connection{
srv: srv,
n: n,
socket: socket,
log: log.LoggerContext{Name: "conn-" + strconv.FormatInt(n, 10)},
http: http.NewConnection(socket, srv.timeout),
}
}
func (conn *connection) handle() {
defer func() {
if r := recover(); r != nil {
conn.log.Error("panic: ", r)
}
_ = conn.socket.Close()
conn.log.Info("connection closed")
}()
for {
err := conn.socket.SetDeadline(time.Now().Add(conn.srv.timeout))
if err != nil {
conn.log.Warn(err)
return
}
var res *http.Response
conn.req, err = conn.http.ReadRequest()
if err != nil {
// invalid request
if httpErr, ok := errors.AsType[*http.HttpError](err); ok {
conn.log.Warn(err)
res = conn.http.CreateErrorResponse(conn.req, true, httpErr)
} else {
conn.logError(err)
conn.lingeringClose()
return
}
} else {
// valid request
conn.log.Info(conn.req.Method, " ", conn.req.Url, " ", conn.req.Protocol)
conn.log.Trace("request headers: ", conn.req.Headers)
switch conn.req.Method {
case "GET", "HEAD":
res = conn.serveFileOrDir()
case "PUT", "OPTIONS":
res = conn.handleUpload()
default:
res = conn.createErrorResponse(false, http.StatusMethodNotAllowed, conn.req.Method)
}
}
conn.log.Info("sending response: ", res.Status,
" content-length: ", res.Headers["content-length"])
conn.log.Trace("response headers: ", res.Headers)
err = conn.http.SendResponse(res)
if err != nil {
conn.logError(err)
return
}
// it is set by CreateResponse
if res.Headers["connection"] == "close" {
conn.lingeringClose()
return
}
}
}
func (conn *connection) logError(err error) {
if errors.Is(err, os.ErrDeadlineExceeded) {
conn.log.Info("time out")
} else if errors.Is(err, io.EOF) ||
errors.Is(err, syscall.EPIPE) ||
errors.Is(err, syscall.ECONNRESET) {
conn.log.Info("closed by client")
} else {
conn.log.Error(err)
}
}
// lingeringClose: shut down our side, then drain the client's unread data,
// so close() doesn't send RST and destroy the response before it's read.
// Limits: 500 ms, 32 KB
func (conn *connection) lingeringClose() {
conn.log.Info("lingering close")
if conn.socket.CloseWrite() != nil {
return
}
_ = conn.socket.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
_, _ = io.Copy(io.Discard, io.LimitReader(conn.socket, 32*1024))
}
func (conn *connection) createErrorResponse(
wantsClose bool, status http.StatusCode, msg string,
) *http.Response {
return conn.http.CreateErrorResponse(conn.req, wantsClose, http.NewHttpError(status, msg))
}
func (conn *connection) serveFileOrDir() *http.Response {
path, share, urlStatus := conn.srv.Conf.Share.MapUrl(conn.req.Url)
// TODO implement authorization
_ = share
switch urlStatus {
case UrlFile:
return conn.serveFile(path)
case UrlDir:
return conn.serveDirListing(path)
case UrlNotFound:
return conn.createErrorResponse(false, http.StatusNotFound, path)
case UrlForbidden:
return conn.createErrorResponse(false, http.StatusForbidden, path)
case UrlIncorrect:
return conn.createErrorResponse(false, http.StatusBadRequest,
"invalid url '"+conn.req.Url+"'")
default:
panic(fmt.Sprintf("unsupported UrlStatus: %d", int32(urlStatus)))
}
}
func (conn *connection) serveDirListing(dirPath string) *http.Response {
htmlBytes, err := html.GenDirListingHtml(&conn.log, dirPath,
conn.req.Url, conn.srv.Conf.HtmlTitle)
if err != nil {
return conn.createErrorResponse(false, http.StatusInternalServerError, err.Error())
}
return conn.http.CreateResponse(http.StatusOK, conn.req, false).
WithBodyBuf(htmlBytes, http.TextHtml)
}
func (conn *connection) serveFile(filePath string) *http.Response {
f, err := os.Open(filePath)
if err != nil {
return conn.createErrorResponse(false, http.StatusInternalServerError, err.Error())
}
var fileOwnershipTransferred bool
defer func() {
if !fileOwnershipTransferred {
_ = f.Close()
}
}()
finf, err := f.Stat()
if err != nil {
return conn.createErrorResponse(false, http.StatusInternalServerError, err.Error())
}
size := finf.Size()
// time is received with second precision
// if not truncate, mtime will always be > ifModifiedSince
mtime := finf.ModTime().UTC().Truncate(time.Second)
ifModifiedSince, errTime := time.Parse(http.TimeFormat, conn.req.Headers["if-modified-since"])
notModified := errTime == nil && !mtime.After(ifModifiedSince)
if notModified {
return conn.http.CreateResponse(http.StatusNotModified, conn.req, false)
}
res := conn.http.CreateResponse(http.StatusOK, conn.req, false)
fileName := path.Base(filePath)
if conn.req.Method != "HEAD" {
// file will be closed by SendResponse()
fileOwnershipTransferred = true
res.WithBodyReader(f, size, http.GetMimeType(fileName))
}
// Tell browser to not use its own caching, remember last-modified
// On next request browser will send if-modified-since and let server check if file changed.
res.Headers["cache-control"] = "no-cache"
res.Headers["last-modified"] = mtime.Format(http.TimeFormat)
res.Headers["content-disposition"] = http.ContentDisposition("inline", fileName)
//TODO: accept "range" header and return StatusPartialContent
// res.Headers["accept-ranges"] = "bytes"
return res
}
// TODO: implement request body and then file uploading
func (conn *connection) handleUpload() *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")
}