Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ gSOAP指定访问控制允许源_C++_Json_Web Services_Jsonp_Gsoap - Fatal编程技术网

C++ gSOAP指定访问控制允许源

C++ gSOAP指定访问控制允许源,c++,json,web-services,jsonp,gsoap,C++,Json,Web Services,Jsonp,Gsoap,我用gSOAP开发了开发者Web服务。其中一个方法返回json输出。但是,浏览器需要传递标头(访问控制允许源)。gSOAP是否支持在发送数据之前传递头 UPD: 找到解决方案。只需向http_响应函数添加一些代码: static int http_response(struct soap *soap, int status, size_t count) { /* some code goes here*/ if ((err = soap->fposthdr(soap, "Access-C

我用gSOAP开发了开发者Web服务。其中一个方法返回json输出。但是,浏览器需要传递标头(访问控制允许源)。gSOAP是否支持在发送数据之前传递头

UPD:

找到解决方案。只需向http_响应函数添加一些代码:

static int
http_response(struct soap *soap, int status, size_t count)
{
 /* some code goes here*/
if ((err = soap->fposthdr(soap, "Access-Control-Allow-Origin", "*")))
      return err;
  if ((err = soap->fposthdr(soap, "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, CONNECT")))
      return err;
  if ((err = soap->fposthdr(soap, "Access-Control-Allow-Headers", "X-Requested-With, Content-Type")))
      return err;
  if ((err = soap->fposthdr(soap, "Access-Control-Allow-Credentials", "true")))
      return err;
 /* some code goes here*/
}
/******************************************************************************/
#ifndef WITH_NOHTTP
#ifndef PALM_1
static int
http_post(struct soap *soap, const char *endpoint, const char *host, int port, const char *path, const char *action, size_t count)
{ register const char *s;
  register int err;
  ... the code of the function (except the return at the end) ...

  /* add more headers */
  if ((err = soap->fposthdr(soap, "ClientCertSubjectDN", "CN=IVR Production")))
    return err;

  return soap->fposthdr(soap, NULL, NULL);
}
#endif
#endif

我从来没有在我自己的代码中找到任何方法来实现这一点,但我可以通过修改stdsoap2.cpp来实现这一点,这似乎还可以,因为您无论如何都需要将其编译到代码中。我所做的是在http_post函数结束之前添加ClientCertSubjectDN头:

static int
http_response(struct soap *soap, int status, size_t count)
{
 /* some code goes here*/
if ((err = soap->fposthdr(soap, "Access-Control-Allow-Origin", "*")))
      return err;
  if ((err = soap->fposthdr(soap, "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, CONNECT")))
      return err;
  if ((err = soap->fposthdr(soap, "Access-Control-Allow-Headers", "X-Requested-With, Content-Type")))
      return err;
  if ((err = soap->fposthdr(soap, "Access-Control-Allow-Credentials", "true")))
      return err;
 /* some code goes here*/
}
/******************************************************************************/
#ifndef WITH_NOHTTP
#ifndef PALM_1
static int
http_post(struct soap *soap, const char *endpoint, const char *host, int port, const char *path, const char *action, size_t count)
{ register const char *s;
  register int err;
  ... the code of the function (except the return at the end) ...

  /* add more headers */
  if ((err = soap->fposthdr(soap, "ClientCertSubjectDN", "CN=IVR Production")))
    return err;

  return soap->fposthdr(soap, NULL, NULL);
}
#endif
#endif

我从来没有在我自己的代码中找到任何方法来实现这一点,但我可以通过修改stdsoap2.cpp来实现这一点,这似乎还可以,因为您无论如何都需要将其编译到代码中。我所做的是在http_post函数结束之前添加ClientCertSubjectDN头:

static int
http_response(struct soap *soap, int status, size_t count)
{
 /* some code goes here*/
if ((err = soap->fposthdr(soap, "Access-Control-Allow-Origin", "*")))
      return err;
  if ((err = soap->fposthdr(soap, "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, CONNECT")))
      return err;
  if ((err = soap->fposthdr(soap, "Access-Control-Allow-Headers", "X-Requested-With, Content-Type")))
      return err;
  if ((err = soap->fposthdr(soap, "Access-Control-Allow-Credentials", "true")))
      return err;
 /* some code goes here*/
}
/******************************************************************************/
#ifndef WITH_NOHTTP
#ifndef PALM_1
static int
http_post(struct soap *soap, const char *endpoint, const char *host, int port, const char *path, const char *action, size_t count)
{ register const char *s;
  register int err;
  ... the code of the function (except the return at the end) ...

  /* add more headers */
  if ((err = soap->fposthdr(soap, "ClientCertSubjectDN", "CN=IVR Production")))
    return err;

  return soap->fposthdr(soap, NULL, NULL);
}
#endif
#endif

实际上,在不更改gsoap代码的情况下,将自己的HTTP头添加到服务器响应中是一种非常麻烦的方法

  • 定义一个变量来存储原始回调钩子fposthdr的地址
  • 定义自己的fposthdr实现
  • 在上述变量中存储函数fposthdr的地址
  • 设置您自己的fposthdr实现
  • 在您自己的fposthdr实现中,使用fposthdr的原始实现来发出HTTP头
  • 代码示例:

    ...
    /* Declaration of own implementation of fposthdr */
    int my_fposthdr(struct soap *soap, const char *key, const char *val);
    /* Definition of variable to store address of original fposthdr */
    int (*org_fposthdr) (struct soap *soap, const char *key, const char *val) = NULL;
    ...
    struct soap *soap
    ...
    /* Store address of original function and set own implementation */
    org_fposthdr = soap->fposthdr;
    soap->fposthdr = my_fposthdr;
    ...
    int my_fposthdr(struct soap *soap, const char *key, const char *val) {
        int res;
        if (key == NULL) {
            res = org_fposthdr (soap, "Access-Control-Allow-Origin", "*");
            ...
        }
        /* Make sure to finally call original fposthdr with key and value being NULL pointers. */
        return org_fposthdr (soap, key, val);
    }
    ...
    
    备注
    上面my_fposthdr的示例实现在gsoap发出的所有“默认”HTTP头之后发出额外的HTTP头。gsoap对每个头调用一次回调钩子,并在末尾再次调用,其中key和val是空指针。因此,您可以让您的代码检测这些空指针并发出您自己的头。最后,您必须使用key和val作为空指针调用原始的fposthdr,否则将不会发送HTTP有效负载,即SOAP响应。

    实际上有一种很麻烦的方法,可以在不更改gsoap代码的情况下将您自己的HTTP头添加到服务器响应中

  • 定义一个变量来存储原始回调钩子fposthdr的地址
  • 定义自己的fposthdr实现
  • 在上述变量中存储函数fposthdr的地址
  • 设置您自己的fposthdr实现
  • 在您自己的fposthdr实现中,使用fposthdr的原始实现来发出HTTP头
  • 代码示例:

    ...
    /* Declaration of own implementation of fposthdr */
    int my_fposthdr(struct soap *soap, const char *key, const char *val);
    /* Definition of variable to store address of original fposthdr */
    int (*org_fposthdr) (struct soap *soap, const char *key, const char *val) = NULL;
    ...
    struct soap *soap
    ...
    /* Store address of original function and set own implementation */
    org_fposthdr = soap->fposthdr;
    soap->fposthdr = my_fposthdr;
    ...
    int my_fposthdr(struct soap *soap, const char *key, const char *val) {
        int res;
        if (key == NULL) {
            res = org_fposthdr (soap, "Access-Control-Allow-Origin", "*");
            ...
        }
        /* Make sure to finally call original fposthdr with key and value being NULL pointers. */
        return org_fposthdr (soap, key, val);
    }
    ...
    
    备注
    上面my_fposthdr的示例实现在gsoap发出的所有“默认”HTTP头之后发出额外的HTTP头。gsoap对每个头调用一次回调钩子,并在末尾再次调用,其中key和val是空指针。因此,您可以让您的代码检测这些空指针并发出您自己的头。最后,您必须调用原始fposthdr,其中key和val是空指针,否则不会发送HTTP有效负载,即SOAP响应。

    我使用的是GET方法。当我尝试以post方式发送标题时,它们会显示为返回文档的一部分。我使用的是GET方法。当我尝试以post方式发送标题时,它们会显示为返回文档的一部分。