private void MakeResponse(HttpResponse response, HttpStatusCode code, string bodyText) { response.StatusCode = (int)code; response.StatusDescription = HttpResponseCodeStatus.Get(code); response.ContentType = "text/json"; byte[] utf8Body = m_utf8.GetBytes(bodyText); response.OutputStream.Write(utf8Body, 0, utf8Body.Length); }
private void RequestReceived(object sender, HttpRequestEventArgs e) { HttpRequest request = e.Request; if (m_auth != null) { if (!TryAuth(request, m_auth)) { m_logger.Trace("[" + request.UserHostAddress + "][error] Auth false, returning Unauthorized"); e.Response.StatusCode = (int)HttpStatusCode.Unauthorized; e.Response.StatusDescription = HttpResponseCodeStatus.Get(HttpStatusCode.Unauthorized); e.Response.Headers.Add("WWW-Authenticate", "Basic realm=\"" + this.m_name.Replace("\"", "") + "\""); return; } ; } ; string requestId = null; // Method allowed if (request.HttpMethod != "POST") { m_logger.Trace("[" + request.UserHostAddress + "][error] Wrong request method"); MakeReponseError(e.Response, requestId, "Wrong request method", HttpStatusCode.Forbidden); return; } ; try { string path = request.Path; string body = ReadInput(request); if (String.IsNullOrWhiteSpace(body)) { ProcessSchemaRequest(e); return; } RequestCommandWrapper command = new RequestCommandWrapper(body, m_jsonSettings); requestId = command.requestId; ProcessServiceRequest(e, command); } catch (Exception ex) { bool needError = !(ex is ArgumentException); m_logger.Log(needError? LogLevel.Error : LogLevel.Warn, "[" + request.UserHostAddress + "][error] Exception: \n" + ex.ToString()); MakeReponseError(e.Response, requestId, "exception: " + ex.Message); return; }; }