private void HandlePost() { // handle a "post" request from the client // if the client has a session open if (sessionId != 0) { try { // get the document name, content length and contents from the client var documentName = reader.ReadLine(); var documentLength = int.Parse(reader.ReadLine()); var documentContents = ReceiveDocument(documentLength); // put the document into the session sessionTable.PutSessionValue(sessionId, documentName, documentContents); // send success to the client SendSuccess(); } catch (SessionException se) { SendError(se.Message); } catch (Exception ex) { SendError(ex.Message); } } else { // error, cannot post without a session SendError($"No session found with ID {sessionId}, cannot post document"); } }
private void HandlePost() { // handle a "post" request from the client // if the client has a session open if (sessionId != 0) { try { // get the document name, content length and contents from the client var documentName = reader.ReadLine(); var documentLength = int.Parse(reader.ReadLine()); var documentContents = ReceiveDocument(documentLength); if (string.IsNullOrWhiteSpace(documentName) || documentName.Equals("/")) { throw new Exception("Document name cannot be empty"); } if (documentName.StartsWith("/")) { // client requested a file var filePath = MakeFilename(documentName); // create new file or append to existing file (append a new line first) if (File.Exists(filePath)) { File.AppendAllText(filePath, "\n"); } File.AppendAllText(filePath, documentContents); } else { // put the document into the session sessionTable.PutSessionValue(sessionId, documentName, documentContents); } // send success to the client SendSuccess(); } catch (SessionException se) { SendError(se.Message); } catch (Exception ex) { SendError(ex.Message); } } else { // error, cannot post without a session SendError($"No session found with ID {sessionId}, cannot post document"); } }
private void HandlePost() { // handle a "post" request from the client // get the document name, content length and contents from the client string documentName = reader.ReadLine(); int contentLength = int.Parse(reader.ReadLine()); string content = ReceiveDocument(contentLength); // if the client has a session open if (sessionId != 0) { try { // check if request is for a file or a session variable if (documentName.Length > 1 && documentName[0] == '/') { // append to the file requested string fileName = documentName.TrimStart('/').Replace('/', '\\'); string filePath = Path.Combine(Directory.GetCurrentDirectory(), fileName); Console.WriteLine("SDConnectedClient.HandlePost() - post requested for file: " + filePath); File.AppendAllText(filePath, "\n" + content); // get from file } else if (documentName.Length > 0) { // put the document into the session sessionTable.PutSessionValue(sessionId, documentName, content); } else { // invalid document name! throw new Exception("Invalid document name!"); } // send success to the client SendSuccess(); } catch (SessionException se) { SendError(se.Message); } catch (Exception ex) { SendError(ex.Message); } } else { // error, cannot post without a session SendError("Unable to post document, no session open!"); } }