Skip to content

Commit

Permalink
add retr
Browse files Browse the repository at this point in the history
  • Loading branch information
Shunpoco committed Jan 8, 2021
1 parent 9b7bd18 commit 1e5cbad
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions ch8/ex02/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
Expand Down Expand Up @@ -33,12 +34,10 @@ func main() {
func dataConn(c net.Conn) {
defer c.Close()
fmt.Fprintln(c, "220 Welcome to my FTP-server")
// dir, err := os.Getwd()
// if err != nil {
// log.Fatal(err)
// }
input := bufio.NewScanner(c)

var conn net.Conn

for input.Scan() {
texts := strings.Split(input.Text(), " ")
fmt.Println(texts)
Expand All @@ -52,6 +51,11 @@ func dataConn(c net.Conn) {
}
fmt.Fprintf(c, "200")
case "RETR": // get
err := handleRetr(conn, texts)
if err != nil {
fmt.Fprintln(c, "400")
break
}
fmt.Fprintln(c, "200 RETR")
case "STOR": // put
fmt.Fprintln(c, "150 STOR")
Expand Down Expand Up @@ -150,3 +154,22 @@ func handleList(conn net.Conn, texts []string) error {
fmt.Fprintln(conn, results)
return nil
}

func handleRetr(conn net.Conn, texts []string) error {
if len(texts) < 2 {
return fmt.Errorf("no filepath")
}

path := texts[1]

content, err := ioutil.ReadFile(path)
if err != nil {
return err
}
_, err = conn.Write(content)
if err != nil {
return err
}

return nil
}

0 comments on commit 1e5cbad

Please sign in to comment.