예제 #1
0
        internal HttpClientStream(HttpClientConnection connection, HttpRequest request, HttpResponseStreamHandler responseHandler)
            : base(connection)
        {
            responseHandler.Validate();

            this.request           = request;
            this.requestBodyStream = new CrtStreamWrapper(request.BodyStream);

            this.responseHandler = responseHandler;

            // Wrap the native callbacks to bind this stream to them as the first argument
            onIncomingHeaders = (responseCode, block, headers, headerCount) =>
            {
                if (ResponseStatusCode == 0)
                {
                    ResponseStatusCode = responseCode;
                }
                responseHandler.OnIncomingHeaders(this, block, headers);
            };

            onIncomingHeaderBlockDone = (block) =>
            {
                responseHandler.OnIncomingHeadersDone(this, block);
            };

            onIncomingBody = (byte[] data, ulong size) =>
            {
                responseHandler.OnIncomingBody(this, data);
            };

            onStreamComplete = (errorCode) =>
            {
                responseHandler.OnStreamComplete(this, errorCode);
            };

            NativeHandle = API.make_new(
                connection.NativeHandle.DangerousGetHandle(),
                request.Method,
                request.Uri,
                request.Headers,
                (UInt32)(request.Headers?.Length ?? 0),
                requestBodyStream.Delegates,
                onIncomingHeaders,
                onIncomingHeaderBlockDone,
                onIncomingBody,
                onStreamComplete);
        }
예제 #2
0
        public Task <StreamResult> MakeRequest(HttpRequest request, HttpResponseStreamHandler responseHandler)
        {
            var bootstrap = new StreamBootstrap();

            responseHandler.StreamComplete += (sender, e) => {
                streams.Remove(bootstrap.Stream);
                if (e.ErrorCode != 0)
                {
                    var message = CRT.ErrorString(e.ErrorCode);
                    bootstrap.TaskSource.SetException(new WebException($"Stream {bootstrap.Stream} failed: {message}"));
                }
                else
                {
                    bootstrap.TaskSource.SetResult(new StreamResult(e.ErrorCode));
                }
            };

            bootstrap.Stream = new HttpClientStream(this, request, responseHandler);
            streams.Add(bootstrap.Stream);
            bootstrap.Stream.Activate();

            return(bootstrap.TaskSource.Task);
        }