395 switch(error) { |
395 switch(error) { |
396 // TODO: handle all errors |
396 // TODO: handle all errors |
397 default: return REQ_ABORTED; |
397 default: return REQ_ABORTED; |
398 } |
398 } |
399 } |
399 } |
400 |
400 |
401 WebdavBackend *dav = rq->davCollection ? |
401 WebdavBackend *dav = rq->davCollection ? |
402 rq->davCollection : &default_backend; |
402 rq->davCollection : &default_backend; |
403 |
403 |
404 // requested uri and path |
404 // requested uri and path |
405 char *path = pblock_findkeyval(pb_key_path, rq->vars); |
405 char *path = pblock_findkeyval(pb_key_path, rq->vars); |
598 |
598 |
599 return ret; |
599 return ret; |
600 } |
600 } |
601 |
601 |
602 int webdav_put(pblock *pb, Session *sn, Request *rq) { |
602 int webdav_put(pblock *pb, Session *sn, Request *rq) { |
603 return REQ_ABORTED; |
603 char *path = pblock_findkeyval(pb_key_path, rq->vars); |
|
604 |
|
605 VFSContext *vfs = vfs_request_context(sn, rq); |
|
606 if(!vfs) { |
|
607 protocol_status(sn, rq, PROTOCOL_SERVER_ERROR, NULL); |
|
608 return REQ_ABORTED; |
|
609 } |
|
610 |
|
611 struct stat s; |
|
612 int create_file = 0; |
|
613 if(vfs_stat(vfs, path, &s)) { |
|
614 if(vfs->vfs_errno == ENOENT) { |
|
615 create_file = O_CREAT; |
|
616 } else { |
|
617 protocol_status(sn, rq, util_errno2status(vfs->vfs_errno), NULL); |
|
618 return REQ_ABORTED; |
|
619 } |
|
620 } |
|
621 |
|
622 if(S_ISDIR(s.st_mode)) { |
|
623 // PUT on collections is not allowed |
|
624 protocol_status(sn, rq, PROTOCOL_METHOD_NOT_ALLOWED, NULL); |
|
625 return REQ_ABORTED; |
|
626 } |
|
627 |
|
628 SYS_FILE fd = vfs_open(vfs, path, O_RDONLY | create_file); |
|
629 if(!fd) { |
|
630 // if it fails, vfs_open sets http status code |
|
631 return REQ_ABORTED; |
|
632 } |
|
633 |
|
634 // TODO: check permissions, lock, ... |
|
635 |
|
636 // all checks done |
|
637 |
|
638 char *expect = pblock_findkeyval(pb_key_expect, rq->headers); |
|
639 if(expect) { |
|
640 if(!strcasecmp(expect, "100-continue")) { |
|
641 if(http_send_continue(sn)) { |
|
642 return REQ_ABORTED; |
|
643 } |
|
644 } |
|
645 } |
|
646 |
|
647 char in[4096]; |
|
648 int r; |
|
649 while((r = netbuf_getbytes(sn->inbuf, in, 2048)) > 0) { |
|
650 int w = 0; |
|
651 while(w < r) { |
|
652 w += system_fwrite(fd, in, r); |
|
653 } |
|
654 } |
|
655 |
|
656 system_fclose(fd); |
|
657 |
|
658 int status = create_file ? PROTOCOL_CREATED : PROTOCOL_NO_CONTENT; |
|
659 protocol_status(sn, rq, status, NULL); |
|
660 |
|
661 return REQ_PROCEED; |
604 } |
662 } |
605 |
663 |
606 int webdav_copy(pblock *pb, Session *sn, Request *rq) { |
664 int webdav_copy(pblock *pb, Session *sn, Request *rq) { |
607 return REQ_ABORTED; |
665 return REQ_ABORTED; |
608 } |
666 } |