private string ServerUrl(ezBuildRequestMessageType eType) { lock (_Lock) { return(string.Format("http://{0}/?type={1}&ID={2}", _ServerAddress.ToString(), (int)eType, _sID)); } }
private void HandleRequest(object state) { HttpListenerContext context = (HttpListenerContext)state; try { HttpListenerRequest request = context.Request; // Get message text string sMessageText; using (var reader = new StreamReader(request.InputStream, UTF8Encoding.UTF8)) { sMessageText = reader.ReadToEnd(); } string sResponseMessage = null; // If no query was given, show help page if (request.QueryString.AllKeys.Count() == 0) { sResponseMessage = ShowHelpPage(); } else { // Retrieve type of message. ezBuildRequestMessageType eType = ezBuildRequestMessageType.INVALID_REQUEST; try { if (request.QueryString.AllKeys.Contains("type")) { eType = (ezBuildRequestMessageType)Convert.ToInt32(request.QueryString["type"]); } } catch (FormatException) { eType = ezBuildRequestMessageType.INVALID_REQUEST; Console.WriteLine("'type' of request invalid or not present!"); context.Response.StatusCode = 400; // Bad Request context.Response.OutputStream.Close(); return; } // Handle message switch (eType) { case ezBuildRequestMessageType.POSTConfiguration: { BuildMachineSettings settings = Newtonsoft.Json.JsonConvert.DeserializeObject <BuildMachineSettings>(sMessageText); sResponseMessage = HandlePOSTConfiguration(settings); } break; case ezBuildRequestMessageType.GETPing: { string sID = request.QueryString["ID"]; sResponseMessage = HandleGETPing(sID); } break; case ezBuildRequestMessageType.GETWork: { string sID = request.QueryString["ID"]; sResponseMessage = HandleGETWork(sID); } break; case ezBuildRequestMessageType.POSTBuildResult: { string sID = request.QueryString["ID"]; string sFilename = request.QueryString["File"]; sResponseMessage = POSTBuildResult(sID, sFilename, sMessageText); } break; case ezBuildRequestMessageType.GETStatus: { sResponseMessage = HandleGetStatus(); } break; case ezBuildRequestMessageType.GETCheckHEADRevision: { _iLastSVNCheckTimestamp = 0; sResponseMessage = "Checking SVN Request received."; } break; case ezBuildRequestMessageType.GETPostToAddress: { string sAddress = request.QueryString["TO"]; int iStartRevision = 0; if (request.QueryString.AllKeys.Contains("StartRevision")) { iStartRevision = Convert.ToInt32(request.QueryString["StartRevision"]); } sResponseMessage = HandleGetPostToAddress(sAddress, iStartRevision); } break; case ezBuildRequestMessageType.GETPause: { sResponseMessage = HandleGetPause(true); } break; case ezBuildRequestMessageType.GETResume: { sResponseMessage = HandleGetPause(false); } break; case ezBuildRequestMessageType.GETEnableHibernateOnIdle: { sResponseMessage = HandleGetHibernate(true); } break; case ezBuildRequestMessageType.GETDisableHibernateOnIdle: { sResponseMessage = HandleGetHibernate(false); } break; case ezBuildRequestMessageType.GETCleanBuild: { string sID = request.QueryString["ID"]; sResponseMessage = HandleGetCleanBuild(sID); } break; default: Console.WriteLine("HandleRequest: invalid message type: '{0}'!", eType); break; } } // Send response if (sResponseMessage == null) { context.Response.StatusCode = 400; // Bad Request context.Response.OutputStream.Close(); } else { context.Response.StatusCode = 200; // OK context.Response.SendChunked = true; var bytes = Encoding.UTF8.GetBytes(sResponseMessage); context.Response.OutputStream.Write(bytes, 0, bytes.Length); context.Response.OutputStream.Close(); } } catch (Exception ex) { Console.WriteLine("HandleRequest Failed: {0}", ex.Message); context.Response.StatusCode = 400; // Bad Request context.Response.OutputStream.Close(); } }