~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to tools/gsoap/macosx-2.7/extras/.svn/text-base/httpmd5.c.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 
3
 
httpmd5.c
4
 
 
5
 
gSOAP HTTP Content-MD5 digest plugin.
6
 
 
7
 
gSOAP XML Web services tools
8
 
Copyright (C) 2000-2004, 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-2004, 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 an md5 engine invoked via a handler:
44
 
 
45
 
        int http_md5_handler(struct soap *soap, void **context, enum http_md5_action action, char *buf, size_t len)
46
 
        context can be set and passed to subsequent calls. Parameters:
47
 
        action =
48
 
        HTTP_MD5_INIT:          init context
49
 
        HTTP_MD5_UPDATE:        update context with data from buf with size len
50
 
        HTTP_MD5_FINAL:         fill buf with 16 bytes MD5 hash value
51
 
        HTTP_MD5_DELETE:        delete context
52
 
        buf                     input data, output MD5 128 bit hash value
53
 
        len                     length of input data
54
 
 
55
 
        Example code:
56
 
        httpmd5test.h, httpmd5test.c
57
 
 
58
 
        Limitations:
59
 
        Does not work with combined chunked + compressed messages.
60
 
        When sending DIME/MIME attachments, you MUST use the SOAP_IO_STORE flag
61
 
        to compute the MD5 hash of the message with attachments. The flag
62
 
        disables streaming DIME.
63
 
*/
64
 
 
65
 
#include "httpmd5.h"
66
 
 
67
 
const char http_md5_id[13] = HTTP_MD5_ID;
68
 
 
69
 
static int http_md5_init(struct soap *soap, struct http_md5_data *data, int (*handler)(struct soap*, void**, enum http_md5_action, char*, size_t));
70
 
static int http_md5_copy(struct soap *soap, struct soap_plugin *dst, struct soap_plugin *src);
71
 
static void http_md5_delete(struct soap *soap, struct soap_plugin *p);
72
 
 
73
 
static int http_md5_post_header(struct soap *soap, const char *key, const char *val);
74
 
static int http_md5_parse_header(struct soap *soap, const char *key, const char *val);
75
 
static int http_md5_prepareinit(struct soap *soap);
76
 
static int http_md5_preparesend(struct soap *soap, const char *buf, size_t len);
77
 
static int http_md5_preparerecv(struct soap *soap, const char *buf, size_t len);
78
 
static int http_md5_disconnect(struct soap *soap);
79
 
 
80
 
int http_md5(struct soap *soap, struct soap_plugin *p, void *arg)
81
 
{ p->id = http_md5_id;
82
 
  p->data = (void*)malloc(sizeof(struct http_md5_data));
83
 
  p->fcopy = http_md5_copy;
84
 
  p->fdelete = http_md5_delete;
85
 
  if (p->data)
86
 
    if (http_md5_init(soap, (struct http_md5_data*)p->data, (int (*)(struct soap*, void**, enum http_md5_action, char*, size_t))arg))
87
 
    { free(p->data); /* error: could not init */
88
 
      return SOAP_EOM; /* return error */
89
 
    }
90
 
  return SOAP_OK;
91
 
}
92
 
 
93
 
static int http_md5_init(struct soap *soap, struct http_md5_data *data, int (*handler)(struct soap*, void**, enum http_md5_action, char*, size_t))
94
 
{ data->fposthdr = soap->fposthdr;
95
 
  soap->fposthdr = http_md5_post_header;
96
 
  data->fparsehdr = soap->fparsehdr;
97
 
  soap->fparsehdr = http_md5_parse_header;
98
 
  data->fprepareinit = soap->fprepareinit;
99
 
  soap->fprepareinit = http_md5_prepareinit;
100
 
  data->fpreparesend = soap->fpreparesend;
101
 
  soap->fpreparesend = http_md5_preparesend;
102
 
  data->fhandler = handler;
103
 
  data->context = NULL;
104
 
  memset(data->md5_digest, 0, sizeof(data->md5_digest));
105
 
  return SOAP_OK;
106
 
}
107
 
 
108
 
static int http_md5_copy(struct soap *soap, struct soap_plugin *dst, struct soap_plugin *src)
109
 
{ *dst = *src;
110
 
  dst->data = (void*)malloc(sizeof(struct http_md5_data));
111
 
  return SOAP_OK;
112
 
}
113
 
 
114
 
static void http_md5_delete(struct soap *soap, struct soap_plugin *p)
115
 
{ struct http_md5_data *data = (struct http_md5_data*)soap_lookup_plugin(soap, http_md5_id);
116
 
  if (data)
117
 
  { data->fhandler(soap, &data->context, HTTP_MD5_DELETE, NULL, 0);
118
 
    free(data);
119
 
  }
120
 
}
121
 
 
122
 
static int http_md5_post_header(struct soap *soap, const char *key, const char *val)
123
 
{ struct http_md5_data *data = (struct http_md5_data*)soap_lookup_plugin(soap, http_md5_id);
124
 
  char buf64[25]; /* 24 base64 chars + '\0' */
125
 
  int err;
126
 
  if (!data)
127
 
    return SOAP_PLUGIN_ERROR;
128
 
  if (!key) /* last line */
129
 
  { if ((err = data->fhandler(soap, &data->context, HTTP_MD5_FINAL, data->md5_digest, 0)))
130
 
      return err;
131
 
    data->fposthdr(soap, "Content-MD5", soap_s2base64(soap, data->md5_digest, buf64, 16));
132
 
  }
133
 
  return data->fposthdr(soap, key, val);
134
 
}
135
 
 
136
 
static int http_md5_parse_header(struct soap *soap, const char *key, const char *val)
137
 
{ struct http_md5_data *data = (struct http_md5_data*)soap_lookup_plugin(soap, http_md5_id);
138
 
  if (!data)
139
 
    return SOAP_PLUGIN_ERROR;
140
 
  if (!soap_tag_cmp(key, "Content-MD5"))
141
 
  { soap_base642s(soap, val, data->md5_digest, 16, NULL);
142
 
    data->fpreparerecv = soap->fpreparerecv;
143
 
    soap->fpreparerecv = http_md5_preparerecv;
144
 
    data->fdisconnect = soap->fdisconnect;
145
 
    soap->fdisconnect = http_md5_disconnect;
146
 
    return SOAP_OK;
147
 
  }
148
 
  return data->fparsehdr(soap, key, val);
149
 
}
150
 
 
151
 
static int http_md5_prepareinit(struct soap *soap)
152
 
{ struct http_md5_data *data = (struct http_md5_data*)soap_lookup_plugin(soap, http_md5_id);
153
 
  if (!data)
154
 
    return SOAP_PLUGIN_ERROR;
155
 
  data->fhandler(soap, &data->context, HTTP_MD5_INIT, NULL, 0);
156
 
  if (soap->fpreparerecv == http_md5_preparerecv)
157
 
    soap->fpreparerecv = data->fpreparerecv;
158
 
  if (soap->fdisconnect == http_md5_disconnect)
159
 
    soap->fdisconnect = data->fdisconnect;
160
 
  if (data->fprepareinit)
161
 
    return data->fprepareinit(soap);
162
 
  return SOAP_OK;
163
 
}
164
 
 
165
 
static int http_md5_preparesend(struct soap *soap, const char *buf, size_t len)
166
 
{ struct http_md5_data *data = (struct http_md5_data*)soap_lookup_plugin(soap, http_md5_id);
167
 
  if (!data)
168
 
    return SOAP_PLUGIN_ERROR;
169
 
  data->fhandler(soap, &data->context, HTTP_MD5_UPDATE, (char*)buf, len);
170
 
  if (data->fpreparesend)
171
 
    return data->fpreparesend(soap, buf, len);
172
 
  return SOAP_OK;
173
 
}
174
 
 
175
 
static int http_md5_preparerecv(struct soap *soap, const char *buf, size_t len)
176
 
{ struct http_md5_data *data = (struct http_md5_data*)soap_lookup_plugin(soap, http_md5_id);
177
 
  if (!data)
178
 
    return SOAP_PLUGIN_ERROR;
179
 
  data->fhandler(soap, &data->context, HTTP_MD5_UPDATE, (char*)buf, len);
180
 
  if (data->fpreparerecv)
181
 
    return data->fpreparerecv(soap, buf, len);
182
 
  return SOAP_OK;
183
 
}
184
 
 
185
 
static int http_md5_disconnect(struct soap *soap)
186
 
{ struct http_md5_data *data = (struct http_md5_data*)soap_lookup_plugin(soap, http_md5_id);
187
 
  int err;
188
 
  char md5_digest[16];
189
 
  if ((err = data->fhandler(soap, &data->context, HTTP_MD5_FINAL, md5_digest, 0)))
190
 
    return err;
191
 
  soap->fpreparerecv = data->fpreparerecv;
192
 
  soap->fdisconnect = data->fdisconnect;
193
 
  if (memcmp(md5_digest, data->md5_digest, 16))
194
 
    return soap_sender_fault(soap, "MD5 digest mismatch: message corrupted", NULL);
195
 
  if (soap->fdisconnect)
196
 
    return soap->fdisconnect(soap);
197
 
  return SOAP_OK;
198
 
}