10

我正在使用来自 Microsoft C++ REST SDK 1.3.1 的 web::http::experimental::listener::http_listener 运行 HTTP 服务器,并尝试编写 HTML 和 Javascript 作为客户端以与服务器交互。

几乎毫不奇怪,我得到了……跨域请求被阻止:同源策略不允许在……处读取远程资源(原因:CORS 标头“Access-Control-Allow-Origin”缺失)。

如何将Access-Control-Allow-Origin:* 放在 http 侦听器端(在 C++ 代码中)?

在 C++ REST 1.3.1 中有可能吗?除了 JSONP,还有其他解决方法吗?

服务器

#include <cpprest/http_listener.h>
#include <cpprest/json.h>
using namespace web;
using namespace web::http;
using namespace web::http::experimental::listener;

http_listener httpSrv;
httpSrv->support(methods::GET, handle_get);

void handle_get(http_request request)
{
  const json::value response;
  request.reply(status_codes::OK, response);
}

客户端 带有 jQ​​uery v1.12.4 的客户端(绑定到 jQuery UI v1.12.0)

    $("button").click(function () {
        $.get(rest_url, function(data, status){
            console.log(status);
            console.log(data);
        });
    });

- - - - - - - - - 更新 - - - - - - - - - - - -

来自答案的解决方案

服务器

  http_listener httpSrv;
  httpSrv.support(methods::GET, handle_get);
  httpSrv.support(methods::POST, handle_post);
  httpSrv.support(methods::OPTIONS, handle_options);
  httpSrv.open().wait();

  //...........

  void handle_options(http_request request)
  {
    http_response response(status_codes::OK);
    response.headers().add(U("Allow"), U("GET, POST, OPTIONS"));
    response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
    response.headers().add(U("Access-Control-Allow-Methods"), U("GET, POST, OPTIONS"));
    response.headers().add(U("Access-Control-Allow-Headers"), U("Content-Type"));
    request.reply(response);
  }

  void handle_get(http_request request)
  {        
    request.reply(status_codes::OK, ...);
  }

  void handle_post(http_request request)
  {
    json::value jsonResponse;

    request
      .extract_json()
      .then([&jsonResponse](pplx::task<json::value> task)
      {
        jsonResponse = process_request(task.get());
      })
     .wait();

    http_response response(status_codes::OK);
    response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
    response.set_body(jsonResponse);
    request.reply(response);
  }  

客户

  function requestREST(request/*json*/,onSuccess/*callback with json response*/) {    
    $.ajax({     
        type: "POST",
        url: "...",
        data: JSON.stringify(request),
        dataType: 'json',
        crossDomain: true,
        contentType: "application/json",
        success: function (response) {
            onSuccess(response);
        },
        timeout:3000,
        statusCode: {
            400: function (response) {
                alert('Not working!');
            },
            0: function (response) {
                alert('Not working!');
            }              
        }
    });
4

1 回答 1

8

要在服务器端 (C++) 添加标头,您需要修改用于发回响应的代码。

目前,您正在使用:

request.reply(status_codes::OK, response);

我们的想法是从一个空响应开始自己编写响应,添加所需的标头,设置实际的正文,然后将响应发送回客户端,而不是在单行中这样做。

要构造一个空响应,我们可以使用以下函数:

web::http::http_response::http_response(http::status_code code)

文档中所述,它将构造一个具有给定状态代码、没有标题和正文的响应。

要访问响应的标头,我们可以使用以下函数:

web::http::http_response::headers()

返回的对象将是包含函数的http_headers类型add

web::http::http_headers::add(const key_type &name, const _t1 &value)

如果提供了标题的名称和值,则此函数将向响应添加标题。

设置标题后,唯一需要设置的就是正文。为此,响应具有以下set_body功能

web::http::http_response::set_body(const json::value &body_data)

最后,替换您的单行代码以创建空响应、设置标题和正文然后将其发送回的完整代码如下所示:

http_response response(status_codes::OK);
response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
response.set_body(jsonResponse);
request.reply(response);

请注意,在代码的最后一部分,我使用U宏来创建目标平台类型的字符串文字。您可以在C++ Rest SDK FAQ中找到有关此U宏的更多信息。

关于使用OPTIONHTTP 动词的预检请求,这些都是在这种情况下预期的。默认情况下,C++ REST SDK 包含这些请求的默认实现。可以在源代码中检查默认实现:

void details::http_listener_impl::handle_options(http_request message)
{
    http_response response(status_codes::OK);
    response.headers().add(U("Allow"), get_supported_methods());
    message.reply(response);
}

它基本上是返回一个200状态代码并添加您的服务器可以处理的支持方法列表。

如果您想覆盖默认实现,例如添加一些预检请求使用的特定标头,例如Access-Control-Allow-Methodsor Access-Control-Allow-Headers,您将需要添加一个特定的处理程序,就像您对GETandPOST请求使用:

web::http::experimental::listener::http_listener::support(const http::method &method, const std::function< void(http_request)> &handler)

不可能使用通用处理程序来处理OPTION请求:

web::http::experimental::listener::http_listener::support(const std::function<void(http_request)> &handler)

如果我们看一下源代码,我们不能使用通用处理程序的原因是,如果一个方法没有特定的处理程序并且使用的是OPTIONHTTP 动词(或 TRACE),则默认处理程序由C++ REST SDK 将被调用:

// Specific method handler takes priority over general.
const method &mtd = msg.method();
if(m_supported_methods.count(mtd))
{
    m_supported_methods[mtd](msg);
}
else if(mtd == methods::OPTIONS)
{
    handle_options(msg);
}
于 2016-08-16T09:37:42.203 回答