Skip to content

Commit

Permalink
fix: resolve buf for not downloading end of file
Browse files Browse the repository at this point in the history
resolve bug that sometimes occure depending on the size and header given
which did not download the end of the files
  • Loading branch information
alipourhabibi committed Dec 15, 2023
1 parent 3a0c274 commit 18a62d2
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ func (s *server) DownloadParallel(resourse string, length int) error {
offset := 0
for offset = 0; offset < 10; offset++ {
wg.Add(1)
go save(s.fd, s.serverAddr, resourse, offset, size, size, &wg)
if offset == 9 {
go save(s.fd, s.serverAddr, resourse, offset, size, size+(length-(size*10)), &wg)
} else {
go save(s.fd, s.serverAddr, resourse, offset, size, size, &wg)
}
}
// wg.Add(1)
// save(s.fd, s.serverAddr, resourse, offset, size, length-(size*10)+size, &wg)
Expand Down Expand Up @@ -211,29 +215,34 @@ func save(serverFD int, serverAddr *syscall.SockaddrInet4, file string, offset,
}
index := 0
if strings.Contains(string(response), "\r\n\r\n") {
header := []byte(strings.Split(string(response), "\r\n\r\n")[0])
response = []byte(strings.Split(string(response), "\r\n\r\n")[1])
response = response[:n-len(header)]
response = response[:len(response)-4]
if len(response) != 0 {
w.Save(response[:n-len(header)-4], int64(b))
index += n - len(header) - 4
b += index
splitedResp := strings.Split(string(response), "\r\n\r\n")
if len(splitedResp) >= 2 {
header := []byte(splitedResp[0])
response = []byte(strings.Join(splitedResp[1:], "\r\n\r\n"))
response = response[:n-len(header)]
response = response[:len(response)-4]
if len(response) != 0 {
w.Save(response, int64(b))
index += len(response)
b += index
}
} else {
wg.Done()
return
}
}

for {
response = make([]byte, 8000)
if index > msgSize {
if index >= msgSize {
break
}
response = make([]byte, 8000)
n, _, err := syscall.Recvfrom(serverFD, response, syscall.MSG_WAITFORONE)
if err != nil {
fmt.Println("Recvfrom: ", err)
syscall.Close(serverFD)
return
}

w.Lock()
w.Save(response[:n], int64(b))
w.Unlock()
Expand Down

0 comments on commit 18a62d2

Please sign in to comment.