| 1 |
/* |
| 2 |
WebDAV 207 multi-status response handling |
| 3 |
Copyright (C) 1999-2006, Joe Orton <[email protected]> |
| 4 |
|
| 5 |
This library is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU Library General Public |
| 7 |
License as published by the Free Software Foundation; either |
| 8 |
version 2 of the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This library is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
Library General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Library General Public |
| 16 |
License along with this library; if not, write to the Free |
| 17 |
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 18 |
MA 02111-1307, USA |
| 19 |
|
| 20 |
*/ |
| 21 |
|
| 22 |
/* Generic handling for WebDAV 207 Multi-Status responses. */ |
| 23 |
|
| 24 |
#include "config.h" |
| 25 |
|
| 26 |
#ifdef HAVE_STDLIB_H |
| 27 |
#include <stdlib.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
#include "ne_alloc.h" |
| 31 |
#include "ne_utils.h" |
| 32 |
#include "ne_xml.h" |
| 33 |
#include "ne_207.h" |
| 34 |
#include "ne_uri.h" |
| 35 |
#include "ne_basic.h" |
| 36 |
|
| 37 |
#include "ne_internal.h" |
| 38 |
|
| 39 |
struct ne_207_parser_s { |
| 40 |
ne_207_start_response *start_response; |
| 41 |
ne_207_end_response *end_response; |
| 42 |
ne_207_start_propstat *start_propstat; |
| 43 |
ne_207_end_propstat *end_propstat; |
| 44 |
ne_xml_parser *parser; |
| 45 |
void *userdata; |
| 46 |
|
| 47 |
ne_uri base; |
| 48 |
|
| 49 |
ne_buffer *cdata; |
| 50 |
|
| 51 |
/* remember whether we are in a response: the validation |
| 52 |
* doesn't encapsulate this since we only count as being |
| 53 |
* "in a response" when we've seen the href element. */ |
| 54 |
int in_response; |
| 55 |
|
| 56 |
/* current position */ |
| 57 |
void *response, *propstat; |
| 58 |
/* caching */ |
| 59 |
ne_status status; |
| 60 |
char *description, *href; |
| 61 |
}; |
| 62 |
|
| 63 |
#define ELM_multistatus 1 |
| 64 |
#define ELM_response 2 |
| 65 |
#define ELM_responsedescription 3 |
| 66 |
#define ELM_href 4 |
| 67 |
#define ELM_prop (NE_207_STATE_PROP) |
| 68 |
#define ELM_status 6 |
| 69 |
#define ELM_propstat 7 |
| 70 |
|
| 71 |
static const struct ne_xml_idmap map207[] = { |
| 72 |
{ "DAV:", "multistatus", ELM_multistatus }, |
| 73 |
{ "DAV:", "response", ELM_response }, |
| 74 |
{ "DAV:", "responsedescription", ELM_responsedescription }, |
| 75 |
{ "DAV:", "href", ELM_href }, |
| 76 |
{ "DAV:", "propstat", ELM_propstat }, |
| 77 |
{ "DAV:", "prop", ELM_prop }, |
| 78 |
{ "DAV:", "status", ELM_status } |
| 79 |
}; |
| 80 |
|
| 81 |
/* Set the callbacks for the parser */ |
| 82 |
void ne_207_set_response_handlers(ne_207_parser *p, |
| 83 |
ne_207_start_response *start, |
| 84 |
ne_207_end_response *end) |
| 85 |
{ |
| 86 |
p->start_response = start; |
| 87 |
p->end_response = end; |
| 88 |
} |
| 89 |
|
| 90 |
void ne_207_set_propstat_handlers(ne_207_parser *p, |
| 91 |
ne_207_start_propstat *start, |
| 92 |
ne_207_end_propstat *end) |
| 93 |
{ |
| 94 |
p->start_propstat = start; |
| 95 |
p->end_propstat = end; |
| 96 |
} |
| 97 |
|
| 98 |
void *ne_207_get_current_response(ne_207_parser *p) |
| 99 |
{ |
| 100 |
return p->response; |
| 101 |
} |
| 102 |
|
| 103 |
void *ne_207_get_current_propstat(ne_207_parser *p) |
| 104 |
{ |
| 105 |
return p->propstat; |
| 106 |
} |
| 107 |
|
| 108 |
/* return non-zero if (child, parent) is an interesting element */ |
| 109 |
static int can_handle(int parent, int child) |
| 110 |
{ |
| 111 |
return (parent == 0 && child == ELM_multistatus) || |
| 112 |
(parent == ELM_multistatus && child == ELM_response) || |
| 113 |
(parent == ELM_response && |
| 114 |
(child == ELM_href || child == ELM_status || |
| 115 |
child == ELM_propstat || child == ELM_responsedescription)) || |
| 116 |
(parent == ELM_propstat && |
| 117 |
(child == ELM_prop || child == ELM_status || |
| 118 |
child == ELM_responsedescription)); |
| 119 |
} |
| 120 |
|
| 121 |
static int cdata_207(void *userdata, int state, const char *buf, size_t len) |
| 122 |
{ |
| 123 |
ne_207_parser *p = userdata; |
| 124 |
|
| 125 |
if ((state == ELM_href || state == ELM_responsedescription || |
| 126 |
state == ELM_status) && p->cdata->used + len < 2048) |
| 127 |
ne_buffer_append(p->cdata, buf, len); |
| 128 |
|
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
static int start_element(void *userdata, int parent, |
| 133 |
const char *nspace, const char *name, |
| 134 |
const char **atts) |
| 135 |
{ |
| 136 |
ne_207_parser *p = userdata; |
| 137 |
int state = ne_xml_mapid(map207, NE_XML_MAPLEN(map207), nspace, name); |
| 138 |
|
| 139 |
if (!can_handle(parent, state)) |
| 140 |
return NE_XML_DECLINE; |
| 141 |
|
| 142 |
/* if not in a response, ignore everything. */ |
| 143 |
if (!p->in_response && state != ELM_response && state != ELM_multistatus && |
| 144 |
state != ELM_href) |
| 145 |
return NE_XML_DECLINE; |
| 146 |
|
| 147 |
if (state == ELM_propstat && p->start_propstat) { |
| 148 |
p->propstat = p->start_propstat(p->userdata, p->response); |
| 149 |
if (p->propstat == NULL) { |
| 150 |
return NE_XML_ABORT; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
ne_buffer_clear(p->cdata); |
| 155 |
|
| 156 |
return state; |
| 157 |
} |
| 158 |
|
| 159 |
#define GIVE_STATUS(p) ((p)->status.reason_phrase?&(p)->status:NULL) |
| 160 |
|
| 161 |
#define HAVE_CDATA(p) ((p)->cdata->used > 1) |
| 162 |
|
| 163 |
static int |
| 164 |
end_element(void *userdata, int state, const char *nspace, const char *name) |
| 165 |
{ |
| 166 |
ne_207_parser *p = userdata; |
| 167 |
const char *cdata = ne_shave(p->cdata->data, "\r\n\t "); |
| 168 |
|
| 169 |
switch (state) { |
| 170 |
case ELM_responsedescription: |
| 171 |
if (HAVE_CDATA(p)) { |
| 172 |
if (p->description) ne_free(p->description); |
| 173 |
p->description = ne_strdup(cdata); |
| 174 |
} |
| 175 |
break; |
| 176 |
case ELM_href: |
| 177 |
/* Now we have the href, begin the response */ |
| 178 |
if (p->start_response && HAVE_CDATA(p)) { |
| 179 |
ne_uri ref, resolved; |
| 180 |
|
| 181 |
if (ne_uri_parse(cdata, &ref) == 0) { |
| 182 |
ne_uri_resolve(&p->base, &ref, &resolved); |
| 183 |
|
| 184 |
p->response = p->start_response(p->userdata, &resolved); |
| 185 |
p->in_response = 1; |
| 186 |
ne_uri_free(&resolved); |
| 187 |
} |
| 188 |
ne_uri_free(&ref); |
| 189 |
} |
| 190 |
break; |
| 191 |
case ELM_status: |
| 192 |
if (HAVE_CDATA(p)) { |
| 193 |
if (p->status.reason_phrase) ne_free(p->status.reason_phrase); |
| 194 |
if (ne_parse_statusline(cdata, &p->status)) { |
| 195 |
char buf[500]; |
| 196 |
NE_DEBUG(NE_DBG_HTTP, "Status line: %s", cdata); |
| 197 |
ne_snprintf(buf, 500, |
| 198 |
_("Invalid HTTP status line in status element " |
| 199 |
"at line %d of response:\nStatus line was: %s"), |
| 200 |
ne_xml_currentline(p->parser), cdata); |
| 201 |
ne_xml_set_error(p->parser, buf); |
| 202 |
return -1; |
| 203 |
} else { |
| 204 |
NE_DEBUG(NE_DBG_XML, "Decoded status line: %s", cdata); |
| 205 |
} |
| 206 |
} |
| 207 |
break; |
| 208 |
case ELM_propstat: |
| 209 |
if (p->end_propstat) |
| 210 |
p->end_propstat(p->userdata, p->propstat, GIVE_STATUS(p), |
| 211 |
p->description); |
| 212 |
p->propstat = NULL; |
| 213 |
if (p->description) ne_free(p->description); |
| 214 |
if (p->status.reason_phrase) ne_free(p->status.reason_phrase); |
| 215 |
p->description = p->status.reason_phrase = NULL; |
| 216 |
break; |
| 217 |
case ELM_response: |
| 218 |
if (!p->in_response) break; |
| 219 |
if (p->end_response) |
| 220 |
p->end_response(p->userdata, p->response, GIVE_STATUS(p), |
| 221 |
p->description); |
| 222 |
p->response = NULL; |
| 223 |
p->in_response = 0; |
| 224 |
if (p->description) ne_free(p->description); |
| 225 |
if (p->status.reason_phrase) ne_free(p->status.reason_phrase); |
| 226 |
p->description = p->status.reason_phrase = NULL; |
| 227 |
break; |
| 228 |
} |
| 229 |
return 0; |
| 230 |
} |
| 231 |
|
| 232 |
ne_207_parser *ne_207_create(ne_xml_parser *parser, const ne_uri *base, |
| 233 |
void *userdata) |
| 234 |
{ |
| 235 |
ne_207_parser *p = ne_calloc(sizeof *p); |
| 236 |
|
| 237 |
p->parser = parser; |
| 238 |
p->userdata = userdata; |
| 239 |
p->cdata = ne_buffer_create(); |
| 240 |
|
| 241 |
ne_uri_copy(&p->base, base); |
| 242 |
|
| 243 |
/* Add handler for the standard 207 elements */ |
| 244 |
ne_xml_push_handler(parser, start_element, cdata_207, end_element, p); |
| 245 |
|
| 246 |
return p; |
| 247 |
} |
| 248 |
|
| 249 |
void ne_207_destroy(ne_207_parser *p) |
| 250 |
{ |
| 251 |
if (p->status.reason_phrase) ne_free(p->status.reason_phrase); |
| 252 |
ne_buffer_destroy(p->cdata); |
| 253 |
ne_uri_free(&p->base); |
| 254 |
ne_free(p); |
| 255 |
} |
| 256 |
|
| 257 |
int ne_accept_207(void *userdata, ne_request *req, const ne_status *status) |
| 258 |
{ |
| 259 |
return (status->code == 207); |
| 260 |
} |
| 261 |
|
| 262 |
/* Handling of 207 errors: we keep a string buffer, and append |
| 263 |
* messages to it as they come down. |
| 264 |
* |
| 265 |
* Note, 424 means it would have worked but something else went wrong. |
| 266 |
* We will have had the error for "something else", so we display |
| 267 |
* that, and skip 424 errors. */ |
| 268 |
|
| 269 |
/* This is passed as userdata to the 207 code. */ |
| 270 |
struct context { |
| 271 |
char *href; |
| 272 |
ne_buffer *buf; |
| 273 |
unsigned int is_error; |
| 274 |
}; |
| 275 |
|
| 276 |
static void *start_response(void *userdata, const ne_uri *uri) |
| 277 |
{ |
| 278 |
struct context *ctx = userdata; |
| 279 |
if (ctx->href) ne_free(ctx->href); |
| 280 |
ctx->href = ne_uri_unparse(uri); |
| 281 |
return NULL; |
| 282 |
} |
| 283 |
|
| 284 |
static void handle_error(struct context *ctx, const ne_status *status, |
| 285 |
const char *description) |
| 286 |
{ |
| 287 |
if (status && status->klass != 2 && status->code != 424) { |
| 288 |
char buf[50]; |
| 289 |
ctx->is_error = 1; |
| 290 |
sprintf(buf, "%d", status->code); |
| 291 |
ne_buffer_concat(ctx->buf, ctx->href, ": ", |
| 292 |
buf, " ", status->reason_phrase, "\n", NULL); |
| 293 |
if (description != NULL) { |
| 294 |
/* TODO: these can be multi-line. Would be good to |
| 295 |
* word-wrap this at col 80. */ |
| 296 |
ne_buffer_concat(ctx->buf, " -> ", description, "\n", NULL); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
} |
| 301 |
|
| 302 |
static void end_response(void *userdata, void *response, |
| 303 |
const ne_status *status, const char *description) |
| 304 |
{ |
| 305 |
struct context *ctx = userdata; |
| 306 |
handle_error(ctx, status, description); |
| 307 |
} |
| 308 |
|
| 309 |
static void |
| 310 |
end_propstat(void *userdata, void *propstat, |
| 311 |
const ne_status *status, const char *description) |
| 312 |
{ |
| 313 |
struct context *ctx = userdata; |
| 314 |
handle_error(ctx, status, description); |
| 315 |
} |
| 316 |
|
| 317 |
/* Dispatch a DAV request and handle a 207 error response appropriately */ |
| 318 |
/* TODO: hook up Content-Type parsing; passing charset to XML parser */ |
| 319 |
int ne_simple_request(ne_session *sess, ne_request *req) |
| 320 |
{ |
| 321 |
int ret; |
| 322 |
struct context ctx = {0}; |
| 323 |
ne_207_parser *p207; |
| 324 |
ne_xml_parser *p = ne_xml_create(); |
| 325 |
ne_uri base = {0}; |
| 326 |
|
| 327 |
/* Mock up a base URI; it should really be retrieved from the |
| 328 |
* request object. */ |
| 329 |
ne_fill_server_uri(sess, &base); |
| 330 |
base.path = ne_strdup("/"); |
| 331 |
p207 = ne_207_create(p, &base, &ctx); |
| 332 |
ne_uri_free(&base); |
| 333 |
|
| 334 |
/* The error string is progressively written into the |
| 335 |
* ne_buffer by the element callbacks */ |
| 336 |
ctx.buf = ne_buffer_create(); |
| 337 |
|
| 338 |
ne_207_set_response_handlers(p207, start_response, end_response); |
| 339 |
ne_207_set_propstat_handlers(p207, NULL, end_propstat); |
| 340 |
|
| 341 |
ne_add_response_body_reader(req, ne_accept_207, ne_xml_parse_v, p); |
| 342 |
|
| 343 |
ret = ne_request_dispatch(req); |
| 344 |
|
| 345 |
if (ret == NE_OK) { |
| 346 |
if (ne_get_status(req)->code == 207) { |
| 347 |
if (ne_xml_failed(p)) { |
| 348 |
/* The parse was invalid */ |
| 349 |
ne_set_error(sess, "%s", ne_xml_get_error(p)); |
| 350 |
ret = NE_ERROR; |
| 351 |
} else if (ctx.is_error) { |
| 352 |
/* If we've actually got any error information |
| 353 |
* from the 207, then set that as the error */ |
| 354 |
ne_set_error(sess, "%s", ctx.buf->data); |
| 355 |
ret = NE_ERROR; |
| 356 |
} |
| 357 |
} else if (ne_get_status(req)->klass != 2) { |
| 358 |
ret = NE_ERROR; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
ne_207_destroy(p207); |
| 363 |
ne_xml_destroy(p); |
| 364 |
ne_buffer_destroy(ctx.buf); |
| 365 |
if (ctx.href) ne_free(ctx.href); |
| 366 |
|
| 367 |
ne_request_destroy(req); |
| 368 |
|
| 369 |
return ret; |
| 370 |
} |
| 371 |
|