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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/*
WebDAV 207 multi-status response handling
Copyright (C) 1999-2006, Joe Orton <joe@manyfish.co.uk>
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
*/
/* Generic handling for WebDAV 207 Multi-Status responses. */
#include "config.h"
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include "ne_alloc.h"
#include "ne_utils.h"
#include "ne_xml.h"
#include "ne_207.h"
#include "ne_uri.h"
#include "ne_basic.h"
#include "ne_internal.h"
struct ne_207_parser_s {
ne_207_start_response *start_response;
ne_207_end_response *end_response;
ne_207_start_propstat *start_propstat;
ne_207_end_propstat *end_propstat;
ne_xml_parser *parser;
void *userdata;
ne_uri base;
ne_buffer *cdata;
/* remember whether we are in a response: the validation
* doesn't encapsulate this since we only count as being
* "in a response" when we've seen the href element. */
int in_response;
/* current position */
void *response, *propstat;
/* caching */
ne_status status;
char *description, *href;
};
#define ELM_multistatus 1
#define ELM_response 2
#define ELM_responsedescription 3
#define ELM_href 4
#define ELM_prop (NE_207_STATE_PROP)
#define ELM_status 6
#define ELM_propstat 7
static const struct ne_xml_idmap map207[] = {
{ "DAV:", "multistatus", ELM_multistatus },
{ "DAV:", "response", ELM_response },
{ "DAV:", "responsedescription", ELM_responsedescription },
{ "DAV:", "href", ELM_href },
{ "DAV:", "propstat", ELM_propstat },
{ "DAV:", "prop", ELM_prop },
{ "DAV:", "status", ELM_status }
};
/* Set the callbacks for the parser */
void ne_207_set_response_handlers(ne_207_parser *p,
ne_207_start_response *start,
ne_207_end_response *end)
{
p->start_response = start;
p->end_response = end;
}
void ne_207_set_propstat_handlers(ne_207_parser *p,
ne_207_start_propstat *start,
ne_207_end_propstat *end)
{
p->start_propstat = start;
p->end_propstat = end;
}
void *ne_207_get_current_response(ne_207_parser *p)
{
return p->response;
}
void *ne_207_get_current_propstat(ne_207_parser *p)
{
return p->propstat;
}
/* return non-zero if (child, parent) is an interesting element */
static int can_handle(int parent, int child)
{
return (parent == 0 && child == ELM_multistatus) ||
(parent == ELM_multistatus && child == ELM_response) ||
(parent == ELM_response &&
(child == ELM_href || child == ELM_status ||
child == ELM_propstat || child == ELM_responsedescription)) ||
(parent == ELM_propstat &&
(child == ELM_prop || child == ELM_status ||
child == ELM_responsedescription));
}
static int cdata_207(void *userdata, int state, const char *buf, size_t len)
{
ne_207_parser *p = userdata;
if ((state == ELM_href || state == ELM_responsedescription ||
state == ELM_status) && p->cdata->used + len < 2048)
ne_buffer_append(p->cdata, buf, len);
return 0;
}
static int start_element(void *userdata, int parent,
const char *nspace, const char *name,
const char **atts)
{
ne_207_parser *p = userdata;
int state = ne_xml_mapid(map207, NE_XML_MAPLEN(map207), nspace, name);
if (!can_handle(parent, state))
return NE_XML_DECLINE;
/* if not in a response, ignore everything. */
if (!p->in_response && state != ELM_response && state != ELM_multistatus &&
state != ELM_href)
return NE_XML_DECLINE;
if (state == ELM_propstat && p->start_propstat) {
p->propstat = p->start_propstat(p->userdata, p->response);
if (p->propstat == NULL) {
return NE_XML_ABORT;
}
}
ne_buffer_clear(p->cdata);
return state;
}
#define GIVE_STATUS(p) ((p)->status.reason_phrase?&(p)->status:NULL)
#define HAVE_CDATA(p) ((p)->cdata->used > 1)
static int
end_element(void *userdata, int state, const char *nspace, const char *name)
{
ne_207_parser *p = userdata;
const char *cdata = ne_shave(p->cdata->data, "\r\n\t ");
switch (state) {
case ELM_responsedescription:
if (HAVE_CDATA(p)) {
if (p->description) ne_free(p->description);
p->description = ne_strdup(cdata);
}
break;
case ELM_href:
/* Now we have the href, begin the response */
if (p->start_response && HAVE_CDATA(p)) {
ne_uri ref, resolved;
if (ne_uri_parse(cdata, &ref) == 0) {
ne_uri_resolve(&p->base, &ref, &resolved);
p->response = p->start_response(p->userdata, &resolved);
p->in_response = 1;
ne_uri_free(&resolved);
}
ne_uri_free(&ref);
}
break;
case ELM_status:
if (HAVE_CDATA(p)) {
if (p->status.reason_phrase) ne_free(p->status.reason_phrase);
if (ne_parse_statusline(cdata, &p->status)) {
char buf[500];
NE_DEBUG(NE_DBG_HTTP, "Status line: %s\n", cdata);
ne_snprintf(buf, 500,
_("Invalid HTTP status line in status element "
"at line %d of response:\nStatus line was: %s"),
ne_xml_currentline(p->parser), cdata);
ne_xml_set_error(p->parser, buf);
return -1;
} else {
NE_DEBUG(NE_DBG_XML, "Decoded status line: %s\n", cdata);
}
}
break;
case ELM_propstat:
if (p->end_propstat)
p->end_propstat(p->userdata, p->propstat, GIVE_STATUS(p),
p->description);
p->propstat = NULL;
if (p->description) ne_free(p->description);
if (p->status.reason_phrase) ne_free(p->status.reason_phrase);
p->description = p->status.reason_phrase = NULL;
break;
case ELM_response:
if (!p->in_response) break;
if (p->end_response)
p->end_response(p->userdata, p->response, GIVE_STATUS(p),
p->description);
p->response = NULL;
p->in_response = 0;
if (p->description) ne_free(p->description);
if (p->status.reason_phrase) ne_free(p->status.reason_phrase);
p->description = p->status.reason_phrase = NULL;
break;
}
return 0;
}
ne_207_parser *ne_207_create(ne_xml_parser *parser, const ne_uri *base,
void *userdata)
{
ne_207_parser *p = ne_calloc(sizeof *p);
p->parser = parser;
p->userdata = userdata;
p->cdata = ne_buffer_create();
ne_uri_copy(&p->base, base);
/* Add handler for the standard 207 elements */
ne_xml_push_handler(parser, start_element, cdata_207, end_element, p);
return p;
}
void ne_207_destroy(ne_207_parser *p)
{
if (p->status.reason_phrase) ne_free(p->status.reason_phrase);
ne_buffer_destroy(p->cdata);
ne_uri_free(&p->base);
ne_free(p);
}
int ne_accept_207(void *userdata, ne_request *req, const ne_status *status)
{
return (status->code == 207);
}
/* Handling of 207 errors: we keep a string buffer, and append
* messages to it as they come down.
*
* Note, 424 means it would have worked but something else went wrong.
* We will have had the error for "something else", so we display
* that, and skip 424 errors. */
/* This is passed as userdata to the 207 code. */
struct context {
char *href;
ne_buffer *buf;
unsigned int is_error;
};
static void *start_response(void *userdata, const ne_uri *uri)
{
struct context *ctx = userdata;
if (ctx->href) ne_free(ctx->href);
ctx->href = ne_uri_unparse(uri);
return NULL;
}
static void handle_error(struct context *ctx, const ne_status *status,
const char *description)
{
if (status && status->klass != 2 && status->code != 424) {
char buf[50];
ctx->is_error = 1;
sprintf(buf, "%d", status->code);
ne_buffer_concat(ctx->buf, ctx->href, ": ",
buf, " ", status->reason_phrase, "\n", NULL);
if (description != NULL) {
/* TODO: these can be multi-line. Would be good to
* word-wrap this at col 80. */
ne_buffer_concat(ctx->buf, " -> ", description, "\n", NULL);
}
}
}
static void end_response(void *userdata, void *response,
const ne_status *status, const char *description)
{
struct context *ctx = userdata;
handle_error(ctx, status, description);
}
static void
end_propstat(void *userdata, void *propstat,
const ne_status *status, const char *description)
{
struct context *ctx = userdata;
handle_error(ctx, status, description);
}
/* Dispatch a DAV request and handle a 207 error response appropriately */
/* TODO: hook up Content-Type parsing; passing charset to XML parser */
int ne_simple_request(ne_session *sess, ne_request *req)
{
int ret;
struct context ctx = {0};
ne_207_parser *p207;
ne_xml_parser *p = ne_xml_create();
ne_uri base = {0};
/* Mock up a base URI; it should really be retrieved from the
* request object. */
ne_fill_server_uri(sess, &base);
base.path = ne_strdup("/");
p207 = ne_207_create(p, &base, &ctx);
ne_uri_free(&base);
/* The error string is progressively written into the
* ne_buffer by the element callbacks */
ctx.buf = ne_buffer_create();
ne_207_set_response_handlers(p207, start_response, end_response);
ne_207_set_propstat_handlers(p207, NULL, end_propstat);
ne_add_response_body_reader(req, ne_accept_207, ne_xml_parse_v, p);
ret = ne_request_dispatch(req);
if (ret == NE_OK) {
if (ne_get_status(req)->code == 207) {
if (ne_xml_failed(p)) {
/* The parse was invalid */
ne_set_error(sess, "%s", ne_xml_get_error(p));
ret = NE_ERROR;
} else if (ctx.is_error) {
/* If we've actually got any error information
* from the 207, then set that as the error */
ne_set_error(sess, "%s", ctx.buf->data);
ret = NE_ERROR;
}
} else if (ne_get_status(req)->klass != 2) {
ret = NE_ERROR;
}
}
ne_207_destroy(p207);
ne_xml_destroy(p);
ne_buffer_destroy(ctx.buf);
if (ctx.href) ne_free(ctx.href);
ne_request_destroy(req);
return ret;
}
|