<commit_before>/*
 * grunt-webdav-sync
 * https://github.com/avisi/grunt-webdav-sync
 *
 * Copyright (c) 2013 <NAME>.
 * Licensed under the MIT license.
 */

'use strict';

var http = require("http");
var path = require("path");
var url = require('url');
var async = require('async');
var request = require('request');

var createRequestOptions = function(remoteURL, method) {
    var parsedUrl = url.parse(remoteURL);
    var auth = parsedUrl.auth; 

    if(auth !== null) {
        var splittedString = auth.split(":");
        auth = {
            user: splittedString[0],
            pass: splittedString[1],
            sendImmediately: false   
        };
    }

    var options = {
        uri: parsedUrl,
        method: method,
        auth: auth
    }; 

    options.uri.auth = "";

    return options
};

var deleteFolderOnRemote = function(grunt, remoteURL, callback) {
    grunt.verbose.writeln("Deleting folder: " + remoteURL); 

    var options = createRequestOptions(remoteURL, 'DELETE');

    request(options, function(error, res, body) {
        if(res.statusCode === 204 || res.statusCode === 404) { //created
            grunt.verbose.writeln("Folder: " + remoteURL + " deleted");
            callback(null, remoteURL);
        } else if (res.statusCode === 423) {
            callback({status: res.statusCode, message: "Could not remove the locked folder For url: " + remoteURL}, null);
        } else {
            callback({status: res.statusCode, message: "Unknown error while deleting a dir gave statuscode: " + res.statusCode}, null);
        }
    }).setMaxListeners(0);
};

var createFolderOnRemote = function(grunt, remoteURL, callback) {
    grunt.verbose.writeln("Creating folder: " + remoteURL);
    
    var options = createRequestOptions(remoteURL, 'MKCOL');
    

    request(options, function(error, res, body) {
        if(res.statusCode === 201) { //created
            grunt.verbose.writeln("Folder: " + remoteURL + " created");
            callback(null, remoteURL);
        } else if (res.statusCode === 401) {
            callback({status: res.statusCode, message: "Resource requires authorization or authorization was denied. For url: " + remoteURL}, null);
        } else if (res.statusCode === 403) {
            callback({status: res.statusCode, message: "The server does not allow collections to be created at the specified location, or the parent collection of the specified request URI exists but cannot accept members."}, null);<commit_msg>remote directories missing is not an error<commit_after>/*
 * grunt-webdav-sync
 * https://github.com/avisi/grunt-webdav-sync
 *
 * Copyright (c) 2013 <NAME>.
 * Licensed under the MIT license.
 */

'use strict';

var http = require("http");
var path = require("path");
var url = require('url');
var async = require('async');
var request = require('request');

var createRequestOptions = function(remoteURL, method) {
    var parsedUrl = url.parse(remoteURL);
    var auth = parsedUrl.auth; 

    if(auth !== null) {
        var splittedString = auth.split(":");
        auth = {
            user: splittedString[0],
            pass: splittedString[1],
            sendImmediately: false   
        };
    }

    var options = {
        uri: parsedUrl,
        method: method,
        auth: auth
    }; 

    options.uri.auth = "";

    return options
};

var deleteFolderOnRemote = function(grunt, remoteURL, callback) {
    grunt.verbose.writeln("Deleting folder: " + remoteURL); 

    var options = createRequestOptions(remoteURL, 'DELETE');

    request(options, function(error, res, body) {
        if(res.statusCode === 204 || res.statusCode === 404) { //created
            grunt.verbose.writeln("Folder: " + remoteURL + " deleted");
            callback(null, remoteURL);
        } else if (res.statusCode === 207) { // res.body contains an XML WebDAV multistatus message; see http://tools.ietf.org/search/rfc2518#section-11
            var matches = res.body.match("status>([^<]+)</"); // cheaper than parsing string using xml2js or similar            
            var status = matches.length > 1 ? matches[1] : "HTTP/1.1 000 No status message returned in " + body;
            // D:status element contains an HTTP response status line (see http://tools.ietf.org/search/rfc2616#section-6.1) like so:
            // HTTP-Version SP Status-Code SP Reason-Phrase CRLF
            matches = status.match("^[^\s]+ ([0-9]{3}) (.+)$");
            var statusCode = parseInt(matches[1]);
            var statusMessage = matches[2];
            
            if (statusCode === 404) { // we don't actually care if remote directories don't exist (yet)
                callback(null, remoteURL);
            } else {
                callback({status: statusCode, message: statusMessage}, null);
            }
        } else if (res.statusCode === 423) {
            callback({status: res.statusCode, message: "Could not remove the locked folder For url: " + remoteURL}, null);
        } else {
            callback({status: res.statusCode, message: "Unknown error while deleting a dir gave statuscode: " + res.statusCode}, null);
        }
    }).setMaxListeners(0);
};

var createFolderOnRemote = function(grunt, remoteURL, callback) {
    grunt.verbose.writeln("Creating folder: " + remoteURL);
    
    var options = createRequestOptions(remoteURL, 'MKCOL');
    

    request(options, function(error, res, body) {
        if(res.statusCode === 201) { //created
            grunt.verbose.writeln("Folder: " + remoteURL + " created");
            callback(null, remoteURL);
        } else if (res.statusCode === 401) {
            callback({status: res.statusCode, message: "Resource requires authorization or authorization was denied. For url: " + remoteURL}, null);
        } else if (res.statusCode === 403) {
            callback({status: res.statusCode, message: "The server does not allow collections to be created at the specified location, or the parent collection of the specified request URI exists but cannot accept members."}, null);
{'fraction_non_alphanumeric': 0.1309030654515327, 'fraction_numerical': 0.013421706710853356, 'mean_word_length': 2.709895513214505, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1265129', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1647, 'n_words': 486}