/// <summary> /// Gets the decoded text content using the provided charset encoding to /// override the charset specified in the Content-Type parameters. /// </summary> /// <remarks> /// Uses the provided charset encoding to convert the raw text content /// into a unicode string, overriding any charset specified in the /// Content-Type header. /// </remarks> /// <returns>The decoded text.</returns> /// <param name="encoding">The charset encoding to use.</param> /// <exception cref="System.ArgumentNullException"> /// <paramref name="encoding"/> is <c>null</c>. /// </exception> public string GetText(Encoding encoding) { if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } if (Content == null) { return(string.Empty); } using (var memory = new MemoryStream()) { using (var filtered = new FilteredStream(memory)) { filtered.Add(FormatOptions.Default.CreateNewLineFilter()); Content.DecodeTo(filtered); filtered.Flush(); } #if !PORTABLE && !NETSTANDARD var buffer = memory.GetBuffer(); #else var buffer = memory.ToArray(); #endif return(encoding.GetString(buffer, 0, (int)memory.Length)); } }
/// <summary> /// Reads a single line from the <see cref="Pop3Stream"/>. /// </summary> /// <returns>The line.</returns> /// <param name="cancellationToken">The cancellation token.</param> /// <exception cref="System.InvalidOperationException"> /// The engine is not connected. /// </exception> /// <exception cref="System.OperationCanceledException"> /// The operation was canceled via the cancellation token. /// </exception> /// <exception cref="System.IO.IOException"> /// An I/O error occurred. /// </exception> public string ReadLine(CancellationToken cancellationToken) { if (stream == null) { throw new InvalidOperationException(); } using (var memory = new MemoryStream()) { int offset, count; byte[] buf; while (!stream.ReadLine(out buf, out offset, out count, cancellationToken)) { memory.Write(buf, offset, count); } memory.Write(buf, offset, count); count = (int)memory.Length; #if !NETFX_CORE buf = memory.GetBuffer(); #else buf = memory.ToArray(); #endif try { return(UTF8.GetString(buf, 0, count)); } catch (DecoderFallbackException) { return(Latin1.GetString(buf, 0, count)); } } }
/// <summary> /// Reads a single line from the <see cref="Pop3Stream"/>. /// </summary> /// <returns>The line.</returns> /// <param name="cancellationToken">A cancellation token.</param> /// <exception cref="System.InvalidOperationException"> /// The engine is not connected. /// </exception> /// <exception cref="System.OperationCanceledException"> /// The operation was canceled via the cancellation token. /// </exception> /// <exception cref="System.IO.IOException"> /// An I/O error occurred. /// </exception> public string ReadLine(CancellationToken cancellationToken) { if (stream == null) { throw new InvalidOperationException(); } cancellationToken.ThrowIfCancellationRequested(); using (var memory = new MemoryStream()) { int offset, count; byte[] buf; while (!stream.ReadLine(out buf, out offset, out count)) { cancellationToken.ThrowIfCancellationRequested(); memory.Write(buf, offset, count); } memory.Write(buf, offset, count); count = (int)memory.Length; #if !NETFX_CORE buf = memory.GetBuffer(); #else buf = memory.ToArray(); #endif return(Latin1.GetString(buf, 0, count)); } }
private async void Button_Click(object sender, RoutedEventArgs e) { string uri = @"https://alexeyd.herokuapp.com/mpro5?a=" + a.Text; var client = new HttpClient(); byte[] request = await client.GetByteArrayAsync(new Uri(uri)); Portable.Text.Encoding encoding = Portable.Text.Encoding.GetEncoding(1251); var s = encoding.GetString(request, 0, request.Length); otv.Content = s; }
async Task <string> ReadLineAsync(bool doAsync, CancellationToken cancellationToken) { if (stream == null) { throw new InvalidOperationException(); } using (var memory = new MemoryStream()) { bool complete; byte[] buf; int count; do { if (doAsync) { complete = await stream.ReadLineAsync(memory, cancellationToken).ConfigureAwait(false); } else { complete = stream.ReadLine(memory, cancellationToken); } } while (!complete); count = (int)memory.Length; #if !NETFX_CORE && !NETSTANDARD buf = memory.GetBuffer(); #else buf = memory.ToArray(); #endif // Trim the <CR><LF> sequence from the end of the line. if (buf[count - 1] == (byte)'\n') { count--; if (buf[count - 1] == (byte)'\r') { count--; } } try { return(UTF8.GetString(buf, 0, count)); } catch (DecoderFallbackException) { return(Latin1.GetString(buf, 0, count)); } } }
private async void Edit_Click(object sender, RoutedEventArgs e) { var mw = Application.Current.Windows .Cast <Window>() .FirstOrDefault(window => window is MainWindow) as MainWindow; string uri = @"https://alexeyd.herokuapp.com/inbase?pk=" + Id.Text; var client = new HttpClient(); byte[] request = await client.GetByteArrayAsync(new Uri(uri)); Portable.Text.Encoding encoding = Portable.Text.Encoding.GetEncoding(1251); var s = encoding.GetString(request, 0, request.Length); if (s == "OK") { mw.frame.Source = "InfSyst/Edit.xaml".ToUri(); } }
/// <summary> /// Gets the decoded text content using the provided charset encoding to /// override the charset specified in the Content-Type parameters. /// </summary> /// <remarks> /// Uses the provided charset encoding to convert the raw text content /// into a unicode string, overriding any charset specified in the /// Content-Type header. /// </remarks> /// <returns>The decoded text.</returns> /// <param name="encoding">The charset encoding to use.</param> /// <exception cref="System.ArgumentNullException"> /// <paramref name="encoding"/> is <c>null</c>. /// </exception> public string GetText(Encoding encoding) { if (encoding == null) { throw new ArgumentNullException("encoding"); } using (var memory = new MemoryStream()) { ContentObject.DecodeTo(memory); #if PORTABLE var buffer = memory.ToArray(); #else var buffer = memory.GetBuffer(); #endif return(encoding.GetString(buffer, 0, (int)memory.Length)); } }
/// <summary> /// Reads a single line from the <see cref="Pop3Stream"/>. /// </summary> /// <returns>The line.</returns> /// <param name="cancellationToken">The cancellation token.</param> /// <exception cref="System.InvalidOperationException"> /// The engine is not connected. /// </exception> /// <exception cref="System.OperationCanceledException"> /// The operation was canceled via the cancellation token. /// </exception> /// <exception cref="System.IO.IOException"> /// An I/O error occurred. /// </exception> public string ReadLine(CancellationToken cancellationToken) { if (stream == null) { throw new InvalidOperationException(); } using (var memory = new MemoryStream()) { int offset, count; byte[] buf; while (!stream.ReadLine(out buf, out offset, out count, cancellationToken)) { memory.Write(buf, offset, count); } memory.Write(buf, offset, count); count = (int)memory.Length; #if !NETFX_CORE && !NETSTANDARD buf = memory.GetBuffer(); #else buf = memory.ToArray(); #endif // Trim the <CR><LF> sequence from the end of the line. if (buf[count - 1] == (byte)'\n') { count--; if (buf[count - 1] == (byte)'\r') { count--; } } try { return(UTF8.GetString(buf, 0, count)); } catch (DecoderFallbackException) { return(Latin1.GetString(buf, 0, count)); } } }
/// <summary> /// Gets the decoded text content using the provided charset encoding to /// override the charset specified in the Content-Type parameters. /// </summary> /// <remarks> /// Uses the provided charset encoding to convert the raw text content /// into a unicode string, overriding any charset specified in the /// Content-Type header. /// </remarks> /// <returns>The decoded text.</returns> /// <param name="encoding">The charset encoding to use.</param> /// <exception cref="System.ArgumentNullException"> /// <paramref name="encoding"/> is <c>null</c>. /// </exception> public string GetText (Encoding encoding) { if (encoding == null) throw new ArgumentNullException (nameof (encoding)); if (Content == null) return string.Empty; using (var memory = new MemoryStream ()) { Content.DecodeTo (memory); #if !PORTABLE && !NETSTANDARD var buffer = memory.GetBuffer (); #else var buffer = memory.ToArray (); #endif return encoding.GetString (buffer, 0, (int) memory.Length); } }
/// <summary> /// Read an SMTP server response. /// </summary> /// <remarks> /// Reads a full command response from the SMTP server. /// </remarks> /// <returns>The response.</returns> /// <param name="cancellationToken">The cancellation token.</param> /// <exception cref="System.ObjectDisposedException"> /// The stream has been disposed. /// </exception> /// <exception cref="System.OperationCanceledException"> /// The operation was canceled via the cancellation token. /// </exception> /// <exception cref="System.IO.IOException"> /// An I/O error occurred. /// </exception> /// <exception cref="SmtpProtocolException"> /// An SMTP protocol error occurred. /// </exception> public SmtpResponse ReadResponse(CancellationToken cancellationToken) { CheckDisposed(); using (var memory = new MemoryStream()) { bool needInput = inputIndex == inputEnd; bool complete = false; bool newLine = true; bool more = true; int code = 0; do { if (needInput) { ReadAhead(cancellationToken); needInput = false; } complete = false; do { int startIndex = inputIndex; if (newLine && inputIndex < inputEnd) { int value; if (!TryParseInt32(input, ref inputIndex, inputEnd, out value)) { throw new SmtpProtocolException("Unable to parse status code returned by the server."); } if (inputIndex == inputEnd) { inputIndex = startIndex; needInput = true; break; } if (code == 0) { code = value; } else if (value != code) { throw new SmtpProtocolException("The status codes returned by the server did not match."); } newLine = false; if (input[inputIndex] != (byte)'\r' && input[inputIndex] != (byte)'\n') { more = input[inputIndex++] == (byte)'-'; } else { more = false; } startIndex = inputIndex; } while (inputIndex < inputEnd && input[inputIndex] != (byte)'\r' && input[inputIndex] != (byte)'\n') { inputIndex++; } memory.Write(input, startIndex, inputIndex - startIndex); if (inputIndex < inputEnd && input[inputIndex] == (byte)'\r') { inputIndex++; } if (inputIndex < inputEnd && input[inputIndex] == (byte)'\n') { if (more) { memory.WriteByte(input[inputIndex]); } complete = true; newLine = true; inputIndex++; } } while (more && inputIndex < inputEnd); if (inputIndex == inputEnd) { needInput = true; } } while (more || !complete); string message = null; try { #if !NETFX_CORE && !COREFX message = UTF8.GetString(memory.GetBuffer(), 0, (int)memory.Length); #else message = UTF8.GetString(memory.ToArray(), 0, (int)memory.Length); #endif } catch (DecoderFallbackException) { #if !NETFX_CORE && !COREFX message = Latin1.GetString(memory.GetBuffer(), 0, (int)memory.Length); #else message = Latin1.GetString(memory.ToArray(), 0, (int)memory.Length); #endif } return(new SmtpResponse((SmtpStatusCode)code, message)); } }
/// <summary> /// Gets the decoded text content using the provided charset encoding to /// override the charset specified in the Content-Type parameters. /// </summary> /// <remarks> /// Uses the provided charset encoding to convert the raw text content /// into a unicode string, overriding any charset specified in the /// Content-Type header. /// </remarks> /// <returns>The decoded text.</returns> /// <param name="encoding">The charset encoding to use.</param> /// <exception cref="System.ArgumentNullException"> /// <paramref name="encoding"/> is <c>null</c>. /// </exception> public string GetText (Encoding encoding) { if (encoding == null) throw new ArgumentNullException ("encoding"); if (ContentObject == null) return string.Empty; using (var memory = new MemoryStream ()) { ContentObject.DecodeTo (memory); #if PORTABLE var buffer = memory.ToArray (); #else var buffer = memory.GetBuffer (); #endif return encoding.GetString (buffer, 0, (int) memory.Length); } }
static byte[] EncodeReceivedHeader (ParserOptions options, FormatOptions format, Encoding charset, string field, string value) { var tokens = new List<ReceivedTokenValue> (); var rawValue = charset.GetBytes (value); var encoded = new StringBuilder (); int lineLength = field.Length + 1; bool date = false; int index = 0; int count = 0; while (index < rawValue.Length) { ReceivedTokenValue token = null; int startIndex = index; if (!ParseUtils.SkipCommentsAndWhiteSpace (rawValue, ref index, rawValue.Length, false) || index >= rawValue.Length) { tokens.Add (new ReceivedTokenValue (startIndex, index - startIndex)); break; } while (index < rawValue.Length && !rawValue[index].IsWhitespace ()) index++; var atom = charset.GetString (rawValue, startIndex, index - startIndex); for (int i = 0; i < ReceivedTokens.Length; i++) { if (atom == ReceivedTokens[i].Atom) { ReceivedTokens[i].Skip (rawValue, ref index); if (ParseUtils.SkipCommentsAndWhiteSpace (rawValue, ref index, rawValue.Length, false)) { if (index < rawValue.Length && rawValue[index] == (byte) ';') { date = true; index++; } } token = new ReceivedTokenValue (startIndex, index - startIndex); break; } } if (token == null) { if (ParseUtils.SkipCommentsAndWhiteSpace (rawValue, ref index, rawValue.Length, false)) { while (index < rawValue.Length && !rawValue[index].IsWhitespace ()) index++; } token = new ReceivedTokenValue (startIndex, index - startIndex); } tokens.Add (token); ParseUtils.SkipWhiteSpace (rawValue, ref index, rawValue.Length); if (date && index < rawValue.Length) { // slurp up the date (the final token) tokens.Add (new ReceivedTokenValue (index, rawValue.Length - index)); break; } } foreach (var token in tokens) { var text = charset.GetString (rawValue, token.StartIndex, token.Length).TrimEnd (); if (count > 0 && lineLength + text.Length + 1 > format.MaxLineLength) { encoded.Append (format.NewLine); encoded.Append ('\t'); lineLength = 1; count = 0; } else { encoded.Append (' '); lineLength++; } lineLength += text.Length; encoded.Append (text); count++; } encoded.Append (format.NewLine); return charset.GetBytes (encoded.ToString ()); }
static byte[] EncodeReceivedHeader(ParserOptions options, FormatOptions format, Encoding charset, string field, string value) { var tokens = new List <ReceivedTokenValue> (); var rawValue = charset.GetBytes(value); var encoded = new StringBuilder(); int lineLength = field.Length + 1; bool date = false; int index = 0; int count = 0; while (index < rawValue.Length) { ReceivedTokenValue token = null; int startIndex = index; if (!ParseUtils.SkipCommentsAndWhiteSpace(rawValue, ref index, rawValue.Length, false) || index >= rawValue.Length) { tokens.Add(new ReceivedTokenValue(startIndex, index - startIndex)); break; } while (index < rawValue.Length && !rawValue[index].IsWhitespace()) { index++; } var atom = charset.GetString(rawValue, startIndex, index - startIndex); for (int i = 0; i < ReceivedTokens.Length; i++) { if (atom == ReceivedTokens[i].Atom) { ReceivedTokens[i].Skip(rawValue, ref index); if (ParseUtils.SkipCommentsAndWhiteSpace(rawValue, ref index, rawValue.Length, false)) { if (index < rawValue.Length && rawValue[index] == (byte)';') { date = true; index++; } } token = new ReceivedTokenValue(startIndex, index - startIndex); break; } } if (token == null) { if (ParseUtils.SkipCommentsAndWhiteSpace(rawValue, ref index, rawValue.Length, false)) { while (index < rawValue.Length && !rawValue[index].IsWhitespace()) { index++; } } token = new ReceivedTokenValue(startIndex, index - startIndex); } tokens.Add(token); ParseUtils.SkipWhiteSpace(rawValue, ref index, rawValue.Length); if (date && index < rawValue.Length) { // slurp up the date (the final token) tokens.Add(new ReceivedTokenValue(index, rawValue.Length - index)); break; } } foreach (var token in tokens) { var text = charset.GetString(rawValue, token.StartIndex, token.Length).TrimEnd(); if (count > 0 && lineLength + text.Length + 1 > format.MaxLineLength) { encoded.Append(format.NewLine); encoded.Append('\t'); lineLength = 1; count = 0; } else { encoded.Append(' '); lineLength++; } lineLength += text.Length; encoded.Append(text); count++; } encoded.Append(format.NewLine); return(charset.GetBytes(encoded.ToString())); }
/// <summary> /// Parses the server's challenge token and returns the next challenge response. /// </summary> /// <remarks> /// Parses the server's challenge token and returns the next challenge response. /// </remarks> /// <returns>The next challenge response.</returns> /// <param name="token">The server's challenge token.</param> /// <param name="startIndex">The index into the token specifying where the server's challenge begins.</param> /// <param name="length">The length of the server's challenge.</param> /// <exception cref="System.InvalidOperationException"> /// The SASL mechanism is already authenticated. /// </exception> /// <exception cref="System.NotSupportedException"> /// THe SASL mechanism does not support SASL-IR. /// </exception> /// <exception cref="SaslException"> /// An error has occurred while parsing the server's challenge token. /// </exception> protected override byte[] Challenge(byte[] token, int startIndex, int length) { if (IsAuthenticated) { throw new InvalidOperationException(); } if (token == null) { throw new NotSupportedException("DIGEST-MD5 does not support SASL-IR."); } switch (state) { case LoginState.Auth: if (token.Length > 2048) { throw new SaslException(MechanismName, SaslErrorCode.ChallengeTooLong, "Server challenge too long."); } challenge = DigestChallenge.Parse(Encoding.UTF8.GetString(token, startIndex, length)); encoding = challenge.Charset != null ? Encoding.UTF8 : Latin1; cnonce = cnonce ?? GenerateEntropy(15); response = new DigestResponse(challenge, encoding, Uri.Scheme, Uri.DnsSafeHost, AuthorizationId, Credentials.UserName, Credentials.Password, cnonce); state = LoginState.Final; return(response.Encode(encoding)); case LoginState.Final: if (token.Length == 0) { throw new SaslException(MechanismName, SaslErrorCode.MissingChallenge, "Server response did not contain any authentication data."); } var text = encoding.GetString(token, startIndex, length); string key, value; if (!DigestChallenge.TryParseKeyValuePair(text, out key, out value)) { throw new SaslException(MechanismName, SaslErrorCode.IncompleteChallenge, "Server response contained incomplete authentication data."); } if (!key.Equals("rspauth", StringComparison.OrdinalIgnoreCase)) { throw new SaslException(MechanismName, SaslErrorCode.InvalidChallenge, "Server response contained invalid data."); } var expected = response.ComputeHash(encoding, Credentials.Password, false); if (value != expected) { throw new SaslException(MechanismName, SaslErrorCode.IncorrectHash, "Server response did not contain the expected hash."); } IsAuthenticated = true; return(new byte[0]); default: throw new IndexOutOfRangeException("state"); } }
private async void Button_Click(object sender, RoutedEventArgs e) { string first = "", second = "", firstsearch = "", howfirstsearch = "", secondsearch = "", howsecondsearch = "", interval = "", down = "", up = ""; if (FirstName.IsChecked == true) { first = "name"; if (SecondName.IsChecked == true) { second = "name"; } if (SecondLatitude.IsChecked == true) { second = "latitude"; } if (SecondLongitude.IsChecked == true) { second = "longitude"; } if (SecondRate.IsChecked == true) { second = "rate"; } if (SecondPhoto.IsChecked == true) { second = "photo"; } } if (FirstLatitude.IsChecked == true) { first = "latitude"; if (SecondName.IsChecked == true) { second = "name"; } if (SecondLatitude.IsChecked == true) { second = "latitude"; } if (SecondLongitude.IsChecked == true) { second = "longitude"; } if (SecondRate.IsChecked == true) { second = "rate"; } if (SecondPhoto.IsChecked == true) { second = "photo"; } } if (FirstLongitude.IsChecked == true) { first = "longitude"; if (SecondName.IsChecked == true) { second = "name"; } if (SecondLatitude.IsChecked == true) { second = "latitude"; } if (SecondLongitude.IsChecked == true) { second = "longitude"; } if (SecondRate.IsChecked == true) { second = "rate"; } if (SecondPhoto.IsChecked == true) { second = "photo"; } } if (FirstRate.IsChecked == true) { first = "rate"; if (SecondName.IsChecked == true) { second = "name"; } if (SecondLatitude.IsChecked == true) { second = "latitude"; } if (SecondLongitude.IsChecked == true) { second = "longitude"; } if (SecondRate.IsChecked == true) { second = "rate"; } if (SecondPhoto.IsChecked == true) { second = "photo"; } } if (FirstPhoto.IsChecked == true) { first = "photo"; if (SecondName.IsChecked == true) { second = "name"; } if (SecondLatitude.IsChecked == true) { second = "latitude"; } if (SecondLongitude.IsChecked == true) { second = "longitude"; } if (SecondRate.IsChecked == true) { second = "rate"; } if (SecondPhoto.IsChecked == true) { second = "photo"; } } if (LowerFirst.IsChecked == true && first != "") { first = "-" + first; } if (LowerSecond.IsChecked == true && second != "") { second = "-" + second; } if (FirstSearchName.IsChecked == true) { howfirstsearch = "name"; firstsearch = FirstSearchBox.Text; } if (FirstSearchLatitude.IsChecked == true) { howfirstsearch = "latitude"; firstsearch = FirstSearchBox.Text; } if (FirstSearchLongitude.IsChecked == true) { howfirstsearch = "longitude"; firstsearch = FirstSearchBox.Text; } if (FirstSearchRate.IsChecked == true) { howfirstsearch = "rate"; firstsearch = FirstSearchBox.Text; } if (FirstSearchPhoto.IsChecked == true) { howfirstsearch = "photo"; firstsearch = FirstSearchBox.Text; } if (SecondSearchName.IsChecked == true && howfirstsearch != "") { howsecondsearch = "name"; secondsearch = SecondSearchBox.Text; } if (SecondSearchLatitude.IsChecked == true && howfirstsearch != "") { howsecondsearch = "latitude"; secondsearch = SecondSearchBox.Text; } if (SecondSearchLongitude.IsChecked == true && howfirstsearch != "") { howsecondsearch = "longitude"; secondsearch = SecondSearchBox.Text; } if (SecondSearchRate.IsChecked == true && howfirstsearch != "") { howsecondsearch = "rate"; secondsearch = SecondSearchBox.Text; } if (SecondSearchPhoto.IsChecked == true && howfirstsearch != "") { howsecondsearch = "photo"; secondsearch = SecondSearchBox.Text; } if (IntervalRate.IsChecked == true) { interval = "rate"; down = Down.Text; up = Up.Text; } if (IntervalLongitude.IsChecked == true) { interval = "longitude"; down = Down.Text; up = Up.Text; } if (IntervalLatitude.IsChecked == true) { interval = "latitude"; down = Down.Text; up = Up.Text; } string uri = @"https://alexeyd.herokuapp.com/jsdb?first=" + first + "&second=" + second + "&firstsearch=" + firstsearch + "&howfirstsearch=" + howfirstsearch + "&secondsearch=" + secondsearch + "&howsecondsearch=" + howsecondsearch + "&interval=" + interval + "&down=" + down + "&up=" + up; byte[] request = await client.GetByteArrayAsync(new Uri(uri)); Portable.Text.Encoding encoding = Portable.Text.Encoding.GetEncoding(1251); var s = encoding.GetString(request, 0, request.Length); var routes = JsonConvert.DeserializeObject <Routes[]>(s); DGView.ItemsSource = routes; }
public static string UrlDecode(string s, Encoding e) { if (s == null) { return(null); } if ((s.IndexOf('%') == -1) && (s.IndexOf('+') == -1)) { return(s); } if (e == null) { e = Encoding.UTF8; } long length = s.Length; List <byte> buf = new List <byte>(); for (int i = 0; i < length; i++) { char ch = s[i]; if (((ch == '%') && ((i + 2) < length)) && (s[i + 1] != '%')) { int num2; if ((s[i + 1] == 'u') && ((i + 5) < length)) { num2 = GetChar(s, i + 2, 4); if (num2 != -1) { WriteCharBytes(buf, (char)num2, e); i += 5; } else { WriteCharBytes(buf, '%', e); } } else { num2 = GetChar(s, i + 1, 2); if (num2 != -1) { WriteCharBytes(buf, (char)num2, e); i += 2; } else { WriteCharBytes(buf, '%', e); } } } else if (ch == '+') { WriteCharBytes(buf, ' ', e); } else { WriteCharBytes(buf, ch, e); } } byte[] bytes = buf.ToArray(); buf = null; return(e.GetString(bytes, 0, bytes?.Length ?? 0)); }