Exemplo n.º 1
0
        private BProtocol detectProtocolFromInputBuffer(ByteBuffer buf)
        {
            BProtocol ret = null;

            // Read the first 4 bytes to detect the protocol.
            int magic = BMessageHeader.detectProtocol(buf);

            if (magic == BMessageHeader.MAGIC_BINARY_STREAM)
            {
                // Version, ByteOrder wird in BInput gelesen
                ret = new BProtocolS(apiDesc);
            }

            if (ret == null)
            {
                throw new BException(BExceptionC.CORRUPT, "Invalid protocol.");
            }

            return(ret);
        }
Exemplo n.º 2
0
        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");
            }
        }