public IEnumerator<IYield> WildCardMessage(DreamContext context, DreamMessage request, Result<DreamMessage> response) { // convert dream message into an XML document XDoc xmessage = new XMessage(request); // respond by sending the message back response.Return(DreamMessage.Ok(xmessage)); yield break; }
public IEnumerator<IYield> WildCardHeaders(DreamContext context, DreamMessage request, Result<DreamMessage> response) { // convert dream message into an XML document XDoc xmessage = new XMessage(request); // select <headers> element XDoc headers = xmessage["headers"]; // send it back response.Return(DreamMessage.Ok(headers)); yield break; }
public IEnumerator<IYield> WildCardBody(DreamContext context, DreamMessage request, Result<DreamMessage> response) { // convert dream message into an XML document XDoc xmessage = new XMessage(request); // select first child of <body> element XDoc body = xmessage["body"]; // respond by sending only the body back response.Return(DreamMessage.Ok(body)); yield break; }
/// <summary> /// WARNING: This method is thread-blocking. Please avoid using it if possible. /// </summary> internal static XDoc ExecuteAction(Plug env, DreamHeaders headers, XDoc action) { string verb = action["@verb"].Contents; string path = action["@path"].Contents; if((path.Length > 0) && (path[0] == '/')) { path = path.Substring(1); } XUri uri; if(!XUri.TryParse(path, out uri)) { uri = env.Uri.AtAbsolutePath(path); } // create message DreamMessage message = DreamMessage.Ok(GetActionBody(action)); message.Headers.AddRange(headers); // apply headers foreach(XDoc header in action["header"]) { message.Headers[header["@name"].Contents] = header.Contents; } // BUG #814: we need to support events // execute action DreamMessage reply = Plug.New(uri).Invoke(verb, message); // prepare response XDoc result = new XMessage(reply); string ID = action["@ID"].Contents; if(!string.IsNullOrEmpty(ID)) { result.Root.Attr("ID", ID); } return result; }
/// <summary> /// WARNING: This method is thread-blocking. Please avoid using it if possible. /// </summary> internal static XDoc ExecutePipe(Plug env, DreamHeaders headers, XDoc pipe) { DreamMessage message = null; foreach(XDoc action in pipe["action"]) { string verb = action["@verb"].Contents; string path = action["@path"].Contents; XUri uri; if(!XUri.TryParse(path, out uri)) { uri = env.Uri.AtPath(path); } // create first message if(message == null) { message = DreamMessage.Ok(GetActionBody(action)); } message.Headers.AddRange(headers); // apply headers foreach(XDoc header in action["header"]) { message.Headers[header["@name"].Contents] = header.Contents; } // execute action message = Plug.New(uri).Invoke(verb, message); if(!message.IsSuccessful) { break; } } // prepare response if(message == null) { return XDoc.Empty; } XDoc result = new XMessage(message); string ID = pipe["@ID"].Contents; if(!string.IsNullOrEmpty(ID)) { result.Root.Attr("ID", ID); } return result; }
public Yield GetTest(DreamContext context, DreamMessage request, Result<DreamMessage> response) { int status = context.GetParam("status", (int)DreamStatus.Ok); XDoc doc = new XMessage(request); doc["status"].ReplaceValue(status.ToInvariantString()); doc.Elem("verb", context.Verb); DreamMessage reply = new DreamMessage((DreamStatus)status, null, doc); string cookieValue = context.GetParam("cookie", null); if(cookieValue != null) { reply.Cookies.Add(DreamCookie.NewSetCookie("test-cookie", cookieValue, Self.Uri, GlobalClock.UtcNow.AddHours(1.0))); } if(context.Verb.EqualsInvariant("HEAD")) { reply = new DreamMessage(reply.Status, null, MimeType.XML, new byte[0]); reply.Headers.ContentLength = doc.ToString().Length; response.Return(reply); } else { response.Return(reply); } yield break; }