查询cpp-netlib HTTP服务器的请求

时间:2015-03-20 13:47:12

标签: linux c++11 cpp-netlib

unanswered question about headers in cpp-netlib HTTP server side类似(但更具体)的问题。

在{/ 1}}模式下使用clang++ 3.5或g++ 4.9在Linux / Debian / Sid / x86-64上使用cpp-netlib 0.11.1

BTW,我还在cpp-netlib googlegroup ...

上询问了一些例子
-std=c++11

如果我使用#include <fstream> #include <iostream> #include <boost/network/protocol/http/server.hpp> #define DEBUG_OUT(Out) do {std::cout << __FILE__ << ":" << __LINE__ \ << ' ' << Out << std::endl;} while(0) namespace netlib_http = boost::network::http; struct Yaca_web; typedef netlib_http::server<Yaca_web> Yaca_server; struct Yaca_web { void operator() (Yaca_server::request const& request, Yaca_server::response &response) { DEBUG_OUT("web request source=" << std::string(source(request)) //<< " uri=" << std::string(uri(request)) ///@@@@@ ); }; void log(const std::string&s) { DEBUG_OUT("log s=" << s); }; }; // end Yaca_web void ya_web_service(int port) { DEBUG_OUT("ya_web_service start port=" << port); Yaca_web webhandler; Yaca_server::options weboptions {webhandler}; weboptions.address("localhost"); weboptions.port(std::to_string(port)); weboptions.reuse_address(true); Yaca_server webserver {weboptions}; DEBUG_OUT("ya_web_service before running server"); webserver.run(); DEBUG_OUT("ya_web_service end port=" << port); } 取消注释该行,但上面的代码无法编译,但我使用///@@@@@作为盲目猜测:

uri(request)

有没有人知道如何获取更多信息:特别是方法,路径或URI,以及HTTP POST请求与webform参数的公共application/x-www-form-urlencoded MIME type字典(如果可能)?

我无法为Web表单找到In file included from ywebx.cc:4: In file included from /usr/include/boost/network/protocol/http/server.hpp:13: In file included from /usr/include/boost/network/protocol/http/request.hpp:18: /usr/include/boost/network/protocol/http/message/wrappers/uri.hpp:25:44: error: no member named 'uri' in 'boost::network::http::basic_request<tags::http_server>' operator string_type() { return message_.uri().raw(); } ~~~~~~~~ ^ ywebx.cc:18:34: note: in instantiation of member function 'boost::network::http::impl::uri_wrapper<boost::network::http::tags::http_server>::operator basic_string' requested here << " uri=" << std::string(uri(request)) ^ ./yacax.h:49:18: note: expanded from macro 'DEBUG_OUT' << ' ' << Out << std::endl;} while(0) ^ HTTP服务的简单示例(类似于examples/post/ libonion的{{3}}中的cpp-netlib cpp-netlib

1 个答案:

答案 0 :(得分:2)

您可以从服务器的请求对象(https://github.com/cpp-netlib/cpp-netlib/blob/0.11-devel/boost/network/protocol/http/impl/request.hpp#L126)获取以下字段:

  • request.destination - 一个字符串,“uri”
  • request.method - 方法,“POST”,“GET”等
  • request.headers - 标题对象的向量,带.name和.value成员
  • request.body - 请求的正文,全部在。

如果您正在使用服务器API的异步版本,您还可以按照http://cpp-netlib.org/0.11.1/reference/http_server.html#connection-object中的文档获取正文流 - 这允许您读取传入请求的主体的块,然后通过设置响应的状态,添加标题等

在您的示例中,您可以通过执行以下操作获得所需内容:

struct Yaca_web {
  void operator()(Yaca_server::request const& req,
                  Yaca_server::response& res) {
    DEBUG_OUT("web request source="
      << std::string(source(request))
      << " uri=" << std::string(destination(request)) ///@@@@@
      );
  }
};

或者,直接使用对象:

struct Yaca_web {
  void operator()(Yaca_server::request const& req,
                  Yaca_server::response& res) {
    DEBUG_OUT("web request source="
      << req.source
      << " uri=" << req.destination
      );
  }
};