summaryrefslogtreecommitdiffstats
path: root/examples/csoap/soapclient.c
blob: 5603da1ba6db5b8cd5df787948f0c58b97f4a549 (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
// soapclient.c
// compile with
// gcc soapclient.c -o soapclient -lcsoap
/* Author: Adrianus Warmenhoven */

#include <stdio.h>
#include <string.h>
#include <libcsoap/soap-client.h>

#define MAX_LINE_LENGTH 65535

// stripcslashes - unescapes the string
void stripcslashes(char *str, int *len) {
  char *source, *target, *end;
  int  nlen = *len, i;
  char numtmp[4];

  for (source=str, end=str+nlen, target=str; source < end; source++) {
    if (*source == '\\' && source+1 < end) {
      source++;
      switch (*source) {
      case 'n':  *target++='\n'; nlen--; break;
      case 'r':  *target++='\r'; nlen--; break;
      case 'a':  *target++='\a'; nlen--; break;
      case 't':  *target++='\t'; nlen--; break;
      case 'v':  *target++='\v'; nlen--; break;
      case 'b':  *target++='\b'; nlen--; break;
      case 'f':  *target++='\f'; nlen--; break;
      case '\\': *target++='\\'; nlen--; break;
      case 'x':
	if (source+1 < end && isxdigit((int)(*(source+1)))) {
	  numtmp[0] = *++source;
	  if (source+1 < end && isxdigit((int)(*(source+1)))) {
	    numtmp[1] = *++source;
	    numtmp[2] = '\0';
	    nlen-=3;
	  } else {
	    numtmp[1] = '\0';
	    nlen-=2;
	  }
	  *target++=(char)strtol(numtmp, NULL, 16);
	  break;
	}
      default: 
	i=0; 
	while (source < end && *source >= '0' && *source <= '7' && i<3) {
	  numtmp[i++] = *source++;
	}
	if (i) {
	  numtmp[i]='\0';
	  *target++=(char)strtol(numtmp, NULL, 8);
	  nlen -= i;
	  source--;
	} else {
	  *target++ = *source;
	  nlen--;
	}
      }
    } else {
      *target++ = *source;
    }
  }

  if (nlen != 0) *target='\0';
  *len = nlen;
} 

// ParseLine - gets a line, finds the commas, unescapes the value and adds it
//   to the SOAP request
void ParseLine(SoapCtx *ctx, char * Buffer, int LineLen) {
  // if wrong line length, return
  if (LineLen <= 0) return;
    
  // alloc buffer for the line, copy it
  char * Line = (char *)malloc(LineLen + 1);
  memcpy(Line, Buffer, LineLen);
  Line[LineLen] = '\0';
    
  // find first comma
  char * FirstCommaPos = strchr(Line, ',');
  if (!FirstCommaPos) return;
    
  // find second comma
  char * SecondCommaPos = strchr(FirstCommaPos + 1, ',');
  if (!SecondCommaPos) return;
    
  // separate the three strings
  FirstCommaPos[0] = '\0';
  SecondCommaPos[0] = '\0';
    
  // unescape
  int len = strlen(SecondCommaPos + 1);
  stripcslashes(SecondCommaPos + 1, &len);
    
  // add to the request
  soap_env_add_item(ctx->env, Line, FirstCommaPos + 1, SecondCommaPos 
		    + 1);
    
  // free the buffer
  free(Line);
}

void printusage(char *FileName) {
  printf("Usage: %s [URL] [URN] [SOAPMETHOD]\n\n", FileName);
  printf("- [URL] is the url of the SOAP server.\n");
  printf("- [URN] is the namespace for the method/webservice.\n");
  printf("- [SOAPMETHOD] is the method to call.\n\n");
  printf("All commandline paramaters are mandatory.\n\n");
  printf("%s reads the parameters for the SOAPMETHOD from STDIN as a comma-separated list\n\n", FileName);
  printf("Each line is one parameter and has the following form:\n\n");
  printf("xsd:[TYPE],[PARAMETERNAME],[PARAMETERVALUE]\n\n");
  printf("- [TYPE] is a basic type for SOAP or a complex type defined by the WSDL.\n");
  printf("- [PARAMETERNAME] is the name for the parameter of the SOAPMETHOD on this line.\n");
  printf("- [PARAMETERVALUE] is the value for the parameter. N.B. \nand other escapes are recognized.\n");
}

int main(int argc, char *argv[]) {
  SoapCtx *ctx, *ctx2;
  herror_t err;
 
  // check the parameter count
  if (argc != 4) {
    printusage(argv[0]);
    return 1;
  }
    
  // init cSOAP client
  err = soap_client_init_args(argc, argv);
  if (err != H_OK) {
    log_error4("%s():%s [%d]", herror_func(err), 
	       herror_message(err), herror_code(err));
    herror_release(err);
    return 1;
  }

  // create a SoapCtx object
  err = soap_ctx_new_with_method(argv[2], argv[3], &ctx);
  if (err != H_OK) {
    log_error4("%s():%s [%d]", herror_func(err), 
	       herror_message(err), herror_code(err));
    herror_release(err);
    return 1;
  }
    
  // create buffer
  char Buffer[MAX_LINE_LENGTH];
  int bytes_read, bytes_left;
    
  // read from stdin until EOF
  while (!feof(stdin)) {
    bytes_read = fread(Buffer, 1, MAX_LINE_LENGTH, stdin);

    // pass each line into ParseLine
    char * EndLinePos;
    while (EndLinePos = strchr(Buffer, '\n')) {
      ParseLine(ctx, Buffer, EndLinePos - Buffer);
      memmove(Buffer, EndLinePos + 1, bytes_read - (EndLinePos - 
						    Buffer + 1));
      Buffer[bytes_read - (EndLinePos - Buffer + 1)] = '\0';
    }
        
    // no '\n' found in the whole Buffer, that means line's too 
long
  bytes_left = strlen(Buffer);
 if (bytes_left == MAX_LINE_LENGTH) {
   log_error1("The parameter line is too long.");
   herror_release(err);
   soap_ctx_free(ctx);
   return 1;            
 }
  }

  // invoke
  err = soap_client_invoke(ctx, &ctx2, argv[1], "");
  if (err != H_OK) {
    log_error4("[%d] %s(): %s ", herror_code(err), 
	       herror_func(err), herror_message(err));
    herror_release(err);
    soap_ctx_free(ctx);
    return 1;
  }
    
  // print the result
  soap_xml_doc_print(ctx2->env->root->doc);
    
  // free the objects
  soap_ctx_free(ctx2);
  soap_ctx_free(ctx);

  // destroy the cSOAP client
  soap_client_destroy();
    
  return 0;
}