public void SendHTML404(OSHttpResponse response, string host) { response.StatusCode = 404; response.AddHeader("Content-Type", "text/html"); string responseString = "<html><head><title>OMG 404 FailWhale OMEGALOL Kappa</title></head><body><H1>It didnt work!</H1><p>There is no module handling this request Kappa</p></body></html>"; byte[] buffer = Encoding.UTF8.GetBytes(responseString); response.SendChunked = false; response.ContentLength64 = buffer.LongLength; response.ContentEncoding = Encoding.UTF8; try { response.OutputStream.Write(buffer, 0, buffer.Length); } catch (Exception ex) { m_Output.LogMessage("error", "[HTTPD]: Error - " + ex.Message + Environment.NewLine); } finally { try { response.Send(); } catch (SocketException e) { m_Output.LogMessage("error", "[HTTPD]: SocketError - " + e.Message + Environment.NewLine); } } }
internal void SendFile(OSHttpRequest req, OSHttpResponse response, FileRequest.FileResponse filedata) { response.StatusCode = 200; response.SendChunked = false; response.ContentType = filedata.MimeType.MIME; long filelength = filedata.Data.LongLength; response.ContentLength64 = filelength; if (!filedata.MimeType.binary) { response.ContentEncoding = filedata.MimeType.Encoding; } try { response.OutputStream.Write(filedata.Data, 0, (int)filelength); } catch (Exception ex) { m_Output.LogMessage("error", "[HTTPD]: Error - " + ex.Message + Environment.NewLine); } finally { try { response.OutputStream.Flush(); response.Send(); } catch (SocketException e) { m_Output.LogMessage("warn", "There was an underlying socket error. Perhaps the socket disconnected." + e.Message + Environment.NewLine); } catch (IOException e) { m_Output.LogMessage("warn", "There was an IO issue: " + e.Message + Environment.NewLine); } } }
public void SendHTML500(OSHttpResponse response) { // I know this statuscode is dumb, but the client doesn't respond to 404s and 500s response.StatusCode = (int)HttpStatusCode.OK; response.AddHeader("Content-type", "text/html"); string responseString = "<html><head><title>OMG 500 It esploded</title></head><body><H1>Something had a problem!</H1><p>There was a problem doing the thing. Only thing. Sometimes it do be like that.</p></body></html>";; byte[] buffer = Encoding.UTF8.GetBytes(responseString); response.SendChunked = false; response.ContentLength64 = buffer.Length; response.ContentEncoding = Encoding.UTF8; try { response.OutputStream.Write(buffer, 0, buffer.Length); } catch (Exception ex) { m_Output.LogMessage("error", "[HTTPD]: Error - " + ex.Message + Environment.NewLine); } finally { //response.OutputStream.Close(); try { response.Send(); //response.FreeContext(); } catch (SocketException e) { // This has to be here to prevent a Linux/Mono crash m_Output.LogMessage("error", "[HTTPD] Socket issue " + e.Message + Environment.NewLine); } } }
public virtual void HandleRequest(OSHttpRequest request, OSHttpResponse response) { try { Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", true); response.SendChunked = false; IRequestHandler requestHandler; string path = request.RawUrl; string handlerKey = GetHandlerKey(request.HttpMethod, path); if (TryGetStreamHandler(handlerKey, out requestHandler)) { byte[] buffer = null; response.ContentType = request.ContentType; if (requestHandler is IGenericHTTPHandler) { IGenericHTTPHandler HttpRequestHandler = requestHandler as IGenericHTTPHandler; Stream requestStream = request.InputStream; Encoding encoding = Encoding.UTF8; string requestBody = string.Empty; using (StreamReader reader = new StreamReader(requestStream, encoding)) { requestBody = reader.ReadToEnd(); } Hashtable keysvals = new Hashtable(); Hashtable headervals = new Hashtable(); string[] querystringkeys = request.QueryString.AllKeys; string[] rHeaders = request.Headers.AllKeys; foreach (string queryname in querystringkeys) { keysvals.Add(queryname, request.Query[queryname]); } foreach (string headername in rHeaders) { headervals.Add(headername, request.Headers[headername]); } keysvals.Add("requestbody", requestBody); keysvals.Add("headers", headervals); keysvals.Add("form", request.Form); keysvals.Add("uri", request.RawUrl); keysvals.Add("contenttype", request.ContentType); if (request.ContentEncoding != null && !keysvals.ContainsKey("encoding")) { keysvals.Add("encoding", request.ContentEncoding); } keysvals.Add("accepttypes", request.AcceptTypes); DoHTTPGruntWork(HttpRequestHandler.Handle(path, keysvals), response); return; } request.InputStream.Close(); if (buffer == null) { return; } if (!response.SendChunked) { response.ContentLength64 = buffer.LongLength; } try { response.OutputStream.Write(buffer, 0, buffer.Length); } catch (HttpListenerException) { m_Output.LogMessage("error", "[HTTPD]: HTTPRequest terminated abnormally " + Environment.NewLine); } try { response.Send(); } catch (SocketException) { m_Output.LogMessage("error", "[HTTPD]: HTTPRequest terminated abnormally " + Environment.NewLine); } catch (IOException) { m_Output.LogMessage("error", "[HTTPD]: HTTPRequest terminated abnormally " + Environment.NewLine); } } switch (request.ContentType) { case null: case "text/html": HandleHTTPRequest(request, response); return; default: if (DoWeHaveAHTTPHandler(request.RawUrl)) { HandleHTTPRequest(request, response); return; } return; } } catch (SocketException e) { m_Output.LogMessage("warn", "[HTTPD]: HandleRequest threw " + e.Message + Environment.NewLine); } catch (IOException e) { m_Output.LogMessage("warn", "[HTTPD]: HandleRequest threw " + e.Message + Environment.NewLine); } catch (InvalidOperationException e) { m_Output.LogMessage("warn", "[HTTPD]: HandleRequest threw " + e.Message + Environment.NewLine); } return; }
internal void DoHTTPGruntWork(Hashtable responsedata, OSHttpResponse response) { int responsecode = (int)responsedata["int_response_code"]; string responseString = string.Empty; if (responsedata.ContainsKey("str_response_string")) { responseString = (string)responsedata["str_response_string"]; } string contentType = string.Empty; if (responsedata.ContainsKey("content_type")) { contentType = (string)responsedata["content_type"]; } string RedirectLocationHeader = string.Empty; if (responsedata.ContainsKey("redirect_location")) { RedirectLocationHeader = (string)responsedata["redirect_location"]; } if (responsedata.ContainsKey("error_status_text")) { response.StatusDescription = (string)responsedata["error_status_text"]; } if (responsedata.ContainsKey("http_protocol_version")) { response.ProtocolVersion = (string)responsedata["http_protocolversion_version"]; } if (responsedata.ContainsKey("keepalive")) { bool keepalive = (bool)responsedata["keepalive"]; response.KeepAlive = keepalive; } if (responsedata.ContainsKey("reusecontext")) { response.ReuseContext = (bool)responsedata["reusecontext"]; } if (string.IsNullOrEmpty(contentType)) { contentType = "text/html"; } response.StatusCode = responsecode; switch (responsecode) { case 302: response.AddHeader("Location", RedirectLocationHeader); break; default: response.AddHeader("Content-Type", contentType); break; } byte[] buffer; if (!contentType.Contains("image")) { buffer = Encoding.UTF8.GetBytes(responseString); } else { buffer = Convert.FromBase64String(responseString); } response.SendChunked = false; response.ContentLength64 = buffer.LongLength; response.ContentEncoding = Encoding.UTF8; try { response.OutputStream.Write(buffer, 0, buffer.Length); } catch (Exception ex) { m_Output.LogMessage("error", "[HTTPD]: Error - " + ex.Message + Environment.NewLine); } finally { try { response.OutputStream.Flush(); response.Send(); } catch (SocketException e) { m_Output.LogMessage("warn", "There was an underlying socket error. Perhaps the socket disconnected." + e.Message + Environment.NewLine); } catch (IOException e) { m_Output.LogMessage("warn", "There was an IO issue: " + e.Message + Environment.NewLine); } } }