aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Network/API/Controllers/NetworkController.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/slskd/Network/API/Controllers/NetworkController.cs')
-rw-r--r--src/slskd/Network/API/Controllers/NetworkController.cs84
1 files changed, 49 insertions, 35 deletions
diff --git a/src/slskd/Network/API/Controllers/NetworkController.cs b/src/slskd/Network/API/Controllers/NetworkController.cs
index 20c2caa0..5d3f4c77 100644
--- a/src/slskd/Network/API/Controllers/NetworkController.cs
+++ b/src/slskd/Network/API/Controllers/NetworkController.cs
@@ -66,50 +66,64 @@ namespace slskd.Network
return BadRequest("Token is not a valid Guid");
}
+ // todo: make sure agent is registered and return unauthorized if not
+
+ if (!HttpContext.Request.HasFormContentType
+ || !MediaTypeHeaderValue.TryParse(HttpContext.Request.ContentType, out var mediaTypeHeader)
+ || string.IsNullOrEmpty(mediaTypeHeader.Boundary.Value))
+ {
+ return new UnsupportedMediaTypeResult();
+ }
+
+ 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
{
- var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType));
- var reader = new MultipartReader(boundary, Request.Body);
-
- var credentialSection = await reader.ReadNextSectionAsync();
- using var sr = new StreamReader(credentialSection.Body);
- credential = sr.ReadToEnd();
-
- var fileSection = await reader.ReadNextSectionAsync();
- ContentDispositionHeaderValue.TryParse(fileSection.ContentDisposition, out var contentDisposition);
- filename = contentDisposition.FileName.Value;
- stream = fileSection.Body;
+ 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();
}
- catch (Exception ex)
+ finally
{
- Log.Warning("Failed to handle file upload from agent {Agent}: {Message}", agentName, ex.Message);
- return BadRequest();
+ stream?.Dispose();
}
-
- if (!Network.TryValidateFileUploadCredential(token: guid, agentName, filename, credential))
- {
- Log.Warning("Failed to authenticate file upload from caller claiming to be agent {Agent}", agentName);
- return Unauthorized();
- }
-
- Console.WriteLine($"credential: {credential}, filename: {filename}, stream pos {stream.Position} len {stream.Length}");
-
- Console.WriteLine("Opening request stream...");
-
- Console.WriteLine("Stream open. calling handler...");
-
- // 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);
-
- stream.Dispose();
- Console.WriteLine("Upload complete");
- return Ok();
}
//[DisableFormValueModelBinding]