// NOTE: Currently this code path is not enabled. // See FillRequestStream in WebRequestPSCmdlet.CoreClr.cs and // GetResponseObject in WebResponseObjectFactory.CoreClr.cs for details. private void InitializeContent() { string contentType = ContentHelper.GetContentType(BaseResponse); string content = null; if (ContentHelper.IsText(contentType)) { Encoding encoding = null; // fill the Content buffer string characterSet = WebResponseHelper.GetCharacterSet(BaseResponse); this.Content = StreamHelper.DecodeStream(RawContentStream, characterSet, out encoding); this.Encoding = encoding; } else { this.Content = string.Empty; } }
internal override void ProcessResponse(WebResponse response) { if (response == null) { throw new ArgumentNullException("response"); } MemoryStream stream = new MemoryStream(); using (BufferingStreamReader reader = new BufferingStreamReader(StreamHelper.GetResponseStream(response))) { if (base.ShouldWriteToPipeline && !this.TryProcessFeedStream(reader)) { RestReturnType type = this.CheckReturnType(response); stream = StreamHelper.ReadStream(reader, response.ContentLength, this); Encoding encoding = ContentHelper.GetEncoding(response); object obj2 = null; Exception exRef = null; string json = StreamHelper.DecodeStream(stream, encoding); bool flag = false; if (type == RestReturnType.Json) { flag = this.TryConvertToJson(json, out obj2, ref exRef) || this.TryConvertToXml(json, out obj2, ref exRef); } else { flag = this.TryConvertToXml(json, out obj2, ref exRef) || this.TryConvertToJson(json, out obj2, ref exRef); } if (!flag) { obj2 = json; } base.WriteObject(obj2); } if (base.ShouldSaveToOutFile) { StreamHelper.SaveStreamToFile(StreamHelper.ReadStream(reader, response.ContentLength, this), base.QualifiedOutFile, this); } } }
/// <summary> /// Reads the response content from the web response. /// </summary> protected void InitializeContent() { string contentType = ContentHelper.GetContentType(BaseResponse); if (ContentHelper.IsText(contentType)) { Encoding encoding = null; // fill the Content buffer string characterSet = WebResponseHelper.GetCharacterSet(BaseResponse); if (string.IsNullOrEmpty(characterSet) && ContentHelper.IsJson(contentType)) { characterSet = Encoding.UTF8.HeaderName; } this.Content = StreamHelper.DecodeStream(RawContentStream, characterSet, out encoding); this.Encoding = encoding; } else { this.Content = string.Empty; } }
private RestReturnType CheckReturnType(WebResponse response) { if (response == null) { throw new ArgumentNullException("response"); } RestReturnType detect = RestReturnType.Detect; string contentType = ContentHelper.GetContentType(response); if (string.IsNullOrEmpty(contentType)) { return(RestReturnType.Detect); } if (ContentHelper.IsJson(contentType)) { return(RestReturnType.Json); } if (ContentHelper.IsXml(contentType)) { detect = RestReturnType.Xml; } return(detect); }
internal static string DecodeStream(Stream stream, ref Encoding encoding) { bool isDefaultEncoding = false; if (encoding == null) { // Use the default encoding if one wasn't provided encoding = ContentHelper.GetDefaultEncoding(); isDefaultEncoding = true; } string content = StreamToString(stream, encoding); if (isDefaultEncoding) { do { // check for a charset attribute on the meta element to override the default. Match match = s_metaexp.Match(content); if (match.Success) { Encoding localEncoding = null; string characterSet = match.Groups["charset"].Value; if (TryGetEncoding(characterSet, out localEncoding)) { stream.Seek(0, SeekOrigin.Begin); content = StreamToString(stream, localEncoding); // report the encoding used. encoding = localEncoding; } } } while (false); } return(content); }
/// <summary> /// the main execution method for cmdlets derived from WebRequestPSCmdlet. /// </summary> protected override void ProcessRecord() { try { // Set cmdlet context for write progress ValidateParameters(); PrepareSession(); // if the request contains an authorization header and PreserveAuthorizationOnRedirect is not set, // it needs to be stripped on the first redirect. bool stripAuthorization = null != WebSession && null != WebSession.Headers && !PreserveAuthorizationOnRedirect.IsPresent && WebSession.Headers.ContainsKey(HttpKnownHeaderNames.Authorization.ToString()); using (HttpClient client = GetHttpClient(stripAuthorization)) { int followedRelLink = 0; Uri uri = Uri; do { if (followedRelLink > 0) { string linkVerboseMsg = string.Format(CultureInfo.CurrentCulture, WebCmdletStrings.FollowingRelLinkVerboseMsg, uri.AbsoluteUri); WriteVerbose(linkVerboseMsg); } using (HttpRequestMessage request = GetRequest(uri, stripAuthorization: false)) { FillRequestStream(request); try { long requestContentLength = 0; if (request.Content != null) { requestContentLength = request.Content.Headers.ContentLength.Value; } string reqVerboseMsg = String.Format(CultureInfo.CurrentCulture, WebCmdletStrings.WebMethodInvocationVerboseMsg, request.Method, request.RequestUri, requestContentLength); WriteVerbose(reqVerboseMsg); HttpResponseMessage response = GetResponse(client, request, stripAuthorization); string contentType = ContentHelper.GetContentType(response); string respVerboseMsg = string.Format(CultureInfo.CurrentCulture, WebCmdletStrings.WebResponseVerboseMsg, response.Content.Headers.ContentLength, contentType); WriteVerbose(respVerboseMsg); if (!response.IsSuccessStatusCode) { string message = String.Format(CultureInfo.CurrentCulture, WebCmdletStrings.ResponseStatusCodeFailure, (int)response.StatusCode, response.ReasonPhrase); HttpResponseException httpEx = new HttpResponseException(message, response); ErrorRecord er = new ErrorRecord(httpEx, "WebCmdletWebResponseException", ErrorCategory.InvalidOperation, request); string detailMsg = ""; StreamReader reader = null; try { reader = new StreamReader(StreamHelper.GetResponseStream(response)); // remove HTML tags making it easier to read detailMsg = System.Text.RegularExpressions.Regex.Replace(reader.ReadToEnd(), "<[^>]*>", ""); } catch (Exception) { // catch all } finally { if (reader != null) { reader.Dispose(); } } if (!String.IsNullOrEmpty(detailMsg)) { er.ErrorDetails = new ErrorDetails(detailMsg); } ThrowTerminatingError(er); } if (_parseRelLink || _followRelLink) { ParseLinkHeader(response, uri); } ProcessResponse(response); UpdateSession(response); // If we hit our maximum redirection count, generate an error. // Errors with redirection counts of greater than 0 are handled automatically by .NET, but are // impossible to detect programmatically when we hit this limit. By handling this ourselves // (and still writing out the result), users can debug actual HTTP redirect problems. if (WebSession.MaximumRedirection == 0) // Indicate "HttpClientHandler.AllowAutoRedirect == false" { if (response.StatusCode == HttpStatusCode.Found || response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.MovedPermanently) { ErrorRecord er = new ErrorRecord(new InvalidOperationException(), "MaximumRedirectExceeded", ErrorCategory.InvalidOperation, request); er.ErrorDetails = new ErrorDetails(WebCmdletStrings.MaximumRedirectionCountExceeded); WriteError(er); } } } catch (HttpRequestException ex) { ErrorRecord er = new ErrorRecord(ex, "WebCmdletWebResponseException", ErrorCategory.InvalidOperation, request); if (ex.InnerException != null) { er.ErrorDetails = new ErrorDetails(ex.InnerException.Message); } ThrowTerminatingError(er); } if (_followRelLink) { if (!_relationLink.ContainsKey("next")) { return; } uri = new Uri(_relationLink["next"]); followedRelLink++; } } }while (_followRelLink && (followedRelLink < _maximumFollowRelLink)); } } catch (CryptographicException ex) { ErrorRecord er = new ErrorRecord(ex, "WebCmdletCertificateException", ErrorCategory.SecurityError, null); ThrowTerminatingError(er); } catch (NotSupportedException ex) { ErrorRecord er = new ErrorRecord(ex, "WebCmdletIEDomNotSupportedException", ErrorCategory.NotImplemented, null); ThrowTerminatingError(er); } }
internal static bool IsText(WebResponse response) { string contentType = ContentHelper.GetContentType(response); return(ContentHelper.IsText(contentType)); }
/// <summary> /// the main execution method for cmdlets derived from WebRequestPSCmdlet. /// </summary> protected override void ProcessRecord() { try { // Set cmdlet context for write progress ValidateParameters(); PrepareSession(); WebRequest request = GetRequest(Uri); FillRequestStream(request); // Some web sites (e.g. Twitter) will return exception on POST when Expect100 is sent // Default behaviour is continue to send body content anyway after a short period // Here it send the two part as a whole. ServicePointManager.Expect100Continue = false; try { string reqVerboseMsg = String.Format(CultureInfo.CurrentCulture, "{0} {1} with {2}-byte payload", request.Method, request.RequestUri, request.ContentLength); WriteVerbose(reqVerboseMsg); WebResponse response = GetResponse(request); try { string contentType = ContentHelper.GetContentType(response); string respVerboseMsg = String.Format(CultureInfo.CurrentCulture, "received {0}-byte response of content type {1}", response.ContentLength, contentType); WriteVerbose(respVerboseMsg); ProcessResponse(response); UpdateSession(response); // If we hit our maximum redirection count, generate an error. // Errors with redirection counts of greater than 0 are handled automatically by .NET, but are // impossible to detect programmatically when we hit this limit. By handling this ourselves // (and still writing out the result), users can debug actual HTTP redirect problems. HttpWebRequest httpRequest = request as HttpWebRequest; if ((httpRequest != null) && (httpRequest.AllowAutoRedirect == false)) { HttpWebResponse webResponse = response as HttpWebResponse; if ((webResponse.StatusCode == HttpStatusCode.Found) || (webResponse.StatusCode == HttpStatusCode.Moved) || webResponse.StatusCode == HttpStatusCode.MovedPermanently) { ErrorRecord er = new ErrorRecord(new InvalidOperationException(), "MaximumRedirectExceeded", ErrorCategory.InvalidOperation, httpRequest); er.ErrorDetails = new ErrorDetails(WebCmdletStrings.MaximumRedirectionCountExceeded); WriteError(er); } } } finally { // Choosing to close the stream instead of Dispose as the // response object is being written to the output pipe. if (response != null) { response.Close(); } } } catch (WebException ex) { WebException exThrown = ex; string detailMsg = String.Empty; try { if (ex.Response != null && ex.Response.ContentLength > 0) { Stream input = StreamHelper.GetResponseStream(ex.Response); StreamReader reader = new StreamReader(input); detailMsg = reader.ReadToEnd(); // If we were asked to not use the IE engine (or this is Invoke-RestMethod), use a simple // regex replace to remove tags. if (UseBasicParsing || (this is InvokeRestMethodCommand)) { detailMsg = System.Text.RegularExpressions.Regex.Replace(detailMsg, "<[^>]*>", ""); } else { // Otherwise, use IE to clean it up, as errors often come back as HTML VerifyInternetExplorerAvailable(false); try { IHTMLDocument2 _parsedHtml = (IHTMLDocument2) new HTMLDocument(); _parsedHtml.write(detailMsg); detailMsg = _parsedHtml.body.outerText; } catch (System.Runtime.InteropServices.COMException) { } } } } // catch all catch (Exception) { } ErrorRecord er = new ErrorRecord(exThrown, "WebCmdletWebResponseException", ErrorCategory.InvalidOperation, request); if (!String.IsNullOrEmpty(detailMsg)) { er.ErrorDetails = new ErrorDetails(detailMsg); } ThrowTerminatingError(er); } } catch (CryptographicException ex) { ErrorRecord er = new ErrorRecord(ex, "WebCmdletCertificateException", ErrorCategory.SecurityError, null); ThrowTerminatingError(er); } catch (NotSupportedException ex) { ErrorRecord er = new ErrorRecord(ex, "WebCmdletIEDomNotSupportedException", ErrorCategory.NotImplemented, null); ThrowTerminatingError(er); } }
internal static string DecodeStream(Stream stream, string characterSet) { Encoding encoding = ContentHelper.GetEncodingOrDefault(characterSet); return(DecodeStream(stream, encoding)); }
/// <summary> /// the main execution method for cmdlets derived from WebRequestPSCmdlet. /// </summary> protected override void ProcessRecord() { try { // Set cmdlet context for write progress ValidateParameters(); PrepareSession(); using (HttpClient client = GetHttpClient()) using (HttpRequestMessage request = GetRequest(Uri)) { FillRequestStream(request); try { long requestContentLength = 0; if (request.Content != null) { requestContentLength = request.Content.Headers.ContentLength.Value; } string reqVerboseMsg = String.Format(CultureInfo.CurrentCulture, "{0} {1} with {2}-byte payload", request.Method, request.RequestUri, requestContentLength); WriteVerbose(reqVerboseMsg); HttpResponseMessage response = GetResponse(client, request); string contentType = ContentHelper.GetContentType(response); string respVerboseMsg = string.Format(CultureInfo.CurrentCulture, "received {0}-byte response of content type {1}", response.Content.Headers.ContentLength, contentType); WriteVerbose(respVerboseMsg); if (!response.IsSuccessStatusCode) { string message = String.Format(CultureInfo.CurrentCulture, WebCmdletStrings.ResponseStatusCodeFailure, (int)response.StatusCode, response.ReasonPhrase); HttpResponseException httpEx = new HttpResponseException(message, response); ErrorRecord er = new ErrorRecord(httpEx, "WebCmdletWebResponseException", ErrorCategory.InvalidOperation, request); string detailMsg = ""; StreamReader reader = null; try { reader = new StreamReader(StreamHelper.GetResponseStream(response)); // remove HTML tags making it easier to read detailMsg = System.Text.RegularExpressions.Regex.Replace(reader.ReadToEnd(), "<[^>]*>", ""); } catch (Exception) { // catch all } finally { if (reader != null) { reader.Dispose(); } } if (!String.IsNullOrEmpty(detailMsg)) { er.ErrorDetails = new ErrorDetails(detailMsg); } ThrowTerminatingError(er); } ProcessResponse(response); UpdateSession(response); // If we hit our maximum redirection count, generate an error. // Errors with redirection counts of greater than 0 are handled automatically by .NET, but are // impossible to detect programmatically when we hit this limit. By handling this ourselves // (and still writing out the result), users can debug actual HTTP redirect problems. if (WebSession.MaximumRedirection == 0) // Indicate "HttpClientHandler.AllowAutoRedirect == false" { if (response.StatusCode == HttpStatusCode.Found || response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.MovedPermanently) { ErrorRecord er = new ErrorRecord(new InvalidOperationException(), "MaximumRedirectExceeded", ErrorCategory.InvalidOperation, request); er.ErrorDetails = new ErrorDetails(WebCmdletStrings.MaximumRedirectionCountExceeded); WriteError(er); } } } catch (HttpRequestException ex) { ErrorRecord er = new ErrorRecord(ex, "WebCmdletWebResponseException", ErrorCategory.InvalidOperation, request); if (ex.InnerException != null) { er.ErrorDetails = new ErrorDetails(ex.InnerException.Message); } ThrowTerminatingError(er); } } } catch (CryptographicException ex) { ErrorRecord er = new ErrorRecord(ex, "WebCmdletCertificateException", ErrorCategory.SecurityError, null); ThrowTerminatingError(er); } catch (NotSupportedException ex) { ErrorRecord er = new ErrorRecord(ex, "WebCmdletIEDomNotSupportedException", ErrorCategory.NotImplemented, null); ThrowTerminatingError(er); } }
/// <summary> /// the main execution method for cmdlets derived from WebRequestPSCmdlet. /// </summary> protected override void ProcessRecord() { try { // Set cmdlet context for write progress ValidateParameters(); PrepareSession(); HttpClient client = GetHttpClient(); HttpRequestMessage request = GetRequest(Uri); FillRequestStream(request); try { long requestContentLength = 0; if (request.Content != null) { requestContentLength = request.Content.Headers.ContentLength.Value; } string reqVerboseMsg = String.Format(CultureInfo.CurrentCulture, "{0} {1} with {2}-byte payload", request.Method, request.RequestUri, requestContentLength); WriteVerbose(reqVerboseMsg); HttpResponseMessage response = GetResponse(client, request); response.EnsureSuccessStatusCode(); string contentType = ContentHelper.GetContentType(response); string respVerboseMsg = string.Format(CultureInfo.CurrentCulture, "received {0}-byte response of content type {1}", response.Content.Headers.ContentLength, contentType); WriteVerbose(respVerboseMsg); ProcessResponse(response); UpdateSession(response); // If we hit our maximum redirection count, generate an error. // Errors with redirection counts of greater than 0 are handled automatically by .NET, but are // impossible to detect programmatically when we hit this limit. By handling this ourselves // (and still writing out the result), users can debug actual HTTP redirect problems. if (WebSession.MaximumRedirection == 0) // Indicate "HttpClientHandler.AllowAutoRedirect == false" { if (response.StatusCode == HttpStatusCode.Found || response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.MovedPermanently) { ErrorRecord er = new ErrorRecord(new InvalidOperationException(), "MaximumRedirectExceeded", ErrorCategory.InvalidOperation, request); er.ErrorDetails = new ErrorDetails(WebCmdletStrings.MaximumRedirectionCountExceeded); WriteError(er); } } } catch (HttpRequestException ex) { ErrorRecord er = new ErrorRecord(ex, "WebCmdletWebResponseException", ErrorCategory.InvalidOperation, request); ThrowTerminatingError(er); } } catch (CryptographicException ex) { ErrorRecord er = new ErrorRecord(ex, "WebCmdletCertificateException", ErrorCategory.SecurityError, null); ThrowTerminatingError(er); } catch (NotSupportedException ex) { ErrorRecord er = new ErrorRecord(ex, "WebCmdletIEDomNotSupportedException", ErrorCategory.NotImplemented, null); ThrowTerminatingError(er); } }
internal static bool IsText(HttpResponseMessage response) { string contentType = response.Content.Headers.ContentType.MediaType; return(ContentHelper.IsText(contentType)); }
internal static bool IsText(WebResponse response) { return(ContentHelper.IsText(ContentHelper.GetContentType(response))); }
protected override void ProcessRecord() { try { this.ValidateParameters(); this.PrepareSession(); WebRequest request = this.GetRequest(this.Uri); this.FillRequestStream(request); ServicePointManager.Expect100Continue = false; try { string text = string.Format(CultureInfo.CurrentCulture, "{0} {1} with {2}-byte payload", new object[] { request.Method, request.RequestUri, request.ContentLength }); base.WriteVerbose(text); WebResponse response = this.GetResponse(request); string contentType = ContentHelper.GetContentType(response); string str3 = string.Format(CultureInfo.CurrentCulture, "received {0}-byte response of content type {1}", new object[] { response.ContentLength, contentType }); base.WriteVerbose(str3); this.ProcessResponse(response); this.UpdateSession(response); HttpWebRequest targetObject = request as HttpWebRequest; if ((targetObject != null) && !targetObject.AllowAutoRedirect) { HttpWebResponse response2 = response as HttpWebResponse; if (((response2.StatusCode == HttpStatusCode.Found) || (response2.StatusCode == HttpStatusCode.MovedPermanently)) || (response2.StatusCode == HttpStatusCode.MovedPermanently)) { ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(), "MaximumRedirectExceeded", ErrorCategory.InvalidOperation, targetObject) { ErrorDetails = new ErrorDetails(WebCmdletStrings.MaximumRedirectionCountExceeded) }; base.WriteError(errorRecord); } } } catch (WebException exception) { WebException exception2 = exception; string outerText = string.Empty; try { if (exception.Response.ContentLength > 0L) { outerText = new StreamReader(StreamHelper.GetResponseStream(exception.Response)).ReadToEnd(); this.VerifyInternetExplorerAvailable(false); try { IHTMLDocument2 document = (IHTMLDocument2)((HTMLDocument)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("25336920-03F9-11CF-8FD0-00AA00686F13")))); document.write(new object[] { outerText }); outerText = document.body.outerText; } catch (COMException) { } } } catch (Exception) { } ErrorRecord record2 = new ErrorRecord(exception2, "WebCmdletWebResponseException", ErrorCategory.InvalidOperation, request); if (!string.IsNullOrEmpty(outerText)) { record2.ErrorDetails = new ErrorDetails(outerText); } base.ThrowTerminatingError(record2); } } catch (CryptographicException exception3) { ErrorRecord record3 = new ErrorRecord(exception3, "WebCmdletCertificateException", ErrorCategory.SecurityError, null); base.ThrowTerminatingError(record3); } catch (NotSupportedException exception4) { ErrorRecord record4 = new ErrorRecord(exception4, "WebCmdletIEDomNotSupportedException", ErrorCategory.NotImplemented, null); base.ThrowTerminatingError(record4); } }