private void handleRequest(object input) { BasicDeliverEventArgs args = (BasicDeliverEventArgs)input; try { IDictionary <string, object> requestHeaders = args.BasicProperties.Headers; string executionId = ""; string endpoint = ""; string paramJsonString = ""; try { requestHeaders.TryGetValue("executionId", out object executionIdBytes); requestHeaders.TryGetValue("endpoint", out object endpointBytes); executionId = Encoding.UTF8.GetString((byte[])executionIdBytes); endpoint = Encoding.UTF8.GetString((byte[])endpointBytes); paramJsonString = Encoding.UTF8.GetString(args.Body); } catch (Exception ex) { PSScriptInvoker.logError("Unexpected exception while parsing request segments and query:\n" + ex.ToString()); writeResponse(args, "ERROR: URL not valid: " + ex.ToString(), 400); return; } PSScriptInvoker.logInfo(string.Format("Received RabbitMQ message (deliveryTag: {0}, executionId: {1}, endpoint: {2}):\n{3}", args.DeliveryTag, executionId, endpoint, paramJsonString)); // Get parameters Dictionary <String, String> parameters = new Dictionary <String, String>(); if (!String.IsNullOrEmpty(paramJsonString)) { parameters = JsonConvert.DeserializeObject <Dictionary <String, String> >(paramJsonString); } // Execute the appropriate script. string[] segments = endpoint.Split('/'); Dictionary <String, String> scriptOutput = scriptExecutor.executePSScriptByHttpSegments(segments, parameters); // Get output variables scriptOutput.TryGetValue("exitCode", out string exitCode); scriptOutput.TryGetValue("result", out string result); if (exitCode == "0") { if (string.IsNullOrEmpty(result)) { writeResponse(args, result, 204); } else { writeResponse(args, result, 200); } } else { writeResponse(args, result, 500); } } catch (JsonReaderException jsonEx) { PSScriptInvoker.logError("Unable to read input JSON:\n" + jsonEx.ToString()); writeResponse(args, jsonEx.ToString(), 400); } catch (Exception ex) { PSScriptInvoker.logError("Unexpected exception while processing message:\n" + ex.ToString()); writeResponse(args, ex.ToString(), 500); } // Acknowledge request if response was written successfully. lock (rabbitMqChannel) { try { rabbitMqChannel.BasicAck(deliveryTag: args.DeliveryTag, multiple: false); } catch (Exception ex) { PSScriptInvoker.logError("Unexpected exception while acknowledging request message:\n" + ex.ToString()); } } }
private void handleRequest(object input) { HttpListenerContext context = (HttpListenerContext)input; // A request was arrived. Get the object. HttpListenerRequest request = context.Request; try { string receivedAuthToken = request.Headers.Get("Authorization"); if (!string.IsNullOrEmpty(authToken) && string.IsNullOrEmpty(receivedAuthToken)) { writeResponse(context.Response, "ERROR: Authorization header missing!", 401); } else { if (!string.IsNullOrEmpty(authToken) && !receivedAuthToken.Equals(authToken)) { PSScriptInvoker.logError(string.Format("Wrong auth token received: '{0}'. Do nothing and return 403 (access denied).", receivedAuthToken)); writeResponse(context.Response, "ERROR: Wrong auth token. Access denied!", 403); } else { // Get the URI segments string[] segments = request.Url.Segments; // See here for more information about URI components: https://tools.ietf.org/html/rfc3986#section-3 // Get parameters Dictionary <String, String> parameters; string body = ""; if (request.HasEntityBody) { body = getRequestBody(request); parameters = JsonConvert.DeserializeObject <Dictionary <String, String> >(body); } else { string query = request.Url.Query; try { // Parse the query string variables into a dictionary. parameters = parseUriQuery(query); } catch (Exception ex) { PSScriptInvoker.logError("Unexpected exception while parsing request segments and query:\n" + ex.ToString()); writeResponse(context.Response, "ERROR: URL not valid: " + request.Url.ToString(), 400); return; } } // Execute the appropriate script. Dictionary <String, String> scriptOutput = scriptExecutor.executePSScriptByHttpSegments(segments, parameters); // Get output variables scriptOutput.TryGetValue("exitCode", out string exitCode); scriptOutput.TryGetValue("result", out string result); if (exitCode == "0") { if (string.IsNullOrEmpty(result)) { writeResponse(context.Response, result, 204); } else { writeResponse(context.Response, result, 200); } } else { writeResponse(context.Response, result, 500); } } } } catch (Exception ex) { PSScriptInvoker.logError("Unexpected exception while processing request:\n" + ex.ToString()); writeResponse(context.Response, ex.ToString(), 500); } }