1

cpp-netlib HTTP サーバー側のヘッダーに関するこの未回答の質問に似た (ただしより具体的な)質問。

これは、モードが3.5 または4.9 のcpp-netlib 0.11.1Linux/Debian/Sid/x86-64 で使用する場合です。clang++g++-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、および一般的なMIME タイプのHTTPPOST要求の場合、Web フォーム引数の辞書 (可能な場合) はありますか?application/x-www-form-urlencoded

cpp-netlibWeb フォーム用の HTTP サービスの簡単な例を見つけることができませんでした( libonioncpp-netlibの C++11 に似たもの) 。examples/post/

4

1 に答える 1

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
      );
  }
};
于 2015-03-23T16:28:33.043 に答える