Java Http代理 [英] Java Http Proxy

查看:128
本文介绍了Java Http代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java编写一个小代理,基本上可以选择2个特定文件并对它们进行一些额外的处理。一个URL只是在传递内容之前从内容中获取一些信息。另一个文件我想过滤响应内容,这只是xml deflate编码(我想删除一些子元素)。

I'm writing a small proxy in Java that basically picks out 2 specific files and does some extra processing on them. One URL it just grabs some info out of the content before passing it along. The other file I want to filter the response content, which is just xml deflate encoded (I want to remove some child elements).

现在,代理工作正常只是通过所有内容。但是,当我尝试过滤xml文件时,它实际上并没有将内容发送到客户端???

Now, the proxy works fine when I just pass though all content. However, when I try to filter the xml file it doesn't actually send the content to the client ???

以下是一些代码:

在接受Socket连接时生成的Thread run()方法中,一旦我确定请求是针对我要过滤的文件,我就调用:

Within the Thread run() method that is spawned when accepting a Socket connection, once I determine the request is for the file I want to filter, I call:

filterRaceFile(serverIn, clientOut);     // This won't send content     
//streamHTTPData(serverIn, clientOut, true); // This passes through fine (but all content of course).

这里是过滤方法本身:

private void filterRaceFile(InputStream is, OutputStream os) {
    // Pass through headers before using deflate and filtering xml
    processHeader(is, os, new StringBuilder(), new StringBuilder());        

    // Seems to be 1 line left, inflater doesn't like it if we don't do this anyway...?
    try {
        os.write(readLine(is, false).getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }       

    InflaterInputStream inflate = new InflaterInputStream(is);
    DeflaterOutputStream deflate = new DeflaterOutputStream(os);
    int c = 0;
    try {
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document xdoc = db.parse(inflate);
        Node id = xdoc.getElementsByTagName("id").item(0);
        Node msg = xdoc.getElementsByTagName("message").item(0);
        StringBuilder xml_buf = new StringBuilder("<race>\n");

        xml_buf.append("<id>").append(id.getTextContent()).append("</id>\n");
        xml_buf.append("<message>").append(msg.getTextContent()).append("</message>\n");
        xml_buf.append("<boats>\n");

        NodeList allBoats = xdoc.getElementsByTagName("boat");
        int N = allBoats.getLength();

        for (int i = 0; i < N; ++i)
        {
            Node boat = allBoats.item(i);
            Element boat_el = (Element)boat;                
            double lat = Double.parseDouble(boat_el.getElementsByTagName("lat").item(0).getTextContent());
            double lon = Double.parseDouble(boat_el.getElementsByTagName("lon").item(0).getTextContent());
            double dist = Geodesic.vincenty_earth_dist(proxy.userLat, proxy.userLon, lat, lon)[0];
            if (dist <= LOS_DIST)
            {
                String boatXML = xmlToString(boat);
                //<?xml version="1.0" encoding="UTF-8"?> is prepended to the xml
                int pos = boatXML.indexOf("?>")+2;
                boatXML = boatXML.substring(pos);
                xml_buf.append(boatXML);
                ++c;
            }
        }
        System.out.printf("%d boats within LOS distance\n", c);

        xml_buf.append("</boats>\n");
        xml_buf.append("</race>");

        byte[] xml_bytes = xml_buf.toString().getBytes("UTF-8");
        deflate.write(xml_bytes);

    } catch (Exception e) {
        e.printStackTrace();
    }

    // flush the OutputStream and return
    try {
        os.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
    }

我还有一个简单的传递方法服务器的InputStream到客户端的OutputStream,也使用readLine并且工作正常 - 即在浏览器中出现的任何其他url没有问题,所以readLine没问题。布尔参数是让它知道它是从deflate流中读取的,因为它在内部使用mark和read,而deflate流不支持。

I also have a simple pass through method that simply writes the server's InputStream to the client's OutputStream, uses readLine also and works fine - ie any other url shows up in the browser no problems, so readLine is ok. The boolean parameter is to let it know it is reading from a deflate stream, as it uses mark and read internally, which isn't supported on deflate streams.

XML是非常简单:

<race> 
  {some data to always pass thru}
    <boats>
       <boat>
          <id>1234</id>
          ....
          <lat>-23.3456</lat>
          <lon>17.2345</lon>
       </boat>
       {more boat elements}
     </boats>
   </race>

它产生了我希望它发送给客户端的xml,但是客户端只是没有接收它(在web调试器中显示内容长度为0,尽管在原始响应中也没有Content-Length标头)

And it produces the xml I want it to send to the client fine, but the client just doesn't receive it (shows content-length of 0 in a web-debugger, although there is no Content-Length header in the original response either)

关于什么的任何想法正在进行,或者我应该做什么我不是?

Any ideas as to what is going on, or what I should be doing that I am not ??

推荐答案

我没有看到任何错误,但是如果您已经在使用 Document ,那么您是否只能修改文档然后使用XML库来编写整个Document?这似乎比手动XML格式更强大。

I don't see any error, but if you are already using a Document, can't you just modify the document and then use your XML library to write the whole Document out? This seems more robust than manual XML formatting.

这篇关于Java Http代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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