forked from roadrunner-server/goridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
windows: do not take errors into account when CRC failed
Signed-off-by: Valery Piashchynski <[email protected]>
- Loading branch information
Showing
2 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// go:build windows | ||
|
||
package internal | ||
|
||
import ( | ||
"bytes" | ||
stderr "errors" | ||
"io" | ||
"time" | ||
|
||
"github.com/roadrunner-server/errors" | ||
"github.com/roadrunner-server/goridge/v3/pkg/frame" | ||
) | ||
|
||
// shortland for the Could not open input file: ../roadrunner/tests/psr-wfsdorker.php | ||
var res = []byte("Could not op") //nolint:gochecknoglobals | ||
|
||
func ReceiveFrame(relay io.Reader, fr *frame.Frame) error { | ||
const op = errors.Op("goridge_frame_receive") | ||
|
||
_, err := io.ReadFull(relay, fr.Header()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if bytes.Equal(fr.Header(), res) { | ||
data, errRa := io.ReadAll(relay) | ||
if errRa == nil && len(data) > 0 { | ||
return errors.E(op, errors.FileNotFound, errors.Str(string(fr.Header())+string(data))) | ||
} | ||
|
||
return errors.E(op, errors.FileNotFound, errors.Str("file not found")) | ||
} | ||
|
||
// we have options | ||
if fr.ReadHL(fr.Header()) > 3 { | ||
// we should read the options | ||
optsLen := (fr.ReadHL(fr.Header()) - 3) * frame.WORD | ||
opts := make([]byte, optsLen) | ||
|
||
// read next part of the frame - options | ||
_, err = io.ReadFull(relay, opts) | ||
if err != nil { | ||
if stderr.Is(err, io.EOF) { | ||
return err | ||
} | ||
return errors.E(op, err) | ||
} | ||
|
||
// we should append frame's | ||
fr.AppendOptions(fr.HeaderPtr(), opts) | ||
} | ||
|
||
// verify header CRC | ||
if !fr.VerifyCRC(fr.Header()) { | ||
type deadliner interface { | ||
SetReadDeadline(time.Time) error | ||
} | ||
|
||
if d, ok := relay.(deadliner); ok { | ||
// we don't care about errors here, because the pipe can be either closed, and then we read all data w/o deadline or deadline will be successfully set. | ||
_ = d.SetReadDeadline(time.Now().Add(time.Second * 2)) | ||
resp, _ := io.ReadAll(relay) | ||
|
||
return errors.E(op, errors.Errorf("CRC verification failed: %s", string(fr.Header())+string(resp))) | ||
} | ||
|
||
// no deadline, so, only 14 bytes | ||
return errors.E(op, errors.Errorf("CRC verification failed: %s", fr.Header())) | ||
} | ||
|
||
// read the read payload | ||
pl := fr.ReadPayloadLen(fr.Header()) | ||
// no payload | ||
if pl == 0 { | ||
return nil | ||
} | ||
|
||
pb := get(pl) | ||
_, err2 := io.ReadFull(relay, (*pb)[:pl]) | ||
if err2 != nil { | ||
if stderr.Is(err2, io.EOF) { | ||
put(pl, pb) | ||
return err | ||
} | ||
put(pl, pb) | ||
return errors.E(op, err2) | ||
} | ||
|
||
fr.WritePayload((*pb)[:pl]) | ||
put(pl, pb) | ||
return nil | ||
} |