summaryrefslogtreecommitdiffstats
path: root/libcsoap/csoapcall.c
blob: d74b51c8b11f8a85ee179b2dfab9972dd7bd07df (plain)
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
/******************************************************************
 *  $Id: csoapcall.c,v 1.1 2003/11/12 13:22:58 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/csoapcall.h"
#include "libcsoap/csoaplog.h"
#include "libcsoap/csoaphttp.h"

#include <stdarg.h>
#include <string.h>

HSOAPCALL SoapCallCreate(
   const char* url,
   const char* uri,
   const char* method)
{
  const char *FUNC = "SoapCallCreate";
  HSOAPCALL call;

  SoapTraceEnter(FUNC, "url=%s;uri=%s;method=%s", 
	  url, uri, method);
  call = (HSOAPCALL)malloc(sizeof(struct SOAPCALL));
  call->m_method = (char*)malloc(strlen(method)+1);

  call->m_env = SoapEnvCreate(uri, method);
  call->m_url = SoapUrlCreate(url);
  strcpy(call->m_method, method);

  SoapTraceLeave(FUNC, "call (%p)", call);
  return call;
}


HSOAPPARAM SoapCallAddParam(
   HSOAPCALL call,
   const char* name,
   const char* type,
   const char* format,
   ...)
{
  const char *FUNC = "SoapCallAddParam";
  HSOAPBODY body;
  HSOAPMETHOD method;
  HSOAPPARAM param, result;
  va_list ap;
  char buffer[SOAP_MAX_STRING_BUFFER];

  SoapTraceEnter(FUNC, "name=%s;type=%s;format=%s", 
	  name, type, format);

  va_start(ap, format);
  vsprintf(buffer, format, ap);
  va_end(ap);
  
  body = SoapEnvGetBody(call->m_env);
  method = SoapBodyGetMethod(body, call->m_method);
  param = SoapMethodParamContext(method);
  result = SoapParamCreate(param, type, NULL, name, "%s", buffer);

  SoapTraceLeave(FUNC, "created param (%p)", result);
  return result;
}
   

HSOAPPARAM SoapCallAddStringParam(
   HSOAPCALL call,
   const char* name,
   const char* format,
   ...)
{
  const char *FUNC = "SoapCallAddStringParam";
  HSOAPBODY body;
  HSOAPMETHOD method;
  HSOAPPARAM param, result;
  va_list ap;
  char buffer[SOAP_MAX_STRING_BUFFER];

  SoapTraceEnter(FUNC, "name=%s;format=%s", 
	  name, format);

  va_start(ap, format);
  vsprintf(buffer, format, ap);
  va_end(ap);
  
  body = SoapEnvGetBody(call->m_env);
  method = SoapBodyGetMethod(body, call->m_method);
  param = SoapMethodParamContext(method);
  result = SoapParamCreateString(param, name, buffer);
  SoapTraceLeave(FUNC, "created param (%p)", result);
  return result;
}


HSOAPPARAM SoapCallAddIntParam(
   HSOAPCALL call,
   const char* name,
   int value)
{
  const char *FUNC = "SoapCallAddIntParam";
  HSOAPBODY body;
  HSOAPMETHOD method;
  HSOAPPARAM param, result;

  SoapTraceEnter(FUNC, "name=%s;value=%d", 
	  name, value);
  
  body = SoapEnvGetBody(call->m_env);
  method = SoapBodyGetMethod(body, call->m_method);
  param = SoapMethodParamContext(method);
  result = SoapParamCreateInt(param, name, value);
  SoapTraceLeave(FUNC, "created param (%p)", result);
  return result;
}

HSOAPPARAM SoapCallAddDoubleParam(
   HSOAPCALL call,
   const char* name,
   double value)
{
  const char *FUNC = "SoapCallAddDoubleParam";
  HSOAPBODY body;
  HSOAPMETHOD method;
  HSOAPPARAM param, result;

  SoapTraceEnter(FUNC, "name=%s;value=%f", 
	  name, value);
  
  body = SoapEnvGetBody(call->m_env);
  method = SoapBodyGetMethod(body, call->m_method);
  param = SoapMethodParamContext(method);
  result = SoapParamCreateDouble(param, name, value);
  SoapTraceLeave(FUNC, "created param (%p)", result);
  return result;
}


HSOAPRES SoapCallInvoke(HSOAPCALL call)
{
  const char* FUNC = "SoapCallInvoke";
  const char* filename = "libcsoap/csoap-client.xml";
  char *buf;
  CSOAP_STATUS status;
  HSOAPENV env;
  HSOAPRES res;

  SoapTraceEnter(FUNC,"%p", call);
  
  if (!call) {
	  SoapTraceLeaveWithError(FUNC, "Call is null!");
	  return NULL;
  }

  if (!call->m_env) {
	  SoapTraceLeaveWithError(FUNC, "Invalid envelope!");
	  return NULL;
  }

  if (!call->m_url) {
    SoapTraceLeaveWithError(FUNC, "Invalid URL");
    return NULL;
  }

/*  xmlSaveFormatFile(filename, ((xmlNodePtr)call->m_env)->doc, 1);
  buffer = xmlBufferCreate();
  xmlNodeDump(buffer, ((xmlNodePtr)call->m_env)->doc,
	  (xmlNodePtr)call->m_env, 1 ,2);
  printf("\n%s\n", (const char*)xmlBufferContent(buffer));
  xmlBufferFree(buffer);
*/

  printf("Sending ...\n");
  status = SoapHttpSend(call->m_url, call->m_env, &env);
  if (status != CSOAP_OK) {
    printf("can not send. code = %d\n", status);
  }
  printf("Sending finished\n");
  /*
    printf("Length: %d\nResponse: \n%s\n\n\r", strlen(buf), buf?buf:"null");
  */
  SoapEnvDump(env); 
  /*  free(buf);*/
  /*  SoapEnvFree(env);*/
  res = SoapResCreate(env, call->m_method);
  SoapTraceLeave(FUNC, "res (%p)", res);
  return res;
}


void SoapCallFree(HSOAPCALL call)
{
  const char* FUNC = "SoapCallFree";
  SoapTraceEnter(FUNC,"%p", call);

  if (!call) {
    SoapTraceLeave(FUNC, "Call is null!");
    return;
  }

  SoapEnvFree(call->m_env);

  if (call->m_url) SoapUrlFree(call->m_url);
  if (call->m_method) free(call->m_method);

  free(call);
  SoapTraceLeave(FUNC, "Call destroyed");
}