// Note: This method will return null in the case where throwOnError is false, and a non-fatal error occurs. // Please exercice caution when passing in throwOnError = false. This should basically only be done in error // code paths, or code paths where there is very good reason that you would not want this method to throw. // When passing in throwOnError = false, please handle the case where this method returns null. public HttpInput GetHttpInput(bool throwOnError) { HttpPipeline pipeline = this.httpPipeline; if ((pipeline != null) && pipeline.IsHttpInputInitialized) { return(pipeline.HttpInput); } HttpInput httpInput = null; if (throwOnError || !this.errorGettingHttpInput) { try { httpInput = GetHttpInput(); this.errorGettingHttpInput = false; } catch (Exception e) { this.errorGettingHttpInput = true; if (throwOnError || Fx.IsFatal(e)) { throw; } DiagnosticUtility.TraceHandledException(e, TraceEventType.Warning); } } return(httpInput); }
bool PrepareReply(ref Message message) { bool closeOnReceivedEof = false; // null means we're done if (message == null) { // A null message means either a one-way request or that the service operation returned null and // hence we can close the HttpOutput. By default we keep the HttpOutput open to allow the writing to the output // even after the HttpInput EOF is received and the HttpOutput will be closed only on close of the HttpRequestContext. closeOnReceivedEof = true; message = CreateAckMessage(HttpStatusCode.Accepted, string.Empty); } if (!listener.ManualAddressing) { if (message.Version.Addressing == AddressingVersion.WSAddressingAugust2004) { if (message.Headers.To == null || listener.AnonymousUriPrefixMatcher == null || !listener.AnonymousUriPrefixMatcher.IsAnonymousUri(message.Headers.To)) { message.Headers.To = message.Version.Addressing.AnonymousUri; } } else if (message.Version.Addressing == AddressingVersion.WSAddressing10 || message.Version.Addressing == AddressingVersion.None) { if (message.Headers.To != null && (listener.AnonymousUriPrefixMatcher == null || !listener.AnonymousUriPrefixMatcher.IsAnonymousUri(message.Headers.To))) { message.Headers.To = null; } } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new ProtocolException(SR.GetString(SR.AddressingVersionNotSupported, message.Version.Addressing))); } } message.Properties.AllowOutputBatching = false; this.httpOutput = GetHttpOutputCore(message); // Reuse the HttpInput we got previously. HttpInput input = this.httpPipeline.HttpInput; if (input != null) { HttpDelayedAcceptStream requestStream = input.GetInputStream(false) as HttpDelayedAcceptStream; if (requestStream != null && TransferModeHelper.IsRequestStreamed(listener.TransferMode) && requestStream.EnableDelayedAccept(this.httpOutput, closeOnReceivedEof)) { return(false); } } return(true); }
public ParseMessageAsyncResult(HttpInput httpInput, AsyncCallback callback, object state) : base(callback, state) { this.httpInput = httpInput; httpInput.ValidateContentType(); this.inputStream = httpInput.InputStream; if (!httpInput.HasContent) { if (httpInput.messageEncoder.MessageVersion != MessageVersion.None) { base.Complete(true); return; } this.message = new NullMessage(); } else if (httpInput.streamed || (httpInput.ContentLength == -1L)) { if (httpInput.streamed) { this.message = httpInput.ReadStreamedMessage(this.inputStream); } else { this.message = httpInput.ReadChunkedBufferedMessage(this.inputStream); } } if (this.message != null) { this.requestException = httpInput.ProcessHttpAddressing(this.message); base.Complete(true); } else { this.buffer = httpInput.GetMessageBuffer(); this.count = this.buffer.Count; this.offset = 0; IAsyncResult asyncResult = this.inputStream.BeginRead(this.buffer.Array, this.offset, this.count, onRead, this); if (asyncResult.CompletedSynchronously && this.ContinueReading(this.inputStream.EndRead(asyncResult))) { base.Complete(true); } } }
public static HttpInput ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory factory, WebException responseException, ChannelBinding channelBinding) { ValidateAuthentication(request, response, responseException, factory); HttpInput input = null; if (((HttpStatusCode.OK > response.StatusCode) || (response.StatusCode >= HttpStatusCode.MultipleChoices)) && (response.StatusCode != HttpStatusCode.InternalServerError)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateUnexpectedResponseException(responseException, response)); } if ((response.StatusCode == HttpStatusCode.InternalServerError) && (string.Compare(response.StatusDescription, "System.ServiceModel.ServiceActivationException", StringComparison.OrdinalIgnoreCase) == 0)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ServiceActivationException(System.ServiceModel.SR.GetString("Hosting_ServiceActivationFailed", new object[] { request.RequestUri }))); } if (string.IsNullOrEmpty(response.ContentType)) { if (!ValidateEmptyContent(response)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(TraceResponseException(new ProtocolException(System.ServiceModel.SR.GetString("HttpContentTypeHeaderRequired"), responseException))); } } else if (response.ContentLength != 0L) { MessageEncoder encoder = factory.MessageEncoderFactory.Encoder; if (!encoder.IsContentTypeSupported(response.ContentType)) { int num; string responseStreamString = GetResponseStreamString(response, out num); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(TraceResponseException(new ProtocolException(System.ServiceModel.SR.GetString("ResponseContentTypeMismatch", new object[] { response.ContentType, encoder.ContentType, num, responseStreamString }), responseException))); } input = HttpInput.CreateHttpInput(response, factory, channelBinding); input.WebException = responseException; } if ((input == null) && (factory.MessageEncoderFactory.MessageVersion == MessageVersion.None)) { input = HttpInput.CreateHttpInput(response, factory, channelBinding); input.WebException = responseException; } return(input); }
public ParseMessageAsyncResult( HttpRequestMessage httpRequestMessage, HttpInput httpInput, AsyncCallback callback, object state) : base(callback, state) { this.httpInput = httpInput; this.httpRequestMessage = httpRequestMessage; this.BeginParse(); }
protected override HttpInput GetHttpInput() { HttpInput httpInput = base.GetHttpInput(); return(httpInput.CreateHttpRequestMessageInput()); }
public async Task <Message> ReceiveReplyAsync(TimeoutHelper timeoutHelper) { _timeoutHelper = timeoutHelper; HttpResponseMessage httpResponse = null; HttpRequestException responseException = null; try { httpResponse = await _httpResponseMessageTask; } catch (HttpRequestException requestException) { responseException = requestException; httpResponse = HttpChannelUtilities.ProcessGetResponseWebException(responseException, _httpRequestMessage, _abortReason); } catch (OperationCanceledException) { if (_timeoutHelper.CancellationToken.IsCancellationRequested) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(SR.Format( SR.HttpRequestTimedOut, _httpRequestMessage.RequestUri, _timeoutHelper.OriginalTimeout))); } else { // Cancellation came from somewhere other than timeoutCts and needs to be handled differently. throw; } } try { HttpInput httpInput = HttpChannelUtilities.ValidateRequestReplyResponse(_httpRequestMessage, httpResponse, _factory, responseException); Message replyMessage = null; if (httpInput != null) { var outException = new OutWrapper <Exception>(); replyMessage = await httpInput.ParseIncomingMessageAsync(outException); Exception exception = outException; Contract.Assert(exception == null, "ParseIncomingMessage should not set an exception after parsing a response message."); } this.TryCompleteHttpRequest(_httpRequestMessage); return(replyMessage); } catch (OperationCanceledException) { if (_timeoutHelper.CancellationToken.IsCancellationRequested) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(SR.Format( SR.HttpResponseTimedOut, _httpRequestMessage.RequestUri, timeoutHelper.OriginalTimeout))); } else { // Cancellation came from somewhere other than timeoutCts and needs to be handled differently. throw; } } }