void SendHeader(string contentType, long contentLength, string ranges) { httpResponse = new HttpResponse(); httpResponse.errorCode = httpRequest.IsGetRange ? 206 : 200; httpResponse.contentLength = contentLength; httpResponse.range = httpRequest.IsGetRange ? "bytes " + ranges : ""; httpResponse.contentType = contentType; if (!httpRequest.KeepAlive) httpResponse.connection = "close"; else httpResponse.connection = "keep-alive"; string headers = httpResponse.Header; byte[] headersBuffer = Encoding.ASCII.GetBytes(headers); Write(headersBuffer, 0, headersBuffer.Length); server.Log(this); IsLogged = true; }
void ClientThread() { try { while (true) { httpRequest = null; httpResponse = null; ClearWatchDog(); State = ClientState.ReadingRequest; string request = GetRequest(); ClearWatchDog(); httpRequest = new HttpRequest(request); IsLogged = false; State = ClientState.Processing; ProcessRequest(); if (!httpRequest.KeepAlive) break; } } catch (Exception ex) { error = ex; } Stop(); }
void SendError(int Code) { State = ClientState.Responsing; string errorString = Code.ToString() + " " + ((HttpStatusCode)Code).ToString(); string html = "<html><body><h1>" + errorString + "</h1></body></html>"; httpResponse = new HttpResponse(); httpResponse.errorCode = Code; httpResponse.contentType = "text/html; charset=UTF-8"; httpResponse.contentLength = html.Length; httpResponse.connection = "close"; string header = httpResponse.Header; string str = header + html; byte[] buffer = Encoding.UTF8.GetBytes(str); Write(buffer, 0, buffer.Length); server.Log(this); IsLogged = true; Stop(); }