简体   繁体   中英

How do I decode the response stream correctly in ServiceStack ProxyFeature?

I am trying to replace URLs in the body of a response in ProxyFeature, but I am getting an encoded body.

Here is the code:

Plugins.Add(new ProxyFeature(
          matchingRequests: req =>
          {
            return req.PathInfo.StartsWith("/proxy");
          },
          resolveUrl: (req) =>
          {
            string var = req.RawUrl.Replace("/proxy/", "");
            return var;
          })
      {
        IgnoreResponseHeaders = {
                    "X-Frame-Options"
                },
        TransformResponse = async (res, responseStream) =>
        {          
          using (var reader = new StreamReader(responseStream,Encoding.UTF8))
          {
            var responseBody = await reader.ReadToEndAsync();
            Console.WriteLine(responseBody);
            var replacedBody = responseBody.Replace("http://","/proxy/http://");
            replacedBody = replacedBody.Replace("https://", "/proxy/https://");

            return MemoryStreamFactory.GetStream(replacedBody.ToUtf8Bytes());
          }
        }

I am not sure what I am doing wrong, since this is more or less a copy of the sample code in the website with minor changes.

The outcome should be any and all URLs in the body should be prepended by "/proxy/", and this should be applied to any URL that the user navigates to.

Good to mention that this works well enough if I remove the "TransformResponse" part.

Any ideas as to what I am doing wrong here.

Thank you.

Updates:

The partial of the content being returned. The url navigated in this case was " https://www.theverge.com ".

?p?}^??d????i+s4?~?<???$?x]??????j??u?,?z?md?F6??G??{???g;?tU??q???????08;???vφ????N?? k???d8??l??GA?x???p?";?f??yk2?R?r2??
fA?z?7Q?Y}??2?v????p<|dù?s???q????t?M?^0_???o?/??V???z?S?5??r-s?N?U?j?:??1Bo?Z?>?-?
??u??{{*v????Q?g??s4??       ;?$;?Q???A0??YFo/{:;6??{?q/;?????????G????s??.??g?E?????w??'wL{?Lw0?-¬?????~????p?u??TC?X??J?j??lW??Z??(???z?u?u????a?W???~?R?t{?J?Q??f?^?6k?R?X????]^M?3??_g?????Y? *????l?xp?sT
~??9?L??4D{1q3V??r??;?'9He(??FeCTP[??/???T?{??j%??h?????@?f?e??k???p?R?&?VM????n<R?+???wR???      ????p?pyo#y??a??7L?????7VL??6n#0o,m?q????????J??#?+-Io??pr#n|????|qU?7?:??mVT?y?M??Mf ??"?=?B??u??F?X/4??f?^,?^?t????N???????fk??T!??y{?SG???a??'???#EWz?O???{???????po~?V]?Vo????Y?σ??@??2QTg??4??n????4?=???P5j!?j????}?7?M'??[??A?/[=?Q??O???     ~-^???,?/f??U?????p???A:??????M.`?.R??????8??]+???T??|o?0????????GD?_0???'{??~x?3?tM??Xe{???T0, f8!?w?j?m=??3??f?????[q?????}??a???r?????l??d[)?????p?w

The garbled output is because the downstream response that's being returned is compressed yet you're trying to read it directly as a string.

If the proxy response is compressed you'll need to decompress it, make any string transformations then compress it back, eg:

Plugins.Add(new ProxyFeature(
    matchingRequests: req => req.PathInfo.StartsWith("/proxy"),
    resolveUrl: req => req.RawUrl.Replace("/proxy/", ""))
{
    IgnoreResponseHeaders = { "X-Frame-Options" },
    TransformResponse = async (res, responseStream) => {
        var enc = res.GetHeader(HttpHeaders.ContentEncoding);
        var useStream = responseStream;
        if (enc != null)
            useStream = responseStream.Decompress(enc);

        using (var reader = new StreamReader(useStream,Encoding.UTF8))
        {
            var responseBody = await reader.ReadToEndAsync();
            var replacedBody = responseBody.Replace("http://","/proxy/http://");
            replacedBody = replacedBody.Replace("https://", "/proxy/https://");

            var bytes = replacedBody.ToUtf8Bytes();
            return new MemoryStream(enc != null ? bytes.CompressBytes(enc) : bytes);
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM