protected internal override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if (request == null) { throw new ArgumentNullException("request"); } this.CheckDisposed(); this.SetOperationStarted(); var tcs = new TaskCompletionSource <HttpResponseMessage>(); var requestState = new HttpRequestState { Tcs = tcs, CancellationToken = cancellationToken, RequestMessage = request }; this._lastRequestUri = request.RequestUri; try { var httpWebRequest = this.CreateAndPrepareWebRequest(request); requestState.WebRequest = httpWebRequest; cancellationToken.Register(_onCancel, httpWebRequest); Task.Factory.StartNew(this._startRequest, requestState); } catch (Exception ex) { this.HandleAsyncException(requestState, ex); } return(tcs.Task); }
private void HandleAsyncException(HttpRequestState state, Exception e) { HttpResponseMessage result; if (this.TryGetExceptionResponse(e as WebException, state.RequestMessage, out result)) { state.Tcs.TrySetResult(result); return; } if (state.CancellationToken.IsCancellationRequested) { state.Tcs.TrySetCanceled(); return; } state.Tcs.TrySetException(e); }
public ParserState ParseBuffer( byte[] buffer, int bytesReady, ref int bytesConsumed) { if (buffer == null) { throw Error.ArgumentNull("buffer"); } ParserState parseStatus = ParserState.NeedMoreData; ParserState subParseStatus = ParserState.NeedMoreData; switch (this._requestStatus) { case HttpRequestState.RequestLine: try { subParseStatus = this._requestLineParser.ParseBuffer(buffer, bytesReady, ref bytesConsumed); } catch (Exception) { subParseStatus = ParserState.Invalid; } if (subParseStatus == ParserState.Done) { this._requestStatus = HttpRequestState.RequestHeaders; subParseStatus = ParserState.NeedMoreData; goto case HttpRequestState.RequestHeaders; } else if (subParseStatus != ParserState.NeedMoreData) { // Report error - either Invalid or DataTooBig parseStatus = subParseStatus; break; } break; // read more data case HttpRequestState.RequestHeaders: if (bytesConsumed >= bytesReady) { // we already can tell we need more data break; } try { subParseStatus = this._headerParser.ParseBuffer(buffer, bytesReady, ref bytesConsumed); } catch (Exception) { subParseStatus = ParserState.Invalid; } if (subParseStatus == ParserState.Done) { parseStatus = subParseStatus; } else if (subParseStatus != ParserState.NeedMoreData) { parseStatus = subParseStatus; break; } break; // need more data } return parseStatus; }
private void StartGettingRequestStream(HttpRequestState state) { state.WebRequest.BeginGetRequestStream(this._getRequestStreamCallback, state); }
private void StartGettingResponse(HttpRequestState state) { state.WebRequest.BeginGetResponse(this._getResponseCallback, state); }
private void PrepareAndStartContentUpload(HttpRequestState state) { this.StartGettingRequestStream(state); }
protected internal override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if (request == null) { throw new ArgumentNullException("request"); } this.CheckDisposed(); this.SetOperationStarted(); var tcs = new TaskCompletionSource<HttpResponseMessage>(); var requestState = new HttpRequestState { Tcs = tcs, CancellationToken = cancellationToken, RequestMessage = request }; this._lastRequestUri = request.RequestUri; try { var httpWebRequest = this.CreateAndPrepareWebRequest(request); requestState.WebRequest = httpWebRequest; cancellationToken.Register(_onCancel, httpWebRequest); Task.Factory.StartNew(this._startRequest, requestState); } catch (Exception ex) { this.HandleAsyncException(requestState, ex); } return tcs.Task; }