public bool process(HttpListenerRequest request, HttpListenerResponse response) { if (request.RawUrl.StartsWith(PAGE_PREFIX)) { try { var requestedFile = request.RawUrl.Substring(PAGE_PREFIX.Length); var contentType = GetContentType(Path.GetExtension(requestedFile)); // Set a mime type, if we have one for this extension if (!string.IsNullOrEmpty(contentType.mimeType)) { response.ContentType = contentType.mimeType; } // Read the data, and set encoding type if text. Assume that any text encoding is UTF-8 (probably). byte[] contentData = System.IO.File.ReadAllBytes(buildPath(escapeFileName(requestedFile))); if (contentType.contentType == HTMLContentType.TextContent) { response.ContentEncoding = Encoding.UTF8; } // Write out the response to the client response.WriteContent(contentData); return true; } catch { } } return false; }
public void SendJsonBytes(HttpListenerResponse res, byte[] data) { res.StatusCode = (int)HttpStatusCode.OK; res.ContentType = "application/json"; res.ContentEncoding = System.Text.Encoding.UTF8; res.AddHeader("Access-Control-Allow-Origin", "*"); res.WriteContent(data); }
private static void onGet(HttpListenerRequest request, HttpListenerResponse response) { var content = getContent(request.RawUrl); if (content != null) { response.WriteContent(content); return; } response.StatusCode = (int)HttpStatusCode.NotFound; }
public void SendContent(HttpListenerResponse res, string path, byte[] content) { string mimeType = HFTMimeType.GetMimeType(path); res.ContentType = mimeType; if (mimeType.StartsWith("text/") || mimeType == "application/javascript") { res.ContentEncoding = System.Text.Encoding.UTF8; } res.StatusCode = (int)HttpStatusCode.OK; res.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1. res.AddHeader("Pragma", "no-cache"); // HTTP 1.0. res.AddHeader("Expires", "0"); // Proxies. res.WriteContent(content); }
public bool process(HttpListenerRequest request, HttpListenerResponse response) { if (!request.RawUrl.StartsWith(PAGE_PREFIX)) return false; // Work out how big this request was long byteCount = request.RawUrl.Length + request.ContentLength64; // Don't count headers + request.Headers.AllKeys.Sum(x => x.Length + request.Headers[x].Length + 1); dataRates.RecieveDataFromClient(Convert.ToInt32(byteCount)); IDictionary<string, object> apiRequests; if (request.HttpMethod.ToUpper() == "POST" && request.HasEntityBody) { System.IO.StreamReader streamReader = new System.IO.StreamReader(request.InputStream); apiRequests = parseJSONBody(streamReader.ReadToEnd()); } else { apiRequests = splitArguments(request.Url.Query); } var results = new Dictionary<string, object>(); var unknowns = new List<string>(); var errors = new Dictionary<string, string>(); foreach (var name in apiRequests.Keys) { try { results[name] = kspAPI.ProcessAPIString(apiRequests[name].ToString()); } catch (IKSPAPI.UnknownAPIException) { unknowns.Add(apiRequests[name].ToString()); } catch (Exception ex) { errors[apiRequests[name].ToString()] = ex.ToString(); } } // If we had any unrecognised API keys, let the user know if (unknowns.Count > 0) results["unknown"] = unknowns; if (errors.Count > 0) results["errors"] = errors; // Now, serialize the dictionary and write to the response var returnData = Encoding.UTF8.GetBytes(SimpleJson.SimpleJson.SerializeObject(results)); response.ContentEncoding = Encoding.UTF8; response.ContentType = "application/json"; response.WriteContent(returnData); dataRates.SendDataToClient(returnData.Length); return true; }
bool HandleRoot(string path, HttpListenerRequest req, HttpListenerResponse res) { if (path.Equals("/index.html") || path.Equals("/enter-name.html")) { var uri = req.Url; string url = uri.GetLeftPart(UriPartial.Authority) + m_gamePath + m_options.controllerFilename + uri.Query + uri.Fragment; res.StatusCode = (int)HttpStatusCode.Redirect; res.AddHeader("Location", url); res.ContentType = "text/html"; res.WriteContent(System.Text.Encoding.UTF8.GetBytes("<script>window.location.href = decodeURIComponent(\"" + Uri.EscapeDataString(url) + "\");</script>")); m_log.Info("redirect: " + url); return true; } return false; }
bool HandleNotFound(string path, HttpListenerRequest req, HttpListenerResponse res) { res.ContentType = "text/text"; res.StatusCode = (int)HttpStatusCode.NotFound; res.WriteContent(System.Text.Encoding.UTF8.GetBytes("unknown path: " + path + "\n")); return true; }