使用分界符生成HTTP服务器响应

3

我正在使用Poco来模拟一个AXIS相机Web服务器,该服务器在客户端请求时发送JPEG帧。

每个Web响应都应使用Content-Type:multipart / x-mixed-replace和预定义的边界进行编码。

我该如何使用Poco完成这个任务? 谢谢。

2个回答

1
< p > response.setContentType() 是正确的,但是在进行 sendBuffer() 时,Poco 没有将预定义的边界字符串放置在每个边界块的顶部。

重要的是将 setChunkedTransferEncoding() 指定为 false,以避免分块传输,这不是我所要求的。

我需要的是这个:
http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

如果这是我的数据缓冲区:“这是第一个边界块。边界块可以在下一个块中继续。这是第二个边界块。”

这就是我需要实现的内容:

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 我必须手动分割缓冲区以获得所需的边界。
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;
}

我认为Poco有解决这个问题的工具。 谢谢


我们已经为MailMessage准备好了,可以参考MailMessageTest::testReadWriteMultiPart来使用。调整它以适应HTTP应该不会太麻烦。 - Alex
1
此外,请查看Poco::Net::MultipartWriter类。 - Günter Obiltschnig
MultipartWriter类看起来不错。我会试一试。感谢Günter(和Alex)的支持。 - Cesar Ortiz

0

应该是这样的:

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接