protected void HandleDeleteRequest( HttpClientContext context, HttpRequest request) { try { LocalRepositoryState repoState = GetRepositoryFromRequest(request); // TODO: Authenticate based on request address String filePath = GetLocalFilePathFromRequest(request); File.Delete(filePath); HttpResponse response = (HttpResponse)request.CreateResponse(context); response.ContentType = "application/octet-stream"; lock (repoState.Manifest) { ManifestFileInfo manFileInfo = GetOrMakeManifestFileInfoFromRequest( repoState.Manifest, request, false); if (manFileInfo == null) { throw new Exception( "File not registered: " + filePath); } manFileInfo.ParentDirectory.Files.Remove( manFileInfo.Name); repoState.SetManifestChanged(); } context.Respond( "HTTP/1.0", HttpStatusCode.OK, "File accepted", "", "text/plain"); } catch (Exception ex) { context.Respond( "HTTP/1.0", HttpStatusCode.InternalServerError, "Internal server error", ex.ToString(), "text/plain"); } }
protected void HandleSetFileInfoRequest( HttpClientContext context, HttpRequest request) { try { LocalRepositoryState repoState = GetRepositoryFromRequest(request); // TODO: Authenticate based on request address // ...delete temp file if not authenticated... // Better to authenticate when the headers are received... String newFilePath = GetLocalFilePathFromRequest(request); FileInfo newFile = new FileInfo(newFilePath); lock (repoState.Manifest) { ManifestFileInfo manFileInfo = GetOrMakeManifestFileInfoFromRequest( repoState.Manifest, request, false); SetManifestFileInfo(manFileInfo, request, newFile); newFile.LastWriteTimeUtc = manFileInfo.LastModifiedUtc; repoState.SetManifestChanged(); } context.Respond( "HTTP/1.0", HttpStatusCode.OK, "Info accepted", "", "text/plain"); } catch (Exception ex) { context.Respond( "HTTP/1.0", HttpStatusCode.InternalServerError, "Internal server error", ex.ToString(), "text/plain"); System.Console.WriteLine(ex.ToString()); } }
protected void HandleGetRequest( HttpClientContext context, HttpRequest request) { try { // TODO: Authenticate based on request address FileInfo fileInfo = new FileInfo(GetLocalFilePathFromRequest(request)); HttpResponse response = (HttpResponse)request.CreateResponse(context); response.ContentType = "application/octet-stream"; if (request.UriParts.Length == 1) { // Requesting manifest Guid repoGuid = GetRepositoryGuidFromRequest(request); Manifest manifestClone = GetRepositoryFromGuid(repoGuid).CloneManifest(); MemoryStream memStream = new MemoryStream(); manifestClone.WriteManifestStream(memStream); memStream.Seek(0, SeekOrigin.Begin); response.ContentLength = memStream.Length; response.SendHeaders(); StreamUtilities.CopyStream(memStream, context.Stream); context.Stream.Close(); } else { // Requesting file using (FileStream stream = new FileStream( fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { response.ContentLength = stream.Length; response.SendHeaders(); StreamUtilities.CopyStream(stream, context.Stream); context.Stream.Close(); } } } catch (Exception ex) { context.Respond( "HTTP/1.0", HttpStatusCode.InternalServerError, "Internal server error", ex.ToString(), "text/plain"); } }
protected void HandleSetManifestInfoRequest( HttpClientContext context, HttpRequest request) { try { LocalRepositoryState repoState = GetRepositoryFromRequest(request); // TODO: Authenticate based on request address // ...delete temp file if not authenticated... // Better to authenticate when the headers are received... Stream infoStream = request.Body; infoStream.Seek(0, SeekOrigin.Begin); Manifest dummyManifest = Manifest.ReadManifestStream(infoStream); lock (repoState.Manifest) { repoState.Manifest.CopyManifestInfoFrom(dummyManifest); repoState.SetManifestChanged(); } context.Respond( "HTTP/1.0", HttpStatusCode.OK, "Info accepted", "", "text/plain"); } catch (Exception ex) { // TODO: Make this a method context.Respond( "HTTP/1.0", HttpStatusCode.InternalServerError, "Internal server error", ex.ToString(), "text/plain"); System.Console.WriteLine(ex.ToString()); } }
protected void HandleCopyOrMoveRequest( HttpClientContext context, HttpRequest request) { try { LocalRepositoryState repoState = GetRepositoryFromRequest(request); // TODO: Authenticate based on request address FileInfo sourceFileInfo = new FileInfo(GetLocalFilePathFromRequest(request)); String destFilePath = GetLocalDestinationFilePathFromRequest(request, repoState); String destDirPath = Path.GetDirectoryName(destFilePath); if (Directory.Exists(destDirPath) == false) { Directory.CreateDirectory(destDirPath); } if (request.Method == "MOVE") { sourceFileInfo.MoveTo(destFilePath); } else { // Must be copy sourceFileInfo.CopyTo(destFilePath); } FileInfo destFile = new FileInfo(destFilePath); SetFileInfoFromRequest(destFile, request); lock (repoState.Manifest) { if (request.Method == "MOVE") { ManifestFileInfo removeFileInfo = GetOrMakeManifestFileInfoFromRequest( repoState.Manifest, request, false); if (removeFileInfo != null) { // If this is ever null, something weird is happening // but we already copied the file so might as well // proceed. removeFileInfo.ParentDirectory.Files.Remove( removeFileInfo.Name); } } ManifestFileInfo manFileInfo = GetOrMakeDestinationManifestFileInfoFromRequest( repoState.Manifest, request); SetManifestFileInfo(manFileInfo, request, destFile); repoState.SetManifestChanged(); } context.Respond( "HTTP/1.0", HttpStatusCode.OK, "File accepted", "", "text/plain"); } catch (Exception ex) { context.Respond( "HTTP/1.0", HttpStatusCode.InternalServerError, "Internal server error", ex.ToString(), "text/plain"); System.Console.WriteLine(ex.ToString()); } }
protected void HandlePutRequest( HttpClientContext context, HttpRequest request) { try { // Close the file request.Body.Close(); LocalRepositoryState repoState = GetRepositoryFromRequest(request); // TODO: Authenticate based on request address // ...delete temp file if not authenticated... // Better to authenticate when the headers are received... string tempFilePath = GetTempFilePathFromRequest(request); FileInfo tempFile = new FileInfo(tempFilePath); String newFilePath = GetLocalFilePathFromRequest(request); String directoryPath = Path.GetDirectoryName(newFilePath); try { if (Directory.Exists(directoryPath) == false) { Directory.CreateDirectory(directoryPath); } if (File.Exists(newFilePath)) { File.Delete(newFilePath); } SetFileInfoFromRequest(tempFile, request); tempFile.MoveTo(newFilePath); } catch (Exception ex) { tempFile.Delete(); throw ex; } FileInfo newFile = new FileInfo(newFilePath); lock (repoState.Manifest) { ManifestFileInfo manFileInfo = GetOrMakeManifestFileInfoFromRequest( repoState.Manifest, request); SetManifestFileInfo(manFileInfo, request, newFile); repoState.SetManifestChanged(); } context.Respond( "HTTP/1.0", HttpStatusCode.OK, "File accepted", "", "text/plain"); } catch (Exception ex) { context.Respond( "HTTP/1.0", HttpStatusCode.InternalServerError, "Internal server error", ex.ToString(), "text/plain"); System.Console.WriteLine(ex.ToString()); } }