Skip to content

Commit

Permalink
Merge pull request apache#122 from ccollins476ad/request-too-big
Browse files Browse the repository at this point in the history
image: Multiple rounds of chunklen reduction
  • Loading branch information
ccollins476ad authored May 9, 2019
2 parents 8cf32aa + c953418 commit fdbcd95
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions nmxact/xact/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,14 @@ func min(a, b int) int {
return b
}

func findChunkLen(s sesn.Sesn, hash []byte, upgrade bool, data []byte,
off int) (int, error) {
func encodeUploadReq(s sesn.Sesn, hash []byte, upgrade bool, data []byte,
off int, chunklen int) ([]byte, error) {

// Let's start by encoding max allowed chunk len and we will see how many
// bytes we need to cut
chunklen := min(len(data)-off, IMAGE_UPLOAD_MAX_CHUNK)
r := buildImageUploadReq(len(data), hash, upgrade, data[off:off+chunklen],
off)
enc, err := mgmt.EncodeMgmt(s, r.Msg())
if err != nil {
return 0, err
return nil, err
}

// If encoded length is larger than MTU, we need to make chunk shorter
Expand All @@ -109,6 +106,32 @@ func findChunkLen(s sesn.Sesn, hash []byte, upgrade bool, data []byte,
chunklen -= overflow
}

return enc, nil
}

func findChunkLen(s sesn.Sesn, hash []byte, upgrade bool, data []byte,
off int) (int, error) {

// Let's start by encoding max allowed chunk len and we will see how many
// bytes we need to cut
chunklen := min(len(data)-off, IMAGE_UPLOAD_MAX_CHUNK)

// Keep reducing the chunk size until the request fits the MTU.
for {
enc, err := encodeUploadReq(s, hash, upgrade, data, off, chunklen)
if err != nil {
return 0, err
}

if len(enc) <= s.MtuOut() {
break
}

// Encoded length is larger than MTU, we need to make chunk shorter
overflow := len(enc) - s.MtuOut()
chunklen -= overflow
}

return chunklen, nil
}

Expand Down

0 comments on commit fdbcd95

Please sign in to comment.