internal static StringBuilder GetRawContentHeader(WebResponse baseResponse) { StringBuilder raw = new StringBuilder(); // add protocol and status line string protocol = WebResponseHelper.GetProtocol(baseResponse); if (!String.IsNullOrEmpty(protocol)) { int statusCode = WebResponseHelper.GetStatusCode(baseResponse); string statusDescription = WebResponseHelper.GetStatusDescription(baseResponse); raw.AppendFormat("{0} {1} {2}", protocol, statusCode, statusDescription); raw.AppendLine(); } // add headers foreach (string key in baseResponse.Headers.AllKeys) { string value = baseResponse.Headers[key]; raw.AppendFormat("{0}: {1}", key, value); raw.AppendLine(); } raw.AppendLine(); return(raw); }
private void InitializeContent() { if (ContentHelper.IsText(ContentHelper.GetContentType(base.BaseResponse))) { string characterSet = WebResponseHelper.GetCharacterSet(base.BaseResponse); this.Content = StreamHelper.DecodeStream(base.RawContentStream, characterSet); } else { this.Content = string.Empty; } }
internal static WebResponseObject GetResponseObject(WebResponse response, MemoryStream contentStream, ExecutionContext executionContext, bool useBasicParsing = false) { if (WebResponseHelper.IsText(response)) { if (!useBasicParsing) { return(new HtmlWebResponseObject(response, contentStream, executionContext)); } return(new BasicHtmlWebResponseObject(response, contentStream)); } return(new WebResponseObject(response, contentStream)); }
internal static WebResponseObject GetResponseObject(HttpResponseMessage response, Stream responseStream, ExecutionContext executionContext) { WebResponseObject output; if (WebResponseHelper.IsText(response)) { output = new BasicHtmlWebResponseObject(response, responseStream); } else { output = new WebResponseObject(response, responseStream); } return(output); }
/// <summary> /// Reads the response content from the web response. /// </summary> private void InitializeContent() { string contentType = ContentHelper.GetContentType(BaseResponse); if (ContentHelper.IsText(contentType)) { // fill the Content buffer string characterSet = WebResponseHelper.GetCharacterSet(BaseResponse); this.Content = StreamHelper.DecodeStream(RawContentStream, characterSet); } else { this.Content = string.Empty; } }
/// <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); this.Content = StreamHelper.DecodeStream(RawContentStream, characterSet, out encoding); this.Encoding = encoding; } else { this.Content = string.Empty; } }
internal static StringBuilder GetRawContentHeader(HttpResponseMessage response) { StringBuilder raw = new StringBuilder(); string protocol = WebResponseHelper.GetProtocol(response); if (!string.IsNullOrEmpty(protocol)) { int statusCode = WebResponseHelper.GetStatusCode(response); string statusDescription = WebResponseHelper.GetStatusDescription(response); raw.AppendFormat("{0} {1} {2}", protocol, statusCode, statusDescription); raw.AppendLine(); } HttpHeaders[] headerCollections = { response.Headers, response.Content == null ? null : response.Content.Headers }; foreach (var headerCollection in headerCollections) { if (headerCollection == null) { continue; } foreach (var header in headerCollection) { // Headers may have multiple entries with different values foreach (var headerValue in header.Value) { raw.Append(header.Key); raw.Append(": "); raw.Append(headerValue); raw.AppendLine(); } } } raw.AppendLine(); return(raw); }
internal static WebResponseObject GetResponseObject(WebResponse response, Stream responseStream, ExecutionContext executionContext, bool useBasicParsing = false) { WebResponseObject output; if (WebResponseHelper.IsText(response)) { if (!useBasicParsing) { output = new HtmlWebResponseObject(response, responseStream, executionContext); } else { output = new BasicHtmlWebResponseObject(response, responseStream); } } else { output = new WebResponseObject(response, responseStream); } return(output); }
internal static StringBuilder GetRawContentHeader(WebResponse baseResponse) { StringBuilder builder = new StringBuilder(); string protocol = WebResponseHelper.GetProtocol(baseResponse); if (!string.IsNullOrEmpty(protocol)) { int statusCode = WebResponseHelper.GetStatusCode(baseResponse); string statusDescription = WebResponseHelper.GetStatusDescription(baseResponse); builder.AppendFormat("{0} {1} {2}", protocol, statusCode, statusDescription); builder.AppendLine(); } foreach (string str3 in baseResponse.Headers.AllKeys) { string str4 = baseResponse.Headers[str3]; builder.AppendFormat("{0}: {1}", str3, str4); builder.AppendLine(); } builder.AppendLine(); return(builder); }
internal static StringBuilder GetRawContentHeader(HttpResponseMessage response) { StringBuilder raw = new StringBuilder(); string protocol = WebResponseHelper.GetProtocol(response); if (!string.IsNullOrEmpty(protocol)) { int statusCode = WebResponseHelper.GetStatusCode(response); string statusDescription = WebResponseHelper.GetStatusDescription(response); raw.AppendFormat("{0} {1} {2}", protocol, statusCode, statusDescription); raw.AppendLine(); } foreach (var entry in response.Headers) { raw.AppendFormat("{0}: {1}", entry.Key, entry.Value.FirstOrDefault()); raw.AppendLine(); } raw.AppendLine(); return(raw); }
internal static WebResponseObject GetResponseObject(HttpResponseMessage response, Stream responseStream, ExecutionContext executionContext, bool useBasicParsing = false) { WebResponseObject output; if (WebResponseHelper.IsText(response)) { output = new BasicHtmlWebResponseObject(response, responseStream); // TODO: This code needs to be enable after the dependency on mshtml is resolved. //if (useBasicParsing) //{ // output = new BasicHtmlWebResponseObject(response, responseStream); //} //else //{ // output = new HtmlWebResponseObject(response, responseStream, executionContext); //} } else { output = new WebResponseObject(response, responseStream); } return(output); }
/// <summary> /// Process the web response and output corresponding objects. /// </summary> /// <param name="response"></param> internal override void ProcessResponse(HttpResponseMessage response) { if (null == response) { throw new ArgumentNullException("response"); } using (BufferingStreamReader responseStream = new BufferingStreamReader(StreamHelper.GetResponseStream(response))) { if (ShouldWriteToPipeline) { // First see if it is an RSS / ATOM feed, in which case we can // stream it - unless the user has overridden it with a return type of "XML" if (TryProcessFeedStream(responseStream)) { // Do nothing, content has been processed. } else { // determine the response type RestReturnType returnType = CheckReturnType(response); // Try to get the response encoding from the ContentType header. Encoding encoding = null; string charSet = response.Content.Headers.ContentType?.CharSet; if (!string.IsNullOrEmpty(charSet)) { // NOTE: Don't use ContentHelper.GetEncoding; it returns a // default which bypasses checking for a meta charset value. StreamHelper.TryGetEncoding(charSet, out encoding); } object obj = null; Exception ex = null; string str = StreamHelper.DecodeStream(responseStream, ref encoding); // NOTE: Tests use this verbose output to verify the encoding. WriteVerbose(string.Format ( System.Globalization.CultureInfo.InvariantCulture, "Content encoding: {0}", string.IsNullOrEmpty(encoding.HeaderName) ? encoding.EncodingName : encoding.HeaderName) ); bool convertSuccess = false; if (returnType == RestReturnType.Json) { convertSuccess = TryConvertToJson(str, out obj, ref ex) || TryConvertToXml(str, out obj, ref ex); } // default to try xml first since it's more common else { convertSuccess = TryConvertToXml(str, out obj, ref ex) || TryConvertToJson(str, out obj, ref ex); } if (!convertSuccess) { // fallback to string obj = str; } WriteObject(obj); } } if (ShouldSaveToOutFile) { StreamHelper.SaveStreamToFile(responseStream, QualifiedOutFile, this); } if (!String.IsNullOrEmpty(ResponseHeadersVariable)) { PSVariableIntrinsics vi = SessionState.PSVariable; vi.Set(ResponseHeadersVariable, WebResponseHelper.GetHeadersDictionary(response)); } } }