void NewRequestThread(Object o) { SocketAsyncEventArgs e = (SocketAsyncEventArgs)o; DataHolderToken token = (DataHolderToken)e.UserToken; OpResult opResult = new OpResult(OpStatusCode.BadRequest); string sCommand = ""; string sParam = ""; string sBody = ""; try { // Show error for index if (token.httpRequest.Length == 0) { sCommand = "<i>No command specified.</i>"; sParam = "<i>No parameters specified.</i>"; } else { string[] req = token.httpRequest.Split(new char[] { '?' }, 2); //Strip off "?" string[] cmd_stack = req[0].Split(new char[] { '/' }); for (int idx = 0; idx < cmd_stack.Length; idx++) { string[] command = cmd_stack[idx].Split(new char[] { ' ' }, 2); if (command.Length == 0) { return; } sCommand = command[0]; sParam = (command.Length == 2 ? command[1] : string.Empty); if (sCommand.Equals("help", StringComparison.InvariantCultureIgnoreCase)) { opResult = remoteCommands.CommandListHTML(AddInModule.GetPortNumber(AddInModule.m_basePortNumber)); } else if (sCommand.Equals("format", StringComparison.InvariantCultureIgnoreCase)) { ICommand formatter = new customCmd(sBody); opResult = formatter.Execute(sParam); sBody = ""; } else if (token.opResult != null) { opResult = token.opResult; } else if (sCommand.Equals("server-settings")) { opResult = ExecuteSettingsCommand(sCommand, sParam); } else if (sCommand.Equals("music-clear-cache")) { startCacheBuildNow(); opResult = new OpResult(); opResult.StatusCode = OpStatusCode.Success; opResult.StatusText = "Cache cleared"; } else { opResult = remoteCommands.Execute(sCommand, sParam, settings); } } } //Get bytes to send to browser if (opResult.isHelpFormat()) { string sTempBody = opResult.ToString(); if (sParam.Length == 0) { sParam = "<i>No parameters specified.</i>"; } if (opResult.StatusCode != OpStatusCode.Ok && opResult.StatusCode != OpStatusCode.Success) { sTempBody = string.Format("<h1>ERROR<hr>Command: {0}<br>Params: {1}<br>Returned: {2} - {3}<hr>See <a href='help'>Help</a></h1>", sCommand, sParam, opResult.StatusCode, opResult.ToString()); } else if (opResult.StatusCode != OpStatusCode.OkImage) { if (sTempBody.Length > 0) { if (sTempBody.TrimStart()[0] != '<') { sTempBody = "<pre>" + sTempBody + "</pre>"; } } else { sTempBody = string.Format("<h1>Ok<hr>Last Command: '{0}'<br>Params: {1}<br>Returned: {2}<hr>See <a href='help'>Help</a></h1>", sCommand, sParam, opResult.StatusCode); } } if (sBody.Length > 0) { sBody += "<HR>"; } sBody += sTempBody; token.dataToSend = GetPageDataToSend(string.Format("{0}\r\n", sBody)); } else if (opResult.StatusCode != OpStatusCode.OkImage) { token.dataToSend = GetPageJsonDataToSend(opResult.ToString()); } else { token.dataToSend = GetImageDataToSend(opResult); } } catch (Exception ex) { logger.Write("Exception in NewRequestThread: " + ex.Message); token.dataToSend = GetPageDataToSend(string.Format("<html><body>EXCEPTION: {0}<hr></body></html>", ex.Message)); Trace.TraceError(ex.ToString()); } //Set send operation variables token.sendBytesRemainingCount = token.dataToSend.Length; token.bytesSentAlreadyCount = 0; StartSend(e); }
void m_httpServer_NewRequest_thread(HttpEventArgs e) { OpResult opResult = new OpResult(OpStatusCode.BadRequest); string sCommand = ""; string sParam = ""; string sBody = ""; string sTempBody = ""; try { // Show error for index if (e.Request.Length == 0) { sCommand = "<i>No command specified.</i>"; sParam = "<i>No parameters specified.</i>"; } else { string[] req = e.Request.Split(new char[] { '?' }, 2); //Strip off "?" string[] cmd_stack = req[0].Split(new char[] { '/' }); for (int idx = 0; idx < cmd_stack.Length; idx++) { sTempBody = ""; string[] command = cmd_stack[idx].Split(new char[] { ' ' }, 2); if (command.Length == 0) { return; } sCommand = command[0]; sParam = (command.Length == 2 ? command[1] : string.Empty); if (sCommand.Equals("help", StringComparison.InvariantCultureIgnoreCase)) { opResult = m_remoteCommands.CommandListHTML(GetPortNumber(m_basePortNumber)); } else if (sCommand.Equals("format", StringComparison.InvariantCultureIgnoreCase)) { ICommand formatter = new customCmd(sBody); opResult = formatter.Execute(sParam); sBody = ""; } else { opResult = m_remoteCommands.Execute(sCommand, sParam); } sTempBody = opResult.ToString(); if (sParam.Length == 0) { sParam = "<i>No parameters specified.</i>"; } if (opResult.StatusCode != OpStatusCode.Ok && opResult.StatusCode != OpStatusCode.Success) { sTempBody = string.Format("<h1>ERROR<hr>Command: {0}<br>Params: {1}<br>Returned: {2} - {3}<hr>See <a href='help'>Help</a></h1>", sCommand, sParam, opResult.StatusCode, opResult.ToString()); //if (sBody.Length > 0) sBody += "<HR>"; //sBody += sTempBody; //break; } else if (opResult.StatusCode != OpStatusCode.OkImage) { if (sTempBody.Length > 0) { if (sTempBody.TrimStart()[0] != '<') { sTempBody = "<pre>" + sTempBody + "</pre>"; } } else { sTempBody = string.Format("<h1>Ok<hr>Last Command: '{0}'<br>Params: {1}<br>Returned: {2}<hr>See <a href='help'>Help</a></h1>", sCommand, sParam, opResult.StatusCode); } //if (sBody.Length > 0) sBody += "<HR>"; //sBody += sTempBody; } if (sBody.Length > 0) { sBody += "<HR>"; } sBody += sTempBody; } } if (opResult.StatusCode == OpStatusCode.OkImage) { m_httpServer.SendImage(opResult.ToString(), opResult.StatusText, e.HttpSocket); } else { m_httpServer.SendPage(string.Format("{0}\r\n", sBody), e.HttpSocket); } } catch (Exception ex) { m_httpServer.SendPage(string.Format("<html><body>EXCEPTION: {0}<hr></body></html>", ex.Message), e.HttpSocket); Trace.TraceError(ex.ToString()); } }