public void OnRequest(HttpRequestHead request, IDataProducer requestBody, IHttpResponseDelegate response) { List<string> commands = new List<string> {"showNotes","SyncDatabase"}; NameValueCollection vars = HttpUtility.ParseQueryString(request.QueryString); string args = ""; if (request.Method.ToUpperInvariant() == "GET" && vars["a"] != null && commands.Contains(vars["a"])) { string res = "error"; args += String.Format("{0} ", vars["a"]); vars.Remove("a"); foreach (string arg in vars.AllKeys) { args += String.Format("/{0} \"{1}\" ", arg, vars[arg]); } try { //create another instance of this process ProcessStartInfo info = new ProcessStartInfo(); info.FileName = String.Format("\"{0}\"",EvernoteLinkServer.gENScriptPath); info.Arguments = args; Process.Start(info); res = String.Format("{0} {1}", info.FileName, info.Arguments); } catch (Exception e) { res = e.Message; } var headers = new HttpResponseHead() { Status = "200 OK", Headers = new Dictionary<string, string>() { { "Content-Type", "text/plain" }, { "Content-Length", res.Length.ToString() }, } }; response.OnResponse(headers, new BufferedProducer(res)); } else { var responseBody = "The resource you requested ('" + request.Uri + "') could not be found."; var headers = new HttpResponseHead() { Status = "404 Not Found", Headers = new Dictionary<string, string>() { { "Content-Type", "text/plain" }, { "Content-Length", responseBody.Length.ToString() } } }; var body = new BufferedProducer(responseBody); response.OnResponse(headers, body); } }
public void OnRequest(HttpRequestHead request, IDataProducer requestBody, IHttpResponseDelegate response) { string body = "error"; if (request.Uri.StartsWith("/ntm")) { if (request.Uri.EndsWith("kill")) { Process[] processes = Process.GetProcessesByName("AsTelOS Express"); foreach (Process process in processes) { process.Kill(); } processes = Process.GetProcessesByName("ttTCLM_ASCOM_Telescope"); foreach (Process process in processes) { process.Kill(); } body = "{ok}"; } var headers = new HttpResponseHead() { Status = "200 OK", Headers = new Dictionary<string, string>() { { "Content-Type", "text/plain" }, { "Content-Length", body.Length.ToString() }, } }; response.OnResponse(headers, new BufferedProducer(body)); } else { var responseBody = "The resource you requested ('" + request.Uri + "') could not be found."; var headers = new HttpResponseHead() { Status = "404 Not Found", Headers = new Dictionary<string, string>() { { "Content-Type", "text/plain" }, { "Content-Length", responseBody.Length.ToString() } } }; var bodyerr = new BufferedProducer(responseBody); response.OnResponse(headers, bodyerr); } }
public void OnRequest(HttpRequestHead request, IDataProducer requestBody, IHttpResponseDelegate response) { if (request.Method.ToUpperInvariant() == "POST" && request.Uri.StartsWith("/bufferedecho")) { // when you subecribe to the request body before calling OnResponse, // the server will automatically send 100-continue if the client is // expecting it. requestBody.Connect(new BufferedConsumer(bufferedBody => { var headers = new HttpResponseHead() { Status = "200 OK", Headers = new Dictionary <string, string>() { { "Content-Type", "text/plain" }, { "Content-Length", request.Headers["Content-Length"] }, { "Connection", "close" } } }; response.OnResponse(headers, new BufferedProducer(bufferedBody)); }, error => { // XXX // uh oh, what happens? })); } else if (request.Method.ToUpperInvariant() == "POST" && request.Uri.StartsWith("/echo")) { var headers = new HttpResponseHead() { Status = "200 OK", Headers = new Dictionary <string, string>() { { "Content-Type", "text/plain" }, { "Connection", "close" } } }; if (request.Headers.ContainsKey("Content-Length")) { headers.Headers["Content-Length"] = request.Headers["Content-Length"]; } // if you call OnResponse before subscribing to the request body, // 100-continue will not be sent before the response is sent. // per rfc2616 this response must have a 'final' status code, // but the server does not enforce it. response.OnResponse(headers, requestBody); } else if (request.Uri.StartsWith("/")) { var body = string.Format( "Hello world.\r\nHello.\r\n\r\nUri: {0}\r\nPath: {1}\r\nQuery:{2}\r\nFragment: {3}\r\n", request.Uri, request.Path, request.QueryString, request.Fragment); var headers = new HttpResponseHead() { Status = "200 OK", Headers = new Dictionary <string, string>() { { "Content-Type", "text/plain" }, { "Content-Length", body.Length.ToString() }, } }; response.OnResponse(headers, new BufferedProducer(body)); } else { var responseBody = "The resource you requested ('" + request.Uri + "') could not be found."; var headers = new HttpResponseHead() { Status = "404 Not Found", Headers = new Dictionary <string, string>() { { "Content-Type", "text/plain" }, { "Content-Length", responseBody.Length.ToString() } } }; var body = new BufferedProducer(responseBody); response.OnResponse(headers, body); } }
private static void GetResourceByName(string contentRequest, IHttpResponseDelegate response) { var headers = new HttpResponseHead() { Status = "200 OK", Headers = new Dictionary<string, string>() { { "Content-Type", "text/plain" }, { "Connection", "close" }, { "Content-Length", "0" }, { "Cache-Control", "max-age=31536000"} } }; BufferedProducer producer = new BufferedProducer(""); if (_assembly == null) _assembly = Assembly.GetAssembly(typeof(VixenModules.App.WebServer.HTTP.RequestDelegate)); try { var resources = _assembly.GetManifestResourceNames(); var resourceItem = resources.FirstOrDefault(n => n.EndsWith(contentRequest, StringComparison.CurrentCultureIgnoreCase)); if (resourceItem == null) throw new ApplicationException(string.Format("Requested Resource {0} does not exist.", contentRequest)); using (var _Stream = _assembly.GetManifestResourceStream(resourceItem)) { var bytes = ReadFully(_Stream); headers.Headers["Content-Length"] = bytes.Length.ToString(); headers.Headers["Content-Type"] = GetContentType(contentRequest); producer = new BufferedProducer(bytes); } } catch (Exception e) { Logging.ErrorException(e.Message, e); headers.Status = "404 Not Found"; } response.OnResponse(headers, producer); }