如何修改使用自定义的IHttpModule和Htt的prequest过滤POST请求? [英] How can I modify a POST request using a custom IHttpModule and an HttpRequest filter?

查看:403
本文介绍了如何修改使用自定义的IHttpModule和Htt的prequest过滤POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概述

我希望能够修改请求参数和内容,第三方Web服务(ArcGIS Server的)。这将被用于创建任何客户端应用和服务器应用之间是否存在一个安全层。

I want to be able to modify request parameters and content to 3rd party web services (ArcGIS Server). This will be used to create a security layer that exists between any client application and the server application.

我想我已经找到了解决办法,但我有在执行一些困难电流。

I think that I have found a solution but I am current having some difficulties in the implementation.

可能的解决方案:修改了自定义请求过滤器请求

有关解决我实现了松散的基础上的在MSDN上显示样本。我有'提高'了code,这样我可以搜索和使用常规的前pressions更换必要的内容。这包括:

For the solution I implemented a custom request filter loosely based on the sample shown on MSDN. I have 'enhanced' the code so that I can search and replace the necessary content using regular expressions. This involves:


  1. 内容(存储在一个字节数组)转换成字符串。

  2. 搜索字符串,并进行必要的修改。

  3. 修改字符串转换成字节数组,并将其写入缓冲区。

的例子如下所示:

public override int Read(byte[] buffer, int offset, int count)
{
    int bytesRead = _stream.Read(buffer, offset, count);

    string orgContent = Encoding.UTF8.GetString(buffer, offset, bytesRead);
    string orgContentDecoded = HttpUtility.UrlDecode(orgContent);

    string layersPattern = @"&layers=(show|hide|include|exclude):([0-9]+,?)+";
    Regex layersRegex = new Regex(layersPattern, RegexOptions.IgnoreCase);

    string[] permittedLayers = new string[] { "0" , "1" };
    string replacementLayers = "&layers=show:" + String.Join(",", permittedLayers);
    string newContentDecoded = layersRegex.Replace(orgContentDecoded, replacementLayers);

    string newContent =  newContentDecoded.Replace(",", "%2C").Replace(":", "%3A");

    byte[] newBuffer = Encoding.UTF8.GetBytes(newContent);
    int newByteCountLength = Encoding.UTF8.GetByteCount(newContent);

    Encoding.UTF8.GetBytes(newContent, 0, Encoding.UTF8.GetByteCount(newContent), buffer, 0);

    return bytesRead;
}

这似乎只要修改后的内容长度的不大于原来内容长度不同的运行良好。举例来说,如果我更换一个1 2一切正常。但是,如果我更换一个1与10(从而增加1邮件大小),然后我收到的ArcGIS Server的错误格式不支持。

This seems to work well so long as the modified content length is not different than the original content length. For instance, if I replace a 1 with a 2 everything works. However, if I replace a 1 with a 10 (thereby increasing the message size by 1) then I receive an error from ArcGIS Server that the format is unsupported.

这带来了两个问题了我的注意:

This has brought two concerns to my attention:


  1. 当前的实现不处理分块的请求。也就是说,如果请求SIE是足够大的阅读可能需要进行单个请求多次。 如何应分块在这种情况下如何处理?

  2. 什么是错误消息的根本原因?是与内容相关的长度比所述流长度不同的问题? 如何我正确地修改内容,以便改变其长度是不是一个问题?

  1. The current implementation does not handle chunked requests. That is, if the request sie is large enough Read may be called multiple times for a single request. How should chunking be handled in this scenario?
  2. What is the root cause of the error message? Is the problem related to the content length being different than the stream length? How do I correctly modify the content so that changing its length is not an issue?

有什么想法?

推荐答案

在回答这个问题的第二部分是返回修改后的内容大小,原始流的不是大小。看哪!

The answer to part two of this question is to return the modified content size, not the size of the original stream. Behold!

// return bytesRead;
return newByteCountLength;

这篇关于如何修改使用自定义的IHttpModule和Htt的prequest过滤POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆