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()); } }
/// <summary> /// Handles the received commands of the m_httpServer control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="args">The <see cref="HttpServer.HttpEventArgs"/> instance containing the event data.</param> void m_httpServer_NewRequest(object sender, HttpEventArgs e) { Thread http_thread = new Thread(new ParameterizedThreadStart(m_httpServer_NewRequest_thread)); http_thread.Start(e); }
void m_httpServer_NewRequest_thread(Object o) { HttpEventArgs e = (HttpEventArgs)o; m_httpServer_NewRequest_thread(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()); } }