简体   繁体   English

使用边界定界符进行HTTP服务器响应

[英]Making a HTTP server response with boundary delimiters

Using Poco, I'm trying to emulate an AXIS camera web-server, that sends jpeg frames on clients request. 使用Poco,我试图模拟一个AXIS摄像机网络服务器,该服务器应客户端请求发送jpeg帧。

Each web response should be encoded with Content-Type: multipart/x-mixed-replace and a predefined boundary. 每个网络响应都应使用Content-Type:multipart / x-mixed-replace和预定义的边界进行编码。

How can I do it with Poco? 如何使用Poco做到这一点? Thanks. 谢谢。

The response.setContentType() is rigth, but when you make the sendBuffer() , Poco doesn't put the predefined boundary strings at top of each boundary block. response.setContentType()严格 ,但是当您创建sendBuffer()时 ,Poco不会将预定义的边界字符串放在每个边界块的顶部。

It is important to specify setChunkedTransferEncoding() to false to avoid chunk transfers, that it is not what I am asking for. 重要的是将setChunkedTransferEncoding()设置false以避免块传输,这不是我要的。

What I need is this: http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html 我需要的是: http : //www.w3.org/Protocols/rfc1341/7_2_Multipart.html

If this is my data Buffer: "This is the first boundary block. A boundary block may continue in the next block. This is the second boundary block." 如果这是我的数据缓冲区:“这是第一个边界块。边界块可以在下一个块中继续。这是第二个边界块。”

This is what I need to implement: 这是我需要实现的:

HTTP/1.1 200 OK
Date: Tue, 01 Dec 2013 10:27:30 GMT
Content-Length: 117
Content-Type: Multipart/mixed; boundary="sample_boundary";

--sample_boundary
This is the first boundary block. A boundary block may

--sample_boundary
continue in the next block. This is the second boundary block.

Playing around with that, I see that I must to split manually my buffer in order to get the desired boundaries. 为此,我看到我必须手动分割缓冲区以获取所需的边界。

This is how I actually do it: 这是我实际上的做法:

std::ostream& ostr = response.send();

char frameBuffer[102410];
char fragment[1024];

string boundary = "--BOUNDARY--";
response.setChunkedTransferEncoding(false);
response.setContentType("multipart/x-mixed-replace; boundary=" + boundary);


//Split my frameBuffer in some fragments with boundary
unsigned int nBytes = sizeof(frameBuffer);
unsigned int _MAX_FRAGMENT_SIZE_ = sizeof(fragment);
unsigned int index=0;

response.setContentLength(frameLength);

while (nBytes>0){

    unsigned int size = (nBytes>_MAX_FRAGMENT_SIZE_)?_MAX_FRAGMENT_SIZE_:nBytes;
    memcpy(fragment, frameBuffer + index, size);

    //Enviamos el fragmento sin mas con su boundary correspondiente.
    ostr << boundary << "\r\n";
    ostr.write(fragment, size);
    ostr << "\r\n";

    index += size;
    nBytes -= size;
}

I thought Poco had tools to solve that. 我认为Poco有解决此问题的工具。 Thanks 谢谢

Something along these lines should do: 遵循这些原则应该可以:

using Poco::Net::HTTPRequestHandler;
using Poco::Net::HTTPServerRequest;
using Poco::Net::HTTPServerResponse;
using Poco::Net::HTTPRequestHandlerFactory;
using Poco::Net::HTTPServerParams;
using Poco::Net::HTTPServer;
using Poco::Net::ServerSocket;
using Poco::Net::SocketAddress;

struct AXISRequestHandler: public HTTPRequestHandler
{
    void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
    {
        // ... get data to send back
        response.setContentType("multipart/x-mixed-replace; boundary=--MY_BOUND");
        response.sendBuffer(data, size);
    }
};

struct AXISRequestHandlerFactory: public HTTPRequestHandlerFactory
{
    HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request)
    {
        return new AXISRequestHandler;
    }
};

ServerSocket svs(SocketAddress("192.168.0.1", 8080));
HTTPServerParams* pParams = new HTTPServerParams;
// ... set server params here to your liking
HTTPServer srv(new AXISRequestHandlerFactory, svs, pParams);
srv.start(); // NB: server will fly off here (to its own thread)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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