private async Task OnReadInternal(int offset) { _timer.Change(Timeout.Infinite, Timeout.Infinite); // Continue reading until full header is received. // Especially important for multipart requests when the second part of the header arrives after a tiny delay // because the web browser has to measure the content length first. while (true) { try { await _ms.WriteAsync(_buffer, 0, offset).ConfigureAwait(false); if (_ms.Length > 32768) { Close(true); return; } } catch { CloseSocket(); Unbind(); return; } if (offset == 0) { CloseSocket(); Unbind(); return; } if (ProcessInput(_ms)) { if (!_context.HaveError) { _context.HttpListenerRequest.FinishInitialization(); } if (_context.HaveError || !_epl.BindContext(_context)) { Close(true); return; } var listener = _context.Listener; if (_lastListener != listener) { RemoveConnection(); listener.AddConnection(this); _lastListener = listener; } _contextBound = true; listener.RegisterContext(_context); return; } offset = await Stream.ReadAsync(_buffer, 0, BufferSize).ConfigureAwait(false); } }
private async Task OnReadInternal(int nread) { _timer.Change(Timeout.Infinite, Timeout.Infinite); // Continue reading until full header is received. // Especially important for multipart requests when the second part of the header arrives after a tiny delay // because the webbrowser has to meassure the content length first. int parsedBytes = 0; while (true) { try { await _ms.WriteAsync(_buffer, parsedBytes, nread - parsedBytes); if (_ms.Length > 32768) { await CloseAsync(true); return; } } catch { CloseSocket(); Unbind(); return; } if (nread == 0) { //if (ms.Length > 0) // SendError (); // Why bother? CloseSocket(); Unbind(); return; } if (ProcessInput(_ms)) { if (!_context.HaveError) { _context.Request.FinishInitialization(); } if (_context.HaveError || !_epl.BindContext(_context)) { await CloseAsync(true); return; } var listener = _context.Listener; if (_lastListener != listener) { RemoveConnection(); listener.AddConnection(this); _lastListener = listener; } _contextBound = true; listener.RegisterContext(_context); return; } parsedBytes = nread; nread += await Stream.ReadAsync(_buffer, nread, BufferSize - nread); } }
private HttpListener SearchListener(Uri uri, out ListenerPrefix prefix) { prefix = null; if (uri == null) { return(null); } var host = uri.Host; var port = uri.Port; var path = WebUtility.UrlDecode(uri.AbsolutePath); var pathSlash = path[path.Length - 1] == '/' ? path : path + "/"; HttpListener bestMatch = null; var bestLength = -1; if (!string.IsNullOrEmpty(host)) { var result = _prefixes; foreach (var p in result.Keys) { if (p.Path.Length < bestLength) { continue; } if (p.Host != host || p.Port != port) { continue; } if (!path.StartsWith(p.Path) && !pathSlash.StartsWith(p.Path)) { continue; } bestLength = p.Path.Length; bestMatch = result[p]; prefix = p; } if (bestLength != -1) { return(bestMatch); } } var list = _unhandled; bestMatch = MatchFromList(path, list, out prefix); if (path != pathSlash && bestMatch == null) { bestMatch = MatchFromList(pathSlash, list, out prefix); } if (bestMatch != null) { return(bestMatch); } list = _all; bestMatch = MatchFromList(path, list, out prefix); if (path != pathSlash && bestMatch == null) { bestMatch = MatchFromList(pathSlash, list, out prefix); } return(bestMatch); }
internal HttpListenerPrefixCollection(HttpListener listener) { _listener = listener; }