使用边界分隔符生成HTTP服务器响应
使用Poco,我试图模拟AXIS camera web服务器,该服务器根据客户端请求发送jpeg帧 每个web响应都应该使用内容类型进行编码:multipart/x-mixed-replace和预定义的边界 我怎么能用Poco做呢?使用边界分隔符生成HTTP服务器响应,http,poco-libraries,multipart-mixed-replace,Http,Poco Libraries,Multipart Mixed Replace,使用Poco,我试图模拟AXIS camera web服务器,该服务器根据客户端请求发送jpeg帧 每个web响应都应该使用内容类型进行编码:multipart/x-mixed-replace和预定义的边界 我怎么能用Poco做呢? 谢谢。按照这些思路应该做一些事情: using Poco::Net::HTTPRequestHandler; using Poco::Net::HTTPServerRequest; using Poco::Net::HTTPServerResponse; using
谢谢。按照这些思路应该做一些事情:
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)
response.setContentType()是rigth,但是当您创建sendBuffer()时,Poco不会将预定义的边界字符串放在每个边界块的顶部
将setChunkedTransferEncoding()指定为false以避免块传输非常重要,因为这不是我想要的
我需要的是:
如果这是我的数据缓冲区:“这是第一个边界块。一个边界块可以在下一个块中继续。这是第二个边界块。”
这就是我需要实现的:
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.
仔细考虑一下,我发现我必须手动分割缓冲区,以获得所需的边界
我实际上就是这样做的:
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;
}
std::ostream&ost=response.send();
字符帧缓冲区[102410];
字符片段[1024];
字符串边界=“--边界--”;
response.setChunkedTransferEncoding(false);
response.setContentType(“multipart/x-mixed-replace;boundary=“+boundary”);
//使用边界将我的帧缓冲区拆分为一些片段
unsigned int-nBytes=sizeof(帧缓冲区);
无符号整数_MAX_FRAGMENT_SIZE_=sizeof(FRAGMENT);
无符号整数索引=0;
response.setContentLength(frameLength);
而(n字节>0){
无符号整数大小=(n字节>最大片段大小)?最大片段大小:n字节;
memcpy(片段、帧缓冲区+索引、大小);
//环境与边界相对应。
ostr我们有它的用途,请参阅一个使用示例。对于HTTP来说,调整它应该不需要太多的工作。另外,看看这个类。MultipartWriter类看起来很不错。我将尝试一下。感谢Günter(和Alex)的支持。