public override bool SendResponse(Request e) { try { var partialPath = e.Url.Replace('/', '\\'); var filePath = Path.Combine(_rootDirectory, partialPath); EstablishDirectory(filePath); var receivedBytes = 0; var fileHandle = new FileStream(filePath, FileMode.Create, FileAccess.Write); var buffer = new byte[256]; while (true) { var countBytes = e.Body.ReadBytes(buffer); if (countBytes == 0) break; fileHandle.Write(buffer, 0, countBytes); receivedBytes += countBytes; } fileHandle.Close(); RequestHelper.Send200_OK(e.Client, "text/plain"); _logger.Debug("Received bytes = " + receivedBytes); } catch (Exception ex) { RequestHelper.Send500_Failure(e.Client, ex.ToString()); } return true; }
public override bool SendResponse(Request e) { var request = new ApiRequest { Client = new ApiSocketWrapper(e.Client), HttpMethod = e.HttpMethod, Url = e.Url, Headers = e.Headers, GetArguments = e.GetArguments, Body = new ApiReadBodyWrapper(e.Body) }; return _apiService.SendResponse(request); }
public override bool SendResponse(Request e) { var filePath = Path.Combine(_rootDirectory, UrlToPath(e.Url)); if (!DoesFileExist(filePath)) { RequestHelper.Send404_NotFound(e.Client); return true; } var mimeType = RequestHelper.GetMimeType(filePath); using (var inputStream = new FileStream(filePath, FileMode.Open)) { RequestHelper.Send200_OK(e.Client, mimeType, (int) inputStream.Length); // Send it in chunks to conserve RAM var sentBytes = 0; var readBuffer = new byte[_bufferSize]; while (sentBytes < inputStream.Length) { try { var bytesRead = inputStream.Read(readBuffer, 0, readBuffer.Length); if (bytesRead <= 0) break; var now = e.Client.Send(readBuffer, bytesRead, SocketFlags.None); sentBytes += now; } catch (Exception ex) { _logger.Debug("FileResponder - error sending - " + ex); return false; } } _logger.Debug("FileResponder - sent bytes - " + sentBytes); } return true; }
public override bool CanRespond(Request e) { return e.HttpMethod == "GET" && e.Url.StartsWith(_folder); }
public abstract bool SendResponse(Request e);
public abstract bool CanRespond(Request e);