// Encode outgoing data. // // Warning: not all type are supported (see below). protected override bool OnBeforeOutgoing(IHubOutgoingInvokerContext context) { _log.Debug(""); var args = context.Invocation.Args; for (int argI = 0; argI < args.Length; argI++) { if (typeof(IEncodable).IsAssignableFrom(args[argI].GetType())) { IEncodable model = (IEncodable)args[argI]; model.Encode(); } else if (args[argI].GetType() == typeof(string)) { args[argI] = HttpUtility.HtmlEncode(args[argI].ToString()); } else if (args[argI].GetType() == typeof(List <string>)) { var items = (List <string>)args[argI]; for (int itemI = 0; itemI < items.Count; itemI++) { items[itemI] = HttpUtility.HtmlEncode(items[itemI]); } //} else { // Won't get encoded! } } return(base.OnBeforeOutgoing(context)); }
public static void Encode(string fileName, IEncodable value) { FileStream stream = File.Open(fileName, FileMode.Create); EncoderUtil.Encode(stream, value); stream.Close(); }
/// <summary> /// Gets the length of the URL that would be used for a simple redirect to carry /// this indirect message to its recipient. /// </summary> /// <param name="message">The message.</param> /// <returns>The number of characters in the redirect URL.</returns> static int getSizeOfPayload(IEncodable message) { Debug.Assert(message != null); UriBuilder redirect = new UriBuilder(message.RedirectUrl); UriUtil.AppendQueryArgs(redirect, message.EncodedFields); return(redirect.Uri.AbsoluteUri.Length); }
/// <summary> /// Gets a URL that can be requested to send an indirect message. /// </summary> public static Uri ExtractUrl(this IResponse message) { DotNetOpenId.Response response = (DotNetOpenId.Response)message; IEncodable encodable = response.EncodableMessage; UriBuilder builder = new UriBuilder(encodable.RedirectUrl); UriUtil.AppendQueryArgs(builder, encodable.EncodedFields); return(builder.Uri); }
public GetRequest(ConsulClient client, string url, QueryOptions options, IEncodable filter) : base(client, url, HttpMethod.Get) { if (string.IsNullOrEmpty(url)) { throw new ArgumentException(nameof(url)); } Options = options ?? QueryOptions.Default; Filter = filter; }
protected virtual Response Create301RedirectResponse(IEncodable message) { WebHeaderCollection headers = new WebHeaderCollection(); UriBuilder builder = new UriBuilder(message.RedirectUrl); UriUtil.AppendQueryArgs(builder, message.EncodedFields); headers.Add(HttpResponseHeader.Location, builder.Uri.AbsoluteUri); Logger.DebugFormat("Redirecting to {0}", builder.Uri.AbsoluteUri); return(new Response(HttpStatusCode.Redirect, headers, new byte[0], message)); }
private T Execute <T>(IEncodable model, string apiUri) { var content = model.AsFormUrlEncodedContent(); var responseMessage = _client.PostAsync(apiUri, content).Result; responseMessage.EnsureSuccessStatusCode(); var response = responseMessage.Content.ReadAsStringAsync().Result; return(JsonConvert.DeserializeObject <T>(response)); }
public void Encode(IEncodable value) { bool hasValue = value != null; this.Encode(hasValue); if (hasValue) { value.Encode(this); } }
public ExtensionObject(IEncodable body, ExpandedNodeId typeId) { if (body == null) { this.BodyType = BodyType.None; return; } this.Body = body; this.BodyType = BodyType.Encodable; this.TypeId = typeId; }
public ExtensionObject(IEncodable body) { if (body == null) { this.BodyType = BodyType.None; return; } this.Body = body; this.BodyType = BodyType.Encodable; this.TypeId = body.GetType().GetTypeInfo().GetCustomAttribute <BinaryEncodingIdAttribute>(false)?.NodeId ?? throw new ServiceResultException(StatusCodes.BadDataEncodingUnsupported); }
static int getSizeOfPayload(IEncodable message) { Debug.Assert(message != null); int size = 0; foreach (var field in message.EncodedFields) { size += field.Key.Length; size += field.Value.Length; } return(size); }
public override Response Encode(IEncodable encodable) { var response = encodable as EncodableResponse; if (response != null) { if (response.NeedsSigning) { signatory.Sign(response); } } return(base.Encode(encodable)); }
/// <summary> /// Encodes messages into <see cref="Response"/> instances. /// </summary> public virtual Response Encode(IEncodable message) { if (message == null) { throw new ArgumentNullException("message"); } EncodingType encode_as = message.EncodingType; Response wr; WebHeaderCollection headers = new WebHeaderCollection(); switch (encode_as) { case EncodingType.DirectResponse: Logger.DebugFormat("Sending direct message response:{0}{1}", Environment.NewLine, Util.ToString(message.EncodedFields)); HttpStatusCode code = (message is Exception) ? HttpStatusCode.BadRequest : HttpStatusCode.OK; // Key-Value Encoding is how response bodies are sent. // Setting the content-type to something other than text/html or text/plain // prevents free hosted sites like GoDaddy's from automatically appending // the <script/> at the end that adds a banner, and invalidating our response. headers.Add(HttpResponseHeader.ContentType, KeyValueFormContentType); wr = new Response(code, headers, ProtocolMessages.KeyValueForm.GetBytes(message.EncodedFields), message); break; case EncodingType.IndirectMessage: Logger.DebugFormat("Sending indirect message:{0}{1}", Environment.NewLine, Util.ToString(message.EncodedFields)); // TODO: either redirect or do a form POST depending on payload size. Debug.Assert(message.RedirectUrl != null); if (getSizeOfPayload(message) <= GetToPostThreshold) { wr = Create301RedirectResponse(message); } else { wr = CreateFormPostResponse(message); } break; default: Logger.ErrorFormat("Cannot encode response: {0}", message); wr = new Response(HttpStatusCode.BadRequest, headers, new byte[0], message); break; } return(wr); }
/// <param name="code">The HTTP status code.</param> /// <param name="headers">The collection of any HTTP headers that should be included. Cannot be null, but can be an empty collection.</param> /// <param name="body">The payload of the response, if any. Cannot be null, but can be an empty array.</param> /// <param name="encodableMessage"> /// Used to assist testing to decipher the field contents of a Response. /// </param> internal Response(HttpStatusCode code, WebHeaderCollection headers, byte[] body, IEncodable encodableMessage) { if (headers == null) { throw new ArgumentNullException("headers"); } if (body == null) { throw new ArgumentNullException("body"); } Debug.Assert(encodableMessage != null, "For testing, this is useful to have."); Code = code; Headers = headers ?? new WebHeaderCollection(); Body = body; EncodableMessage = encodableMessage; }
protected virtual Response CreateFormPostResponse(IEncodable message) { WebHeaderCollection headers = new WebHeaderCollection(); MemoryStream body = new MemoryStream(); StreamWriter bodyWriter = new StreamWriter(body); StringBuilder hiddenFields = new StringBuilder(); foreach (var field in message.EncodedFields) { hiddenFields.AppendFormat("\t<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />\r\n", HttpUtility.HtmlEncode(field.Key), HttpUtility.HtmlEncode(field.Value)); } bodyWriter.WriteLine(FormPostFormat, HttpUtility.HtmlEncode(message.RedirectUrl.AbsoluteUri), hiddenFields); bodyWriter.Flush(); return(new Response(HttpStatusCode.OK, headers, body.ToArray(), message)); }
public MapEntity Read() { MapEntity result = null; BinaryInput stream = _stream as BinaryInput; EntityType type = (EntityType)stream.ReadByte(); if (type == EntityType.Building) { result = new BuildingEntity(factory, ScrollAffected); } IEncodable encodable = result as IEncodable; if (encodable != null) { encodable.Decode(stream); } return(result); }
public void Write(IAction t) { BinaryOutput stream = _stream as BinaryOutput; int i = 0; if (t is SetTileAction) { i = 1; } if (t is MultiAction) { i = 2; } if (t is FillAction) { i = 3; } if (t is RectangleAction) { i = 4; } if (t is AddEntityAction) { i = 5; } if (t is RemoveEntityAction) { i = 6; } stream.Write(i); IEncodable encodable = t as IEncodable; if (encodable != null) { encodable.Encode(stream); } }
public IAction Read() { IAction result = null; BinaryInput stream = _stream as BinaryInput; int i = stream.ReadInt32(); if (i == 1) { result = new SetTileAction(); } if (i == 2) { result = new MultiAction(); } if (i == 3) { result = new FillAction(); } if (i == 4) { result = new RectangleAction(); } if (i == 5) { result = new AddEntityAction(); } if (i == 6) { result = new RemoveEntityAction(); } IEncodable encodable = result as IEncodable; if (encodable != null) { encodable.Decode(stream); } return(result); }
public NamedEncodable(string name, IEncodable value) : base(name) { this.value = value; }
internal GetRequest <TOut> Get <TOut>(string path, QueryOptions opts = null, IEncodable filter = null) { return(new GetRequest <TOut>(this, path, opts, filter)); }
public IEncodedValue(IEncodable value) { }
public ExtensionObject(IEncodable body) { this.Body = body; this.BodyType = body != null ? BodyType.Encodable : BodyType.None; }
private static void CheckEncoded(string expected, IEncodable expression) => Assert.Equal(expected, expression.Encode());
public void Write(IEncodable value) { value.Encode(this); }
public override IEncodable Decode(BinaryInput stream) { base.Decode(stream); value = stream.ReadObject <T>(); return(this); }
public EncodeEventArgs(IEncodable encodable) { Message = encodable; }
public static void Encode(Stream stream, IEncodable value) { BinaryOutput output = new BinaryOutput(stream); value.Encode(output); }
public static void Encode(BinaryOutput output, IEncodable value) { value.Encode(output); }
public Variant(IEncodable value) { this.Value = new ExtensionObject(value); this.Type = VariantType.ExtensionObject; this.ArrayDimensions = null; }
public async static Task SendSecretMessage(Stream s, byte[] key, IEncodable message, CancellationToken token = default) { await SendSecretMessage(s, key, message.Encode(), token); }
public override Response Encode(IEncodable encodable) { var response = encodable as EncodableResponse; if (response != null) { if (response.NeedsSigning) { signatory.Sign(response); } } return base.Encode(encodable); }
/// <param name="code">The HTTP status code.</param> /// <param name="headers">The collection of any HTTP headers that should be included. Cannot be null, but can be an empty collection.</param> /// <param name="body">The payload of the response, if any. Cannot be null, but can be an empty array.</param> /// <param name="encodableMessage"> /// Used to assist testing to decipher the field contents of a Response. /// </param> internal Response(HttpStatusCode code, WebHeaderCollection headers, byte[] body, IEncodable encodableMessage) { if (headers == null) throw new ArgumentNullException("headers"); if (body == null) throw new ArgumentNullException("body"); Debug.Assert(encodableMessage != null, "For testing, this is useful to have."); Code = code; Headers = headers ?? new WebHeaderCollection(); Body = body; EncodableMessage = encodableMessage; }
/// <summary> /// Encodes messages into <see cref="Response"/> instances. /// </summary> public virtual Response Encode(IEncodable message) { if (message == null) throw new ArgumentNullException("message"); EncodingType encode_as = message.EncodingType; Response wr; WebHeaderCollection headers = new WebHeaderCollection(); switch (encode_as) { case EncodingType.DirectResponse: Logger.DebugFormat("Sending direct message response:{0}{1}", Environment.NewLine, Util.ToString(message.EncodedFields)); HttpStatusCode code = (message is Exception) ? HttpStatusCode.BadRequest : HttpStatusCode.OK; // Key-Value Encoding is how response bodies are sent. // Setting the content-type to something other than text/html or text/plain // prevents free hosted sites like GoDaddy's from automatically appending // the <script/> at the end that adds a banner, and invalidating our response. headers.Add(HttpResponseHeader.ContentType, KeyValueFormContentType); wr = new Response(code, headers, ProtocolMessages.KeyValueForm.GetBytes(message.EncodedFields), message); break; case EncodingType.IndirectMessage: Logger.DebugFormat("Sending indirect message:{0}{1}", Environment.NewLine, Util.ToString(message.EncodedFields)); // TODO: either redirect or do a form POST depending on payload size. Debug.Assert(message.RedirectUrl != null); if (getSizeOfPayload(message) <= GetToPostThreshold) wr = Create301RedirectResponse(message); else wr = CreateFormPostResponse(message); break; default: Logger.ErrorFormat("Cannot encode response: {0}", message); wr = new Response(HttpStatusCode.BadRequest, headers, new byte[0], message); break; } return wr; }
protected virtual Response CreateFormPostResponse(IEncodable message) { WebHeaderCollection headers = new WebHeaderCollection(); MemoryStream body = new MemoryStream(); StreamWriter bodyWriter = new StreamWriter(body); StringBuilder hiddenFields = new StringBuilder(); foreach (var field in message.EncodedFields) { hiddenFields.AppendFormat("\t<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />\r\n", HttpUtility.HtmlEncode(field.Key), HttpUtility.HtmlEncode(field.Value)); } bodyWriter.WriteLine(FormPostFormat, HttpUtility.HtmlEncode(message.RedirectUrl.AbsoluteUri), hiddenFields); bodyWriter.Flush(); return new Response(HttpStatusCode.OK, headers, body.ToArray(), message); }
public static void DecodeCopy(IEncodable encodable, IEncodable toCopy) { Decode(encodable, Encode(toCopy.Encode())); }
public static string Encode(IEncodable encodable) { return Encode(encodable.Encode()); }
internal FaultyRequest(OpenIdProvider provider, IEncodable response) : base(provider) { Response = response; }
public static void Decode(IEncodable encodable, Encoding encodedElement) { encodable.Decode(encodedElement); }
protected virtual Response Create301RedirectResponse(IEncodable message) { WebHeaderCollection headers = new WebHeaderCollection(); UriBuilder builder = new UriBuilder(message.RedirectUrl); UriUtil.AppendQueryArgs(builder, message.EncodedFields); headers.Add(HttpResponseHeader.Location, builder.Uri.AbsoluteUri); Logger.DebugFormat("Redirecting to {0}", builder.Uri.AbsoluteUri); return new Response(HttpStatusCode.Redirect, headers, new byte[0], message); }
static int getSizeOfPayload(IEncodable message) { Debug.Assert(message != null); int size = 0; foreach (var field in message.EncodedFields) { size += field.Key.Length; size += field.Value.Length; } return size; }
public static void Decode(IEncodable encodable, string encodedString) { // Debug.Log ("Decoding string \""+encodedString+"\""); encodable.Decode(new Encoding(encodedString)); }