/// <inheritdoc /> public HttpRequestMessage Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) { if (MessagePackBinary.IsNil(bytes, offset)) { readSize = 1; return(null); } int startOffset = offset; int count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); offset += readSize; if (count != 5) { throw new InvalidOperationException("Request format invalid."); } // RequestPart.Version: string versionStr = MessagePackBinary.ReadString(bytes, offset, out readSize); offset += readSize; if (!Version.TryParse(versionStr, out Version version)) { throw new InvalidOperationException($"Request version '{versionStr}' in invalid format!"); } // RequestPart.UriScheme: string uriStr = MessagePackBinary.ReadString(bytes, offset, out readSize); offset += readSize; if (!Uri.TryCreate(uriStr, UriKind.Absolute, out Uri uri)) { throw new InvalidOperationException($"Request Uri '{uri}' could not be parsed"); } // RequestPart.Method: HttpMethod method = HttpMethodFormatter.Instance.Deserialize(bytes, offset, formatterResolver, out readSize); offset += readSize; // Create request so we can add headers and content HttpRequestMessage request = new HttpRequestMessage(method, uri) { Version = version }; // RequestPart.Headers: int headersCount = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize); offset += readSize; if (headersCount > 0) { while (headersCount-- > 0) { string key = MessagePackBinary.ReadString(bytes, offset, out readSize); offset += readSize; int valuesCount = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); offset += readSize; // Note, we can't have an empty array, and if we tried adding one it would be ignored anyway // therefore it's safe to just start looping and call add for each entry. while (valuesCount-- > 0) { string headerValue = MessagePackBinary.ReadString(bytes, offset, out readSize); offset += readSize; request.Headers.TryAddWithoutValidation(key, headerValue); } } } // RequestPart.Content: #pragma warning disable DF0023 // Marks undisposed objects assinged to a property, originated from a method invocation. request.Content = HttpContentFormatter.Deserialize(bytes, offset, out readSize); #pragma warning restore DF0023 // Marks undisposed objects assinged to a property, originated from a method invocation. offset += readSize; readSize = offset - startOffset; return(request); }
/// <inheritdoc /> public HttpResponseMessage Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) { if (MessagePackBinary.IsNil(bytes, offset)) { readSize = 1; return(null); } int startOffset = offset; int count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); offset += readSize; if (count != 5) { throw new InvalidOperationException("Response format invalid."); } // Version string versionStr = MessagePackBinary.ReadString(bytes, offset, out readSize); offset += readSize; if (!Version.TryParse(versionStr, out Version version)) { throw new InvalidOperationException($"Response version '{versionStr}' in invalid format!"); } // Status Code HttpStatusCode statusCode = (HttpStatusCode)MessagePackBinary.ReadUInt16(bytes, offset, out readSize); offset += readSize; // ReasonPhrase string reasonPhrase = MessagePackBinary.ReadString(bytes, offset, out readSize); offset += readSize; // Create response so we can start updating it's headers HttpResponseMessage response = new HttpResponseMessage(statusCode) { Version = version, ReasonPhrase = reasonPhrase }; // Headers int headersCount = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize); offset += readSize; if (headersCount > 0) { while (headersCount-- > 0) { string key = MessagePackBinary.ReadString(bytes, offset, out readSize); offset += readSize; int valuesCount = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); offset += readSize; // Note, we can't have an empty array, and if we tried adding one it would be ignored anyway // therefore it's safe to just start looping and call add for each entry. while (valuesCount-- > 0) { string headerValue = MessagePackBinary.ReadString(bytes, offset, out readSize); offset += readSize; response.Headers.TryAddWithoutValidation(key, headerValue); } } } // Content #pragma warning disable DF0023 // Marks undisposed objects assinged to a property, originated from a method invocation. response.Content = HttpContentFormatter.Deserialize(bytes, offset, out readSize); #pragma warning restore DF0023 // Marks undisposed objects assinged to a property, originated from a method invocation. readSize = offset - startOffset; return(response); }