public byte[] doAPICall(BaseRequestHandler caller, string path, Stream requestData, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
{
OSDMap resp = new OSDMap();
object checking = Enum.Parse(typeof(WebAPIHttpMethod), caller.HttpMethod);
if (checking != null)
{
WebAPIHttpMethod HttpMethod = (WebAPIHttpMethod)checking;
if (m_APIMethods.ContainsKey(HttpMethod))
{
string methodPath = path.Substring(caller.Path.Length).Trim();
if (methodPath != string.Empty && methodPath.Substring(0, 1) == "/")
{
methodPath = methodPath.Substring(1);
}
string[] parts = new string[0];
if (methodPath != string.Empty)
{
parts = methodPath.Split('/');
}
for (int i = 0; i < parts.Length; ++i)
{
parts[i] = HttpUtility.UrlDecode(parts[i]);
}
if (parts.Length == 0)
{
httpResponse.StatusCode = 404;
return new byte[0];
}
string method = parts.Length < 1 ? string.Empty : parts[0];
try
{
OSDMap args = new OSDMap();
string body;
if (HttpMethod == WebAPIHttpMethod.GET)
{
foreach (string key in httpRequest.Query.Keys)
{
args[HttpUtility.UrlDecode(key)] = ((OSDMap)OSDParser.DeserializeJson("{\"foo\":" + HttpUtility.UrlDecode(httpRequest.Query[key].ToString()) + "}"))["foo"];
}
body = OSDParser.SerializeJsonString(args);
MainConsole.Instance.TraceFormat("[WebAPI]: HTTP GET {0} query String: {1}", method, body);
}
else if (HttpMethod == WebAPIHttpMethod.POST)
{
StreamReader sr = new StreamReader(requestData);
body = sr.ReadToEnd();
sr.Close();
body = body.Trim();
args = body == string.Empty ? new OSDMap(0) : (OSDMap)OSDParser.DeserializeJson(body);
MainConsole.Instance.TraceFormat("[WebAPI]: HTTP POST {0} query String: {1}", method, body);
}
//Make sure that the person who is calling can access the web service
if (AllowAPICall(method, httpRequest, httpResponse))
{
if (m_APIMethods[HttpMethod].ContainsKey(method))
{
object[] methodArgs = new object[1] { args }; ;
WebAPIMethod attr = (WebAPIMethod)Attribute.GetCustomAttribute(m_APIMethods[HttpMethod][method], typeof(WebAPIMethod));
if (attr.PassOnRequestingAgentID)
{
methodArgs = new object[2] { args, authUser(httpRequest) };
}
resp = (OSDMap)m_APIMethods[HttpMethod][method].Invoke(this, methodArgs);
}
else
{
resp["Failed"] = "Unsupported method";
}
}
}
catch (Exception e)
{
MainConsole.Instance.TraceFormat("[WebAPI] Exception thrown: " + e.ToString());
}
if (resp.Count == 0)
{
resp.Add("response", OSD.FromString("Failed"));
}
}
else
{
httpResponse.StatusCode = 405;
resp["Failed"] = string.Format("No methods implemented for HTTP {0}", HttpMethod.ToString());
}
}
else
{
httpResponse.StatusCode = 405;
resp["Failed"] = "Method Not Allowed";
}
UTF8Encoding encoding = new UTF8Encoding();
httpResponse.ContentType = "application/json";
return encoding.GetBytes(OSDParser.SerializeJsonString(resp, true));
}