public override void SendUnknownResponseHeader(string name, string value) { WebTrace.WriteLine("SendUnknownResponseHeader (" + name + ", " + value + ")"); if (String.Compare(name, "connection", true, CultureInfo.InvariantCulture) == 0) { sentConnection = true; if (value.ToLower().IndexOf("keep-alive") == -1) { keepAlive = false; } } if (!sentConnection && !haveContentLength && String.Compare(name, "Content-Length", true, CultureInfo.InvariantCulture) == 0) { haveContentLength = true; contentLength = Int64.Parse(value); // This should work, otherwise HttpResponse throws. } if (!headersSent) { responseHeaders.Append(name); responseHeaders.Append(": "); responseHeaders.Append(value); responseHeaders.Append("\r\n"); } }
public bool Start(bool bgThread) { if (started) { throw new InvalidOperationException("The server is already started."); } if (vpathToHost == null) { throw new InvalidOperationException("SetApplications must be called first."); } if (vpathToHost.Count == 0) { throw new InvalidOperationException("No applications defined or all of them are disabled"); } listen_socket = webSource.CreateSocket(); listen_socket.Listen(500); listen_socket.Blocking = false; runner = new Thread(new ThreadStart(RunServer)); runner.IsBackground = bgThread; runner.Start(); stop = false; WebTrace.WriteLine("Server started."); return(true); }
public override void FlushResponse(bool finalFlush) { try { WebTrace.WriteLine("FlushResponse({0}), {1}", finalFlush, headersSent); if (!headersSent) { responseHeaders.Insert(0, serverHeader); responseHeaders.Insert(0, status); responseHeaders.Append("\r\n"); byte [] b = Encoding.GetBytes(responseHeaders.ToString()); output.Write(b, 0, b.Length); headersSent = true; } foreach (byte [] bytes in response) { output.Write(bytes, 0, bytes.Length); } output.Flush(); response.Clear(); if (finalFlush) { CloseConnection(); } } catch (Exception e) { WebTrace.WriteLine(e.ToString()); } }
public override void SendUnknownResponseHeader(string name, string value) { WebTrace.WriteLine("SendUnknownResponseHeader (" + name + ", " + value + ")"); if (!headersSent) { responseHeaders.AppendFormat("{0}: {1}\r\n", name, value); } }
void RealStop() { runner.Abort(); listen_socket.Close(); UnloadAll(); Thread.Sleep(1000); WebTrace.WriteLine("Server stopped."); }
public override byte [] GetQueryStringRawBytes() { WebTrace.WriteLine("GetQueryStringRawBytes()"); if (queryString == null) { return(null); } return(Encoding.GetBytes(queryString)); }
// blr public override int ReadEntityBody(byte [] buffer, int size) { WebTrace.WriteLine("ReadEntityBody()"); if (buffer == null || size == 0) { return(0); } return(output.Read(buffer, 0, size)); }
public override string GetRawUrl() { WebTrace.WriteLine("GetRawUrl()"); if (queryString != null && queryString.Length > 0) { return(path + "?" + queryString); } return(path); }
public override int ReadEntityBody(byte [] buffer, int size) { WebTrace.WriteLine("ReadEntityBody()"); if (size == 0) { return(0); } return(ReadInput(buffer, 0, size)); }
public void Stop() { if (!started) { throw new InvalidOperationException("The server is not started."); } stop = true; // listen_socket.Stop (); WebTrace.WriteLine("Server stopped."); }
public override string GetKnownRequestHeader(int index) { if (headers == null) { return(null); } string headerName = HttpWorkerRequest.GetKnownRequestHeaderName(index); WebTrace.WriteLine(String.Format("GetKnownRequestHeader({0}) -> {1}", index, headerName)); return(headers [headerName] as string); }
public override void SendResponseFromMemory(byte [] data, int length) { WebTrace.WriteLine("SendResponseFromMemory ()"); if (length <= 0) { return; } byte [] bytes = new byte [length]; Array.Copy(data, 0, bytes, 0, length); response.Add(bytes); }
public override string GetUriPath() { WebTrace.WriteLine("GetUriPath()"); string result = path; if (pathInfo != null && pathInfo.Length > 0) { result += pathInfo; } return(result); }
public override void CloseConnection() { WebTrace.WriteLine("CloseConnection()"); if (requestBroker != null) { // We check for headersSent as broken user code might call // CloseConnection at an early stage. requestBroker.Close(requestId, (headersSent ? keepAlive : false)); requestBroker = null; FreeMemoryStream(response); response = null; } }
public void Start() { if (started) { throw new InvalidOperationException("The server is already started."); } listen_socket = new TcpListener(bindAddress); listen_socket.Start(); runner = new Thread(new ThreadStart(RunServer)); runner.Start(); stop = false; WebTrace.WriteLine("Server started."); }
public override void SendResponseFromMemory(byte [] data, int length) { WebTrace.WriteLine("SendResponseFromMemory ()"); if (requestBroker == null || length <= 0) { return; } if (data.Length < length) { length = data.Length; } response.Write(data, 0, length); }
public override string GetRawUrl() { WebTrace.WriteLine("GetRawUrl()"); string result = path; if (pathInfo != null && pathInfo.Length > 0) { result += pathInfo; } if (queryString != null && queryString.Length > 0) { return(result + "?" + queryString); } return(result); }
private void RunServer() { started = true; TcpClient client; while (!stop) { if (listen_socket.Pending()) { client = listen_socket.AcceptTcpClient(); WebTrace.WriteLine("Accepted connection."); Worker worker = new Worker(client, this); ThreadPool.QueueUserWorkItem(new WaitCallback(worker.Run)); } else { Thread.Sleep(50); } } listen_socket.Stop(); started = false; }
protected override bool GetRequestData() { try { if (!GetRequestLine()) { return(false); } if (protocol == null) { protocol = "HTTP/1.0"; } else if (!GetRequestHeaders()) { return(false); } WebTrace.WriteLine("verb: " + verb); WebTrace.WriteLine("path: " + path); WebTrace.WriteLine("queryString: " + queryString); WebTrace.WriteLine("protocol: " + protocol); if (headers != null) { foreach (string key in headers.Keys) { WebTrace.WriteLine(key + ": " + headers [key]); } } } catch (Exception) { return(false); } return(TryDirectory()); }
void RunServer() { spool.AddReadSocket(listen_socket); started = true; Socket client; int w; while (!stop) { ArrayList wSockets = spool.SelectRead(); w = wSockets.Count; for (int i = 0; i < w; i++) { Socket s = (Socket)wSockets [i]; if (s == listen_socket) { try { client = s.Accept(); client.Blocking = true; } catch (Exception e) { continue; } WebTrace.WriteLine("Accepted connection."); SetSocketOptions(client); spool.AddReadSocket(client, DateTime.UtcNow); continue; } spool.RemoveReadSocket(s); IWorker worker = webSource.CreateWorker(s, this); ThreadPool.QueueUserWorkItem(new WaitCallback(worker.Run)); } } started = false; }
public override byte [] GetPreloadedEntityBody() { WebTrace.WriteLine("GetPreloadedEntityBody"); return(null); }
public override bool HeadersSent() { WebTrace.WriteLine("HeadersSent() -> " + headersSent); return(headersSent); }
public override bool IsClientConnected() { WebTrace.WriteLine("IsClientConnected()"); return(requestBroker != null && requestBroker.IsConnected(requestId)); }
public override string GetHttpVersion() { WebTrace.WriteLine("GetHttpVersion()"); return(protocol); }
public override int GetRemotePort() { WebTrace.WriteLine("GetRemotePort()"); return(((IPEndPoint)remoteEP).Port); }
public override string GetRemoteAddress() { WebTrace.WriteLine("GetRemoteAddress()"); return(((IPEndPoint)remoteEP).Address.ToString()); }
public override string GetPathInfo() { WebTrace.WriteLine("GetPathInfo()"); return(pathInfo); }
public override string GetQueryString() { WebTrace.WriteLine("GetQueryString()"); return(queryString); }
public override string GetLocalAddress() { WebTrace.WriteLine("GetLocalAddress()"); return(localAddress); }
public override int GetLocalPort() { WebTrace.WriteLine("GetLocalPort()"); return(localPort); }