mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-16 07:30:42 +01:00
Dep/gsoap
* Added httpget and httppost plugins * Compile with ssl support
This commit is contained in:
@@ -18,6 +18,14 @@ target_include_directories(gsoap
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
target_compile_definitions(gsoap
|
||||
PUBLIC
|
||||
-DWITH_OPENSSL)
|
||||
|
||||
target_link_libraries(gsoap
|
||||
PUBLIC
|
||||
openssl)
|
||||
|
||||
set_target_properties(gsoap
|
||||
PROPERTIES
|
||||
FOLDER
|
||||
|
||||
336
dep/gsoap/httpget.cpp
Normal file
336
dep/gsoap/httpget.cpp
Normal file
@@ -0,0 +1,336 @@
|
||||
/*
|
||||
httpget.c
|
||||
|
||||
gSOAP HTTP GET plugin.
|
||||
|
||||
See instructions below.
|
||||
|
||||
gSOAP XML Web services tools
|
||||
Copyright (C) 2000-2008, Robert van Engelen, Genivia Inc., All Rights Reserved.
|
||||
This part of the software is released under ONE of the following licenses:
|
||||
GPL, the gSOAP public license, OR Genivia's license for commercial use.
|
||||
--------------------------------------------------------------------------------
|
||||
gSOAP public license.
|
||||
|
||||
The contents of this file are subject to the gSOAP Public License Version 1.3
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at
|
||||
http://www.cs.fsu.edu/~engelen/soaplicense.html
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
for the specific language governing rights and limitations under the License.
|
||||
|
||||
The Initial Developer of the Original Code is Robert A. van Engelen.
|
||||
Copyright (C) 2000-2008 Robert A. van Engelen, Genivia inc. All Rights Reserved.
|
||||
--------------------------------------------------------------------------------
|
||||
GPL license.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify it under
|
||||
the terms of the GNU General Public License as published by the Free Software
|
||||
Foundation; either version 2 of the License, or (at your option) any later
|
||||
version.
|
||||
|
||||
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along with
|
||||
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
||||
Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Author contact information:
|
||||
engelen@genivia.com / engelen@acm.org
|
||||
|
||||
This program is released under the GPL with the additional exemption that
|
||||
compiling, linking, and/or using OpenSSL is allowed.
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
Compile & link with stand-alone gSOAP server for HTTP GET support.
|
||||
Compile & link with gSOAP clients for client-side HTTP GET requests.
|
||||
|
||||
Usage (server side):
|
||||
|
||||
struct soap soap;
|
||||
soap_init(&soap);
|
||||
soap_register_plugin_arg(&soap, http_get, http_get_handler);
|
||||
...
|
||||
... = soap_copy(&soap); // copies plugin too but not its data: plugin data is shared since fcopy is not set
|
||||
...
|
||||
soap_done(&soap); // detach plugin (calls plugin->fdelete)
|
||||
|
||||
You need to define a HTTP GET handling function at the server-side:
|
||||
|
||||
int http_get_handler(struct soap*)
|
||||
|
||||
which will be called from the plugin upon an HTTP GET request.
|
||||
The function should return an error code or SOAP_OK;
|
||||
This function should produce a valid HTTP response, for example:
|
||||
|
||||
soap_response(soap, SOAP_HTML); // use this to return HTML ...
|
||||
soap_response(soap, SOAP_OK); // ... or use this to return a SOAP message
|
||||
...
|
||||
soap_send(soap, "<HTML>...</HTML>"); // example HTML
|
||||
...
|
||||
soap_end_send(soap);
|
||||
|
||||
To get query string key-value pairs within a service routine, use:
|
||||
|
||||
char *s;
|
||||
s = query(soap);
|
||||
while (s)
|
||||
{ char *key = query(soap, &s);
|
||||
char *val = query(soap, &s);
|
||||
...
|
||||
}
|
||||
|
||||
This will garble soap->path, which contains the HTTP path of the form:
|
||||
/path?query
|
||||
where path and/or ?query may be absent. The path info is obtained from
|
||||
the HTTP request URL: http://domain/path?query
|
||||
The URL should not exceed the length of SOAP_TAGLEN. Adjust SOAP_TAGLEN
|
||||
in stdsoap2.h if necessary.
|
||||
|
||||
Usage (client side):
|
||||
|
||||
For SOAP calls, declare a one-way response message in the header file,
|
||||
for example:
|
||||
int ns__methodResponse(... params ..., void);
|
||||
The params will hold the return values returned by the server's SOAP
|
||||
response message.
|
||||
|
||||
Client code:
|
||||
|
||||
struct soap soap;
|
||||
soap_init(&soap);
|
||||
soap_register_plugin(&soap, http_get); // register plugin
|
||||
...
|
||||
if (soap_get_connect(&soap, endpoint, action))
|
||||
... connect error ...
|
||||
else if (soap_recv_ns__methodResponse(&soap, ... params ...))
|
||||
... error ...
|
||||
else
|
||||
... ok ...
|
||||
soap_destroy(&soap);
|
||||
soap_end(&soap);
|
||||
soap_done(&soap);
|
||||
|
||||
Note that the endpoint URL may contain a query string with key-value
|
||||
pairs to pass to the server, e.g.: http://domain/path?key=val&key=val
|
||||
|
||||
To use general HTTP GET, for example to retrieve an HTML document, use:
|
||||
|
||||
struct soap soap;
|
||||
char *buf = NULL;
|
||||
size_t len;
|
||||
soap_init(&soap);
|
||||
soap_register_plugin(&soap, http_get); // register plugin
|
||||
if (soap_get_connect(&soap, endpoint, action)
|
||||
|| soap_begin_recv(&soap))
|
||||
... connect/recv error ...
|
||||
else
|
||||
buf = soap_get_http_body(&soap, &len);
|
||||
soap_end_recv(&soap);
|
||||
... process data in buf[0..len-1]
|
||||
soap_destroy(&soap);
|
||||
soap_end(&soap);
|
||||
soap_done(&soap);
|
||||
|
||||
The soap_get_http_body() function above moves HTTP body content into a
|
||||
buffer.
|
||||
*/
|
||||
|
||||
#include "httpget.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
const char http_get_id[13] = HTTP_GET_ID;
|
||||
|
||||
static int http_get_init(struct soap *soap, struct http_get_data *data, int (*handler)(struct soap*));
|
||||
static void http_get_delete(struct soap *soap, struct soap_plugin *p);
|
||||
static int http_get_parse(struct soap *soap);
|
||||
|
||||
int http_get(struct soap *soap, struct soap_plugin *p, void *arg)
|
||||
{ p->id = http_get_id;
|
||||
p->data = (void*)malloc(sizeof(struct http_get_data));
|
||||
/* p->fcopy = http_get_copy; obsolete, see note with http_get_copy() */
|
||||
p->fdelete = http_get_delete;
|
||||
if (p->data)
|
||||
if (http_get_init(soap, (struct http_get_data*)p->data, (int (*)(struct soap*))arg))
|
||||
{ free(p->data); /* error: could not init */
|
||||
return SOAP_EOM; /* return error */
|
||||
}
|
||||
return SOAP_OK;
|
||||
}
|
||||
|
||||
static int http_get_init(struct soap *soap, struct http_get_data *data, int (*handler)(struct soap*))
|
||||
{ data->fparse = soap->fparse; /* save old HTTP header parser callback */
|
||||
data->fget = handler;
|
||||
data->stat_get = 0;
|
||||
data->stat_post = 0;
|
||||
data->stat_fail = 0;
|
||||
memset(data->min, 0, sizeof(data->min));
|
||||
memset(data->hour, 0, sizeof(data->hour));
|
||||
memset(data->day, 0, sizeof(data->day));
|
||||
soap->fparse = http_get_parse; /* replace HTTP header parser callback with ours */
|
||||
return SOAP_OK;
|
||||
}
|
||||
|
||||
/* We will share the plugin data among all soap copies created with soap_copy(), so we don't have to define this
|
||||
static int http_get_copy(struct soap *soap, struct soap_plugin *dst, struct soap_plugin *src)
|
||||
{ *dst = *src;
|
||||
return SOAP_OK;
|
||||
}
|
||||
*/
|
||||
|
||||
static void http_get_delete(struct soap *soap, struct soap_plugin *p)
|
||||
{ free(p->data); /* free allocated plugin data (this function is not called for shared plugin data, but only when the final soap_done() is invoked on the original soap struct) */
|
||||
}
|
||||
|
||||
static int http_get_parse(struct soap *soap)
|
||||
{
|
||||
#ifndef WITH_LEAN
|
||||
time_t t;
|
||||
struct tm T, *pT;
|
||||
#endif
|
||||
struct http_get_data *data = (struct http_get_data*)soap_lookup_plugin(soap, http_get_id);
|
||||
if (!data)
|
||||
return SOAP_PLUGIN_ERROR;
|
||||
#ifndef WITH_LEAN
|
||||
time(&t);
|
||||
#ifdef HAVE_LOCALTIME_R
|
||||
pT = localtime_r(&t, &T);
|
||||
#else
|
||||
pT = localtime(&t);
|
||||
#endif
|
||||
/* updates should be in mutex, but we don't mind some inaccuracy in the count to preserve performance */
|
||||
data->day[pT->tm_yday]++;
|
||||
data->day[(pT->tm_yday + 1) % 365] = 0;
|
||||
data->hour[pT->tm_hour]++;
|
||||
data->hour[(pT->tm_hour + 1) % 24] = 0;
|
||||
data->min[pT->tm_min]++;
|
||||
data->min[(pT->tm_min + 1) % 60] = 0;
|
||||
#endif
|
||||
soap->error = data->fparse(soap); /* parse HTTP header */
|
||||
if (soap->error == SOAP_OK)
|
||||
{ /* update should be in mutex, but we don't mind some inaccuracy in the count */
|
||||
data->stat_post++;
|
||||
}
|
||||
else if (soap->error == SOAP_GET_METHOD && data->fget)
|
||||
{ soap->error = SOAP_OK;
|
||||
if ((soap->error = data->fget(soap))) /* call user-defined HTTP GET handler */
|
||||
{ /* update should be in mutex, but we don't mind some inaccuracy in the count */
|
||||
data->stat_fail++;
|
||||
return soap->error;
|
||||
}
|
||||
/* update should be in mutex, but we don't mind some inaccuracy in the count */
|
||||
data->stat_get++;
|
||||
return SOAP_STOP; /* stop processing the request and do not return SOAP Fault */
|
||||
}
|
||||
else
|
||||
{ /* update should be in mutex, but we don't mind some inaccuracy in the count */
|
||||
data->stat_fail++;
|
||||
}
|
||||
return soap->error;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int soap_get_connect(struct soap *soap, const char *endpoint, const char *action)
|
||||
{ return soap_connect_command(soap, SOAP_GET, endpoint, action);
|
||||
}
|
||||
|
||||
char *query(struct soap *soap)
|
||||
{ return strchr(soap->path, '?');
|
||||
}
|
||||
|
||||
char *query_key(struct soap *soap, char **s)
|
||||
{ char *t = *s;
|
||||
if (t && *t)
|
||||
{ *s = (char*)soap_decode_string(t, strlen(t), t + 1);
|
||||
return t;
|
||||
}
|
||||
return *s = NULL;
|
||||
}
|
||||
|
||||
char *query_val(struct soap *soap, char **s)
|
||||
{ char *t = *s;
|
||||
if (t && *t == '=')
|
||||
{ *s = (char*)soap_decode_string(t, strlen(t), t + 1);
|
||||
return t;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int soap_encode_string(const char *s, char *t, size_t len)
|
||||
{ register int c;
|
||||
register size_t n = len;
|
||||
while ((c = *s++) && n-- > 1)
|
||||
{ if (c == ' ')
|
||||
*t++ = '+';
|
||||
else if (c == '!'
|
||||
|| c == '$'
|
||||
|| (c >= '(' && c <= '.')
|
||||
|| (c >= '0' && c <= '9')
|
||||
|| (c >= 'A' && c <= 'Z')
|
||||
|| c == '_'
|
||||
|| (c >= 'a' && c <= 'z'))
|
||||
*t++ = (char)c;
|
||||
else if (n > 2)
|
||||
{ *t++ = '%';
|
||||
*t++ = (char)((c >> 4) + (c > 159 ? '7' : '0'));
|
||||
c &= 0xF;
|
||||
*t++ = (char)(c + (c > 9 ? '7' : '0'));
|
||||
n -= 2;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
*t = '\0';
|
||||
return len - n;
|
||||
}
|
||||
|
||||
const char* soap_decode_string(char *buf, size_t len, const char *val)
|
||||
{ const char *s;
|
||||
char *t;
|
||||
for (s = val; *s; s++)
|
||||
if (*s != ' ' && *s != '=')
|
||||
break;
|
||||
if (*s == '"')
|
||||
{ t = buf;
|
||||
s++;
|
||||
while (*s && *s != '"' && --len)
|
||||
*t++ = *s++;
|
||||
*t = '\0';
|
||||
do s++;
|
||||
while (*s && *s != '&' && *s != '=');
|
||||
}
|
||||
else
|
||||
{ t = buf;
|
||||
while (*s && *s != '&' && *s != '=' && --len)
|
||||
{ switch (*s)
|
||||
{ case '+':
|
||||
*t++ = ' ';
|
||||
case ' ':
|
||||
s++;
|
||||
break;
|
||||
case '%':
|
||||
*t++ = ((s[1] >= 'A' ? (s[1]&0x7) + 9 : s[1] - '0') << 4) + (s[2] >= 'A' ? (s[2]&0x7) + 9 : s[2] - '0');
|
||||
s += 3;
|
||||
break;
|
||||
default:
|
||||
*t++ = *s++;
|
||||
}
|
||||
}
|
||||
*t = '\0';
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
88
dep/gsoap/httpget.h
Normal file
88
dep/gsoap/httpget.h
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
httpget.h
|
||||
|
||||
gSOAP HTTP GET plugin.
|
||||
|
||||
See httpget.c for usage instructions.
|
||||
|
||||
gSOAP XML Web services tools
|
||||
Copyright (C) 2000-2008, Robert van Engelen, Genivia Inc., All Rights Reserved.
|
||||
This part of the software is released under ONE of the following licenses:
|
||||
GPL, the gSOAP public license, OR Genivia's license for commercial use.
|
||||
--------------------------------------------------------------------------------
|
||||
gSOAP public license.
|
||||
|
||||
The contents of this file are subject to the gSOAP Public License Version 1.3
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at
|
||||
http://www.cs.fsu.edu/~engelen/soaplicense.html
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
for the specific language governing rights and limitations under the License.
|
||||
|
||||
The Initial Developer of the Original Code is Robert A. van Engelen.
|
||||
Copyright (C) 2000-2008 Robert A. van Engelen, Genivia inc. All Rights Reserved.
|
||||
--------------------------------------------------------------------------------
|
||||
GPL license.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify it under
|
||||
the terms of the GNU General Public License as published by the Free Software
|
||||
Foundation; either version 2 of the License, or (at your option) any later
|
||||
version.
|
||||
|
||||
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along with
|
||||
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
||||
Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Author contact information:
|
||||
engelen@genivia.com / engelen@acm.org
|
||||
|
||||
This program is released under the GPL with the additional exemption that
|
||||
compiling, linking, and/or using OpenSSL is allowed.
|
||||
--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#ifndef HTTPGET_H
|
||||
#define HTTPGET_H
|
||||
|
||||
#include "stdsoap2.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define HTTP_GET_ID "HTTP-GET-1.1" /* plugin identification */
|
||||
|
||||
extern const char http_get_id[];
|
||||
|
||||
/* This is the local plugin data shared among all copies of the soap struct: */
|
||||
struct http_get_data
|
||||
{ int (*fparse)(struct soap*); /* to save and call the internal HTTP header parser */
|
||||
int (*fget)(struct soap*); /* user-defined server-side HTTP GET handler */
|
||||
size_t stat_get; /* HTTP GET usage statistics */
|
||||
size_t stat_post; /* HTTP POST usage statistics */
|
||||
size_t stat_fail; /* HTTP failure statistics */
|
||||
size_t min[60]; /* Hits by the minute */
|
||||
size_t hour[24]; /* Hits by the hour */
|
||||
size_t day[366]; /* Hits by day */
|
||||
};
|
||||
|
||||
int http_get(struct soap*, struct soap_plugin*, void*);
|
||||
int soap_get_connect(struct soap*, const char*, const char*);
|
||||
|
||||
char *query(struct soap*);
|
||||
char *query_key(struct soap*, char**);
|
||||
char *query_val(struct soap*, char**);
|
||||
|
||||
int soap_encode_string(const char*, char*, size_t);
|
||||
const char* soap_decode_string(char*, size_t, const char*);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
312
dep/gsoap/httppost.cpp
Normal file
312
dep/gsoap/httppost.cpp
Normal file
@@ -0,0 +1,312 @@
|
||||
/*
|
||||
httppost.c
|
||||
|
||||
gSOAP HTTP POST plugin for non-SOAP payloads.
|
||||
|
||||
See instructions below.
|
||||
|
||||
Revisions:
|
||||
register multiple POST content handlers, each for a content type
|
||||
|
||||
Note: multipart/related and multipart/form-data are already handled in gSOAP.
|
||||
|
||||
gSOAP XML Web services tools
|
||||
Copyright (C) 2004-2005, Robert van Engelen, Genivia, Inc. All Rights Reserved.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
gSOAP public license.
|
||||
|
||||
The contents of this file are subject to the gSOAP Public License Version 1.3
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at
|
||||
http://www.cs.fsu.edu/~engelen/soaplicense.html
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
for the specific language governing rights and limitations under the License.
|
||||
|
||||
The Initial Developer of the Original Code is Robert A. van Engelen.
|
||||
Copyright (C) 2000-2004 Robert A. van Engelen, Genivia inc. All Rights Reserved.
|
||||
--------------------------------------------------------------------------------
|
||||
GPL license.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify it under
|
||||
the terms of the GNU General Public License as published by the Free Software
|
||||
Foundation; either version 2 of the License, or (at your option) any later
|
||||
version.
|
||||
|
||||
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along with
|
||||
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
||||
Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Author contact information:
|
||||
engelen@genivia.com / engelen@acm.org
|
||||
|
||||
This program is released under the GPL with the additional exemption that
|
||||
compiling, linking, and/or using OpenSSL is allowed.
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
Compile & link with stand-alone gSOAP server.
|
||||
|
||||
Usage (server side):
|
||||
|
||||
Define a NULL-terminated array of type-handler pairs, each with a media
|
||||
type and a the handler function:
|
||||
|
||||
struct http_post_handlers my_handlers[] =
|
||||
{ { "image/jpg", jpeg_handler },
|
||||
{ "image/ *", image_handler },
|
||||
{ "text/html", html_handler },
|
||||
{ "text/ *", text_handler },
|
||||
{ "text/ *;*", text_handler },
|
||||
{ "POST", generic_POST_handler },
|
||||
{ "PUT", generic_PUT_handler },
|
||||
{ "DELETE", generic_DELETE_handler },
|
||||
{ NULL }
|
||||
};
|
||||
Note that '*' can be used as a wildcard and some media types may have
|
||||
optional parameters (after ';').
|
||||
|
||||
Register the plugin and the handlers:
|
||||
|
||||
struct soap soap;
|
||||
soap_init(&soap);
|
||||
soap_register_plugin_arg(&soap, http_post, my_handlers);
|
||||
...
|
||||
... = soap_copy(&soap); // copies plugin too but not its data: plugin data is shared since fcopy is not set
|
||||
...
|
||||
soap_done(&soap); // detach plugin (calls plugin->fdelete)
|
||||
|
||||
A POST handler function is triggered by the media type in the
|
||||
http_post_handlers table. Use http_copy_body() as below to retrieve
|
||||
HTTP POST body data:
|
||||
|
||||
int image_handler(struct soap *soap)
|
||||
{ const char *buf;
|
||||
size_t len;
|
||||
// if necessary, check type in soap->http_content
|
||||
if (soap->http_content && !soap_tag_cmp(soap->http_content, "image/gif")
|
||||
return 404;
|
||||
if (!(buf = soap_get_http_body(soap, &len))
|
||||
return soap->error;
|
||||
soap_end_recv(soap);
|
||||
// ... process image in buf[0..len-1]
|
||||
// reply with empty HTTP OK response:
|
||||
return soap_send_empty_response(soap, SOAP_OK);
|
||||
}
|
||||
|
||||
This function should also produce a valid HTTP response, for example:
|
||||
|
||||
if (we want to return HTML)
|
||||
soap_response(soap, SOAP_HTML); // use this to return HTML...
|
||||
else
|
||||
{ soap->http_content = "image/jpeg"; // a jpeg image
|
||||
soap_response(soap, SOAP_FILE); // SOAP_FILE sets custom http content
|
||||
}
|
||||
...
|
||||
soap_send(soap, "<HTML>...</HTML>"); // example HTML
|
||||
...
|
||||
soap_end_send(soap);
|
||||
soap_closesock(soap); // close, but keep open only with HTTP keep-alive
|
||||
|
||||
The soap_send(soap, char*) and soap_send_raw(soap, char*, size_t) can
|
||||
be used to return content from server.
|
||||
|
||||
Usage (client side):
|
||||
|
||||
char *buf; // to hold the HTTP response body data
|
||||
size_t len;
|
||||
...
|
||||
if (soap_post_connect(soap, "URL", "SOAP action or NULL", "media type")
|
||||
|| soap_send(soap, ...)
|
||||
|| soap_end_send(soap))
|
||||
... error ...
|
||||
if (soap_begin_recv(&soap)
|
||||
|| soap_http_body(&soap, &buf, &len)
|
||||
|| soap_end_recv(&soap))
|
||||
... error ...
|
||||
// ... use buf[0..len-1]
|
||||
soap_closesock(soap); // close, but keep open only with HTTP keep-alive
|
||||
soap_end(soap); // also deletes buf content
|
||||
|
||||
The soap_send(soap, char*) and soap_send_raw(soap, char*, size_t) can
|
||||
be used to send content to the server as shown above.
|
||||
|
||||
*/
|
||||
|
||||
#include "httppost.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
const char http_post_id[14] = HTTP_POST_ID;
|
||||
|
||||
static int http_post_init(struct soap *soap, struct http_post_data *data, struct http_post_handlers *handlers);
|
||||
static void http_post_delete(struct soap *soap, struct soap_plugin *p);
|
||||
static int http_post_parse_header(struct soap *soap, const char*, const char*);
|
||||
static http_handler_t http_lookup_handler(struct soap *soap, const char *type, struct http_post_data *data);
|
||||
|
||||
static int http_fput(struct soap *soap);
|
||||
static int http_fdel(struct soap *soap);
|
||||
|
||||
int http_post(struct soap *soap, struct soap_plugin *p, void *arg)
|
||||
{ p->id = http_post_id;
|
||||
p->data = (void*)malloc(sizeof(struct http_post_data));
|
||||
p->fdelete = http_post_delete;
|
||||
if (p->data)
|
||||
if (http_post_init(soap, (struct http_post_data*)p->data, (struct http_post_handlers *)arg))
|
||||
{ free(p->data); /* error: could not init */
|
||||
return SOAP_EOM; /* return error */
|
||||
}
|
||||
return SOAP_OK;
|
||||
}
|
||||
|
||||
static int http_post_init(struct soap *soap, struct http_post_data *data, struct http_post_handlers *handlers)
|
||||
{ data->fparsehdr = soap->fparsehdr; /* save old HTTP header parser callback */
|
||||
soap->fparsehdr = http_post_parse_header; /* replace HTTP header parser callback with ours */
|
||||
data->fput = soap->fput;
|
||||
soap->fput = http_fput;
|
||||
data->fdel = soap->fdel;
|
||||
soap->fdel = http_fdel;
|
||||
data->handlers = handlers;
|
||||
return SOAP_OK;
|
||||
}
|
||||
|
||||
static void http_post_delete(struct soap *soap, struct soap_plugin *p)
|
||||
{ soap->fparsehdr = ((struct http_post_data*)p->data)->fparsehdr;
|
||||
soap->fput = ((struct http_post_data*)p->data)->fput;
|
||||
soap->fdel = ((struct http_post_data*)p->data)->fdel;
|
||||
free(p->data); /* free allocated plugin data (this function is not called for shared plugin data, but only when the final soap_done() is invoked on the original soap struct) */
|
||||
}
|
||||
|
||||
static int http_post_parse_header(struct soap *soap, const char *key, const char *val)
|
||||
{ struct http_post_data *data = (struct http_post_data*)soap_lookup_plugin(soap, http_post_id);
|
||||
if (!data)
|
||||
return SOAP_PLUGIN_ERROR;
|
||||
soap->error = data->fparsehdr(soap, key, val); /* parse HTTP header */
|
||||
if (soap->error == SOAP_OK)
|
||||
{ if (!soap_tag_cmp(key, "Content-Type"))
|
||||
{ /* check content type */
|
||||
soap->fform = http_lookup_handler(soap, val, data);
|
||||
if (!soap->fform)
|
||||
soap->fform = http_lookup_handler(soap, "POST", data);
|
||||
if (soap->fform)
|
||||
return SOAP_FORM; /* calls soap->fform after processing the HTTP header */
|
||||
}
|
||||
}
|
||||
return soap->error;
|
||||
}
|
||||
|
||||
static http_handler_t http_lookup_handler(struct soap *soap, const char *type, struct http_post_data *data)
|
||||
{ struct http_post_handlers *p;
|
||||
for (p = data->handlers; p && p->type; p++)
|
||||
{ if (!soap_tag_cmp(type, p->type))
|
||||
{ DBGLOG(TEST,SOAP_MESSAGE(fdebug, "Found HTTP POST plugin handler for '%s'\n", type));
|
||||
return p->handler;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int http_fput(struct soap *soap)
|
||||
{ struct http_post_data *data = (struct http_post_data*)soap_lookup_plugin(soap, http_post_id);
|
||||
if (!data)
|
||||
return SOAP_PLUGIN_ERROR;
|
||||
soap->fform = http_lookup_handler(soap, "PUT", data);
|
||||
if (soap->fform)
|
||||
return SOAP_FORM;
|
||||
return 405;
|
||||
}
|
||||
|
||||
static int http_fdel(struct soap *soap)
|
||||
{ struct http_post_data *data = (struct http_post_data*)soap_lookup_plugin(soap, http_post_id);
|
||||
if (!data)
|
||||
return SOAP_PLUGIN_ERROR;
|
||||
soap->fform = http_lookup_handler(soap, "DELETE", data);
|
||||
if (soap->fform)
|
||||
return SOAP_STOP;
|
||||
return 405;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int soap_post_connect(struct soap *soap, const char *endpoint, const char *action, const char *type)
|
||||
{ if ((soap->omode & SOAP_IO) != SOAP_IO_CHUNK) /* not chunking: store in buf */
|
||||
soap->omode = (soap->omode & ~SOAP_IO) | SOAP_IO_STORE;
|
||||
soap->http_content = type;
|
||||
return soap_connect_command(soap, SOAP_POST_FILE, endpoint, action);
|
||||
}
|
||||
|
||||
int soap_put_connect(struct soap *soap, const char *endpoint, const char *action, const char *type)
|
||||
{ if ((soap->omode & SOAP_IO) != SOAP_IO_CHUNK) /* not chunking: store in buf */
|
||||
soap->omode = (soap->omode & ~SOAP_IO) | SOAP_IO_STORE;
|
||||
soap->http_content = type;
|
||||
return soap_connect_command(soap, SOAP_PUT, endpoint, action);
|
||||
}
|
||||
|
||||
int soap_delete_connect(struct soap *soap, const char *endpoint, const char *action, const char *type)
|
||||
{ if (soap_connect_command(soap, SOAP_DEL, endpoint, action)
|
||||
|| soap_end_send(soap))
|
||||
return soap_closesock(soap);
|
||||
return soap_closesock(soap);
|
||||
}
|
||||
|
||||
int soap_http_body(struct soap *soap, char **buf, size_t *len)
|
||||
{ char *s = *buf = NULL;
|
||||
*len = 0;
|
||||
/* It is unlikely chunked and/or compressed POST messages are sent by browsers, but we need to handle them */
|
||||
if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK
|
||||
#ifdef WITH_ZLIB
|
||||
|| soap->zlib_in != SOAP_ZLIB_NONE
|
||||
#endif
|
||||
)
|
||||
{ register size_t k;
|
||||
soap_wchar c = EOF;
|
||||
soap->labidx = 0;
|
||||
do
|
||||
{ if (soap_append_lab(soap, NULL, 0))
|
||||
return soap->error;
|
||||
s = soap->labbuf + soap->labidx;
|
||||
k = soap->lablen - soap->labidx;
|
||||
soap->labidx = soap->lablen;
|
||||
while (k--)
|
||||
{ if ((c = soap_getchar(soap)) == (int)EOF)
|
||||
break;
|
||||
*s++ = c;
|
||||
}
|
||||
} while (c != (int)EOF);
|
||||
*len = soap->lablen - k - 1;
|
||||
*buf = (char*)soap_malloc(soap, *len + 1);
|
||||
memcpy(*buf, soap->labbuf, *len + 1);
|
||||
}
|
||||
else
|
||||
{ if (soap->length)
|
||||
{ s = (char*)soap_malloc(soap, soap->length + 1);
|
||||
if (s)
|
||||
{ char *t = s;
|
||||
size_t i;
|
||||
for (i = soap->length; i; i--)
|
||||
{ soap_wchar c;
|
||||
if ((c = soap_getchar(soap)) == (int)EOF)
|
||||
return soap->error = SOAP_EOF;
|
||||
*t++ = c;
|
||||
}
|
||||
*t = '\0';
|
||||
}
|
||||
}
|
||||
*buf = s;
|
||||
*len = soap->length;
|
||||
}
|
||||
return soap_end_recv(soap);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
98
dep/gsoap/httppost.h
Normal file
98
dep/gsoap/httppost.h
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
httppost.h
|
||||
|
||||
gSOAP HTTP POST plugin for non-SOAP payloads.
|
||||
|
||||
See httppost.c for instructions.
|
||||
|
||||
Revisions:
|
||||
register multiple POST content handlers, each for a content type
|
||||
|
||||
gSOAP XML Web services tools
|
||||
Copyright (C) 2004-2009, Robert van Engelen, Genivia, Inc. All Rights Reserved.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
gSOAP public license.
|
||||
|
||||
The contents of this file are subject to the gSOAP Public License Version 1.3
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at
|
||||
http://www.cs.fsu.edu/~engelen/soaplicense.html
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
for the specific language governing rights and limitations under the License.
|
||||
|
||||
The Initial Developer of the Original Code is Robert A. van Engelen.
|
||||
Copyright (C) 2000-2004 Robert A. van Engelen, Genivia inc. All Rights Reserved.
|
||||
--------------------------------------------------------------------------------
|
||||
GPL license.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify it under
|
||||
the terms of the GNU General Public License as published by the Free Software
|
||||
Foundation; either version 2 of the License, or (at your option) any later
|
||||
version.
|
||||
|
||||
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along with
|
||||
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
||||
Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Author contact information:
|
||||
engelen@genivia.com / engelen@acm.org
|
||||
|
||||
This program is released under the GPL with the additional exemption that
|
||||
compiling, linking, and/or using OpenSSL is allowed.
|
||||
--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#ifndef HTTPPOST_H
|
||||
#define HTTPPOST_H
|
||||
|
||||
#include "stdsoap2.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define HTTP_POST_ID "HTTP-POST-1.1" /* plugin identification */
|
||||
|
||||
extern const char http_post_id[];
|
||||
|
||||
typedef int (*http_handler_t)(struct soap*);
|
||||
|
||||
struct http_post_handlers
|
||||
{ const char *type;
|
||||
http_handler_t handler;
|
||||
};
|
||||
|
||||
/* This is the local plugin data shared among all copies of the soap struct: */
|
||||
struct http_post_data
|
||||
{ int (*fparsehdr)(struct soap*, const char*, const char*); /* to save and call the internal HTTP header parser */
|
||||
int (*fput)(struct soap*); /* to save */
|
||||
int (*fdel)(struct soap*); /* to save */
|
||||
struct http_post_handlers *handlers; /* the server-side POST content type handlers */
|
||||
};
|
||||
|
||||
/* the http post plugin, note: argument should be a table of type-handler pairs */
|
||||
int http_post(struct soap*, struct soap_plugin*, void*);
|
||||
|
||||
/* a function to send HTTP POST, should be followd by a soap_send to transmit and soap_get_http_body to retrieve the HTTP body returned into an internal buffer */
|
||||
int soap_post_connect(struct soap*, const char *endpoint, const char *action, const char *type);
|
||||
|
||||
/* a function to send HTTP PUT, should be followed by a soap_send to transmit data */
|
||||
int soap_put_connect(struct soap*, const char *endpoint, const char *action, const char *type);
|
||||
|
||||
/* a function to send HTTP DELETE */
|
||||
int soap_delete_connect(struct soap*, const char *endpoint, const char *action, const char *type);
|
||||
|
||||
/* a function to retrieve the HTTP body into an internal buffer */
|
||||
int soap_http_body(struct soap*, char **buf, size_t *len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user