Subject: | peek() support for 2-item list context returns |
Hi,
as of Net::SSLeay v1.58 peek() returns with data or undef on error only.
For better non-blocking support, peek() should returns underlaying error code, if
requested by 2-item list context.
So, lets handle peek() returns logic like read() it does.
# returns:
# in scalar context: data read from the TLS/SSL connection, undef on error
# in list context: two-item array consisting of data read (undef on error),
# and return code from SSL_peek().
--- a/SSLeay.xs 2014-05-01 00:00:00.000000000 +0000
+++ b/SSLeay.xs 2014-05-01 00:00:00.000000000 +0000
@@ -1411,11 +1411,25 @@
PREINIT:
char *buf;
int got;
- CODE:
+ PPCODE:
New(0, buf, max, char);
- ST(0) = sv_newmortal(); /* Undefined to start with */
- if ((got = SSL_peek(s, buf, max)) >= 0)
- sv_setpvn( ST(0), buf, got);
+ got = SSL_peek(s, buf, max);
+
+ /* If in list context, return 2-item list:
+ * first return value: data gotten, or undef on error (got<0)
+ * second return value: result from SSL_peek()
+ */
+ if (GIMME_V==G_ARRAY) {
+ EXTEND(SP, 2);
+ PUSHs(sv_2mortal(got>=0 ? newSVpvn(buf, got) : newSV(0)));
+ PUSHs(sv_2mortal(newSViv(got)));
+
+ /* If in scalar or void context, return data gotten, or undef on error. */
+ } else {
+ EXTEND(SP, 1);
+ PUSHs(sv_2mortal(got>=0 ? newSVpvn(buf, got) : newSV(0)));
+ }
+
Safefree(buf);