private BProtocol createNegotiatedProtocol(BNegotiate nego) { BProtocol protocol = null; if (nego.protocols.IndexOf(BNegotiate.BINARY_STREAM) == 0) { int negotiatedBypsVersion = Math.Min(BMessageHeader.BYPS_VERSION_CURRENT, nego.bversion); long negotiatedVersion = Math.Min(apiDesc.version, nego.version); nego.protocols = BNegotiate.BINARY_STREAM; if (nego.byteOrder == ByteOrder.UNDEFINED) { nego.byteOrder = ByteOrder.LITTLE_ENDIAN; } if (nego.byteOrder != ByteOrder.LITTLE_ENDIAN) { throw new BException(BExceptionC.CORRUPT, "Protocol requires unsupported byte order BIG_ENDIAN"); } nego.version = negotiatedVersion; protocol = new BProtocolS(apiDesc, negotiatedBypsVersion, negotiatedVersion, nego.byteOrder); } else { throw new BException(BExceptionC.CORRUPT, "Protocol negotiation failed."); } return(protocol); }
public void negotiateProtocolClient(BAsyncResultIF <Boolean> asyncResult) { if (log.isDebugEnabled()) { log.debug("negotiateProtocolClient("); } ByteBuffer buf = ByteBuffer.allocate(BNegotiate.NEGOTIATE_MAX_SIZE); BNegotiate nego = new BNegotiate(apiDesc); nego.write(buf); buf.flip(); BAsyncResultIF <BMessage> outerResult = new MyNegoAsyncResult(this, asyncResult); if (log.isDebugEnabled()) { log.debug("wire.send"); } BMessageHeader header = new BMessageHeader(); header.messageId = wire.makeMessageId(); BMessage msg = new BMessage(header, buf, null); wire.send(msg, outerResult); if (log.isDebugEnabled()) { log.debug(")negotiateProtocolClient"); } }
public BNegotiate(BNegotiate rhs) { protocols = rhs.protocols; bversion = rhs.bversion; version = rhs.version; byteOrder = rhs.byteOrder; targetId = rhs.targetId; sessionId = rhs.sessionId; }
public BNegotiate(BNegotiate rhs) { protocols = rhs.protocols; bversion = rhs.bversion; version = rhs.version; byteOrder = rhs.byteOrder; targetId = rhs.targetId; sessionId = rhs.sessionId; }
public void applyNegotiate(BNegotiate nego) { lock (this) { BNegotiate negoCopy = new BNegotiate(nego); protocol = createNegotiatedProtocol(negoCopy); setTargetId(nego.targetId); setSessionId(nego.sessionId); } }
public void setAsyncResult(ByteBuffer buf, Exception ex) { if (log.isDebugEnabled()) { log.debug("setAsyncResult" + this + "(buf=" + buf + ", ex=" + ex); } if (Interlocked.Increment(ref isOpen) == 1) { try { if (ex == null && buf != null && buf.remaining() != 0) { BMessageHeader header = new BMessageHeader(); bool nego = BNegotiate.isNegotiateMessage(buf); if (nego) { BNegotiate negoResponse = new BNegotiate(); negoResponse.read(buf); header.messageId = messageId; BTransport utransport = wire.getClientUtilityRequests().getTransport(); utransport.applyNegotiate(negoResponse); } else { header.read(buf); } BMessage msg = buf != null ? new BMessage(header, buf, null) : null; if (log.isDebugEnabled()) { log.debug("asyncResult.set"); } asyncResult.setAsyncResult(msg, ex); } else { asyncResult.setAsyncResult(null, ex); } } catch (Exception e) { asyncResult.setAsyncResult(null, e); } } if (log.isDebugEnabled()) { log.debug(")setAsyncResult"); } }
public BProtocol negotiateProtocolServer(BTargetId targetId, ByteBuffer buf, BAsyncResultIF <ByteBuffer> asyncResult) { if (log.isDebugEnabled()) { log.debug("negotiateProtocolServer(targetId=" + targetId); } BProtocol ret = null; try { if (log.isDebugEnabled()) { log.debug("read nego msg"); } BNegotiate nego = new BNegotiate(); nego.read(buf); lock (this) { this.protocol = ret = createNegotiatedProtocol(nego); this.setTargetId(targetId); this.setSessionId(targetId.toSessionId()); } if (log.isDebugEnabled()) { log.debug("protocol=" + this.protocol + ", targetId=" + this.targetId); } ByteBuffer bout = ByteBuffer.allocate(BNegotiate.NEGOTIATE_MAX_SIZE); try { nego.targetId = targetId; nego.sessionId = targetId.toSessionId(); nego.write(bout); bout.flip(); asyncResult.setAsyncResult(bout, null); } finally { } } catch (Exception e) { asyncResult.setAsyncResult(null, e); } if (log.isDebugEnabled()) { log.debug(")negotiateProtocolServer=" + ret); } return(ret); }
public void setAsyncResult(BMessage msg, Exception ex) { if (log.isDebugEnabled()) { log.debug("setAsyncResult(msg=" + msg + ", ex=" + ex); } try { if (ex != null) { innerResult.setAsyncResult(false, ex); } else { if (log.isDebugEnabled()) { log.debug("read message"); } BNegotiate nego = new BNegotiate(); nego.read(msg.buf); transport.applyNegotiate(nego); if (log.isDebugEnabled()) { log.debug("protocol=" + transport.protocol + ", targetId=" + transport.targetId); } transport.internalAuthenticate(innerResult); } } catch (Exception e) { innerResult.setAsyncResult(false, e); } if (log.isDebugEnabled()) { log.debug(")setAsyncResult"); } }
public void internalSend(RequestToCancel request) { if (log.isDebugEnabled()) { log.debug("internalSend(" + request); } ByteBuffer requestDataBuffer = request.buf; bool isNegotiate = BNegotiate.isNegotiateMessage(requestDataBuffer); bool isJson = isNegotiate || BMessageHeader.detectProtocol(requestDataBuffer) == BMessageHeader.MAGIC_JSON; String destUrl = url; // Negotiate? if (isNegotiate) { // Send a GET request and pass the negotate string as parameter String negoStr = Encoding.UTF8.GetString(requestDataBuffer.array(), requestDataBuffer.position(), requestDataBuffer.remaining()); negoStr = System.Uri.EscapeDataString(negoStr); destUrl = makeUrl(getServletPathForNegotiationAndAuthentication(), new String[] { "negotiate", negoStr }); } // Reverse request (long-poll) ? else if (request.requestDirection == ERequestDirection.REVERSE) { destUrl = makeUrl(getServletPathForReverseRequest(), null); } HttpWebRequest conn = (HttpWebRequest)HttpWebRequest.Create(destUrl); request.setConnection(conn); conn.Method = isNegotiate ? "GET" : "POST"; conn.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; // Content-Type for POST request if (!isNegotiate) { conn.ContentType = isJson ? "application/json" : "application/byps"; } conn.Accept = "application/json, application/byps, text/plain, text/html"; // BYPS-36: Accept GZIP for both, json and byps conn.Headers.Add("Accept-Encoding", "gzip"); applySession(conn); IAsyncResult asyncResult = null; if (isNegotiate) { asyncResult = conn.BeginGetResponse(new AsyncCallback(this.getResponseCallback), request); } else { asyncResult = conn.BeginGetRequestStream(new AsyncCallback(this.getRequestStreamCallback), request); } request.startTimeoutWatcher(conn, asyncResult, request.timeoutMillisRequest); if (log.isDebugEnabled()) { log.debug(")internalSend"); } }
public void negotiateProtocolClient(BAsyncResultIF <Boolean> asyncResult) { if (log.isDebugEnabled()) { log.debug("negotiateProtocolClient("); } if (log.isDebugEnabled()) { log.debug("negotiateActive=" + negotiateActive); } // Check that we do not run into recursive authentication requests. lock (asyncResultsWaitingForAuthentication) { // Already have an active negotiation request? if (negotiateActive) { // Are there threads waiting? if (asyncResultsWaitingForAuthentication.Count != 0) { // Most likely slow or recursive authentication BException ex = new BException(BExceptionC.FORBIDDEN, "Authentication procedure failed. Server returned 401 for every request. " + "A common reason for this error is slow authentication handling."); // ... or calling a function that requires authentication in BAuthentication.authenticate() - see. TestRemoteWithAuthentication.testAuthenticateBlocksRecursion asyncResult.setAsyncResult(false, ex); return; } else { // Correction: if no threads are waiting then there cannot be an aktive negotiation request. negotiateActive = true; } } else { // Now, this is the active negotiation request. negotiateActive = true; } } ByteBuffer buf = ByteBuffer.allocate(BNegotiate.NEGOTIATE_MAX_SIZE); BNegotiate nego = new BNegotiate(apiDesc); nego.write(buf); buf.flip(); BAsyncResultIF <BMessage> outerResult = new MyNegoAsyncResult(this, asyncResult); if (log.isDebugEnabled()) { log.debug("wire.send"); } BMessageHeader header = new BMessageHeader(); header.messageId = wire.makeMessageId(); BMessage msg = new BMessage(header, buf, null); wire.send(msg, outerResult); if (log.isDebugEnabled()) { log.debug(")negotiateProtocolClient"); } }