public void Handle(Task task) { HttpListenerContext httpContext = task.TaskData as HttpListenerContext; if (httpContext == null) return; long taskInPoolTime = task.Timer.ElapsedMilliseconds; Log.Write("Incoming request: " + httpContext.Request.Url + ". Taken from task pool after " + taskInPoolTime + " ms of waiting."); try { byte[] readBuffer = new byte[httpContext.Request.ContentLength64]; httpContext.Request.InputStream.Read(readBuffer, 0, readBuffer.Length); //if (httpContext.Request.InputStream.Read(readBuffer, 0, readBuffer.Length)== 0) // throw new Exception("Can't read request body."); var p = new Parameters(readBuffer, httpContext.Request.QueryString); ICommand command = CommandFactory.GetCommand(p); Log.Write("Executing"); byte[] buffer = Encoding.UTF8.GetBytes(command.Execute()); WriteResponse(httpContext, buffer, HttpStatusCode.OK, "OK", "text/plain"); Log.Write("Executed"); } catch (Exception ex) { HandleError(httpContext, ex); } finally { FinshHandling(httpContext); long taskProcessingTime = task.Timer.ElapsedMilliseconds - taskInPoolTime; Log.Write("Request processed in " + taskProcessingTime + " ms. Total: " + (taskInPoolTime+taskProcessingTime) + " ms."); } }
public static ICommand GetCommand(Parameters parameters) { string method = parameters.HasParam("method") ? parameters.GetParam("method").ToLower() : ""; switch (method) { case "verify": return new VerifyCommand(parameters); case "verifyforjson": return new VerifyForJsonCommand(parameters); case "verifybyget": return new VerifyByGetCommand(parameters); default: throw new UnknownMethodException("unknown method: " + method); } }
public VerifyForJsonCommand(Parameters parameters) : base(parameters) { string body = Encoding.UTF8.GetString(parameters.Body); try { _cert = body.Split(':')[0].Trim(new[] { '}', ' ', '"' }); _jsonParsed = true; } catch (Exception ex) { } }
public VerifyCommand(Parameters parameters) : base(parameters) { }
protected Command(Parameters parameters) { this.parameters = parameters; }