Send a Content-Length on Go client's upload.

This commit is contained in:
Brad Fitzpatrick 2011-01-13 09:59:49 -08:00
parent 729a816971
commit 3e90b7c1fa
1 changed files with 14 additions and 6 deletions

View File

@ -106,18 +106,26 @@ func (c *Client) Upload(h *UploadHandle) (*PutResult, os.Error) {
}
}
// TODO: use a proper random boundary
boundary := "sdf8sd8f7s9df9s7df9sd7sdf9s879vs7d8v7sd8v7sd8v"
req = http.NewPostRequest(uploadUrl,
"multipart/form-data; boundary="+boundary,
io.MultiReader(
strings.NewReader(fmt.Sprintf(
multiPartHeader := fmt.Sprintf(
"--%s\r\nContent-Type: application/octet-stream\r\n" +
"Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n\r\n",
boundary,
h.BlobRef, h.BlobRef)),
h.BlobRef, h.BlobRef)
multiPartFooter := "\r\n--"+boundary+"--\r\n"
log.Printf("Uploading to URL: %s", uploadUrl)
req = http.NewPostRequest(uploadUrl,
"multipart/form-data; boundary="+boundary,
io.MultiReader(
strings.NewReader(multiPartHeader),
h.Contents,
strings.NewReader("\r\n--"+boundary+"--\r\n")))
strings.NewReader(multiPartFooter)))
req.Header["Authorization"] = authHeader
req.ContentLength = int64(len(multiPartHeader)) + h.Size + int64(len(multiPartFooter))
req.TransferEncoding = nil
resp, err = req.Send()
if err != nil {
return error("upload http error", err)