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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
|
/*
Basic HTTP and WebDAV methods
Copyright (C) 1999-2003, 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
*/
#include "config.h"
#include <sys/types.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <errno.h>
#include "ne_request.h"
#include "ne_alloc.h"
#include "ne_utils.h"
#include "ne_basic.h"
#include "ne_207.h"
#ifndef NEON_NODAV
#include "ne_uri.h"
#endif
#ifdef USE_DAV_LOCKS
#include "ne_locks.h"
#endif
#include "ne_dates.h"
#include "ne_i18n.h"
/* Header parser to retrieve Last-Modified date */
static void get_lastmodified(void *userdata, const char *value) {
time_t *modtime = userdata;
*modtime = ne_httpdate_parse(value);
}
int ne_getmodtime(ne_session *sess, const char *uri, time_t *modtime)
{
ne_request *req = ne_request_create(sess, "HEAD", uri);
int ret;
ne_add_response_header_handler(req, "Last-Modified", get_lastmodified,
modtime);
*modtime = -1;
ret = ne_request_dispatch(req);
if (ret == NE_OK && ne_get_status(req)->klass != 2) {
*modtime = -1;
ret = NE_ERROR;
}
ne_request_destroy(req);
return ret;
}
/* PUT's from fd to URI */
int ne_put(ne_session *sess, const char *uri, int fd)
{
ne_request *req = ne_request_create(sess, "PUT", uri);
int ret;
#ifdef USE_DAV_LOCKS
ne_lock_using_resource(req, uri, 0);
ne_lock_using_parent(req, uri);
#endif
ne_set_request_body_fd(req, fd);
ret = ne_request_dispatch(req);
if (ret == NE_OK && ne_get_status(req)->klass != 2)
ret = NE_ERROR;
ne_request_destroy(req);
return ret;
}
/* Conditional HTTP put.
* PUTs from fd to uri, returning NE_FAILED if resource as URI has
* been modified more recently than 'since'.
*/
int
ne_put_if_unmodified(ne_session *sess, const char *uri, int fd,
time_t since)
{
ne_request *req;
char *date;
int ret;
if (ne_version_pre_http11(sess)) {
time_t modtime;
/* Server is not minimally HTTP/1.1 compliant. Do a HEAD to
* check the remote mod time. Of course, this makes the
* operation very non-atomic, but better than nothing. */
ret = ne_getmodtime(sess, uri, &modtime);
if (ret != NE_OK) return ret;
if (modtime != since)
return NE_FAILED;
}
req = ne_request_create(sess, "PUT", uri);
date = ne_rfc1123_date(since);
/* Add in the conditionals */
ne_add_request_header(req, "If-Unmodified-Since", date);
ne_free(date);
#ifdef USE_DAV_LOCKS
ne_lock_using_resource(req, uri, 0);
/* FIXME: this will give 412 if the resource doesn't exist, since
* PUT may modify the parent... does that matter? */
#endif
ne_set_request_body_fd(req, fd);
ret = ne_request_dispatch(req);
if (ret == NE_OK) {
if (ne_get_status(req)->code == 412) {
ret = NE_FAILED;
} else if (ne_get_status(req)->klass != 2) {
ret = NE_ERROR;
}
}
ne_request_destroy(req);
return ret;
}
struct get_context {
int error;
ne_session *session;
off_t total;
int fd; /* used in get_to_fd */
ne_content_range *range;
};
static void get_to_fd(void *userdata, const char *block, size_t length)
{
struct get_context *ctx = userdata;
ssize_t ret;
if (!ctx->error) {
while (length > 0) {
ret = write(ctx->fd, block, length);
if (ret < 0) {
char err[200];
ctx->error = 1;
ne_strerror(errno, err, sizeof err);
ne_set_error(ctx->session, _("Could not write to file: %s"),
err);
break;
} else {
length -= ret;
block += ret;
}
}
}
}
static int accept_206(void *ud, ne_request *req, const ne_status *st)
{
return (st->code == 206);
}
static void clength_hdr_handler(void *ud, const char *value)
{
struct get_context *ctx = ud;
off_t len = strtol(value, NULL, 10);
if (ctx->range->end == -1) {
ctx->range->end = ctx->range->start + len - 1;
ctx->range->total = len;
}
else if (len != ctx->total) {
NE_DEBUG(NE_DBG_HTTP,
"Expecting %" NE_FMT_OFF_T " bytes, "
"got entity of length %" NE_FMT_OFF_T "\n",
ctx->total, len);
ne_set_error(ctx->session, _("Response not of expected length"));
ctx->error = 1;
}
}
static void content_range_hdr_handler(void *ud, const char *value)
{
struct get_context *ctx = ud;
if (strncmp(value, "bytes ", 6) != 0) {
ne_set_error(ctx->session, ("Response range using unrecognized unit"));
ctx->error = 1;
}
/* TODO: verify against requested range. */
}
int ne_get_range(ne_session *sess, const char *uri,
ne_content_range *range, int fd)
{
ne_request *req = ne_request_create(sess, "GET", uri);
struct get_context ctx;
const ne_status *status;
int ret;
if (range->end == -1) {
ctx.total = -1;
}
else {
ctx.total = (range->end - range->start) + 1;
}
NE_DEBUG(NE_DBG_HTTP, "Range total: %" NE_FMT_OFF_T "\n", ctx.total);
ctx.fd = fd;
ctx.error = 0;
ctx.range = range;
ctx.session = sess;
ne_add_response_header_handler(req, "Content-Length",
clength_hdr_handler, &ctx);
ne_add_response_header_handler(req, "Content-Range",
content_range_hdr_handler,
&ctx);
ne_add_response_body_reader(req, accept_206, get_to_fd, &ctx);
if (range->end == -1) {
ne_print_request_header(req, "Range", "bytes=%" NE_FMT_OFF_T "-",
range->start);
}
else {
ne_print_request_header(req, "Range",
"bytes=%" NE_FMT_OFF_T "-%" NE_FMT_OFF_T,
range->start, range->end);
}
ne_add_request_header(req, "Accept-Ranges", "bytes");
ret = ne_request_dispatch(req);
status = ne_get_status(req);
if (ctx.error) {
ret = NE_ERROR;
} else if (status && status->code == 416) {
/* connection is terminated too early with Apache/1.3, so we check
* this even if ret == NE_ERROR... */
ne_set_error(sess, _("Range is not satisfiable"));
ret = NE_ERROR;
}
else if (ret == NE_OK) {
if (status->klass == 2 && status->code != 206) {
ne_set_error(sess, _("Resource does not support ranged GETs."));
ret = NE_ERROR;
}
else if (status->klass != 2) {
ret = NE_ERROR;
}
}
ne_request_destroy(req);
return ret;
}
/* Get to given fd */
int ne_get(ne_session *sess, const char *uri, int fd)
{
ne_request *req = ne_request_create(sess, "GET", uri);
struct get_context ctx;
int ret;
ctx.total = -1;
ctx.fd = fd;
ctx.error = 0;
ctx.session = sess;
/* Read the value of the Content-Length header into ctx.total */
ne_add_response_header_handler(req, "Content-Length",
ne_handle_numeric_header,
&ctx.total);
ne_add_response_body_reader(req, ne_accept_2xx, get_to_fd, &ctx);
ret = ne_request_dispatch(req);
if (ctx.error) {
ret = NE_ERROR;
} else if (ret == NE_OK && ne_get_status(req)->klass != 2) {
ret = NE_ERROR;
}
ne_request_destroy(req);
return ret;
}
/* Get to given fd */
int ne_post(ne_session *sess, const char *uri, int fd, const char *buffer)
{
ne_request *req = ne_request_create(sess, "POST", uri);
struct get_context ctx;
int ret;
ctx.total = -1;
ctx.fd = fd;
ctx.error = 0;
ctx.session = sess;
/* Read the value of the Content-Length header into ctx.total */
ne_add_response_header_handler(req, "Content-Length",
ne_handle_numeric_header, &ctx.total);
ne_add_response_body_reader(req, ne_accept_2xx, get_to_fd, &ctx);
ne_set_request_body_buffer(req, buffer, strlen(buffer));
ret = ne_request_dispatch(req);
if (ctx.error) {
ret = NE_ERROR;
}
else if (ret == NE_OK && ne_get_status(req)->klass != 2) {
ret = NE_ERROR;
}
ne_request_destroy(req);
return ret;
}
void ne_content_type_handler(void *userdata, const char *value)
{
ne_content_type *ct = userdata;
char *sep, *stype;
ct->value = ne_strdup(value);
stype = strchr(ct->value, '/');
if (!stype) {
NE_FREE(ct->value);
return;
}
*stype++ = '\0';
ct->type = ct->value;
sep = strchr(stype, ';');
if (sep) {
char *tok;
/* look for the charset parameter. TODO; probably better to
* hand-carve a parser than use ne_token/strstr/shave here. */
*sep++ = '\0';
do {
tok = ne_qtoken(&sep, ';', "\"\'");
if (tok) {
tok = strstr(tok, "charset=");
if (tok)
ct->charset = ne_shave(tok+8, "\"\'");
} else {
break;
}
} while (sep != NULL);
}
/* set subtype, losing any trailing whitespace */
ct->subtype = ne_shave(stype, " \t");
/* 2616#3.7.1: subtypes of text/ default to charset ISO-8859-1. */
if (ct->charset == NULL && strcasecmp(ct->type, "text") == 0)
ct->charset = "ISO-8859-1";
}
static void dav_hdr_handler(void *userdata, const char *value)
{
char *tokens = ne_strdup(value), *pnt = tokens;
ne_server_capabilities *caps = userdata;
do {
char *tok = ne_qtoken(&pnt, ',', "\"'");
if (!tok) break;
tok = ne_shave(tok, " \r\t\n");
if (strcmp(tok, "1") == 0) {
caps->dav_class1 = 1;
} else if (strcmp(tok, "2") == 0) {
caps->dav_class2 = 1;
} else if (strcmp(tok, "<http://apache.org/dav/propset/fs/1>") == 0) {
caps->dav_executable = 1;
}
} while (pnt != NULL);
ne_free(tokens);
}
int ne_options(ne_session *sess, const char *uri,
ne_server_capabilities *caps)
{
ne_request *req = ne_request_create(sess, "OPTIONS", uri);
int ret;
ne_add_response_header_handler(req, "DAV", dav_hdr_handler, caps);
ret = ne_request_dispatch(req);
if (ret == NE_OK && ne_get_status(req)->klass != 2) {
ret = NE_ERROR;
}
ne_request_destroy(req);
return ret;
}
#ifndef NEON_NODAV
void ne_add_depth_header(ne_request *req, int depth)
{
const char *value;
switch(depth) {
case NE_DEPTH_ZERO:
value = "0";
break;
case NE_DEPTH_ONE:
value = "1";
break;
default:
value = "infinity";
break;
}
ne_add_request_header(req, "Depth", value);
}
static int copy_or_move(ne_session *sess, int is_move, int overwrite,
int depth, const char *src, const char *dest)
{
ne_request *req = ne_request_create( sess, is_move?"MOVE":"COPY", src );
/* 2518 S8.9.2 says only use Depth: infinity with MOVE. */
if (!is_move) {
ne_add_depth_header(req, depth);
}
#ifdef USE_DAV_LOCKS
if (is_move) {
ne_lock_using_resource(req, src, NE_DEPTH_INFINITE);
}
ne_lock_using_resource(req, dest, NE_DEPTH_INFINITE);
/* And we need to be able to add members to the destination's parent */
ne_lock_using_parent(req, dest);
#endif
ne_print_request_header(req, "Destination", "%s://%s%s",
ne_get_scheme(sess),
ne_get_server_hostport(sess), dest);
ne_add_request_header(req, "Overwrite", overwrite?"T":"F");
return ne_simple_request(sess, req);
}
int ne_copy(ne_session *sess, int overwrite, int depth,
const char *src, const char *dest)
{
return copy_or_move(sess, 0, overwrite, depth, src, dest);
}
int ne_move(ne_session *sess, int overwrite,
const char *src, const char *dest)
{
return copy_or_move(sess, 1, overwrite, 0, src, dest);
}
/* Deletes the specified resource. (and in only two lines of code!) */
int ne_delete(ne_session *sess, const char *uri)
{
ne_request *req = ne_request_create(sess, "DELETE", uri);
#ifdef USE_DAV_LOCKS
ne_lock_using_resource(req, uri, NE_DEPTH_INFINITE);
ne_lock_using_parent(req, uri);
#endif
/* joe: I asked on the DAV WG list about whether we might get a
* 207 error back from a DELETE... conclusion, you shouldn't if
* you don't send the Depth header, since we might be an HTTP/1.1
* client and a 2xx response indicates success to them. But
* it's all a bit unclear. In any case, DAV servers today do
* return 207 to DELETE even if we don't send the Depth header.
* So we handle 207 errors appropriately. */
return ne_simple_request(sess, req);
}
int ne_mkcol(ne_session *sess, const char *uri)
{
ne_request *req;
char *real_uri;
int ret;
if (ne_path_has_trailing_slash(uri)) {
real_uri = ne_strdup(uri);
} else {
real_uri = ne_concat(uri, "/", NULL);
}
req = ne_request_create(sess, "MKCOL", real_uri);
#ifdef USE_DAV_LOCKS
ne_lock_using_resource(req, real_uri, 0);
ne_lock_using_parent(req, real_uri);
#endif
ret = ne_simple_request(sess, req);
ne_free(real_uri);
return ret;
}
#endif /* NEON_NODAV */
|