public void Curl(IManosContext ctx, string url, string method, string auth, string [] header_keys = null)
{
// TODO: Should we set the Host header automagically?
Uri u = null;
if (!Uri.TryCreate (url, UriKind.Absolute, out u)) {
CurlError (ctx, "Url is invalid: {0}", url);
return;
}
if (u.Scheme == "https") {
CurlError (ctx, "https connections are not supported yet.");
return;
}
{
// TODO: Manos will handle this DNS stuff internally soon.
IPAddress [] addrs = Dns.GetHostAddresses (u.Host);
if (addrs.Length == 0) {
CurlError (ctx, "Could not resolve host: {0}", u.Host);
return;
}
UriBuilder builder = new UriBuilder (u);
builder.Host = addrs [0].ToString ();
url = builder.ToString ();
}
HttpRequest r = new HttpRequest (url) {
Method = GetHttpMethod (method),
};
r.Headers.SetNormalizedHeader ("Host", u.Host);
if (header_keys != null) {
var header_vals = ctx.Request.Data.GetList ("header_vals");
for (int i = 0; i < header_keys.Length; i++) {
r.Headers.SetHeader (header_keys [i], header_vals [i].SafeValue);
}
}
if (auth == "basic")
HttpUtility.AddBasicAuthentication (r, ctx.Request.Data ["username"], ctx.Request.Data ["password"]);
AddBody (ctx, r);
r.OnResponse += (response) => {
var res = new Dictionary<object,object> ();
var response_md = new StringBuilder ();
var request_md = new StringBuilder ();
r.WriteMetadata (request_md);
response.WriteMetadata (response_md);
res ["header"] = PrettyPrintMetadata (response_md.ToString ());
res ["request"] = PrettyPrintMetadata (request_md.ToString ()) + PrintBody (r.GetBody ());
res ["body"] = "<div><pre>" + HttpUtility.HtmlEncode (response.PostBody) + "</pre></div>";
ctx.Response.End (JSON.JsonEncode (res));
};
try {
r.Execute ();
} catch (Exception e) {
CurlError (ctx, e.Message);
}
}