gsoap使用筆記

gsoap入門

1 使用gsoap搭建web service

參考了參考文檔[2]中的介紹,有一點點修改。

1.1 編譯calc客戶端

  1. 根據wsdl文件生成gsoap用的頭文件
wsdl2h -o calc.h http://www.genivia.com/calc.wsdl
  1. 如果用我們自己搭建的webserver,需要修改頭文件中的如下代碼,
    把port字段修改為自己的webservice的url。
//gsoap ns2  service port:      http://websrv.cs.fsu.edu/~engelen/calcserver.cgi

改為

//gsoap ns2  service port:      http://localhost:9999/cgi-bin/calcserver.cgi
  1. 生成client proxy類的源代碼
soapcpp2 -j -CL -I/usr/share/gsoap/import calc.h

-j生成C++ proxy類,供client調用,-CL生成客戶端程序,-I指明gsoap相關頭文件路徑,這個根據gsoap安裝位置而定,例子中的路徑是ubuntu通過apt-get安裝的gsoap的路徑。

  1. 編寫client程序calcclient.cpp
#include "calc.nsmap"      // XML namespace mapping table (only needed once at the global level)
#include "soapcalcProxy.h" // the proxy class, also #includes "soapH.h" and "soapStub.h"
int main()
{
  calcProxy calc;
  double sum;
  if (calc.add(1.23, 4.56, sum) == SOAP_OK)
    std::cout << "Sum = " << sum << std::endl;
  else
    calc.soap_stream_fault(std::cerr);
  calc.destroy(); // same as: soap_destroy(calc.soap);
  soap_end(calc.soap);
}

調用proxy類calcProxyadd方法,傳遞了2個參數1.23 4.56,打印結果。

  1. 編譯
c++ -o calcclient calcclient.cpp soapC.cpp soapcalcProxy.cpp -lgsoap++

如果第二步沒修改port的話,這時候執行calcclient就能得到結果了,如果修改了port,我們需要自己搭建一個webservice來提供calc服務。

1.2 搭建服務器

1.2.1 編譯服務器端程序

  1. 與client第一步一樣,生成calc.h
wsdl2h -o calc.h http://www.genivia.com/calc.wsdl
  1. 生成server端的接口類
soapcpp2 -j -SL -I/usr/share/gsoap/import calc.h
  1. 編寫cgi程序calcserver.cpp,實現calc服務器端的接口
#include "calc.nsmap"        // XML namespace mapping table (only needed once at the global level)
#include "soapcalcService.h" // the service class, also #includes "soapH.h" and "soapStub.h"
int main()
{
  calcService calc(SOAP_XML_INDENT);
  if (calc.serve() != SOAP_OK)
    calc.soap_stream_fault(std::cerr);
  calc.destroy(); // same as: soap_destroy(calc.soap);
  soap_end(calc.soap);
}
int calcService::add(double a, double b, double &result)
{
  result = a + b;
  return SOAP_OK;
}
int calcService::sub(double a, double b, double &result)
{
  result = a - b;
  return SOAP_OK;
}
int calcService::mul(double a, double b, double &result)
{
  result = a * b;
  return SOAP_OK;
}
int calcService::div(double a, double b, double &result)
{
  if (b == 0.0)
  {
    char *msg = (char*)soap_malloc(this->soap, 1024);
    snprintf(msg, 1024, "Trying to divide %f by zero", a);
    return this->soap_senderfault(msg, NULL);
  }
  result = a / b;
  return SOAP_OK;
}
int calcService::pow(double a, double b, double &result)
{
  result = ::pow(a, b);
  // soap_errno is like errno, but compatible with Win32
  if (soap_errno == EDOM)
  {
    char *msg = (char*)soap_malloc(this->soap, 1024);
    snprintf(msg, 1024, "<error xmlns=\"http://tempuri.org/\">Can't take power of %f to %f</error>", a, b);
    return this->soap_senderfault("Power function domain error", msg);
  }
  return SOAP_OK;
}
  1. 編譯,測試
c++ -o calcserver calcserver.cpp soapC.cpp soapcalcService.cpp -lgsoap++
./calcserver < calc.add.req.xml

這時候calcserver是一個cgi程序,從標準輸入讀取soap命令,輸出到標準輸出上。接下來搭建一個webserver來提供cgi服務。

1.2.2 搭建cgi服務器

簡單起見,使用docker啟動一個apache來作為web server。

  1. 下載apache鏡像
docker pull httpd:2.4
  1. 修改http.conf文件,啟動apache對cgi的支持。
    取得http.conf文件,修改其中的cgi支持,打開下面內容的注釋即可
LoadModule cgi_module modules/mod_cgi.so
  • http.conf在docker container的路徑為:/usr/local/apache2/conf/httpd.conf
  • 這步不做的話,運行client的時候,會提示如下錯誤,即apache把cgi請求當成了下載文件的請求。
Error 200 fault: SOAP-ENV:Client[no subcode]
"Error 200"
Detail: [no detail]
  1. 啟動服務,在calcserverhttp.conf所在目錄輸入如下命令:
docker run -it --rm --name my-apache-app -p 9999:80 -v "$PWD":/usr/local/apache2/cgi-bin -v "$PWD"/http.conf:/usr/local/apache2/conf/httpd.conf -v /usr/lib/x86_64-linux-gnu/libgsoap++.so.4:/usr/lib/x86_64-linux-gnu/libgsoap++.so.4 httpd:2.4

1.3 其他

1.3.1 問題

gsoap也支持自己作為web服務器提供服務,只需要修改server代碼,即可,但我沒有試驗成功。

int main()
{
  calcService calc(SOAP_XML_INDENT);
  //if (calc.serve() != SOAP_OK) <--這里改為調用run
  if (calc.run(9999) != SOAP_OK)
    calc.soap_stream_fault(std::cerr);
  calc.destroy(); // same as: soap_destroy(calc.soap);
  soap_end(calc.soap);
}

這時運行服務器程序時始終提示:

setsockopt unset IPV6_V6ONLY failed in soap_bind()

1.3.2 curl調試webservice服務器

可以使用curl模擬客戶端請求一個soap服務[4],命令如下:

curl -v --header "content-type: application/soap+xml"  --data @calc.add.req.xml localhost:9999/cgi-bin/calcserver.cgi

2 編譯arm-linux版本的gsoap

2.1 下載gsoap源碼

http://liquidtelecom.dl.sourceforge.net/project/gsoap2/gSOAP/gsoap_2.8.33.zip

2.2 配置編譯環境

  1. 下載linaro toolchain,解壓到目錄/usr/local/linaro-multilib-2014.06-gcc4.9
  2. 設置PATH環境變量。export PATH=/usr/local/linaro-multilib-2014.06-gcc4.9:$PATH

2.3 config&make

2.3.1 生成makefile

gsoap-2.8$ ./configure --host=arm-linux-gnueabihf --prefix=/usr/local/arm-linux
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for arm-linux-gnueabihf-strip... arm-linux-gnueabihf-strip
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... arm-unknown-linux-gnueabihf
checking whether make sets $(MAKE)... (cached) yes
checking for arm-linux-gnueabihf-g++... arm-linux-gnueabihf-g++
checking whether the C++ compiler works... yes
checking for C++ compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... yes
...
  • --host=arm-linux-gnueabihf - 表明編譯的庫是運行在arm linux上,這個也是gcc編譯器的前綴。
  • --prefix=/usr/local/arm-linux - 表明變編譯好的庫的安裝路徑

2.3.2 手動生成wsdl.h文件

gsoap-2.8$ cd gsoap/wsdl
gsoap-2.8/gsoap/wsdl$ ../../target-amd64/bin/soapcpp2 -SC -pwsdl -I. -I../../gsoap/import ./wsdl.h

**  The gSOAP code generator for C and C++, soapcpp2 release 2.8.33
**  Copyright (C) 2000-2016, Robert van Engelen, Genivia Inc.
**  All Rights Reserved. This product is provided "as is", without any warranty.
**  The soapcpp2 tool and its generated software are released under the GPL.
**  ----------------------------------------------------------------------------
**  A commercial use license is available from Genivia Inc., contact@genivia.com
**  ----------------------------------------------------------------------------

soapcpp2: using both options -C and -S omits client/server code
Saving wsdlStub.h annotated copy of the source interface file
Saving wsdlH.h serialization functions to #include in projects
Saving xmime.nsmap namespace mapping table
Saving wsdlC.cpp serialization functions

Compilation successful 

../../target-amd64/bin/soapcpp2這個是你的host機器可以運行的soapcpp2

如果不做這步,在編譯過程中會提示如下錯誤,因為編譯時,生成的soapcpp2是只能在arm linux上運行的。

make[4]: Entering directory `/home/meng/git/gsoap-2.8/gsoap/wsdl'
../../gsoap/src/soapcpp2 -SC -pwsdl -I. -I../../gsoap/import ./wsdl.h
/bin/bash: ../../gsoap/src/soapcpp2: cannot execute binary file: Exec format error
make[4]: *** [wsdlC.cpp] Error 126

2.3.3 修改代碼

修改config.h文件,刪除最后一行的#define malloc rpl_malloc
否則編譯會出錯
`

2.3.4 修改makefile

修改gsoap/wsdl目錄下的Makefile,刪除下面2行(因為wsdl2h對其他庫有依賴,并且我們也不需要arm版本的wsdl2h程序):

wsdl2h$(EXEEXT): $(wsdl2h_OBJECTS) $(wsdl2h_DEPENDENCIES) $(EXTRA_wsdl2h_DEPENDENCIES) 
    #@rm -f wsdl2h$(EXEEXT)
    #$(AM_V_CXXLD)$(wsdl2h_LINK) $(wsdl2h_OBJECTS) $(wsdl2h_LDADD) $(LIBS)

2.3.5 編譯,安裝

gsoap-2.8$ make
gsoap-2.8$ make install

在目錄/usr/local/arm-linux下是編譯好的gsoap庫,注:我們沒有編譯出來arm linux版本的wadl2h

2.4 編譯arm-linux版calcserver.cgi

  • 編譯cgi版本的calcserver.cgi
calcserver$ arm-linux-gnueabihf-g++ -o calcserver.cgi calcserver.cpp soapC.cpp soapcalcService.cpp -lgsoap++ -DWITH_FASTCGI -L/usr/local/arm-linux/lib -I/usr/local/arm-linux/include
  • 編譯支持fastcgi版本的calcserver.cgi[5]
calcserver$ arm-linux-gnueabihf-g++ -o calcserver.cgi calcserver.cpp soapC.cpp soapcalcService.cpp /gsoap-source-path/gsoap/stdsoap2.cpp -DWITH_FASTCGI  -I/usr/local/arm-linux/include -I/fcgi-lib-path/fcgi/include -L/fcgi-lib-path/fcgi/usr/lib -lfcgi
  • fcgi-lib-path - 這個指向arm linux版的fcgi庫
  • gsoap-source-path - 這個指向gsoap的源碼路徑`
  • 我們編譯stdsoap2.cpp這個文件,而不去鏈接libsoap++.a
  • 定義WITH_FASTCGI

2.5 配置lighttpd

如果編譯的是cgi版本,直接把編譯好的calcserver.cgi放到cig-bin目錄下即可。

如果是fastcgi版本,需要修改lighttpd配置文件,增加如下內容(前提lighttpd已經配置好支持fastcgi了):

fastcgi.server = (
  "calcserver.cgi" =>
  (( "socket" => "/tmp/fcgi.socket",
     "check-local" => "disable",
     "bin-path" => "/webSvr/web/cgi-bin/calcserver.cgi",
     "max-procs" => 1,
  ))
)

3 參考文檔

  1. Apache Tutorial: Dynamic Content with CGI
  2. Getting Started with gSOAP
  3. gSoap missing stdsoap2.cpp, undefined reference to `soap_peek_element'
  4. Curl實現web serivce調試調用
  5. 19.32 FastCGI Support - genivia.com
  6. linux設備上的Onvif 實現4:成功編譯gsoap 2.8.15 - blog.csdn
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容