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

标签 linux c++11 cpp-netlib

类似于(但比)这个 unanswered question about headers in cpp-netlib HTTP server side 的问题。

这是在 Linux/Debian/Sid/x86-64 上使用 cpp-netlib 0.11.1clang++ 3.5 或 g++ 4.9 在 -std=c++11模式。

顺便说一句,我还在 cpp-netlib googlegroup 上问了一些例子......

#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) 作为盲目猜测:

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)
             ^

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

我无法找到用于 Web 表单的 cpp-netlib HTTP 服务的简单示例(与 examples/post/ 的 C++11 中的 cpp-netlib 类似) libonion )

最佳答案

您可以从服务器的请求对象(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 -- header 对象向量,具有 .name 和 .value 成员
  • request.body -- 请求的正文,全部在里面。

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

在你的例子中,你可以通过这样做得到你想要的:

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
      );
  }
};

关于linux - 查询cpp-netlib HTTP服务器的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29168059/

相关文章:

android - linux和android之间的Iotivity通信

linux - bash 中 expr 的语法错误

linux - 如何为systemd框架创建RPM包?

c++ - 模版的歧义

c++ - 使用 cpp-netlib 发送 HTTP POST 请求

linux - tls conn Read 中的 Golang panic - 仅在 linux 上?

c++ - 如何在 C++ 中从 .cr2 读取图像数据?

c++11 - 如何使用 c++11 为旧 Linux 版本构建二进制文件?

c++ - 带有 cpp-netlib 的 Https 服务器

c++ - cpp-netlib hello world 示例不会在 linux 上编译