src/java.net.http/share/classes/jdk/internal/net/http/common/SSLFlowDelegate.java
branchhttp-client-branch
changeset 56223 377c5ebfc319
parent 56208 d37c08ce784a
child 56234 fc391230cf7b
equal deleted inserted replaced
56218:fd7bd32963ef 56223:377c5ebfc319
   377             } catch (Throwable ex) {
   377             } catch (Throwable ex) {
   378                 errorCommon(ex);
   378                 errorCommon(ex);
   379                 handleError(ex);
   379                 handleError(ex);
   380             }
   380             }
   381         }
   381         }
       
   382 
       
   383         EngineResult unwrapBuffer(ByteBuffer src) throws IOException {
       
   384             ByteBuffer dst = getAppBuffer();
       
   385             while (true) {
       
   386                 SSLEngineResult sslResult = engine.unwrap(src, dst);
       
   387                 switch (sslResult.getStatus()) {
       
   388                     case BUFFER_OVERFLOW:
       
   389                         // may happen only if app size buffer was changed.
       
   390                         // get it again if app buffer size changed
       
   391                         int appSize = engine.getSession().getApplicationBufferSize();
       
   392                         ByteBuffer b = ByteBuffer.allocate(appSize + dst.position());
       
   393                         dst.flip();
       
   394                         b.put(dst);
       
   395                         dst = b;
       
   396                         break;
       
   397                     case CLOSED:
       
   398                         return doClosure(new EngineResult(sslResult));
       
   399                     case BUFFER_UNDERFLOW:
       
   400                         // handled implicitly by compaction/reallocation of readBuf
       
   401                         return new EngineResult(sslResult);
       
   402                     case OK:
       
   403                         dst.flip();
       
   404                         return new EngineResult(sslResult, dst);
       
   405                 }
       
   406             }
       
   407         }
   382     }
   408     }
   383 
   409 
   384     public interface Monitorable {
   410     public interface Monitorable {
   385         public String getInfo();
   411         public String getInfo();
   386     }
   412     }
   581                     writer.addData(HS_TRIGGER);
   607                     writer.addData(HS_TRIGGER);
   582                 }
   608                 }
   583             } catch (Throwable ex) {
   609             } catch (Throwable ex) {
   584                 errorCommon(ex);
   610                 errorCommon(ex);
   585                 handleError(ex);
   611                 handleError(ex);
       
   612             }
       
   613         }
       
   614 
       
   615         @SuppressWarnings("fallthrough")
       
   616         EngineResult wrapBuffers(ByteBuffer[] src) throws SSLException {
       
   617             debugw.log(Level.DEBUG, () -> "wrapping " + Utils.remaining(src) + " bytes");
       
   618             ByteBuffer dst = getNetBuffer();
       
   619             while (true) {
       
   620                 SSLEngineResult sslResult = engine.wrap(src, dst);
       
   621                 debugw.log(Level.DEBUG, () -> "SSLResult: " + sslResult);
       
   622                 switch (sslResult.getStatus()) {
       
   623                     case BUFFER_OVERFLOW:
       
   624                         // Shouldn't happen. We allocated buffer with packet size
       
   625                         // get it again if net buffer size was changed
       
   626                         debugw.log(Level.DEBUG, "BUFFER_OVERFLOW");
       
   627                         int appSize = engine.getSession().getApplicationBufferSize();
       
   628                         ByteBuffer b = ByteBuffer.allocate(appSize + dst.position());
       
   629                         dst.flip();
       
   630                         b.put(dst);
       
   631                         dst = b;
       
   632                         break; // try again
       
   633                     case CLOSED:
       
   634                         debugw.log(Level.DEBUG, "CLOSED");
       
   635                         // fallthrough. There could be some remaining data in dst.
       
   636                         // CLOSED will be handled by the caller.
       
   637                     case OK:
       
   638                         dst.flip();
       
   639                         final ByteBuffer dest = dst;
       
   640                         debugw.log(Level.DEBUG, () -> "OK => produced: "
       
   641                                 + dest.remaining()
       
   642                                 + " not wrapped: "
       
   643                                 + Utils.remaining(src));
       
   644                         return new EngineResult(sslResult, dest);
       
   645                     case BUFFER_UNDERFLOW:
       
   646                         // Shouldn't happen.  Doesn't returns when wrap()
       
   647                         // underflow handled externally
       
   648                         // assert false : "Buffer Underflow";
       
   649                         debug.log(Level.DEBUG, "BUFFER_UNDERFLOW");
       
   650                         return new EngineResult(sslResult);
       
   651                     default:
       
   652                         debugw.log(Level.DEBUG, "ASSERT");
       
   653                         assert false;
       
   654                 }
   586             }
   655             }
   587         }
   656         }
   588 
   657 
   589         private boolean needWrap() {
   658         private boolean needWrap() {
   590             return engine.getHandshakeStatus() == HandshakeStatus.NEED_WRAP;
   659             return engine.getHandshakeStatus() == HandshakeStatus.NEED_WRAP;
   740                 handleError(t);
   809                 handleError(t);
   741             }
   810             }
   742         });
   811         });
   743     }
   812     }
   744 
   813 
   745 
       
   746     EngineResult unwrapBuffer(ByteBuffer src) throws IOException {
       
   747         ByteBuffer dst = getAppBuffer();
       
   748         while (true) {
       
   749             SSLEngineResult sslResult = engine.unwrap(src, dst);
       
   750             switch (sslResult.getStatus()) {
       
   751                 case BUFFER_OVERFLOW:
       
   752                     // may happen only if app size buffer was changed.
       
   753                     // get it again if app buffer size changed
       
   754                     int appSize = engine.getSession().getApplicationBufferSize();
       
   755                     ByteBuffer b = ByteBuffer.allocate(appSize + dst.position());
       
   756                     dst.flip();
       
   757                     b.put(dst);
       
   758                     dst = b;
       
   759                     break;
       
   760                 case CLOSED:
       
   761                     return doClosure(new EngineResult(sslResult));
       
   762                 case BUFFER_UNDERFLOW:
       
   763                     // handled implicitly by compaction/reallocation of readBuf
       
   764                     return new EngineResult(sslResult);
       
   765                 case OK:
       
   766                      dst.flip();
       
   767                      return new EngineResult(sslResult, dst);
       
   768             }
       
   769         }
       
   770     }
       
   771 
       
   772     // FIXME: acknowledge a received CLOSE request from peer
   814     // FIXME: acknowledge a received CLOSE request from peer
   773     EngineResult doClosure(EngineResult r) throws IOException {
   815     EngineResult doClosure(EngineResult r) throws IOException {
   774         debug.log(Level.DEBUG,
   816         debug.log(Level.DEBUG,
   775                 "doClosure(%s): %s [isOutboundDone: %s, isInboundDone: %s]",
   817                 "doClosure(%s): %s [isOutboundDone: %s, isInboundDone: %s]",
   776                 r.result, engine.getHandshakeStatus(),
   818                 r.result, engine.getHandshakeStatus(),
   871     }
   913     }
   872 
   914 
   873     final String dbgString() {
   915     final String dbgString() {
   874         return "SSLFlowDelegate(" + tubeName + ")";
   916         return "SSLFlowDelegate(" + tubeName + ")";
   875     }
   917     }
   876 
       
   877     @SuppressWarnings("fallthrough")
       
   878     EngineResult wrapBuffers(ByteBuffer[] src) throws SSLException {
       
   879         debug.log(Level.DEBUG, () -> "wrapping "
       
   880                     + Utils.remaining(src) + " bytes");
       
   881         ByteBuffer dst = getNetBuffer();
       
   882         while (true) {
       
   883             SSLEngineResult sslResult = engine.wrap(src, dst);
       
   884             debug.log(Level.DEBUG, () -> "SSLResult: " + sslResult);
       
   885             switch (sslResult.getStatus()) {
       
   886                 case BUFFER_OVERFLOW:
       
   887                     // Shouldn't happen. We allocated buffer with packet size
       
   888                     // get it again if net buffer size was changed
       
   889                     debug.log(Level.DEBUG, "BUFFER_OVERFLOW");
       
   890                     int appSize = engine.getSession().getApplicationBufferSize();
       
   891                     ByteBuffer b = ByteBuffer.allocate(appSize + dst.position());
       
   892                     dst.flip();
       
   893                     b.put(dst);
       
   894                     dst = b;
       
   895                     break; // try again
       
   896                 case CLOSED:
       
   897                     debug.log(Level.DEBUG, "CLOSED");
       
   898                     // fallthrough. There could be some remaining data in dst.
       
   899                     // CLOSED will be handled by the caller.
       
   900                 case OK:
       
   901                     dst.flip();
       
   902                     final ByteBuffer dest = dst;
       
   903                     debug.log(Level.DEBUG, () -> "OK => produced: "
       
   904                                            + dest.remaining()
       
   905                                            + " not wrapped: "
       
   906                                            + Utils.remaining(src));
       
   907                     return new EngineResult(sslResult, dest);
       
   908                 case BUFFER_UNDERFLOW:
       
   909                     // Shouldn't happen.  Doesn't returns when wrap()
       
   910                     // underflow handled externally
       
   911                     // assert false : "Buffer Underflow";
       
   912                     debug.log(Level.DEBUG, "BUFFER_UNDERFLOW");
       
   913                     return new EngineResult(sslResult);
       
   914                 default:
       
   915                     debug.log(Level.DEBUG, "ASSERT");
       
   916                     assert false;
       
   917             }
       
   918         }
       
   919     }
       
   920 }
   918 }