net icon indicating copy to clipboard operation
net copied to clipboard

fix the error that the final Read of the frame payload bytes discarded

Open jemygraw opened this issue 7 years ago • 8 comments

This is a little snippet displaying the error caused by this line.

package main

import (
	"bufio"
	"flag"
	"fmt"
	"os"

	"golang.org/x/net/websocket"
)

func main() {
	var origin string
	var url string
	flag.StringVar(&origin, "origin", "", "websocket origin")
	flag.StringVar(&url, "url", "", "websocket remote url")
	flag.Parse()

	ws, err := websocket.Dial(url, "", origin)
	if err != nil {
		panic(err)
		return
	}

	buffer := make([]byte, 100)
	bScanner := bufio.NewScanner(os.Stdin)
	fmt.Print("> ")
	for bScanner.Scan() {
		line := bScanner.Text()
		ws.Write([]byte(line + "\r\n"))

		num, err := ws.Read(buffer)
		if err != nil {
			ws.Close()
			return
		}
		fmt.Println("total:", ws.Len(), "read:", num)
		buffer = make([]byte, ws.Len())

		remained := ws.Len() - num
		for {
			fmt.Println("remained:", remained)
			num, err = ws.Read(buffer)
			if err != nil {
				ws.Close()
				return
			}
			remained -= num
			fmt.Println("next read:", num)
		}
		// for next command
		fmt.Println()
		fmt.Print("> ")
	}
}

When the buffer size of the ws.Read is not enough, we call this Read multiple times, but the last call of this ws.Read is led into the block again, which is wrong under this case.

jemygraw avatar Nov 06 '18 03:11 jemygraw

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

googlebot avatar Nov 06 '18 03:11 googlebot

I signed it!

jemygraw avatar Nov 06 '18 03:11 jemygraw

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google. In order to pass this check, please resolve this problem and have the pull request author add another comment and the bot will run again. If the bot doesn't comment, it means it doesn't think anything has changed.

googlebot avatar Nov 06 '18 03:11 googlebot

I signed it!

jemygraw avatar Nov 06 '18 03:11 jemygraw

CLAs look good, thanks!

googlebot avatar Nov 06 '18 03:11 googlebot

This PR (HEAD: f3953088871eee9367cb2419f69eada3048c973c) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/net/+/147697 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off) See the Wiki page for more info

gopherbot avatar Nov 06 '18 03:11 gopherbot

Message from Gerrit User 5976:

Patch Set 1:

Congratulations on opening your first change. Thank you for your contribution!

Next steps: Within the next week or so, a maintainer will review your change and provide feedback. See https://golang.org/doc/contribute.html#review for more info and tips to get your patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be surprising to people new to the project. The careful, iterative review process is our way of helping mentor contributors and ensuring that their contributions have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which little code gets reviewed or merged. If a reviewer responds with a comment like R=go1.11, it means that this CL will be reviewed as part of the next development cycle. See https://golang.org/s/release for more details.


Please don’t reply on this GitHub thread. Visit golang.org/cl/147697. After addressing review feedback, remember to publish your drafts!

gopherbot avatar Nov 06 '18 03:11 gopherbot

Message from Gerrit User 29736:

Patch Set 1:

(1 comment)

if you really need this change, it's better to file a new issue at https://github.com/golang/go/issues because this package is sort of package suffering from years of neglect.

also if you want to proceed this cl should contain a test case.


Please don’t reply on this GitHub thread. Visit golang.org/cl/147697. After addressing review feedback, remember to publish your drafts!

gopherbot avatar Nov 06 '18 04:11 gopherbot