Skip to content

Commit

Permalink
progress πŸ˜…πŸ˜…πŸ˜…
Browse files Browse the repository at this point in the history
  • Loading branch information
fclairamb committed Mar 10, 2024
1 parent f5571b7 commit 58e4978
Show file tree
Hide file tree
Showing 21 changed files with 1,036 additions and 1,019 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ linters:
- unconvert
- unparam
- unused
- usestdlibvars
- varnamelen
# - varcheck -> unused
- whitespace
# - wrapcheck
Expand Down
32 changes: 16 additions & 16 deletions asciiconverter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func newASCIIConverter(r io.Reader, mode convertMode) *asciiConverter {
}
}

func (c *asciiConverter) Read(p []byte) (int, error) {
func (c *asciiConverter) Read(bytes []byte) (int, error) {
var data []byte
var n int
var readBytes int
var err error

if len(c.remaining) > 0 {
Expand All @@ -41,21 +41,21 @@ func (c *asciiConverter) Read(p []byte) (int, error) {
} else {
data, _, err = c.reader.ReadLine()
if err != nil {
return n, err
return readBytes, err
}
}

n = len(data)
if n > 0 {
maxSize := len(p) - 2
if n > maxSize {
copy(p, data[:maxSize])
readBytes = len(data)
if readBytes > 0 {
maxSize := len(bytes) - 2
if readBytes > maxSize {
copy(bytes, data[:maxSize])
c.remaining = data[maxSize:]

return maxSize, nil
}

copy(p[:n], data[:n])
copy(bytes[:readBytes], data[:readBytes])
}

// we can have a partial read if the line is too long
Expand All @@ -68,22 +68,22 @@ func (c *asciiConverter) Read(p []byte) (int, error) {
// client transfers it in ASCII mode
err = c.reader.UnreadByte()
if err != nil {
return n, err
return readBytes, err

Check warning on line 71 in asciiconverter.go

View check run for this annotation

Codecov / codecov/patch

asciiconverter.go#L71

Added line #L71 was not covered by tests
}

lastByte, err := c.reader.ReadByte()

if err == nil && lastByte == '\n' {
switch c.mode {
case convertModeToCRLF:
p[n] = '\r'
p[n+1] = '\n'
n += 2
bytes[readBytes] = '\r'
bytes[readBytes+1] = '\n'
readBytes += 2
case convertModeToLF:
p[n] = '\n'
n++
bytes[readBytes] = '\n'
readBytes++
}
}

return n, err //nolint:wrapcheck // here wrapping errors brings nothing
return readBytes, err //nolint:wrapcheck // here wrapping errors brings nothing
}
12 changes: 6 additions & 6 deletions asciiconverter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ func TestASCIIConvert(t *testing.T) {
lines := []byte("line1\r\nline2\r\n\r\nline4")
src := bytes.NewBuffer(lines)
dst := bytes.NewBuffer(nil)
c := newASCIIConverter(src, convertModeToLF)
_, err := io.Copy(dst, c)
converter := newASCIIConverter(src, convertModeToLF)
_, err := io.Copy(dst, converter)
require.NoError(t, err)
require.Equal(t, []byte("line1\nline2\n\nline4"), dst.Bytes())

lines = []byte("line1\nline2\n\nline4")
dst = bytes.NewBuffer(nil)
c = newASCIIConverter(bytes.NewBuffer(lines), convertModeToCRLF)
_, err = io.Copy(dst, c)
converter = newASCIIConverter(bytes.NewBuffer(lines), convertModeToCRLF)
_, err = io.Copy(dst, converter)
require.NoError(t, err)
require.Equal(t, []byte("line1\r\nline2\r\n\r\nline4"), dst.Bytes())

Expand All @@ -31,8 +31,8 @@ func TestASCIIConvert(t *testing.T) {
}

dst = bytes.NewBuffer(nil)
c = newASCIIConverter(bytes.NewBuffer(buf), convertModeToCRLF)
_, err = io.Copy(dst, c)
converter = newASCIIConverter(bytes.NewBuffer(buf), convertModeToCRLF)
_, err = io.Copy(dst, converter)
require.NoError(t, err)
require.Equal(t, buf, dst.Bytes())
}
Expand Down
20 changes: 11 additions & 9 deletions client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,23 @@ type clientHandler struct {
}

// newClientHandler initializes a client handler when someone connects
func (server *FtpServer) newClientHandler(connection net.Conn, id uint32, transferType TransferType) *clientHandler {
p := &clientHandler{
func (server *FtpServer) newClientHandler(
connection net.Conn,
clientID uint32,
transferType TransferType,
) *clientHandler {
return &clientHandler{
server: server,
conn: connection,
id: id,
id: clientID,
writer: bufio.NewWriter(connection),
reader: bufio.NewReaderSize(connection, maxCommandSize),
connectedAt: time.Now().UTC(),
path: "/",
selectedHashAlgo: HASHAlgoSHA256,
currentTransferType: transferType,
logger: server.Logger.With("clientId", id),
logger: server.Logger.With("clientId", clientID),
}

return p
}

func (c *clientHandler) disconnect() {
Expand Down Expand Up @@ -742,14 +744,14 @@ func getIPFromRemoteAddr(remoteAddr net.Addr) (net.IP, error) {
return nil, &ipValidationError{error: "nil remote address"}
}

ip, _, err := net.SplitHostPort(remoteAddr.String())
ipAddress, _, err := net.SplitHostPort(remoteAddr.String())
if err != nil {
return nil, fmt.Errorf("error parsing remote address: %w", err)
}

remoteIP := net.ParseIP(ip)
remoteIP := net.ParseIP(ipAddress)
if remoteIP == nil {
return nil, &ipValidationError{error: fmt.Sprintf("invalid remote IP: %v", ip)}
return nil, &ipValidationError{error: fmt.Sprintf("invalid remote IP: %v", ipAddress)}
}

return remoteIP, nil
Expand Down
Loading

0 comments on commit 58e4978

Please sign in to comment.