private static void requestCallback(IAsyncResult asynchronousResult) { NetWebClient netWebClient = asynchronousResult.AsyncState as NetWebClient; Stream stream = null; try { HttpWebRequest httpWebRequest = netWebClient.request_; stream = httpWebRequest.EndGetRequestStream(asynchronousResult); stream.Write(netWebClient.bytes_, 0, netWebClient.bytes_.Length); netWebClient.request(); } catch (WebException ex) { netWebClient.setError(ex.Status, ex.Message, 5, ex.Response as HttpWebResponse); } catch (Exception ex2) { netWebClient.setError(WebExceptionStatus.UnknownError, ex2.Message, 5, null); } finally { netWebClient.bytes_ = null; if (stream != null) { stream.Dispose(); } } }
private static byte[] preprocess(byte[] bytes, MemoryStream memoryStream, NetWebClient.Encoding encoding, bool encrypt) { NetWebClient.clearStream(memoryStream); if (encoding != NetWebClient.Encoding.Deflate) { if (encoding != NetWebClient.Encoding.GZip) { memoryStream.Write(bytes, 0, bytes.Length); } else { using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Compress, true)) { gzipStream.Write(bytes, 0, bytes.Length); } } } else { uint num = NetWebClient.calcAdler32(bytes.LongLength, bytes); memoryStream.WriteByte(120); memoryStream.WriteByte(156); if (bytes.Length <= 128) { NetWebClient.deflateRawBlock(memoryStream, bytes, 0, bytes.Length); } else { using (DeflateStream deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress, true)) { deflateStream.Write(bytes, 0, bytes.Length); } } memoryStream.WriteByte((byte)(num >> 24 & 255U)); memoryStream.WriteByte((byte)(num >> 16 & 255U)); memoryStream.WriteByte((byte)(num >> 8 & 255U)); memoryStream.WriteByte((byte)(num >> 0 & 255U)); } if (encrypt) { memoryStream.Position = 0L; int num2 = (int)memoryStream.Length; int num3 = Cryptography.calcEncryptedSize(num2); NetWebClient.shared_.resizeTemporary(num3); byte[] tmp_ = NetWebClient.shared_.tmp_; memoryStream.Read(tmp_, 0, num3); Cryptography.padding(num2, tmp_); bytes = new byte[num3]; Cryptography.Instance.encrypt(num3, bytes, num3, tmp_); } else { bytes = memoryStream.ToArray(); } return(bytes); }
private static void deflateRawBlock(Stream stream, byte[] buffer, int offset, int size) { byte value = 1; ushort num = (ushort)size; ushort x = unchecked ((ushort)~num); stream.WriteByte(value); NetWebClient.write(stream, num); NetWebClient.write(stream, x); stream.Write(buffer, offset, size); }
private static void timeoutCallback(object state, bool timedout) { if (!timedout) { return; } NetWebClient netWebClient = state as NetWebClient; if (netWebClient.request_ == null) { netWebClient.setError(WebExceptionStatus.UnknownError, string.Empty, 5, null); return; } netWebClient.setError(WebExceptionStatus.Timeout, string.Empty, 5, null); }
private static bool checkHash(Stream received, MemoryStream decompressed) { if (received.Length < 4L) { return(false); } received.Seek(-4L, SeekOrigin.End); uint num = (uint)((uint)((byte)received.ReadByte()) << 24); num |= (uint)((uint)((byte)received.ReadByte()) << 16); num |= (uint)((uint)((byte)received.ReadByte()) << 8); num |= (uint)((uint)((byte)received.ReadByte()) << 0); uint num2 = NetWebClient.calcAdler32(decompressed); return(num2 == num); }
public bool request(byte[] bytes, string userAgent, bool compress) { this.request_.Method = "POST"; this.request_.ContentType = "application/json"; this.request_.UserAgent = ((!string.IsNullOrEmpty(userAgent)) ? userAgent : string.Empty); this.request_.Headers.Add("charset", "UTF-8"); this.encoding_ = ((!compress) ? NetWebClient.Encoding.Raw : NetWebClient.Encoding.Deflate); this.resetStatus(); NetWebClient.shared_.clear(); bool result; try { if (bytes != null && 0 < bytes.Length) { if (compress) { this.request_.Headers.Add("Content-Encoding", "deflate"); } bool encrypt = 0 < this.encryptVersion_; if (encrypt) { this.request_.Headers.Add("Ongeki-Encoding", this.encryptVersion_.ToString()); } this.bytes_ = NetWebClient.preprocess(bytes, NetWebClient.shared_.memoryStream_, this.encoding_, encrypt); this.beginGetRequestStream(); this.State = 2; result = true; } else { this.request_.ContentLength = 0L; result = this.request(); } } catch (WebException ex) { this.setError(ex.Status, ex.Message, 5, ex.Response as HttpWebResponse); result = false; } catch (Exception ex2) { this.setError(WebExceptionStatus.UnknownError, ex2.Message, 5, null); result = false; } return(result); }
protected bool reset() { OperationManager instance = Singleton <OperationManager> .instance; string baseUri = instance.getBaseUri(); string apiName = (0 < NetConfig.EncryptVersion) ? Packet.scrambleAPI(this.query_) : this.query_.URL; this.client_ = NetWebClient.Create(baseUri + apiName, NetConfig.EncryptVersion, NetConfig.UseTLS); this.time_ = 0f; if (this.client_ != null) { this.client_.TimeOutInMSec = NetConfig.TimeOutInMSec; this.state_ = Packet.State.Ready; this.status_ = Packet.Status.OK; return(true); } this.state_ = Packet.State.Error; this.status_ = Packet.Status.Error_Create; return(false); }
private static void responseCallback(IAsyncResult asynchronousResult) { NetWebClient netWebClient = asynchronousResult.AsyncState as NetWebClient; try { HttpWebRequest httpWebRequest = netWebClient.request_; netWebClient.response_ = (httpWebRequest.EndGetResponse(asynchronousResult) as HttpWebResponse); netWebClient.responseStream_ = netWebClient.response_.GetResponseStream(); NetWebClient.clearStream(NetWebClient.shared_.memoryStream_); netWebClient.responseStream_.BeginRead(NetWebClient.shared_.buffer_, 0, 1024, new AsyncCallback(NetWebClient.readCallback), netWebClient); } catch (WebException ex) { netWebClient.setError(ex.Status, ex.Message, 5, ex.Response as HttpWebResponse); } catch (Exception ex2) { netWebClient.setError(WebExceptionStatus.UnknownError, ex2.Message, 5, null); } }
public static NetWebClient Create(string url, int encryptVersion, bool tls) { NetWebClient netWebClient = new NetWebClient(); NetWebClient result; try { netWebClient.request_ = (WebRequest.Create(url) as HttpWebRequest); netWebClient.request_.CachePolicy = netWebClient.cachePolicy_; netWebClient.encryptVersion_ = encryptVersion; netWebClient.State = 1; if (tls) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(NetWebClient.CertificateValidation); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; } result = netWebClient; } catch (Exception) { result = null; } return(result); }
private static void readCallback(IAsyncResult asynchronousResult) { NetWebClient netWebClient = asynchronousResult.AsyncState as NetWebClient; try { Stream stream = netWebClient.responseStream_; int num = stream.EndRead(asynchronousResult); if (0 < num) { NetWebClient.shared_.memoryStream_.Write(NetWebClient.shared_.buffer_, 0, num); stream.BeginRead(NetWebClient.shared_.buffer_, 0, 1024, new AsyncCallback(NetWebClient.readCallback), netWebClient); } else if (asynchronousResult.IsCompleted) { byte[] buffer_ = NetWebClient.shared_.buffer_; NetWebClient.shared_.memoryStream_.Position = 0L; if (0 < netWebClient.encryptVersion_) { if (NetWebClient.descryptTo(NetWebClient.shared_.compressedStream_, NetWebClient.shared_.memoryStream_) < 0) { throw new Exception("A message may be corrupted."); } NetWebClient.swap <MemoryStream>(ref NetWebClient.shared_.memoryStream_, ref NetWebClient.shared_.compressedStream_); } NetWebClient.Encoding encoding = netWebClient.encoding_; if (encoding != NetWebClient.Encoding.Deflate) { if (encoding == NetWebClient.Encoding.GZip) { NetWebClient.swap <MemoryStream>(ref NetWebClient.shared_.memoryStream_, ref NetWebClient.shared_.compressedStream_); using (GZipStream gzipStream = new GZipStream(NetWebClient.shared_.compressedStream_, CompressionMode.Decompress, true)) { NetWebClient.copyTo(NetWebClient.shared_.memoryStream_, gzipStream, buffer_, 1024); } } } else { NetWebClient.swap <MemoryStream>(ref NetWebClient.shared_.memoryStream_, ref NetWebClient.shared_.compressedStream_); MemoryStream memoryStream_ = NetWebClient.shared_.memoryStream_; MemoryStream compressedStream_ = NetWebClient.shared_.compressedStream_; compressedStream_.ReadByte(); compressedStream_.ReadByte(); using (DeflateStream deflateStream = new DeflateStream(compressedStream_, CompressionMode.Decompress, true)) { NetWebClient.copyTo(memoryStream_, deflateStream, buffer_, 1024); } if (!NetWebClient.checkHash(compressedStream_, memoryStream_)) { netWebClient.setError(WebExceptionStatus.UnknownError, "Invalid Hash", 5, null); return; } } netWebClient.setSuccess(4); } } catch (WebException ex) { netWebClient.setError(ex.Status, ex.Message, 5, ex.Response as HttpWebResponse); } catch (Exception ex2) { netWebClient.setError(WebExceptionStatus.UnknownError, ex2.Message, 5, null); } }