public static void Mock() { BHttpContext.SetCurrent(); MHttpRequest request = new MHttpRequest(); MHttpResponse response = new MHttpResponse(); MHttpCookieCollection cookies = new MHttpCookieCollection(); response.CookiesGet = () => cookies; response.OnCookieAddHttpCookie = (cookie) => Nothing(); response.RequestGet = () => request; cookies.GetString = (key) => null; Dictionary <object, object> items = new Dictionary <object, object>(); Dictionary <string, string> reqItems = new Dictionary <string, string>(); request.ItemGetString = (key) => reqItems.ContainsKey(key) ? reqItems[key] : null; new MHttpContext(BHttpContext.Current) { ItemsGet = () => items, RequestGet = () => request, ResponseGet = () => response }; MHttpUtility.ParseQueryStringString = ParseQueryString; MHttpUtility.UrlEncodeString = (url) => Uri.EscapeDataString(url).Replace("%20", "+"); }
Task SendResponse(MHttpResponse response, string html, MHttpStream local) { html = m_info.ReplaceResponseHtml(html); response.Headers.RemoveHopByHopHeaders(); response.SetContent(html); return(response.SendAsync(local)); }
async Task AdAsync(MHttpStream localStream) { try { var response = MHttpResponse.Create(200); response.SetContent(m_info.ADVideoBytes); await response.SendAsync(localStream).ConfigureAwait(false); } finally { localStream?.Close(); } }
Task GetSeleteHtmlAsync(Func <MHttpStream, Task> requset, MHttpStream local) { var list = new List <Task>(); var source = new TaskCompletionSource <Func <Task> >(); async Task createOneRequest() { MHttpResponse response = await GetResponseAsync(requset).ConfigureAwait(false); string html = response.Content.GetString(); if (m_info.CheckingVideoHtml(html)) { source.TrySetResult(() => SendResponse(response, html, local)); } } async Task sendFirstResponse() { Task allTask = Task.WhenAll(list.ToArray()); var task = source.Task; Task t = await Task.WhenAny(allTask, task).ConfigureAwait(false); if (object.ReferenceEquals(t, task)) { var v = await task.ConfigureAwait(false); await v().ConfigureAwait(false); } else { await GetOneHtmlAsync(requset, local).ConfigureAwait(false); } } foreach (var item in Enumerable.Range(0, m_info.MaxRefreshRequestCount)) { list.Add(Task.Run(createOneRequest)); } return(sendFirstResponse()); }
static void ReadConnectRequest(Socket socket, Func <string, TunnelPackAction> func) { Task.Run(async() => { Stream stream = new NetworkStream(socket, true); MHttpStream httpStream = new MHttpStream(socket, stream, 1024); MHttpRequest request = await MHttpRequest.ReadAsync(httpStream, 1024 * 1024).ConfigureAwait(false); MHttpResponse response = MHttpResponse.Create(200); await response.SendAsync(httpStream).ConfigureAwait(false); Uri uri = new Uri($"http://{request.Path}/"); func(uri.Host)(uri, new TunnelPack(socket, stream)); }); }
async Task GetOneHtmlAsync(Func <MHttpStream, Task> request, MHttpStream local) { MHttpResponse response = await GetResponseAsync(request).ConfigureAwait(false); await SendResponse(response, response.Content.GetString(), local).ConfigureAwait(false); }