【问题标题】:Making a HTTP server response with boundary delimiters使用边界分隔符进行 HTTP 服务器响应
【发布时间】:2023-03-26 21:35:01
【问题描述】:

使用 Poco,我正在尝试模拟一个 AXIS 摄像头网络服务器,它根据客户端请求发送 jpeg 帧。

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

我怎样才能用 Poco 做到这一点? 谢谢。

【问题讨论】:

    标签: http poco-libraries multipart-mixed-replace


    【解决方案1】:

    应该这样做:

    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)
    

    【讨论】:

      【解决方案2】:

      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.
      

      玩弄这个,我发现我必须手动拆分缓冲区才能获得所需的边界。

      这就是我实际的做法:

      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 有工具可以解决这个问题。 谢谢

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-04
      • 2017-04-12
      • 1970-01-01
      • 2023-03-05
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      相关资源
      最近更新 更多