~vcs-imports/simias/trunk

« back to all changes in this revision

Viewing changes to simias/tools/gsoap/gsoap-linux-2.7/plugin/httpdatest.c

  • Committer: kalidasbala
  • Date: 2007-08-25 12:48:51 UTC
  • Revision ID: vcs-imports@canonical.com-20070825124851-vlfvzun3732ld196
Latest gsoap code update

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
httpdatest.c
 
4
 
 
5
gSOAP HTTP Digest Authentication example application.
 
6
 
 
7
gSOAP XML Web services tools
 
8
Copyright (C) 2000-2005, Robert van Engelen, Genivia, Inc., All Rights Reserved.
 
9
 
 
10
--------------------------------------------------------------------------------
 
11
gSOAP public license.
 
12
 
 
13
The contents of this file are subject to the gSOAP Public License Version 1.3
 
14
(the "License"); you may not use this file except in compliance with the
 
15
License. You may obtain a copy of the License at
 
16
http://www.cs.fsu.edu/~engelen/soaplicense.html
 
17
Software distributed under the License is distributed on an "AS IS" basis,
 
18
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
19
for the specific language governing rights and limitations under the License.
 
20
 
 
21
The Initial Developer of the Original Code is Robert A. van Engelen.
 
22
Copyright (C) 2000-2005, Robert van Engelen, Genivia, Inc., All Rights Reserved.
 
23
--------------------------------------------------------------------------------
 
24
GPL license.
 
25
 
 
26
This program is free software; you can redistribute it and/or modify it under
 
27
the terms of the GNU General Public License as published by the Free Software
 
28
Foundation; either version 2 of the License, or (at your option) any later
 
29
version.
 
30
 
 
31
This program is distributed in the hope that it will be useful, but WITHOUT ANY
 
32
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 
33
PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
34
 
 
35
You should have received a copy of the GNU General Public License along with
 
36
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
37
Place, Suite 330, Boston, MA 02111-1307 USA
 
38
 
 
39
Author contact information:
 
40
engelen@genivia.com / engelen@acm.org
 
41
--------------------------------------------------------------------------------
 
42
 
 
43
Requires OpenSSL
 
44
 
 
45
Compile:
 
46
 
 
47
soapcpp2 -c httpdatest.h
 
48
cc -DWITH_OPENSSL -o httpdatest httpdatest.c soapC.c soapClient.c soapServer.c httpda.c md5evp.c threads.c stdsoap2.c -lssl -lcrypto -lz
 
49
 
 
50
*/
 
51
 
 
52
#include "httpda.h"
 
53
#include "soapH.h"
 
54
#include "ns.nsmap"
 
55
 
 
56
static char authrealm[] = "gSOAP Authentication Test";
 
57
 
 
58
int run_serve(int port);
 
59
int run_tests(int,char**);
 
60
 
 
61
int main(int argc, char **argv)
 
62
{
 
63
  if (argc < 2)
 
64
    return run_serve(8080);
 
65
  else if (argc < 3)
 
66
    return run_serve(atoi(argv[1]));
 
67
  else
 
68
    return run_tests(argc, argv);
 
69
}
 
70
 
 
71
int run_serve(int port)
 
72
{
 
73
  struct soap *soap = soap_new();
 
74
  int ret;
 
75
  soap_register_plugin(soap, http_da);
 
76
  if (!soap_valid_socket(soap_bind(soap, NULL, port, 100)))
 
77
    soap_print_fault(soap, stderr);
 
78
  else
 
79
  {
 
80
    fprintf(stderr, "Bind to port %d successful\n", port);
 
81
    soap->accept_timeout = 3600; /* let server time out after one hour */
 
82
    for (;;)
 
83
    {
 
84
      int sock = soap_accept(soap);
 
85
      if (!soap_valid_socket(sock))
 
86
      {
 
87
        if (soap->errnum)
 
88
          soap_print_fault(soap, stderr);
 
89
        else
 
90
        {
 
91
          fprintf(stderr, "Server timed out\n");
 
92
          break;
 
93
        }
 
94
      }
 
95
      fprintf(stderr, "Accepting socket %d connection from IP %d.%d.%d.%d\n", sock, (int)(soap->ip>>24)&0xFF, (int)(soap->ip>>16)&0xFF, (int)(soap->ip>>8)&0xFF, (int)soap->ip&0xFF);
 
96
      if (soap_serve(soap))
 
97
        soap_print_fault(soap, stderr);
 
98
      fprintf(stderr, "Served\n\n");
 
99
      soap_destroy(soap);
 
100
      soap_end(soap);
 
101
    } 
 
102
  }
 
103
  ret = soap->error;
 
104
  soap_end(soap);
 
105
  soap_done(soap);
 
106
  free(soap);
 
107
  return ret;
 
108
}
 
109
 
 
110
int run_tests(int argc, char **argv)
 
111
{
 
112
  struct soap *soap = soap_new();
 
113
  struct ns__echoString r;
 
114
  char *endpoint, *arg;
 
115
  int ret;
 
116
 
 
117
  soap_register_plugin(soap, http_da);
 
118
 
 
119
  endpoint = argv[1];
 
120
  arg = argv[2];
 
121
 
 
122
  if (soap_call_ns__echoString(soap, endpoint, NULL, arg, &r))
 
123
  {
 
124
    if (soap->error == 401)
 
125
    {
 
126
      if (!strcmp(soap->authrealm, authrealm))
 
127
      {
 
128
        /* save userid and passwd for basic or digest authentication */
 
129
        struct http_da_info info;
 
130
        http_da_save(soap, &info, authrealm, "Mufasa", "Circle Of Life");
 
131
        if (!soap_call_ns__echoString(soap, endpoint, NULL, arg, &r))
 
132
        {
 
133
          soap_end(soap);
 
134
          /* need to restore for authentication */
 
135
          http_da_restore(soap, &info);
 
136
          if (!soap_call_ns__echoString(soap, endpoint, NULL, arg, &r))
 
137
          {
 
138
            if (!strcmp(arg, r.arg))
 
139
              printf("EchoString test OK\n");
 
140
            else
 
141
              printf("Transmission error\n");
 
142
          } 
 
143
        }
 
144
        http_da_release(soap, &info);
 
145
        /* regular calls may follow */
 
146
      }
 
147
    }
 
148
  }
 
149
  if (soap->error)
 
150
    soap_print_fault(soap, stderr);
 
151
  ret = soap->error;
 
152
  soap_end(soap);
 
153
  soap_done(soap);
 
154
  free(soap);
 
155
  return ret;
 
156
}
 
157
 
 
158
int ns__echoString(struct soap *soap, char *arg, struct ns__echoString *response)
 
159
{
 
160
  if (soap->userid && soap->passwd)     /* Basic authentication: we may want to reject this since the password was send in the clear */
 
161
  { if (!strcmp(soap->userid, "Mufasa")
 
162
     && !strcmp(soap->passwd, "Circle Of Life"))
 
163
    {
 
164
      response->arg = arg;
 
165
      return SOAP_OK;
 
166
    }
 
167
  }
 
168
  else if (soap->authrealm && soap->userid)
 
169
  {
 
170
    /* simulate database lookup on userid to find passwd */
 
171
    if (!strcmp(soap->authrealm, authrealm) && !strcmp(soap->userid, "Mufasa"))
 
172
    {
 
173
      char *passwd = "Circle Of Life";
 
174
 
 
175
      if (!http_da_verify_post(soap, passwd))
 
176
      {
 
177
        response->arg = arg;
 
178
        return SOAP_OK;
 
179
      }
 
180
    }
 
181
  }
 
182
  soap->authrealm = authrealm;
 
183
  return 401; /* Not authorized, challenge digest authentication with httpda plugin */
 
184
}