-
Notifications
You must be signed in to change notification settings - Fork 0
Simple ftp #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
IgnatSergeev
wants to merge
15
commits into
main
Choose a base branch
from
simple-ftp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Simple ftp #52
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
982b4ec
Init commit
IgnatSergeev 4ec008b
Checking request
IgnatSergeev e78d958
Checking request
IgnatSergeev 1d5f2e1
Finished request
IgnatSergeev c43c48e
Added response
IgnatSergeev 5ccefdd
Saved facotry
IgnatSergeev f65c99b
Separated protocol
IgnatSergeev 35d127b
Added handling
IgnatSergeev f38c42c
Finished server
IgnatSergeev 05ba1d8
Added client
IgnatSergeev 136b764
Finished client and server
IgnatSergeev 92c7409
Moved request
IgnatSergeev 6fb65f2
Checkign
IgnatSergeev e63cd38
Fixed
IgnatSergeev a12505d
Added tests
IgnatSergeev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| using System.Net.Sockets; | ||
| using SimpleFtp.Protocol; | ||
|
|
||
| namespace SimpleFtp.Client; | ||
|
|
||
| public class FtpClient | ||
| { | ||
| private readonly TcpClient _client = new TcpClient(); | ||
| private StreamReader? _reader; | ||
| private StreamWriter? _writer; | ||
|
|
||
| private FtpClient(string hostname, int port) | ||
| { | ||
| Hostname = hostname; | ||
| Port = port; | ||
| } | ||
|
|
||
| public string Hostname { get; private set; } | ||
| public int Port { get; private set; } | ||
|
|
||
| public static async Task<FtpClient> Connect(string hostname, int port) | ||
| { | ||
| var client = new FtpClient(hostname, port); | ||
| await client._client.ConnectAsync(hostname, port); | ||
| client._reader = new StreamReader(client._client.GetStream()); | ||
| client._writer = new StreamWriter(client._client.GetStream()); | ||
| client._writer.AutoFlush = true; | ||
| return client; | ||
| } | ||
|
|
||
| public Response SendRequest(Request request) | ||
| { | ||
| _writer?.Write(request.ToString()); | ||
| var data = _reader?.ReadLine() + "\n"; | ||
| return ResponseFactory.Create(data); | ||
| } | ||
|
|
||
| public void Disconnect() | ||
| { | ||
| _client.Close(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| using SimpleFtp.Client; | ||
| using SimpleFtp.Protocol; | ||
|
|
||
| var client = await FtpClient.Connect("localhost", 32768); | ||
| Console.WriteLine("Connected"); | ||
| while (true) | ||
| { | ||
| var command = Console.ReadLine(); | ||
| if (command == "exit") | ||
| { | ||
| client.Disconnect(); | ||
| break; | ||
| } | ||
|
|
||
| if (command == null) | ||
| { | ||
| Console.WriteLine("Incorrect command"); | ||
| continue; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var request = RequestFactory.Create(command + "\n"); | ||
| var response = client.SendRequest(request); | ||
| Console.Write(response.ToString()); | ||
| } | ||
| catch (RequestParseException) | ||
| { | ||
| Console.WriteLine("Incorrect command"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net7.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <RootNamespace>SimpleFtpClient</RootNamespace> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\SimpleFtp.Protocol\SimpleFtp.Protocol.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| namespace SimpleFtp.Protocol; | ||
|
|
||
| public class GetRequest : Request | ||
| { | ||
| public string Path { get; private set; } | ||
|
|
||
| public GetRequest(string path) | ||
| { | ||
| Path = path; | ||
| } | ||
|
|
||
| public override string ToString() | ||
| { | ||
| return "2 " + Path + "\n"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| namespace SimpleFtp.Protocol; | ||
|
|
||
| public class ListRequest : Request | ||
| { | ||
| public string Path { get; private set; } | ||
|
|
||
| public ListRequest(string path) | ||
| { | ||
| Path = path; | ||
| } | ||
|
|
||
| public override string ToString() | ||
| { | ||
| return "1 " + Path + "\n"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace SimpleFtp.Protocol; | ||
|
|
||
| public abstract class Request | ||
| { | ||
| public abstract override string ToString(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace SimpleFtp.Protocol; | ||
|
|
||
| public static partial class RequestFactory | ||
| { | ||
| private const string GetPattern = "2 (?<path>[1-9a-zA-Z./\\\\]+)\n"; | ||
| private const string ListPattern = "1 (?<path>[1-9a-zA-Z./\\\\]+)\n"; | ||
|
|
||
| public static Request Create(string request) | ||
| { | ||
| if (GetRegex().IsMatch(request)) | ||
| { | ||
| var match = GetRegex().Match(request); | ||
| return new GetRequest(match.Groups["path"].Value); | ||
| } | ||
| if (ListRegex().IsMatch(request)) | ||
| { | ||
| var match = ListRegex().Match(request); | ||
| return new ListRequest(match.Groups["path"].Value); | ||
| } | ||
|
|
||
| throw new RequestParseException(); | ||
| } | ||
|
|
||
| [GeneratedRegex(GetPattern)] | ||
| private static partial Regex GetRegex(); | ||
|
|
||
| [GeneratedRegex(ListPattern)] | ||
| private static partial Regex ListRegex(); | ||
| } |
5 changes: 5 additions & 0 deletions
5
C#/forSpbu/SimpleFtp.Protocol/Request/RequestParseException.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| namespace SimpleFtp.Protocol; | ||
|
|
||
| public class RequestParseException : FormatException | ||
| { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| namespace SimpleFtp.Protocol; | ||
|
|
||
| public class GetResponse : Response | ||
| { | ||
| private readonly byte[]? _file; | ||
| private int Size => _file?.Length ?? -1; | ||
|
|
||
| public GetResponse(byte[] fileBytes) | ||
| { | ||
| _file = fileBytes; | ||
| } | ||
|
|
||
| public GetResponse() | ||
| { | ||
| } | ||
|
|
||
| public override string ToString() => Size + " " + System.Text.Encoding.UTF8.GetString(_file ?? Array.Empty<byte>()) + "\n"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| namespace SimpleFtp.Protocol; | ||
|
|
||
| public class ListResponse : Response | ||
| { | ||
| private readonly IEnumerable<(string name, bool isDir)>? _list; | ||
|
|
||
| private int Size => _list?.Count() ?? -1; | ||
|
|
||
| public ListResponse(IEnumerable<(string name, bool isDir)> dirList) | ||
| { | ||
| _list = dirList; | ||
| } | ||
|
|
||
| public ListResponse() | ||
| { | ||
| } | ||
|
|
||
| public override string ToString() => | ||
| Size + " " + | ||
| string.Join(' ', (_list ?? Array.Empty<(string name, bool isDir)>()) | ||
| .Select<(string name, bool isDir), string>(x => x.name + " " + x.isDir)) + "\n"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace SimpleFtp.Protocol; | ||
|
|
||
| public abstract class Response | ||
| { | ||
| public abstract override string ToString(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| using System.Text; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace SimpleFtp.Protocol; | ||
|
|
||
| public static partial class ResponseFactory | ||
| { | ||
| private const string ListPattern = "(?<size>[0-9]+) ((?<names>[1-9a-zA-Z./\\\\]+) (?<isDirs>False|True) )*((?<names>[1-9a-zA-Z./\\\\]+) (?<isDirs>False|True))+\n"; | ||
| private const string GetPattern = "(?<size>[0-9]+) (?<content>.+)\n"; | ||
|
|
||
| public static Response Create(string response) | ||
| { | ||
| if (ListRegex().IsMatch(response)) | ||
| { | ||
| var match = ListRegex().Match(response); | ||
|
|
||
| var isDirs = match.Groups["isDirs"].Captures.Select(x => x.Value == "True").ToArray(); | ||
| var names = match.Groups["names"].Captures.Select(x => x.Value).ToArray(); | ||
| if (!int.TryParse(match.Groups["size"].Value, out var size) || names.Length != size) | ||
| { | ||
| throw new ResponseParseException(); | ||
| } | ||
|
|
||
| var list = new (string, bool)[size]; | ||
| for (int i = 0; i < size; i++) | ||
| { | ||
| list[i] = (names[i], isDirs[i]); | ||
| } | ||
| return new ListResponse(list); | ||
| } | ||
| if (GetRegex().IsMatch(response)) | ||
| { | ||
| var match = GetRegex().Match(response); | ||
|
|
||
| var content = match.Groups["content"].Value; | ||
| if (!int.TryParse(match.Groups["size"].Value, out _)) | ||
| { | ||
| throw new ResponseParseException(); | ||
| } | ||
|
|
||
| return new GetResponse(Encoding.ASCII.GetBytes(content)); | ||
| } | ||
|
|
||
| throw new ResponseParseException(); | ||
| } | ||
|
|
||
|
|
||
| [GeneratedRegex(ListPattern)] | ||
| private static partial Regex ListRegex(); | ||
|
|
||
| [GeneratedRegex(GetPattern)] | ||
| private static partial Regex GetRegex(); | ||
| } |
5 changes: 5 additions & 0 deletions
5
C#/forSpbu/SimpleFtp.Protocol/Response/ResponseParseException.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| namespace SimpleFtp.Protocol; | ||
|
|
||
| public class ResponseParseException : FormatException | ||
| { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net7.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| using SimpleFtp; | ||
| using SimpleFtp.Protocol; | ||
|
|
||
| var server = new FtpServer(); | ||
| var cancellation = new CancellationTokenSource(); | ||
| Task.Run(() => server.Listen(cancellation)); | ||
| var input = Console.ReadLine(); | ||
| if (input == "exit") | ||
| { | ||
| cancellation.Cancel(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В случае get-запроса ответ некорректно считается: \n может раньше конца команды оказаться -- внутри содержимого файла.
Еще в случае get-запроса лучше передавать содержимое файла в поток, который предоставит пользователь. Чтобы файл напрямую в память не читать (так как он большой может быть), а предоставить право выбора вызывающему