diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2022-10-23 12:21:51 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2022-10-23 12:21:51 -0500 |
| commit | 5211113da3d95863645cc2925be5d2d64c38101b (patch) | |
| tree | 4fbe45cbff50a4dcc1574320006170984750d5a9 /src/slskd/Network/API/Controllers | |
| parent | 913821a6c012b287603230b2dc93191b9f7cfdc9 (diff) | |
| download | slskd-5211113da3d95863645cc2925be5d2d64c38101b.tar.gz | |
refactor stream handling
Diffstat (limited to 'src/slskd/Network/API/Controllers')
| -rw-r--r-- | src/slskd/Network/API/Controllers/NetworkController.cs | 77 |
1 files changed, 53 insertions, 24 deletions
diff --git a/src/slskd/Network/API/Controllers/NetworkController.cs b/src/slskd/Network/API/Controllers/NetworkController.cs index 3ae2a6b5..1ee311a7 100644 --- a/src/slskd/Network/API/Controllers/NetworkController.cs +++ b/src/slskd/Network/API/Controllers/NetworkController.cs @@ -25,6 +25,8 @@ namespace slskd.Network using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; + using Microsoft.AspNetCore.WebUtilities; + using Microsoft.Net.Http.Headers; using Serilog; using slskd.Shares; @@ -61,37 +63,64 @@ namespace slskd.Network return BadRequest("Token is not a valid Guid"); } - string credential; - IFormFile file; + // todo: make sure agent is registered and return unauthorized if not - try + if (!HttpContext.Request.HasFormContentType + || !MediaTypeHeaderValue.TryParse(HttpContext.Request.ContentType, out var mediaTypeHeader) + || string.IsNullOrEmpty(mediaTypeHeader.Boundary.Value)) { - credential = Request.Form["credential"].ToString(); - file = Request.Form.Files[0]; + return new UnsupportedMediaTypeResult(); } - catch (Exception ex) + + Log.Debug("Handling file upload for token {Token} from a caller claiming to be agent {Agent}", token, agentName); + + string credential = default; + Stream stream = default; + string filename = default; + + try { - Log.Warning("Failed to handle file upload from agent {Agent}: {Message}", agentName, ex.Message); - return BadRequest(); + try + { + var reader = new MultipartReader(mediaTypeHeader.Boundary.Value, Request.Body); + + // the first section contains the credentail + var section = await reader.ReadNextSectionAsync(); + using var sectionReader = new StreamReader(section.Body); + credential = sectionReader.ReadToEnd(); + + // the second section contains the file + section = await reader.ReadNextSectionAsync(); + var contentDisposition = ContentDispositionHeaderValue.Parse(section.ContentDisposition); + filename = contentDisposition.FileName.Value; + stream = section.Body; + } + catch (Exception ex) + { + Log.Warning("Failed to handle file upload for token {Token} from a caller claiming to be agent {Agent}: {Message}", token, agentName, ex.Message); + return BadRequest(); + } + + if (!Network.TryValidateFileUploadCredential(token: guid, agentName, filename, credential)) + { + Log.Warning("Failed to authenticate file upload token {Token} from a caller claiming to be agent {Agent}", agentName); + return Unauthorized(); + } + + Log.Information("File upload of {Filename} ({Token}) from agent {Agent} validated and authenticated. Forwarding file stream.", filename, token, agentName); + + // pass the stream back to the network service, which will in turn pass it to the + // upload service, and use it to feed data into the remote upload. await this call, + // it will complete when the upload is complete. + await Network.HandleGetFileStreamResponse(agentName, filename, id: guid, stream); + + Log.Information("File upload of {Filename} ({Token}) from agent {Agent} complete", filename, token, agentName); + return Ok(); } - - if (!Network.TryValidateFileUploadCredential(token: guid, agentName, filename: file.FileName, credential)) + finally { - Log.Warning("Failed to authenticate file upload from caller claiming to be agent {Agent}", agentName); - return Unauthorized(); + stream?.Dispose(); } - - // get the stream from the multipart upload - var stream = Request.Form.Files.First().OpenReadStream(); - - // pass the stream back to the network service, which will in turn pass it to the - // upload service, and use it to feed data into the remote upload. await this call, - // it will complete when the upload is complete. - await Network.HandleGetFileStreamResponse(agentName, filename: file.FileName, id: guid, stream); - - stream.Dispose(); - Console.WriteLine("Upload complete"); - return Ok(); } [HttpPost("shares/{agentName}/{token}")] |
