@@ -397,7 +397,7 @@ mutable struct SSLStream <: IO
397
397
ssl_context:: SSLContext
398
398
rbio:: BIO
399
399
wbio:: BIO
400
- io:: IO
400
+ io:: TCPSocket
401
401
lock:: ReentrantLock
402
402
@static if VERSION < v " 1.7"
403
403
close_notify_received:: Threads.Atomic{Bool}
407
407
@atomic closed:: Bool
408
408
end
409
409
410
- function SSLStream (ssl_context:: SSLContext , io:: IO )
410
+ function SSLStream (ssl_context:: SSLContext , io:: TCPSocket )
411
411
# Create a read and write BIOs.
412
412
bio_read:: BIO = BIO (io; finalize= false )
413
413
bio_write:: BIO = BIO (io; finalize= false )
422
422
end
423
423
424
424
# backwards compat
425
- SSLStream (ssl_context:: SSLContext , io:: IO , :: IO ) = SSLStream (ssl_context, io)
425
+ SSLStream (ssl_context:: SSLContext , io:: TCPSocket , :: TCPSocket ) = SSLStream (ssl_context, io)
426
426
Base. getproperty (ssl:: SSLStream , nm:: Symbol ) = nm === :bio_read_stream ? ssl : getfield (ssl, nm)
427
427
428
428
SSLStream (tcp:: TCPSocket ) = SSLStream (SSLContext (OpenSSL. TLSClientMethod ()), tcp)
@@ -432,45 +432,44 @@ Base.isopen(ssl::SSLStream)::Bool = !@atomicget(ssl.closed)
432
432
Base. iswritable (ssl:: SSLStream ):: Bool = isopen (ssl) && isopen (ssl. io)
433
433
check_isopen (ssl:: SSLStream , op) = isopen (ssl) || throw (Base. IOError (" $op requires ssl to be open" , 0 ))
434
434
435
- function geterror (f, ssl:: SSLStream )
436
- ret = f ()
437
- if ret <= 0
438
- err = get_error (ssl. ssl, ret)
439
- if err == SSL_ERROR_ZERO_RETURN
440
- @atomicset ssl. close_notify_received = true
441
- elseif err == SSL_ERROR_NONE
442
- # pass
443
- elseif err == SSL_ERROR_WANT_READ
444
- return SSL_ERROR_WANT_READ
445
- elseif err == SSL_ERROR_WANT_WRITE
446
- return SSL_ERROR_WANT_WRITE
447
- else
448
- close (ssl, false )
449
- throw (Base. IOError (OpenSSLError (err). msg, 0 ))
435
+ macro geterror (expr)
436
+ esc (quote
437
+ ret = $ expr
438
+ if ret <= 0
439
+ err = get_error (ssl. ssl, ret)
440
+ if err == SSL_ERROR_ZERO_RETURN
441
+ @atomicset ssl. close_notify_received = true
442
+ elseif err == SSL_ERROR_NONE
443
+ # pass
444
+ elseif err == SSL_ERROR_WANT_READ
445
+ ret = SSL_ERROR_WANT_READ
446
+ elseif err == SSL_ERROR_WANT_WRITE
447
+ ret = SSL_ERROR_WANT_WRITE
448
+ else
449
+ close (ssl, false )
450
+ throw (Base. IOError (OpenSSLError (err). msg, 0 ))
451
+ end
450
452
end
451
- end
452
- return ret
453
+ end )
453
454
end
454
455
455
456
function Base. unsafe_write (ssl:: SSLStream , in_buffer:: Ptr{UInt8} , in_length:: UInt )
456
457
check_isopen (ssl, " unsafe_write" )
457
- return geterror (ssl) do
458
- ccall (
459
- ( :SSL_write , libssl) ,
460
- Cint,
461
- (SSL, Ptr{Cvoid}, Cint) ,
462
- ssl . ssl ,
463
- in_buffer,
464
- in_length )
465
- end
458
+ @ geterror ccall (
459
+ ( :SSL_write , libssl),
460
+ Cint ,
461
+ (SSL, Ptr{Cvoid}, Cint) ,
462
+ ssl . ssl ,
463
+ in_buffer ,
464
+ in_length
465
+ )
466
+ return ret
466
467
end
467
468
468
469
function Sockets. connect (ssl:: SSLStream ; require_ssl_verification:: Bool = true )
469
470
while true
470
471
check_isopen (ssl, " connect" )
471
- ret = geterror (ssl) do
472
- ssl_connect (ssl. ssl)
473
- end
472
+ @geterror ssl_connect (ssl. ssl)
474
473
if (ret == 1 || ret == SSL_ERROR_NONE)
475
474
break
476
475
elseif ret == SSL_ERROR_WANT_READ
@@ -537,17 +536,16 @@ function Base.unsafe_read(ssl::SSLStream, buf::Ptr{UInt8}, nbytes::UInt)
537
536
while nread < nbytes
538
537
(! isopen (ssl) || eof (ssl)) && throw (EOFError ())
539
538
readbytes = Ref {Csize_t} ()
540
- geterror (ssl) do
541
- ccall (
542
- (:SSL_read_ex , libssl),
543
- Cint,
544
- (SSL, Ptr{Int8}, Csize_t, Ptr{Csize_t}),
545
- ssl. ssl,
546
- buf + nread,
547
- nbytes - nread,
548
- readbytes)
549
- end
550
- nread += readbytes[]
539
+ @geterror ccall (
540
+ (:SSL_read_ex , libssl),
541
+ Cint,
542
+ (SSL, Ptr{Int8}, Csize_t, Ptr{Csize_t}),
543
+ ssl. ssl,
544
+ buf + nread,
545
+ nbytes - nread,
546
+ readbytes
547
+ )
548
+ nread += Int (readbytes[])
551
549
end
552
550
return nread
553
551
end
@@ -600,19 +598,18 @@ function Base.eof(ssl::SSLStream)::Bool
600
598
end
601
599
# if we're here, we know there are unprocessed bytes,
602
600
# so we call peek to force processing
603
- err = geterror (ssl) do
604
- ccall (
605
- (:SSL_peek , libssl),
606
- Cint,
607
- (SSL, Ref{UInt8}, Cint),
608
- ssl. ssl,
609
- PEEK_REF,
610
- 1 )
611
- end
601
+ @geterror ccall (
602
+ (:SSL_peek , libssl),
603
+ Cint,
604
+ (SSL, Ref{UInt8}, Cint),
605
+ ssl. ssl,
606
+ PEEK_REF,
607
+ 1
608
+ )
612
609
# if we get WANT_READ back, that means there were pending bytes
613
610
# to be processed, but not a full record, so we need to wait
614
611
# for additional bytes to come in before we can process
615
- err == SSL_ERROR_WANT_READ && eof (ssl. io)
612
+ ret == SSL_ERROR_WANT_READ && eof (ssl. io)
616
613
end
617
614
end
618
615
bytesavailable (ssl) > 0 && return false
0 commit comments