1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/******************************************************************
* $Id: soap-client.c,v 1.8 2004/10/15 13:33:13 snowdrop Exp $
*
* CSOAP Project: A SOAP client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Email: ayaz@jprogrammer.net
******************************************************************/
#include <libcsoap/soap-client.h>
#include <nanohttp/nanohttp-client.h>
#include <string.h>
/*--------------------------------- */
static SoapEnv *_soap_client_build_result(hresponse_t *res);
/*--------------------------------- */
int soap_client_init_args(int argc, char *argv[])
{
return !httpc_init(argc, argv);
}
static
long _file_get_size(const char* filename)
{
FILE *f = fopen(filename, "r");
long size;
if (!f) return -1;
fseek(f, 0, SEEK_END);
size=ftell(f);
fclose(f);
return size;
}
SoapCtx*
soap_client_invoke(SoapCtx *call, const char *url, const char *soap_action)
{
/* Status */
hstatus_t status;
/* Result document */
SoapEnv* doc;
/* Result Context */
SoapCtx *ctx;
/* Buffer variables*/
xmlBufferPtr buffer;
char *content;
char tmp[15];
/* Transport variables */
httpc_conn_t *conn;
hresponse_t *res;
/* multipart/related start id*/
char start_id[150];
static int counter=1;
part_t *part;
int file_count=0;
long total_size;
long file_size;
/* Create buffer */
buffer = xmlBufferCreate();
xmlNodeDump(buffer, call->env->root->doc,call->env->root, 1 ,0);
content = (char*)xmlBufferContent(buffer);
/* Transport via HTTP */
conn = httpc_new();
/* Set soap action */
if (soap_action != NULL) {
httpc_set_header(conn, "SoapAction", soap_action);
}
/* check for attachments */
if (!call->attachments)
{
/* content-type is always 'text/xml' */
httpc_set_header(conn, HEADER_CONTENT_TYPE, "text/xml");
sprintf(tmp, "%d", (int)strlen(content));
httpc_set_header(conn, HEADER_CONTENT_LENGTH, tmp);
status = httpc_post_begin(conn, url);
if (status != H_OK) {
httpc_free(conn);
xmlBufferFree(buffer);
return soap_ctx_new(
soap_env_new_with_fault(Fault_Client,
"Can not begin post envelope","",""));
}
status = http_output_stream_write_string(conn->out, content);
if (status != H_OK) {
httpc_free(conn);
xmlBufferFree(buffer);
return soap_ctx_new(
soap_env_new_with_fault(Fault_Client,
"Can not post envelope","",""));
}
/* res = httpc_post(conn, url, (int)strlen(content), content);*/
res = httpc_post_end(conn);
if (res == NULL) {
httpc_free(conn);
xmlBufferFree(buffer);
return soap_ctx_new(
soap_env_new_with_fault(Fault_Client,
conn->errmsg,"",""));
}
}
else
{
/* Use content-length transport */
/*for (part=call->attachments->parts; part; part=part->next) {
file_size = _file_get_size(part->filename);
if (file_size == -1) [
httpc_free(conn);
xmlBufferFree(buffer);
return soap_ctx_new(
soap_env_new_with_fault(Fault_Client,
"Can not open file!","",""));
}
total_size += file_size + BOUDARY_LENGTH;
}*/
/* Use chunked transport */
httpc_set_header(conn, HEADER_TRANSFER_ENCODING, TRANSFER_ENCODING_CHUNKED);
sprintf(start_id, "289247829121218%d", counter++);
status = httpc_mime_begin(conn, url, start_id, "", "text/xml");
if (status != H_OK) {
httpc_free(conn);
xmlBufferFree(buffer);
return soap_ctx_new(
soap_env_new_with_fault(Fault_Client,
"Can not begin MIME transport","",""));
}
status = httpc_mime_next(conn, start_id, "text/xml", "binary");
if (status != H_OK) {
httpc_free(conn);
xmlBufferFree(buffer);
return soap_ctx_new(
soap_env_new_with_fault(Fault_Client,
"MIME transport error","",""));
}
status = http_output_stream_write(conn->out, content, strlen(content));
if (status != H_OK) {
httpc_free(conn);
xmlBufferFree(buffer);
return soap_ctx_new(
soap_env_new_with_fault(Fault_Client,
"MIME transport error","",""));
}
for (part=call->attachments->parts; part; part=part->next)
{
status = httpc_mime_send_file(conn, part->id,
part->content_type, part->transfer_encoding, part->filename);
if (status != H_OK) {
log_error2("Send file failed. Status:%d", status);
httpc_free(conn);
xmlBufferFree(buffer);
return soap_ctx_new(
soap_env_new_with_fault(Fault_Client,
"MIME transport error while sending file","",""));
}
}
res = httpc_mime_end(conn);
if (!res)
{
httpc_free(conn);
xmlBufferFree(buffer);
return soap_ctx_new(
soap_env_new_with_fault(Fault_Client,
"MIME transport error","",""));
}
}
/* Free buffer */
xmlBufferFree(buffer);
/* Build result */
/* TODO: If res == NULL, find out where and why it is NULL! */
doc = _soap_client_build_result(res);
/* Create Context */
ctx = soap_ctx_new(doc);
soap_ctx_add_files(ctx, res->attachments);
return ctx;
}
static
SoapEnv* _soap_client_build_result(hresponse_t *res)
{
xmlDocPtr doc;
SoapEnv *env;
char *buffer;
log_verbose2("Building result (%p)", res);
if (res == NULL)
return soap_env_new_with_fault(Fault_Client,
"Response is NULL","","");
if (res->in == NULL)
return soap_env_new_with_fault(Fault_Client,
"Empty response from server!","","");
/*
doc = xmlParseDoc(BAD_CAST res->body);
if (doc == NULL) {
return soap_env_new_with_fault(Fault_Client,
"Response is not in XML format!","","");
}
env = soap_env_new_from_doc(doc);
*/
env = soap_env_new_from_stream(res->in);
if (env == NULL) {
xmlFreeDoc(doc);
return soap_env_new_with_fault(Fault_Client,
"Can not create envelope","","");
}
return env;
}
SoapCtx *soap_client_ctx_new(const char *urn, const char *method)
{
SoapCtx *ctx = soap_ctx_new(soap_env_new_with_method(urn, method));
return ctx;
}
|