/* Copyright (c) Citrix Systems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using XenAdmin;
using XenCenterLib;
using XenAdmin.Network;
using XenAdmin.Actions;
namespace XenAPI
{
public class HTTPHelper
{
public enum ProxyStyle
{
// Note that these numbers make it into user settings files, so need to be preserved.
DirectConnection = 0,
SystemProxy = 1,
SpecifiedProxy = 2
}
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
//
// The following functions do puts and gets to and from the filesystem
// updating Actions etc where appropriate.
//
///
/// HTTP PUT file from path to HTTP action f, updating action with progress every 500ms.
///
/// Action on which to update the progress
/// Whether to apply a timeout
/// path of file to put
public static String Put(AsyncAction action, bool timeout, string path, string hostname, Delegate f, params object[] p)
{
return Put(action, XenAdminConfigManager.Provider.GetProxyTimeout(timeout), path, hostname, f, p);
}
///
/// HTTP PUT file from path to HTTP action f, updating action with progress every 500ms.
///
/// Action on which to update the progress
/// Timeout value in ms
/// path of file to put
public static String Put(AsyncAction action, int timeout, string path, string hostname, Delegate f, params object[] p)
{
Session session = action.Session;
action.RelatedTask = XenAPI.Task.create(session, "uploadTask", hostname);
try
{
HTTP.UpdateProgressDelegate progressDelegate = delegate(int percent)
{
action.Tick(percent, action.Description);
};
return Put(progressDelegate, action.GetCancelling, timeout, action.Connection,
action.RelatedTask, ref session, path, hostname, f, p);
}
finally
{
action.Session = session;
Task.destroy(session, action.RelatedTask);
}
}
///
/// A general HttpPut method, with delegates for progress and cancelling
///
/// Delegate called periodically (500ms) with percent complete
/// Delegate called periodically to see if need to cancel
/// Whether to apply a timeout
/// The task used to track this http get
/// Session used to make api calls (may be replaced if logged out, hence ref)
/// Path to file to put
/// The result of the task passed in
public static String Put(XenAPI.HTTP.UpdateProgressDelegate progressDelegate, HTTP.FuncBool cancellingDelegate, bool timeout,
IXenConnection connection, XenRef task, ref Session session, string path,
string hostname, Delegate f, params object[] p)
{
return Put(progressDelegate, cancellingDelegate, XenAdminConfigManager.Provider.GetProxyTimeout(timeout),
connection, task, ref session, path, hostname, f, p);
}
///
/// A general HttpPut method, with delegates for progress and cancelling
///
/// Delegate called periodically (500ms) with percent complete
/// Delegate called periodically to see if need to cancel
/// Timeout value in ms
/// The task used to track this http get
/// Session used to make api calls (may be replaced if logged out, hence ref)
/// Path to file to put
/// The result of the task passed in
public static String Put(XenAPI.HTTP.UpdateProgressDelegate progressDelegate, HTTP.FuncBool cancellingDelegate, int timeout,
IXenConnection connection, XenRef task, ref Session session, string path,
string hostname, Delegate f, params object[] p)
{
log.DebugFormat("HTTP PUTTING file from {0} to {1}", path, hostname);
HTTP.FuncBool cancellingDelegate2 = (HTTP.FuncBool)delegate()
{
return (XenAdminConfigManager.Provider.ForcedExiting ||
cancellingDelegate != null && cancellingDelegate());
};
try
{
List