5
gSOAP HTTP Content-MD5 digest plugin.
7
gSOAP XML Web services tools
8
Copyright (C) 2000-2004, Robert van Engelen, Genivia, Inc., All Rights Reserved.
10
--------------------------------------------------------------------------------
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.
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
--------------------------------------------------------------------------------
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
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.
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
39
Author contact information:
40
engelen@genivia.com / engelen@acm.org
41
--------------------------------------------------------------------------------
43
Requires an md5 engine invoked via a handler:
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:
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
56
httpmd5test.h, httpmd5test.c
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.
67
const char http_md5_id[13] = HTTP_MD5_ID;
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);
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);
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;
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 */
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));
108
static int http_md5_copy(struct soap *soap, struct soap_plugin *dst, struct soap_plugin *src)
110
dst->data = (void*)malloc(sizeof(struct http_md5_data));
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);
117
{ data->fhandler(soap, &data->context, HTTP_MD5_DELETE, NULL, 0);
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' */
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)))
131
data->fposthdr(soap, "Content-MD5", soap_s2base64(soap, data->md5_digest, buf64, 16));
133
return data->fposthdr(soap, key, val);
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);
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;
148
return data->fparsehdr(soap, key, val);
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);
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);
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);
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);
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);
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);
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);
189
if ((err = data->fhandler(soap, &data->context, HTTP_MD5_FINAL, md5_digest, 0)))
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);