通过haproxy修改http响应的内容

时间:2017-10-20 13:03:42

标签: response haproxy

我通过haproxy路由soap请求,并且在部署新版本后,通过向XML元素<myresponse>添加名称空间<newnamespace:myresponse>,响应请求更改ist响应结构的系统之一。

我无法控制的客户端系统无法处理对有效负载的更改。因为我无法更改此客户的代码,我想通过haproxy从响应有效负载中删除newnamespace:

我在我的后端配置中尝试了选项rspirep ^:\ "mynewnamespace:" "",但它没有任何效果

是可能的,如果是的话,怎么样?

2 个答案:

答案 0 :(得分:1)

what HAProxy is and is not部分我们可以读到,HAProxy不是:

  

数据清理程序:它不会修改请求体和响应。

答案 1 :(得分:0)

几个月前,我在服务器上遇到了类似的问题,我看到了这个答案。现在,我找到了解决问题的方法,我想发布自己的方法来帮助其他可能会被拒之门外的人。坦白地说,这可能对您造成太大的伤害

如MoEmEn所述,haproxy不会修改请求或响应的主体。但是,在haproxy's configuration guide中,它提到了添加一些过滤器的功能,其中之一允许您压缩主体请求。这肯定会改变身体。

one of haproxy's documentation中,它说明了如何自己实现过滤器。基本上,它使您可以在每个解析阶段检查http请求/响应。完成后,它会调用您在过滤器中定义的回调函数,然后您可以对该数据执行任何操作。过滤器回调如下所示:

struct flt_ops {
    /*
     * Callbacks to manage the filter lifecycle
     */
    int  (*init)             (struct proxy *p, struct flt_conf *fconf);
    void (*deinit)           (struct proxy *p, struct flt_conf *fconf);
    int  (*check)            (struct proxy *p, struct flt_conf *fconf);
    int  (*init_per_thread)  (struct proxy *p, struct flt_conf *fconf);
    void (*deinit_per_thread)(struct proxy *p, struct flt_conf *fconf);

    /*
     * Stream callbacks
     */
    int  (*attach)            (struct stream *s, struct filter *f);
    int  (*stream_start)      (struct stream *s, struct filter *f);
    int  (*stream_set_backend)(struct stream *s, struct filter *f, struct proxy *be);
    void (*stream_stop)       (struct stream *s, struct filter *f);
    void (*detach)            (struct stream *s, struct filter *f);
    void (*check_timeouts)    (struct stream *s, struct filter *f);

    /*
     * Channel callbacks
     */
    int  (*channel_start_analyze)(struct stream *s, struct filter *f,
                                  struct channel *chn);
    int  (*channel_pre_analyze)  (struct stream *s, struct filter *f,
                                  struct channel *chn,
                                  unsigned int an_bit);
    int  (*channel_post_analyze) (struct stream *s, struct filter *f,
                                  struct channel *chn,
                                  unsigned int an_bit);
    int  (*channel_end_analyze)  (struct stream *s, struct filter *f,
                                  struct channel *chn);

    /*
     * HTTP callbacks
     */
    int  (*http_headers)       (struct stream *s, struct filter *f,
                                struct http_msg *msg);
    int  (*http_data)          (struct stream *s, struct filter *f,
                                struct http_msg *msg);
    int  (*http_chunk_trailers)(struct stream *s, struct filter *f,
                                struct http_msg *msg);
    int  (*http_end)           (struct stream *s, struct filter *f,
                                struct http_msg *msg);
    int  (*http_forward_data)  (struct stream *s, struct filter *f,
                                struct http_msg *msg,
                                unsigned int len);

    void (*http_reset)         (struct stream *s, struct filter *f,
                                struct http_msg *msg);
    void (*http_reply)         (struct stream *s, struct filter *f,
                                short status,
                                const struct buffer *msg);

    /*
     * TCP callbacks
     */
    int  (*tcp_data)        (struct stream *s, struct filter *f,
                             struct channel *chn);
    int  (*tcp_forward_data)(struct stream *s, struct filter *f,
                             struct channel *chn,
                             unsigned int len);
};

您可能感兴趣的是

http_data

,因为您将删除前缀“ newnamespace:”。 Haproxy提供了一个很好的功能,可以轻松删除某些内容:

/* This function writes the string <str> at position <pos> which must be in
 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
 * <l> and <r> are updated to be valid after the shift. The shift value
 * (positive or negative) is returned. If there's no space left, the move is
 * not done. The function does not adjust ->o because it does not make sense to
 * use it on data scheduled to be sent. For the same reason, it does not make
 * sense to call this function on unparsed data, so <orig> is not updated. The
 * string length is taken from parameter <len>. If <len> is null, the <str>
 * pointer is allowed to be null.
 */
int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len)

假设您发现“ newnamespace:”以char * pos开头,以char *结尾结束,您可以致电

buffer_replace2(buf, pos, end, NULL, 0)

删除字符串。

虽然可能有点太多,但可以解决您的问题